diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d94651..bb74115 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,21 @@ Changelog for OpenEstate-IS24-REST 0.4-SNAPSHOT (not released yet) ------------------------------- -... +### bugfixes + +- Less strict validation of e-mail addresses. + E-mail addresses are currently validated with classes provided by + [commons-validator](https://commons.apache.org/validator/) when XML is read + or written. This validator does not handle new top level domains (nTLD) + properly. Therefore the validation is replaced by a more simple approach, + that validates the e-mail address against the pattern, that is specified by + IS24 in `common-1.0.xsd`. + +### updates + +- The package `org.openestate.is24.restapi.utils.validator`, that contains + some classes of [commons-validator](https://commons.apache.org/validator/), + was removed from source code. 0.3.1 (10 Mar 2016) diff --git a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/XmlUtils.java b/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/XmlUtils.java index eb656f3..4cc867d 100644 --- a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/XmlUtils.java +++ b/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/XmlUtils.java @@ -45,7 +45,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.time.DateUtils; -import org.openestate.is24.restapi.utils.validator.EmailValidator; import org.openestate.is24.restapi.xml.common.Attachment; import org.openestate.is24.restapi.xml.common.City; import org.openestate.is24.restapi.xml.common.Continent; @@ -107,6 +106,8 @@ public final class XmlUtils "<[^<>\\n]*>", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE ); private final static Pattern BR_TAG_PATTERN = Pattern.compile( "<\\s*br[^<>\\n]*>", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE ); + private final static Pattern EMAIL_PATTERN = Pattern.compile( + ".+@.+\\..+", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE ); private XmlUtils() { @@ -430,7 +431,7 @@ public static String parseEmail( String value ) { String val = parseText( value, 5, 300 ); if (val==null) return null; - return (EmailValidator.getInstance().isValid( val ))? val: null; + return (EMAIL_PATTERN.matcher( val ).matches())? val: null; } /** @@ -1193,7 +1194,7 @@ public static String printDecimalPositive( BigDecimal value ) public static String printEmail( String value ) { String val = printText( value, 5, 300 ); - if (val==null || !EmailValidator.getInstance().isValid( val )) + if (val==null || !EMAIL_PATTERN.matcher( val ).matches()) { throw new IllegalArgumentException( "The provided email '" + value + "' is invalid!" ); diff --git a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/DomainValidator.java b/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/DomainValidator.java deleted file mode 100644 index 4b8e448..0000000 --- a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/DomainValidator.java +++ /dev/null @@ -1,507 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openestate.is24.restapi.utils.validator; - -import java.io.Serializable; -import java.util.Arrays; -import java.util.List; - -/** - *
Domain name validation routines.
- * - *- * This validator provides methods for validating Internet domain names - * and top-level domains. - *
- * - *Domain names are evaluated according - * to the standards RFC1034, - * section 3, and RFC1123, - * section 2.1. No accomodation is provided for the specialized needs of - * other applications; if the domain name has been URL-encoded, for example, - * validation will fail even though the equivalent plaintext version of the - * same name would have passed. - *
- * - *- * Validation is also provided for top-level domains (TLDs) as defined and - * maintained by the Internet Assigned Numbers Authority (IANA): - *
- * - *.arpa
, etc.).com, .org
, etc.).us, .uk, .cn
, etc.)- * (NOTE: This class does not provide IP address lookup for domain names or - * methods to ensure that a given domain name matches a specific IP; see - * {@link java.net.InetAddress} for that functionality.) - *
- * - * @version $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Thu, 05 Jan 2012) $ - * @since Validator 1.4 - */ -public class DomainValidator implements Serializable { - - private static final long serialVersionUID = -4407125112880174009L; - - // Regular expression strings for hostnames (derived from RFC2396 and RFC 1123) - private static final String DOMAIN_LABEL_REGEX = "\\p{Alnum}(?>[\\p{Alnum}-]*\\p{Alnum})*"; - private static final String TOP_LABEL_REGEX = "\\p{Alpha}{2,}"; - private static final String DOMAIN_NAME_REGEX = - "^(?:" + DOMAIN_LABEL_REGEX + "\\.)+" + "(" + TOP_LABEL_REGEX + ")$"; - - private final boolean allowLocal; - - /** - * Singleton instance of this validator, which - * doesn't consider local addresses as valid. - */ - private static final DomainValidator DOMAIN_VALIDATOR = new DomainValidator(false); - - /** - * Singleton instance of this validator, which does - * consider local addresses valid. - */ - private static final DomainValidator DOMAIN_VALIDATOR_WITH_LOCAL = new DomainValidator(true); - - /** - * RegexValidator for matching domains. - */ - private final RegexValidator domainRegex = - new RegexValidator(DOMAIN_NAME_REGEX); - /** - * RegexValidator for matching the a local hostname - */ - private final RegexValidator hostnameRegex = - new RegexValidator(DOMAIN_LABEL_REGEX); - - /** - * Returns the singleton instance of this validator. It - * will not consider local addresses as valid. - * @return the singleton instance of this validator - */ - public static DomainValidator getInstance() { - return DOMAIN_VALIDATOR; - } - - /** - * Returns the singleton instance of this validator, - * with local validation as required. - * @param allowLocal Should local addresses be considered valid? - * @return the singleton instance of this validator - */ - public static DomainValidator getInstance(boolean allowLocal) { - if(allowLocal) { - return DOMAIN_VALIDATOR_WITH_LOCAL; - } - return DOMAIN_VALIDATOR; - } - - /** Private constructor. */ - private DomainValidator(boolean allowLocal) { - this.allowLocal = allowLocal; - } - - /** - * Returns true if the specifiedString
parses
- * as a valid domain name with a recognized top-level domain.
- * The parsing is case-sensitive.
- * @param domain the parameter to check for domain name syntax
- * @return true if the parameter is a valid domain name
- */
- public boolean isValid(String domain) {
- String[] groups = domainRegex.match(domain);
- if (groups != null && groups.length > 0) {
- return isValidTld(groups[0]);
- } else if(allowLocal) {
- if (hostnameRegex.isValid(domain)) {
- return true;
- }
- }
- return false;
- }
-
- /**
- * Returns true if the specified String
matches any
- * IANA-defined top-level domain. Leading dots are ignored if present.
- * The search is case-sensitive.
- * @param tld the parameter to check for TLD status
- * @return true if the parameter is a TLD
- */
- public boolean isValidTld(String tld) {
- if(allowLocal && isValidLocalTld(tld)) {
- return true;
- }
- return isValidInfrastructureTld(tld)
- || isValidGenericTld(tld)
- || isValidCountryCodeTld(tld);
- }
-
- /**
- * Returns true if the specified String
matches any
- * IANA-defined infrastructure top-level domain. Leading dots are
- * ignored if present. The search is case-sensitive.
- * @param iTld the parameter to check for infrastructure TLD status
- * @return true if the parameter is an infrastructure TLD
- */
- public boolean isValidInfrastructureTld(String iTld) {
- return INFRASTRUCTURE_TLD_LIST.contains(chompLeadingDot(iTld.toLowerCase()));
- }
-
- /**
- * Returns true if the specified String
matches any
- * IANA-defined generic top-level domain. Leading dots are ignored
- * if present. The search is case-sensitive.
- * @param gTld the parameter to check for generic TLD status
- * @return true if the parameter is a generic TLD
- */
- public boolean isValidGenericTld(String gTld) {
- return GENERIC_TLD_LIST.contains(chompLeadingDot(gTld.toLowerCase()));
- }
-
- /**
- * Returns true if the specified String
matches any
- * IANA-defined country code top-level domain. Leading dots are
- * ignored if present. The search is case-sensitive.
- * @param ccTld the parameter to check for country code TLD status
- * @return true if the parameter is a country code TLD
- */
- public boolean isValidCountryCodeTld(String ccTld) {
- return COUNTRY_CODE_TLD_LIST.contains(chompLeadingDot(ccTld.toLowerCase()));
- }
-
- /**
- * Returns true if the specified String
matches any
- * widely used "local" domains (localhost or localdomain). Leading dots are
- * ignored if present. The search is case-sensitive.
- * @param iTld the parameter to check for local TLD status
- * @return true if the parameter is an local TLD
- */
- public boolean isValidLocalTld(String iTld) {
- return LOCAL_TLD_LIST.contains(chompLeadingDot(iTld.toLowerCase()));
- }
-
- private String chompLeadingDot(String str) {
- if (str.startsWith(".")) {
- return str.substring(1);
- } else {
- return str;
- }
- }
-
- // ---------------------------------------------
- // ----- TLDs defined by IANA
- // ----- Authoritative and comprehensive list at:
- // ----- http://data.iana.org/TLD/tlds-alpha-by-domain.txt
-
- private static final String[] INFRASTRUCTURE_TLDS = new String[] {
- "arpa", // internet infrastructure
- "root" // diagnostic marker for non-truncated root zone
- };
-
- private static final String[] GENERIC_TLDS = new String[] {
- "aero", // air transport industry
- "asia", // Pan-Asia/Asia Pacific
- "biz", // businesses
- "cat", // Catalan linguistic/cultural community
- "com", // commercial enterprises
- "coop", // cooperative associations
- "info", // informational sites
- "jobs", // Human Resource managers
- "mobi", // mobile products and services
- "museum", // museums, surprisingly enough
- "name", // individuals' sites
- "net", // internet support infrastructure/business
- "org", // noncommercial organizations
- "pro", // credentialed professionals and entities
- "tel", // contact data for businesses and individuals
- "travel", // entities in the travel industry
- "gov", // United States Government
- "edu", // accredited postsecondary US education entities
- "mil", // United States Military
- "int" // organizations established by international treaty
- };
-
- private static final String[] COUNTRY_CODE_TLDS = new String[] {
- "ac", // Ascension Island
- "ad", // Andorra
- "ae", // United Arab Emirates
- "af", // Afghanistan
- "ag", // Antigua and Barbuda
- "ai", // Anguilla
- "al", // Albania
- "am", // Armenia
- "an", // Netherlands Antilles
- "ao", // Angola
- "aq", // Antarctica
- "ar", // Argentina
- "as", // American Samoa
- "at", // Austria
- "au", // Australia (includes Ashmore and Cartier Islands and Coral Sea Islands)
- "aw", // Aruba
- "ax", // Åland
- "az", // Azerbaijan
- "ba", // Bosnia and Herzegovina
- "bb", // Barbados
- "bd", // Bangladesh
- "be", // Belgium
- "bf", // Burkina Faso
- "bg", // Bulgaria
- "bh", // Bahrain
- "bi", // Burundi
- "bj", // Benin
- "bm", // Bermuda
- "bn", // Brunei Darussalam
- "bo", // Bolivia
- "br", // Brazil
- "bs", // Bahamas
- "bt", // Bhutan
- "bv", // Bouvet Island
- "bw", // Botswana
- "by", // Belarus
- "bz", // Belize
- "ca", // Canada
- "cc", // Cocos (Keeling) Islands
- "cd", // Democratic Republic of the Congo (formerly Zaire)
- "cf", // Central African Republic
- "cg", // Republic of the Congo
- "ch", // Switzerland
- "ci", // Côte d'Ivoire
- "ck", // Cook Islands
- "cl", // Chile
- "cm", // Cameroon
- "cn", // China, mainland
- "co", // Colombia
- "cr", // Costa Rica
- "cu", // Cuba
- "cv", // Cape Verde
- "cx", // Christmas Island
- "cy", // Cyprus
- "cz", // Czech Republic
- "de", // Germany
- "dj", // Djibouti
- "dk", // Denmark
- "dm", // Dominica
- "do", // Dominican Republic
- "dz", // Algeria
- "ec", // Ecuador
- "ee", // Estonia
- "eg", // Egypt
- "er", // Eritrea
- "es", // Spain
- "et", // Ethiopia
- "eu", // European Union
- "fi", // Finland
- "fj", // Fiji
- "fk", // Falkland Islands
- "fm", // Federated States of Micronesia
- "fo", // Faroe Islands
- "fr", // France
- "ga", // Gabon
- "gb", // Great Britain (United Kingdom)
- "gd", // Grenada
- "ge", // Georgia
- "gf", // French Guiana
- "gg", // Guernsey
- "gh", // Ghana
- "gi", // Gibraltar
- "gl", // Greenland
- "gm", // The Gambia
- "gn", // Guinea
- "gp", // Guadeloupe
- "gq", // Equatorial Guinea
- "gr", // Greece
- "gs", // South Georgia and the South Sandwich Islands
- "gt", // Guatemala
- "gu", // Guam
- "gw", // Guinea-Bissau
- "gy", // Guyana
- "hk", // Hong Kong
- "hm", // Heard Island and McDonald Islands
- "hn", // Honduras
- "hr", // Croatia (Hrvatska)
- "ht", // Haiti
- "hu", // Hungary
- "id", // Indonesia
- "ie", // Ireland (Éire)
- "il", // Israel
- "im", // Isle of Man
- "in", // India
- "io", // British Indian Ocean Territory
- "iq", // Iraq
- "ir", // Iran
- "is", // Iceland
- "it", // Italy
- "je", // Jersey
- "jm", // Jamaica
- "jo", // Jordan
- "jp", // Japan
- "ke", // Kenya
- "kg", // Kyrgyzstan
- "kh", // Cambodia (Khmer)
- "ki", // Kiribati
- "km", // Comoros
- "kn", // Saint Kitts and Nevis
- "kp", // North Korea
- "kr", // South Korea
- "kw", // Kuwait
- "ky", // Cayman Islands
- "kz", // Kazakhstan
- "la", // Laos (currently being marketed as the official domain for Los Angeles)
- "lb", // Lebanon
- "lc", // Saint Lucia
- "li", // Liechtenstein
- "lk", // Sri Lanka
- "lr", // Liberia
- "ls", // Lesotho
- "lt", // Lithuania
- "lu", // Luxembourg
- "lv", // Latvia
- "ly", // Libya
- "ma", // Morocco
- "mc", // Monaco
- "md", // Moldova
- "me", // Montenegro
- "mg", // Madagascar
- "mh", // Marshall Islands
- "mk", // Republic of Macedonia
- "ml", // Mali
- "mm", // Myanmar
- "mn", // Mongolia
- "mo", // Macau
- "mp", // Northern Mariana Islands
- "mq", // Martinique
- "mr", // Mauritania
- "ms", // Montserrat
- "mt", // Malta
- "mu", // Mauritius
- "mv", // Maldives
- "mw", // Malawi
- "mx", // Mexico
- "my", // Malaysia
- "mz", // Mozambique
- "na", // Namibia
- "nc", // New Caledonia
- "ne", // Niger
- "nf", // Norfolk Island
- "ng", // Nigeria
- "ni", // Nicaragua
- "nl", // Netherlands
- "no", // Norway
- "np", // Nepal
- "nr", // Nauru
- "nu", // Niue
- "nz", // New Zealand
- "om", // Oman
- "pa", // Panama
- "pe", // Peru
- "pf", // French Polynesia With Clipperton Island
- "pg", // Papua New Guinea
- "ph", // Philippines
- "pk", // Pakistan
- "pl", // Poland
- "pm", // Saint-Pierre and Miquelon
- "pn", // Pitcairn Islands
- "pr", // Puerto Rico
- "ps", // Palestinian territories (PA-controlled West Bank and Gaza Strip)
- "pt", // Portugal
- "pw", // Palau
- "py", // Paraguay
- "qa", // Qatar
- "re", // Réunion
- "ro", // Romania
- "rs", // Serbia
- "ru", // Russia
- "rw", // Rwanda
- "sa", // Saudi Arabia
- "sb", // Solomon Islands
- "sc", // Seychelles
- "sd", // Sudan
- "se", // Sweden
- "sg", // Singapore
- "sh", // Saint Helena
- "si", // Slovenia
- "sj", // Svalbard and Jan Mayen Islands Not in use (Norwegian dependencies; see .no)
- "sk", // Slovakia
- "sl", // Sierra Leone
- "sm", // San Marino
- "sn", // Senegal
- "so", // Somalia
- "sr", // Suriname
- "st", // São Tomé and Príncipe
- "su", // Soviet Union (deprecated)
- "sv", // El Salvador
- "sy", // Syria
- "sz", // Swaziland
- "tc", // Turks and Caicos Islands
- "td", // Chad
- "tf", // French Southern and Antarctic Lands
- "tg", // Togo
- "th", // Thailand
- "tj", // Tajikistan
- "tk", // Tokelau
- "tl", // East Timor (deprecated old code)
- "tm", // Turkmenistan
- "tn", // Tunisia
- "to", // Tonga
- "tp", // East Timor
- "tr", // Turkey
- "tt", // Trinidad and Tobago
- "tv", // Tuvalu
- "tw", // Taiwan, Republic of China
- "tz", // Tanzania
- "ua", // Ukraine
- "ug", // Uganda
- "uk", // United Kingdom
- "um", // United States Minor Outlying Islands
- "us", // United States of America
- "uy", // Uruguay
- "uz", // Uzbekistan
- "va", // Vatican City State
- "vc", // Saint Vincent and the Grenadines
- "ve", // Venezuela
- "vg", // British Virgin Islands
- "vi", // U.S. Virgin Islands
- "vn", // Vietnam
- "vu", // Vanuatu
- "wf", // Wallis and Futuna
- "ws", // Samoa (formerly Western Samoa)
- "ye", // Yemen
- "yt", // Mayotte
- "yu", // Serbia and Montenegro (originally Yugoslavia)
- "za", // South Africa
- "zm", // Zambia
- "zw", // Zimbabwe
- };
-
- private static final String[] LOCAL_TLDS = new String[] {
- "localhost", // RFC2606 defined
- "localdomain" // Also widely used as localhost.localdomain
- };
-
- private static final List INFRASTRUCTURE_TLD_LIST = Arrays.asList(INFRASTRUCTURE_TLDS);
- private static final List GENERIC_TLD_LIST = Arrays.asList(GENERIC_TLDS);
- private static final List COUNTRY_CODE_TLD_LIST = Arrays.asList(COUNTRY_CODE_TLDS);
- private static final List LOCAL_TLD_LIST = Arrays.asList(LOCAL_TLDS);
-}
diff --git a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/EmailValidator.java b/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/EmailValidator.java
deleted file mode 100644
index b9e53cd..0000000
--- a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/EmailValidator.java
+++ /dev/null
@@ -1,178 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.openestate.is24.restapi.utils.validator;
-
-import java.io.Serializable;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * Perform email validations.
- *- * This class is a Singleton; you can retrieve the instance via the getInstance() method. - *
- *- * Based on a script by Sandeep V. Tamhankar - * http://javascript.internet.com - *
- *- * This implementation is not guaranteed to catch all possible errors in an email address. - * For example, an address like nobody@noplace.somedog will pass validator, even though there - * is no TLD "somedog" - *
. - * - * @version $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Thu, 05 Jan 2012) $ - * @since Validator 1.4 - */ -public class EmailValidator implements Serializable { - - private static final long serialVersionUID = 1705927040799295880L; - - private static final String SPECIAL_CHARS = "\\p{Cntrl}\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]"; - private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]"; - private static final String QUOTED_USER = "(\"[^\"]*\")"; - private static final String WORD = "((" + VALID_CHARS + "|')+|" + QUOTED_USER + ")"; - - private static final String LEGAL_ASCII_REGEX = "^\\p{ASCII}+$"; - private static final String EMAIL_REGEX = "^\\s*?(.+)@(.+?)\\s*$"; - private static final String IP_DOMAIN_REGEX = "^\\[(.*)\\]$"; - private static final String USER_REGEX = "^\\s*" + WORD + "(\\." + WORD + ")*$"; - - private static final Pattern MATCH_ASCII_PATTERN = Pattern.compile(LEGAL_ASCII_REGEX); - private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX); - private static final Pattern IP_DOMAIN_PATTERN = Pattern.compile(IP_DOMAIN_REGEX); - private static final Pattern USER_PATTERN = Pattern.compile(USER_REGEX); - - private final boolean allowLocal; - - /** - * Singleton instance of this class, which - * doesn't consider local addresses as valid. - */ - private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator(false); - - /** - * Singleton instance of this class, which does - * consider local addresses valid. - */ - private static final EmailValidator EMAIL_VALIDATOR_WITH_LOCAL = new EmailValidator(true); - - /** - * Returns the Singleton instance of this validator. - * - * @return singleton instance of this validator. - */ - public static EmailValidator getInstance() { - return EMAIL_VALIDATOR; - } - - /** - * Returns the Singleton instance of this validator, - * with local validation as required. - * - * @param allowLocal Should local addresses be considered valid? - * @return singleton instance of this validator - */ - public static EmailValidator getInstance(boolean allowLocal) { - if(allowLocal) { - return EMAIL_VALIDATOR_WITH_LOCAL; - } - return EMAIL_VALIDATOR; - } - - /** - * Protected constructor for subclasses to use. - * - * @param allowLocal Should local addresses be considered valid? - */ - protected EmailValidator(boolean allowLocal) { - super(); - this.allowLocal = allowLocal; - } - - /** - *Checks if a field has a valid e-mail address.
- * - * @param email The value validation is being performed on. Anull
- * value is considered invalid.
- * @return true if the email address is valid.
- */
- public boolean isValid(String email) {
- if (email == null) {
- return false;
- }
-
- Matcher asciiMatcher = MATCH_ASCII_PATTERN.matcher(email);
- if (!asciiMatcher.matches()) {
- return false;
- }
-
- // Check the whole email address structure
- Matcher emailMatcher = EMAIL_PATTERN.matcher(email);
- if (!emailMatcher.matches()) {
- return false;
- }
-
- if (email.endsWith(".")) {
- return false;
- }
-
- if (!isValidUser(emailMatcher.group(1))) {
- return false;
- }
-
- if (!isValidDomain(emailMatcher.group(2))) {
- return false;
- }
-
- return true;
- }
-
- /**
- * Returns true if the domain component of an email address is valid.
- *
- * @param domain being validated.
- * @return true if the email address's domain is valid.
- */
- protected boolean isValidDomain(String domain) {
- // see if domain is an IP address in brackets
- Matcher ipDomainMatcher = IP_DOMAIN_PATTERN.matcher(domain);
-
- if (ipDomainMatcher.matches()) {
- InetAddressValidator inetAddressValidator =
- InetAddressValidator.getInstance();
- return inetAddressValidator.isValid(ipDomainMatcher.group(1));
- } else {
- // Domain is symbolic name
- DomainValidator domainValidator =
- DomainValidator.getInstance(allowLocal);
- return domainValidator.isValid(domain);
- }
- }
-
- /**
- * Returns true if the user component of an email address is valid.
- *
- * @param user being validated
- * @return true if the user name is valid.
- */
- protected boolean isValidUser(String user) {
- return USER_PATTERN.matcher(user).matches();
- }
-
-}
diff --git a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/InetAddressValidator.java b/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/InetAddressValidator.java
deleted file mode 100644
index ff8ce82..0000000
--- a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/InetAddressValidator.java
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.openestate.is24.restapi.utils.validator;
-
-import java.io.Serializable;
-
-/**
- * InetAddress validation and conversion routines (java.net.InetAddress
).
This class provides methods to validate a candidate IP address. - * - *
- * This class is a Singleton; you can retrieve the instance via the {@link #getInstance()} method. - *
- * - * @version $Revision: 1227719 $ - * @since Validator 1.4 - */ -public class InetAddressValidator implements Serializable { - - private static final long serialVersionUID = -919201640201914789L; - - private static final String IPV4_REGEX = - "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"; - - /** - * Singleton instance of this class. - */ - private static final InetAddressValidator VALIDATOR = new InetAddressValidator(); - - /** IPv4 RegexValidator */ - private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX); - - /** - * Returns the singleton instance of this validator. - * @return the singleton instance of this validator - */ - public static InetAddressValidator getInstance() { - return VALIDATOR; - } - - /** - * Checks if the specified string is a valid IP address. - * @param inetAddress the string to validate - * @return true if the string validates as an IP address - */ - public boolean isValid(String inetAddress) { - return isValidInet4Address(inetAddress); - } - - /** - * Validates an IPv4 address. Returns true if valid. - * @param inet4Address the IPv4 address to validate - * @return true if the argument contains a valid IPv4 address - */ - public boolean isValidInet4Address(String inet4Address) { - // verify that address conforms to generic IPv4 format - String[] groups = ipv4Validator.match(inet4Address); - - if (groups == null) return false; - - // verify that address subgroups are legal - for (int i = 0; i <= 3; i++) { - String ipSegment = groups[i]; - if (ipSegment == null || ipSegment.length() <= 0) { - return false; - } - - int iIpSegment = 0; - - try { - iIpSegment = Integer.parseInt(ipSegment); - } catch(NumberFormatException e) { - return false; - } - - if (iIpSegment > 255) { - return false; - } - - } - - return true; - } -} diff --git a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/RegexValidator.java b/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/RegexValidator.java deleted file mode 100644 index 2f9e006..0000000 --- a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/RegexValidator.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openestate.is24.restapi.utils.validator; - -import java.io.Serializable; -import java.util.regex.Pattern; -import java.util.regex.Matcher; - -/** - * Regular Expression validation (using JDK 1.4+ regex support). - *- * Construct the validator either for a single regular expression or a set (array) of - * regular expressions. By default validation is case sensitive but constructors - * are provided to allow case in-sensitive validation. For example to create - * a validator which does case in-sensitive validation for a set of regular - * expressions: - *
- * String[] regexs = new String[] {...}; - * RegexValidator validator = new RegexValidator(regexs, false); - *- *
- *
true
or false
:boolean valid = validator.isValid(value);
String result = validator.validate(value);
String[] result = validator.match(value);
- * Cached instances pre-compile and re-use {@link Pattern}(s) - which according
- * to the {@link Pattern} API are safe to use in a multi-threaded environment.
- *
- * @version $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Thu, 05 Jan 2012) $
- * @since Validator 1.4
- */
-public class RegexValidator implements Serializable {
-
- private static final long serialVersionUID = -8832409930574867162L;
-
- private final Pattern[] patterns;
-
- /**
- * Construct a case sensitive validator for a single
- * regular expression.
- *
- * @param regex The regular expression this validator will
- * validate against
- */
- public RegexValidator(String regex) {
- this(regex, true);
- }
-
- /**
- * Construct a validator for a single regular expression
- * with the specified case sensitivity.
- *
- * @param regex The regular expression this validator will
- * validate against
- * @param caseSensitive when true
matching is case
- * sensitive, otherwise matching is case in-sensitive
- */
- public RegexValidator(String regex, boolean caseSensitive) {
- this(new String[] {regex}, caseSensitive);
- }
-
- /**
- * Construct a case sensitive validator that matches any one
- * of the set of regular expressions.
- *
- * @param regexs The set of regular expressions this validator will
- * validate against
- */
- public RegexValidator(String[] regexs) {
- this(regexs, true);
- }
-
- /**
- * Construct a validator that matches any one of the set of regular
- * expressions with the specified case sensitivity.
- *
- * @param regexs The set of regular expressions this validator will
- * validate against
- * @param caseSensitive when true
matching is case
- * sensitive, otherwise matching is case in-sensitive
- */
- public RegexValidator(String[] regexs, boolean caseSensitive) {
- if (regexs == null || regexs.length == 0) {
- throw new IllegalArgumentException("Regular expressions are missing");
- }
- patterns = new Pattern[regexs.length];
- int flags = (caseSensitive ? 0: Pattern.CASE_INSENSITIVE);
- for (int i = 0; i < regexs.length; i++) {
- if (regexs[i] == null || regexs[i].length() == 0) {
- throw new IllegalArgumentException("Regular expression[" + i + "] is missing");
- }
- patterns[i] = Pattern.compile(regexs[i], flags);
- }
- }
-
- /**
- * Validate a value against the set of regular expressions.
- *
- * @param value The value to validate.
- * @return true
if the value is valid
- * otherwise false
.
- */
- public boolean isValid(String value) {
- if (value == null) {
- return false;
- }
- for (int i = 0; i < patterns.length; i++) {
- if (patterns[i].matcher(value).matches()) {
- return true;
- }
- }
- return false;
- }
-
- /**
- * Validate a value against the set of regular expressions
- * returning the array of matched groups.
- *
- * @param value The value to validate.
- * @return String array of the groups matched if
- * valid or null
if invalid
- */
- public String[] match(String value) {
- if (value == null) {
- return null;
- }
- for (int i = 0; i < patterns.length; i++) {
- Matcher matcher = patterns[i].matcher(value);
- if (matcher.matches()) {
- int count = matcher.groupCount();
- String[] groups = new String[count];
- for (int j = 0; j < count; j++) {
- groups[j] = matcher.group(j+1);
- }
- return groups;
- }
- }
- return null;
- }
-
-
- /**
- * Validate a value against the set of regular expressions
- * returning a String value of the aggregated groups.
- *
- * @param value The value to validate.
- * @return Aggregated String value comprised of the
- * groups matched if valid or null
if invalid
- */
- public String validate(String value) {
- if (value == null) {
- return null;
- }
- for (int i = 0; i < patterns.length; i++) {
- Matcher matcher = patterns[i].matcher(value);
- if (matcher.matches()) {
- int count = matcher.groupCount();
- if (count == 1) {
- return matcher.group(1);
- }
- StringBuffer buffer = new StringBuffer();
- for (int j = 0; j < count; j++) {
- String component = matcher.group(j+1);
- if (component != null) {
- buffer.append(component);
- }
- }
- return buffer.toString();
- }
- }
- return null;
- }
-
- /**
- * Provide a String representation of this validator.
- * @return A String representation of this validator
- */
- public String toString() {
- StringBuffer buffer = new StringBuffer();
- buffer.append("RegexValidator{");
- for (int i = 0; i < patterns.length; i++) {
- if (i > 0) {
- buffer.append(",");
- }
- buffer.append(patterns[i].pattern());
- }
- buffer.append("}");
- return buffer.toString();
- }
-
-}
diff --git a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/package-info.java b/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/package-info.java
deleted file mode 100644
index d631257..0000000
--- a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/validator/package-info.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright 2014-2016 OpenEstate.org.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * Some classes from the "commons-validator" library of the Apache Foundation.
- *
- * @see commons-validator
- */
-
-package org.openestate.is24.restapi.utils.validator;
diff --git a/OpenEstate-IS24-REST/src/test/java/org/openestate/is24/restapi/XmlUtilsTest.java b/OpenEstate-IS24-REST/src/test/java/org/openestate/is24/restapi/XmlUtilsTest.java
index f112001..138e1f2 100644
--- a/OpenEstate-IS24-REST/src/test/java/org/openestate/is24/restapi/XmlUtilsTest.java
+++ b/OpenEstate-IS24-REST/src/test/java/org/openestate/is24/restapi/XmlUtilsTest.java
@@ -33,6 +33,54 @@ public class XmlUtilsTest
{
//private final static Logger LOGGER = LoggerFactory.getLogger( XmlUtilsTest.class );
+ @Test
+ public void testPrintEmail()
+ {
+ String[] validEmails = new String[]{
+ "example@example.com",
+ "example@example.de",
+ "example@example.net",
+ "example@example.biz",
+ "example@example.immobilien",
+ };
+
+ String[] invalidEmails = new String[]{
+ "",
+ "@",
+ "@example.com",
+ ".@.com",
+ ".@localhost",
+ "example@.com",
+ "example@",
+ };
+
+ for (String email : validEmails)
+ {
+ try
+ {
+ Assert.assertEquals( "print valid " + email,
+ email, XmlUtils.printEmail( email ) );
+ }
+ catch (IllegalArgumentException ex)
+ {
+ Assert.fail( "valid email '" + email + "' is not printed" );
+ }
+ }
+
+ for (String email : invalidEmails)
+ {
+ try
+ {
+ String m = XmlUtils.printEmail( email );
+ Assert.fail( "invalid email '" + email + "' was printed as '" + m + "'" );
+ }
+ catch (IllegalArgumentException ex)
+ {
+ Assert.assertTrue( "print invalid " + email, true );
+ }
+ }
+ }
+
@Test
public void testPrintText()
{