All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aoindustries.aoserv.client.validator.UserId Maven / Gradle / Ivy

There is a newer version: 1.92.0
Show newest version
/*
 * aoserv-client - Java client for the AOServ platform.
 * Copyright (C) 2010-2013, 2016  AO Industries, Inc.
 *     [email protected]
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aoserv-client.
 *
 * aoserv-client is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aoserv-client is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aoserv-client.  If not, see .
 */
package com.aoindustries.aoserv.client.validator;

import com.aoindustries.aoserv.client.DtoFactory;
import com.aoindustries.io.FastExternalizable;
import com.aoindustries.io.FastObjectInput;
import com.aoindustries.io.FastObjectOutput;
import com.aoindustries.util.Internable;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInput;
import java.io.ObjectInputValidation;
import java.io.ObjectOutput;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Represents a user ID that may be used by certain types of accounts.  User ids must:
 * 
    *
  • Be non-null
  • *
  • Be non-empty
  • *
  • Be between 1 and 255 characters
  • *
  • Must start with [a-z]
  • *
  • Uses only ASCII 0x21 through 0x7f, excluding space , : ( ) [ ] ' " | & ; A-Z \ /
  • *
  • * If contains any @ symbol, must also be a valid email address. Please note that the * reverse is not implied - email addresses may exist that are not valid user ids. *
  • *
  • May not start with cyrus@
  • *
* * @see Email#validate(java.lang.String) * * @author AO Industries, Inc. */ final public class UserId implements Comparable, FastExternalizable, ObjectInputValidation, DtoFactory, Internable { public static final int MAX_LENGTH=255; /** * Validates a user id. */ public static ValidationResult validate(String id) { if(id==null) return new InvalidResult(ApplicationResources.accessor, "UserId.validate.isNull"); int len = id.length(); if(len==0) return new InvalidResult(ApplicationResources.accessor, "UserId.validate.isEmpty"); if(len > MAX_LENGTH) return new InvalidResult(ApplicationResources.accessor, "UserId.validate.tooLong", MAX_LENGTH, len); // The first character must be [a-z] char ch = id.charAt(0); if(ch < 'a' || ch > 'z') return new InvalidResult(ApplicationResources.accessor, "UserId.validate.startAToZ"); // The rest may have additional characters boolean hasAt = false; for (int c = 1; c < len; c++) { ch = id.charAt(c); if(ch==' ') return new InvalidResult(ApplicationResources.accessor, "UserId.validate.noSpace"); if(ch<=0x21 || ch>0x7f) return new InvalidResult(ApplicationResources.accessor, "UserId.validate.specialCharacter"); if(ch>='A' && ch<='Z') return new InvalidResult(ApplicationResources.accessor, "UserId.validate.noCapital"); switch(ch) { case ',' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.comma"); case ':' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.colon"); case '(' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.leftParen"); case ')' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.rightParen"); case '[' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.leftSquare"); case ']' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.rightSquare"); case '\'' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.apostrophe"); case '"' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.quote"); case '|' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.verticalBar"); case '&' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.ampersand"); case ';' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.semicolon"); case '\\' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.backslash"); case '/' : return new InvalidResult(ApplicationResources.accessor, "UserId.validate.slash"); case '@' : hasAt = true; break; } } // More strict at sign control is required for user@domain structure in Cyrus virtdomains. if(hasAt) { // Must also be a valid email address ValidationResult result = Email.validate(id); if(!result.isValid()) return result; if(id.startsWith("cyrus@")) return new InvalidResult(ApplicationResources.accessor, "UserId.validate.startWithCyrusAt"); } return ValidResult.getInstance(); } private static final ConcurrentMap interned = new ConcurrentHashMap<>(); /** * If id is null, returns null. */ public static UserId valueOf(String id) throws ValidationException { if(id==null) return null; //UserId existing = interned.get(id); //return existing!=null ? existing : new UserId(id); return new UserId(id); } /* public static UserId valueOfInterned(String id) throws ValidationException { UserId existing = interned.get(id); return existing!=null ? existing : new UserId(id).intern(); }*/ private String id; private UserId(String id) throws ValidationException { this.id = id; validate(); } private void validate() throws ValidationException { ValidationResult result = validate(id); if(!result.isValid()) throw new ValidationException(result); } @Override public boolean equals(Object O) { return O!=null && O instanceof UserId && id.equals(((UserId)O).id) ; } @Override public int hashCode() { return id.hashCode(); } @Override public int compareTo(UserId other) { return this==other ? 0 : id.compareTo(other.id); } @Override public String toString() { return id; } /** * Interns this id much in the same fashion as String.intern(). * * @see String#intern() */ @Override public UserId intern() { try { UserId existing = interned.get(id); if(existing==null) { String internedId = id.intern(); UserId addMe = id==internedId ? this : new UserId(internedId); existing = interned.putIfAbsent(internedId, addMe); if(existing==null) existing = addMe; } return existing; } catch(ValidationException err) { // Should not fail validation since original object passed throw new AssertionError(err.getMessage()); } } @Override public com.aoindustries.aoserv.client.dto.UserId getDto() { return new com.aoindustries.aoserv.client.dto.UserId(id); } // private static final long serialVersionUID = -837866431257794645L; public UserId() { } @Override public long getSerialVersionUID() { return serialVersionUID; } @Override public void writeExternal(ObjectOutput out) throws IOException { FastObjectOutput fastOut = FastObjectOutput.wrap(out); try { fastOut.writeFastUTF(id); } finally { fastOut.unwrap(); } } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { if(id!=null) throw new IllegalStateException(); FastObjectInput fastIn = FastObjectInput.wrap(in); try { id = fastIn.readFastUTF(); } finally { fastIn.unwrap(); } } @Override public void validateObject() throws InvalidObjectException { try { validate(); } catch(ValidationException err) { InvalidObjectException newErr = new InvalidObjectException(err.getMessage()); newErr.initCause(err); throw newErr; } } // }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy