001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Wojciech Sulejman
018 */
019
020package org.nuxeo.ecm.platform.signature.core.user;
021
022import java.util.Arrays;
023import java.util.HashSet;
024import java.util.List;
025import java.util.Set;
026
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XObject;
029import org.nuxeo.ecm.platform.signature.api.exception.CertException;
030
031/**
032 * Provides default values for new user certificates.
033 *
034 * @author <a href="mailto:ws@nuxeo.com">Wojciech Sulejman</a>
035 */
036
037@XObject("userDescriptor")
038public class CUserDescriptor {
039
040    /**
041     * An ISO 3166 code to be set in new user certificates
042     */
043    @XNode("countryCode")
044    protected String countryCode;
045
046    /**
047     * Your organization name to be set in new user certificates
048     */
049    @XNode("organization")
050    protected String organization;
051
052    /**
053     * Your organizational unit name to be set in new user certificates
054     */
055    @XNode("organizationalUnit")
056    protected String organizationalUnit;
057
058    public String getCountryCode() {
059        return countryCode;
060    }
061
062    public void setCountryCode(String countryCode) {
063        if (!validateCountryCode(countryCode)) {
064            throw new CertException("Invalid country code for user certificate");
065        }
066        this.countryCode = countryCode;
067    }
068
069    public String getOrganization() {
070        return organization;
071    }
072
073    public void setOrganization(String organization) {
074        this.organization = organization;
075    }
076
077    public String getOrganizationalUnit() {
078        return organizationalUnit;
079    }
080
081    public void setOrganizationalUnit(String organizationalUnit) {
082        this.organizationalUnit = organizationalUnit;
083    }
084
085    boolean validateCountryCode(String countryCode) {
086        String[] validCountryCodes = { "AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW",
087                "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW",
088                "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX",
089                "CC", "CO", "KM", "CG", "CD", "CK", "CR", "CI", "HR", "CU", "CW", "CY", "CZ", "DK", "DJ", "DM", "DO",
090                "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM",
091                "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA",
092                "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ",
093                "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK",
094                "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME",
095                "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO",
096                "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", "RO", "RU",
097                "RW", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG",
098                "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE", "CH", "SY", "TW",
099                "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB",
100                "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW" };
101        List<String> countryCodeList = Arrays.asList(validCountryCodes);
102        Set<String> validCountryCodeSet = new HashSet<String>(countryCodeList);
103        return validCountryCodeSet.contains(countryCode);
104    }
105
106}