2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / javax / security / auth / x500 / X500Principal.java
blob036cab7f91aa4d948eccb610ee0a08c41707b3a7
1 /* X500Principal.java -- X.500 principal.
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.security.auth.x500;
41 import java.io.ByteArrayInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.NotActiveException;
45 import java.io.ObjectInputStream;
46 import java.io.ObjectOutputStream;
47 import java.io.Serializable;
49 import java.security.Principal;
51 import java.util.HashSet;
52 import java.util.LinkedList;
54 import gnu.java.security.x509.X500DistinguishedName;
56 public final class X500Principal implements Principal, Serializable
58 private static final long serialVersionUID = -500463348111345721L;
60 // Constants and fields.
61 // ------------------------------------------------------------------------
63 public static final String CANONICAL = "CANONICAL";
65 public static final String RFC1779 = "RFC1779";
67 public static final String RFC2253 = "RFC2253";
69 private transient X500DistinguishedName name;
71 // Constructors.
72 // ------------------------------------------------------------------------
74 public X500Principal(String name)
76 if (name == null)
77 throw new NullPointerException();
78 this.name = new X500DistinguishedName(name);
81 public X500Principal(byte[] encoded)
83 try
85 name = new X500DistinguishedName(encoded);
87 catch (IOException ioe)
89 throw new IllegalArgumentException(ioe.toString());
93 public X500Principal(InputStream encoded)
95 try
97 name = new X500DistinguishedName(encoded);
99 catch (IOException ioe)
101 throw new IllegalArgumentException(ioe.toString());
105 // Instance methods.
106 // ------------------------------------------------------------------------
108 public boolean equals(Object o)
110 return ((X500Principal) o).name.equals(name);
113 public byte[] getEncoded()
115 return name.getEncoded();
118 public String getName()
120 return getName(RFC2253);
123 public String getName(String format)
125 if (format.equalsIgnoreCase(RFC2253))
126 return name.toRFC2253();
127 else if (format.equalsIgnoreCase(RFC1779))
128 return name.toRFC1779();
129 else if (format.equalsIgnoreCase(CANONICAL))
130 return name.toCanonical();
131 throw new IllegalArgumentException("unsupported format " + format);
134 // Serialization methods.
135 // ------------------------------------------------------------------------
137 private void writeObject(ObjectOutputStream out) throws IOException
139 out.writeObject(name.getEncoded());
142 private void readObject(ObjectInputStream in)
143 throws IOException, NotActiveException, ClassNotFoundException
145 byte[] buf = (byte[]) in.readObject();
146 name = new X500DistinguishedName(buf);