📄 x500principal.java
字号:
/* X500Principal.java -- X.500 principal. Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.security.auth.x500;import gnu.java.security.OID;import gnu.java.security.der.DER;import gnu.java.security.der.DERReader;import gnu.java.security.der.DERValue;import java.io.ByteArrayInputStream;import java.io.EOFException;import java.io.IOException;import java.io.InputStream;import java.io.NotActiveException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Reader;import java.io.Serializable;import java.io.StringReader;import java.security.Principal;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.LinkedList;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Set;public final class X500Principal implements Principal, Serializable{ private static final long serialVersionUID = -500463348111345721L; // Constants and fields. // ------------------------------------------------------------------------ public static final String CANONICAL = "CANONICAL"; public static final String RFC1779 = "RFC1779"; public static final String RFC2253 = "RFC2253"; private static final OID CN = new OID("2.5.4.3"); private static final OID C = new OID("2.5.4.6"); private static final OID L = new OID("2.5.4.7"); private static final OID ST = new OID("2.5.4.8"); private static final OID STREET = new OID("2.5.4.9"); private static final OID O = new OID("2.5.4.10"); private static final OID OU = new OID("2.5.4.11"); private static final OID DC = new OID("0.9.2342.19200300.100.1.25"); private static final OID UID = new OID("0.9.2342.19200300.100.1.1"); private transient List components; private transient Map currentRdn; private transient boolean fixed; private transient byte[] encoded; // Constructors. // ------------------------------------------------------------------------ private X500Principal() { components = new LinkedList(); currentRdn = new LinkedHashMap(); components.add (currentRdn); } public X500Principal (String name) { this(); if (name == null) throw new NullPointerException(); try { parseString (name); } catch (IOException ioe) { IllegalArgumentException iae = new IllegalArgumentException("malformed name"); iae.initCause (ioe); throw iae; } } public X500Principal (byte[] encoded) { this(new ByteArrayInputStream (encoded)); } public X500Principal (InputStream encoded) { this(); try { parseDer (encoded); } catch (IOException ioe) { throw new IllegalArgumentException (ioe.toString()); } } // Instance methods. // ------------------------------------------------------------------------ public int hashCode() { int result = size(); for (int i = 0; i < size(); ++i) { Map m = (Map) components.get(i); for (Iterator it2 = m.entrySet().iterator(); it2.hasNext(); ) { Map.Entry e = (Map.Entry) it2.next(); // We don't bother looking at the value of the entry. result = result * 31 + ((OID) e.getKey()).hashCode(); } } return result; } public boolean equals(Object o) { if (!(o instanceof X500Principal)) return false; if (size() != ((X500Principal) o).size()) return false; for (int i = 0; i < size(); i++) { Map m = (Map) components.get (i); for (Iterator it2 = m.entrySet().iterator(); it2.hasNext(); ) { Map.Entry e = (Map.Entry) it2.next(); OID oid = (OID) e.getKey(); String v1 = (String) e.getValue(); String v2 = ((X500Principal) o).getComponent (oid, i); if (v2 == null) return false; if (!compressWS (v1).equalsIgnoreCase (compressWS (v2))) return false; } } return true; } public byte[] getEncoded() { if (encoded == null) encodeDer(); return (byte[]) encoded.clone(); } public String getName() { return getName (RFC2253); } public String getName (final String format) { boolean rfc2253 = RFC2253.equalsIgnoreCase (format) || CANONICAL.equalsIgnoreCase (format); boolean rfc1779 = RFC1779.equalsIgnoreCase (format); boolean canon = CANONICAL.equalsIgnoreCase (format); if (! (rfc2253 || rfc1779 || canon)) throw new IllegalArgumentException ("unsupported format " + format); StringBuffer str = new StringBuffer(); for (Iterator it = components.iterator(); it.hasNext(); ) { Map m = (Map) it.next(); for (Iterator it2 = m.entrySet().iterator(); it2.hasNext(); ) { Map.Entry entry = (Map.Entry) it2.next(); OID oid = (OID) entry.getKey(); String value = (String) entry.getValue(); if (oid.equals (CN)) str.append ("CN"); else if (oid.equals (C)) str.append ("C"); else if (oid.equals (L)) str.append ("L"); else if (oid.equals (ST)) str.append ("ST"); else if (oid.equals (STREET)) str.append ("STREET"); else if (oid.equals (O)) str.append ("O"); else if (oid.equals (OU)) str.append ("OU"); else if (oid.equals (DC) && rfc2253) str.append ("DC"); else if (oid.equals (UID) && rfc2253) str.append ("UID"); else str.append (oid.toString()); str.append('='); str.append(value); if (it2.hasNext()) str.append('+'); } if (it.hasNext()) str.append(','); } if (canon) return str.toString().toUpperCase (Locale.US).toLowerCase (Locale.US); return str.toString(); } public String toString() { return getName (RFC2253); } // Serialization methods. // ------------------------------------------------------------------------ private void writeObject (ObjectOutputStream out) throws IOException { if (encoded != null) encodeDer(); out.writeObject (encoded); } private void readObject (ObjectInputStream in) throws IOException, NotActiveException, ClassNotFoundException { byte[] buf = (byte[]) in.readObject(); parseDer (new ByteArrayInputStream (buf)); } // Own methods. // ------------------------------------------------------------------------- private int size() { return components.size(); } private String getComponent(OID oid, int rdn) { if (rdn >= size()) return null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -