001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: MailAddressValidator.java,v 1.3 2008/06/25 05:41:48 qcheng Exp $ 026 * 027 */ 028 029/** 030 * Portions Copyrighted [2011] [ForgeRock AS] 031 */ 032package com.iplanet.ums.validation; 033 034import java.util.StringTokenizer; 035import java.util.Vector; 036 037/** 038 * Validates mail address This class is constructed using default(noarguments) 039 * constructor and mail address is passed to validate function with optional 040 * rules The passed mail address is validated for authenticity and boolean value 041 * is returned accordingly. 042 * 043 * @supported.all.api 044 */ 045public class MailAddressValidator implements IValidator { 046 047 /** 048 * Determines if the value is a valid email address string 049 * 050 * @param value 051 * string value to validate 052 * @param rule 053 * not used by this method 054 * @return true if the value represents a valid email address string 055 */ 056 public boolean validate(String value, String rule) { 057 return validate(value); 058 } 059 060 /** 061 * Check if the given email address is valid 062 * 063 * @param addr 064 * value to test 065 * @return true if the string contains valid email characters 066 */ 067 public boolean validate(String addr) { 068 String namePart; 069 String domainPart; 070 StringTokenizer tok; 071 072 int endindex = addr.indexOf('@'); 073 074 if (endindex == -1) { 075 return false; 076 } 077 078 namePart = addr.substring(0, endindex); 079 domainPart = addr.substring(endindex + 1, addr.length()); 080 081 tok = new StringTokenizer(namePart, "."); 082 while (tok.hasMoreTokens()) { 083 if (!isValidLocalPart(tok.nextToken())) { 084 return false; 085 } 086 } 087 088 DomainValidator validator = new DomainValidator(); 089 if (!validator.validate(domainPart)) { 090 return false; 091 } 092 093 return true; 094 } 095 096 /** 097 * Determines if the character is in inValidChars array 098 */ 099 private boolean isValidChar(String value) { 100 if (inValidChars.contains(value)) { 101 return false; 102 } else { 103 return true; 104 } 105 } 106 107 /** 108 * Check if the given name is valid 109 */ 110 private boolean isValidLocalPart(String atom) { 111 char ch; 112 int val; 113 Character character; 114 115 StringBuilder buf = new StringBuilder(atom); 116 117 for (int i = 0; i < buf.length(); i++) { 118 119 ch = buf.charAt(i); 120 val = ch; 121 character = new Character(ch); 122 123 if (val < 33 || val > 126 || !isValidChar(character.toString())) { 124 return false; 125 } 126 } 127 128 return true; 129 } 130 131 private static Vector inValidChars = new Vector(); 132 133 static { 134 // rfc822 special 135 inValidChars.addElement("("); 136 inValidChars.addElement(")"); 137 inValidChars.addElement("<"); 138 inValidChars.addElement(">"); 139 inValidChars.addElement("@"); 140 inValidChars.addElement(","); 141 inValidChars.addElement(";"); 142 inValidChars.addElement(":"); 143 inValidChars.addElement("\\"); 144 inValidChars.addElement("\""); 145 inValidChars.addElement("."); 146 inValidChars.addElement("["); 147 inValidChars.addElement("]"); 148 inValidChars.addElement(" "); 149 inValidChars.addElement("\t"); 150 // dangerous characters 151 inValidChars.addElement("!"); 152 inValidChars.addElement("%"); 153 inValidChars.addElement("+"); 154 inValidChars.addElement("/"); 155 // annoying/confusing characters 156 inValidChars.addElement("="); 157 inValidChars.addElement("{"); 158 inValidChars.addElement("}"); 159 inValidChars.addElement("#"); 160 // unix shell met-characters 161 inValidChars.addElement("$"); 162 inValidChars.addElement("&"); 163 inValidChars.addElement("*"); 164 inValidChars.addElement("?"); 165 inValidChars.addElement("|"); 166 inValidChars.addElement("~"); 167 } 168 169}
Copyright © 2010-2017, ForgeRock All Rights Reserved.