001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.security.auth.x500;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.Serializable;
025: import java.security.Principal;
026:
027: import org.apache.harmony.auth.internal.nls.Messages;
028: import org.apache.harmony.security.x501.Name;
029:
030: public final class X500Principal implements Serializable, Principal {
031:
032: private static final long serialVersionUID = -500463348111345721L;
033:
034: public static final String CANONICAL = "CANONICAL"; //$NON-NLS-1$
035:
036: public static final String RFC1779 = "RFC1779"; //$NON-NLS-1$
037:
038: public static final String RFC2253 = "RFC2253"; //$NON-NLS-1$
039:
040: //Distinguished Name
041: private transient Name dn;
042:
043: public X500Principal(byte[] name) {
044: super ();
045: if (name == null) {
046: throw new IllegalArgumentException(Messages
047: .getString("auth.00")); //$NON-NLS-1$
048: }
049: try {
050: // FIXME dn = new Name(name);
051: dn = (Name) Name.ASN1.decode(name);
052: } catch (IOException e) {
053: IllegalArgumentException iae = new IllegalArgumentException(
054: Messages.getString("auth.2B")); //$NON-NLS-1$
055: iae.initCause(e);
056: throw iae;
057: }
058: }
059:
060: public X500Principal(InputStream in) {
061: super ();
062: if (in == null) {
063: throw new NullPointerException(Messages
064: .getString("auth.2C")); //$NON-NLS-1$
065: }
066: try {
067: // FIXME dn = new Name(is);
068: dn = (Name) Name.ASN1.decode(in);
069: } catch (IOException e) {
070: IllegalArgumentException iae = new IllegalArgumentException(
071: Messages.getString("auth.2B")); //$NON-NLS-1$
072: iae.initCause(e);
073: throw iae;
074: }
075: }
076:
077: public X500Principal(String name) {
078: super ();
079: if (name == null) {
080: throw new NullPointerException(Messages
081: .getString("auth.00")); //$NON-NLS-1$
082: }
083: try {
084: dn = new Name(name);
085: } catch (IOException e) {
086: IllegalArgumentException iae = new IllegalArgumentException(
087: Messages.getString("auth.2D")); //$NON-NLS-1$
088: iae.initCause(e);
089: throw iae;
090: }
091: }
092:
093: @Override
094: public boolean equals(Object o) {
095: if (this == o) {
096: return true;
097: }
098: if (o == null || this .getClass() != o.getClass()) {
099: return false;
100: }
101: X500Principal principal = (X500Principal) o;
102: return dn.getName(CANONICAL).equals(
103: principal.dn.getName(CANONICAL));
104: }
105:
106: public byte[] getEncoded() {
107: byte[] src = dn.getEncoded();
108: byte[] dst = new byte[src.length];
109: System.arraycopy(src, 0, dst, 0, dst.length);
110: return dst;
111: }
112:
113: public String getName() {
114: return dn.getName(RFC2253);
115: }
116:
117: public String getName(String format) {
118: return dn.getName(format);
119: }
120:
121: @Override
122: public int hashCode() {
123: return dn.getName(CANONICAL).hashCode();
124: }
125:
126: @Override
127: public String toString() {
128: return dn.getName(RFC1779);
129: }
130:
131: private void writeObject(ObjectOutputStream out) throws IOException {
132: out.writeObject(dn.getEncoded());
133: }
134:
135: private void readObject(ObjectInputStream in) throws IOException,
136: ClassNotFoundException {
137:
138: dn = (Name) Name.ASN1.decode((byte[]) in.readObject());
139: }
140: }
|