1 7 8 package javax.security.auth.x500; 9 10 import java.io.*; 11 import java.security.Principal ; 12 import sun.security.x509.X500Name; 13 import sun.security.util.*; 14 15 43 public final class X500Principal implements Principal , java.io.Serializable { 44 45 private static final long serialVersionUID = -500463348111345721L; 46 47 50 public static final String RFC1779 = "RFC1779"; 51 54 public static final String RFC2253 = "RFC2253"; 55 58 public static final String CANONICAL = "CANONICAL"; 59 60 65 private transient X500Name thisX500Name; 66 67 74 X500Principal(X500Name x500Name) { 75 thisX500Name = x500Name; 76 } 77 78 99 public X500Principal(String name) { 100 if (name == null) { 101 throw new NullPointerException 102 (sun.security.util.ResourcesMgr.getString 103 ("provided null name")); 104 } 105 106 try { 107 thisX500Name = new X500Name(name); 108 } catch (Exception e) { 109 IllegalArgumentException iae = new IllegalArgumentException 110 ("improperly specified input name: " + name); 111 iae.initCause(e); 112 throw iae; 113 } 114 115 } 116 117 151 public X500Principal(byte[] name) { 152 try { 153 thisX500Name = new X500Name(name); 154 } catch (Exception e) { 155 IllegalArgumentException iae = new IllegalArgumentException 156 ("improperly specified input name"); 157 iae.initCause(e); 158 throw iae; 159 } 160 } 161 162 180 public X500Principal(InputStream is) { 181 if (is == null) { 182 throw new NullPointerException ("provided null input stream"); 183 } 184 185 try { 186 if (is.markSupported()) 187 is.mark(is.available() + 1); 188 DerValue der = new DerValue(is); 189 thisX500Name = new X500Name(der.data); 190 } catch (Exception e) { 191 if (is.markSupported()) { 192 try { 193 is.reset(); 194 } catch (IOException ioe) { 195 IllegalArgumentException iae = new IllegalArgumentException 196 ("improperly specified input stream " + 197 ("and unable to reset input stream")); 198 iae.initCause(e); 199 throw iae; 200 } 201 } 202 IllegalArgumentException iae = new IllegalArgumentException 203 ("improperly specified input stream"); 204 iae.initCause(e); 205 throw iae; 206 } 207 } 208 209 218 public String getName() { 219 return getName(X500Principal.RFC2253); 220 } 221 222 283 public String getName(String format) { 284 if (format != null) { 285 if (format.equalsIgnoreCase(RFC1779)) { 286 return thisX500Name.getRFC1779Name(); 287 } else if (format.equalsIgnoreCase(RFC2253)) { 288 return thisX500Name.getRFC2253Name(); 289 } else if (format.equalsIgnoreCase(CANONICAL)) { 290 return thisX500Name.getRFC2253CanonicalName(); 291 } 292 } 293 throw new IllegalArgumentException ("invalid format specified"); 294 } 295 296 307 public byte[] getEncoded() { 308 try { 309 return thisX500Name.getEncoded(); 310 } catch (IOException e) { 311 throw new RuntimeException ("unable to get encoding", e); 312 } 313 } 314 315 321 public String toString() { 322 return thisX500Name.toString(); 323 } 324 325 343 public boolean equals(Object o) { 344 if (this == o) { 345 return true; 346 } 347 if (o instanceof X500Principal == false) { 348 return false; 349 } 350 X500Principal other = (X500Principal )o; 351 return this.thisX500Name.equals(other.thisX500Name); 352 } 353 354 362 public int hashCode() { 363 return thisX500Name.hashCode(); 364 } 365 366 373 private void writeObject(java.io.ObjectOutputStream s) 374 throws IOException { 375 s.writeObject(thisX500Name.getEncodedInternal()); 376 } 377 378 381 private void readObject(java.io.ObjectInputStream s) 382 throws java.io.IOException , 383 java.io.NotActiveException , 384 ClassNotFoundException { 385 386 thisX500Name = new X500Name((byte[])s.readObject()); 388 } 389 } 390 | Popular Tags |