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    package org.apache.geronimo.corba.security.config.css;
018    
019    import org.omg.CORBA.Any;
020    import org.omg.CSI.GSS_NT_ExportedNameHelper;
021    import org.omg.CSI.IdentityToken;
022    import org.omg.GSSUP.GSSUPMechOID;
023    import org.omg.IOP.CodecPackage.InvalidTypeForEncoding;
024    
025    import org.apache.geronimo.corba.util.Util;
026    
027    
028    /**
029     * @version $Revision: 503493 $ $Date: 2007-02-04 13:47:55 -0800 (Sun, 04 Feb 2007) $
030     */
031    public class CSSSASITTPrincipalNameStatic implements CSSSASIdentityToken {
032    
033        private final String oid;
034        private final String name;
035        private transient IdentityToken token;
036    
037        public CSSSASITTPrincipalNameStatic(String name) {
038    
039            this(GSSUPMechOID.value.substring(4), name);
040        }
041    
042        public CSSSASITTPrincipalNameStatic(String oid, String name) {
043            this.oid = (oid == null ? GSSUPMechOID.value.substring(4) : oid);
044            this.name = name;
045        }
046    
047        public IdentityToken encodeIdentityToken() {
048    
049            if (token == null) {
050                Any any = Util.getORB().create_any();
051                //TODO consider including a domain in this scoped-username
052                GSS_NT_ExportedNameHelper.insert(any, Util.encodeGSSExportName(oid, name));
053    
054                byte[] encoding = null;
055                try {
056                    encoding = Util.getCodec().encode_value(any);
057                } catch (InvalidTypeForEncoding itfe) {
058                    throw new IllegalStateException("Unable to encode principal name '" + name + "' " + itfe, itfe);
059                }
060    
061                token = new IdentityToken();
062                token.principal_name(encoding);
063            }
064            return token;
065        }
066    
067        public String toString() {
068            StringBuffer buf = new StringBuffer();
069            toString("", buf);
070            return buf.toString();
071        }
072    
073        public void toString(String spaces, StringBuffer buf) {
074            String moreSpaces = spaces + "  ";
075            buf.append(spaces).append("CSSSASITTPrincipalNameStatic: [\n");
076            buf.append(moreSpaces).append("oid: ").append(oid).append("\n");
077            buf.append(moreSpaces).append("name: ").append(name).append("\n");
078            buf.append(spaces).append("]\n");
079        }
080    
081    }