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.tss;
018    
019    import java.io.Serializable;
020    import javax.security.auth.Subject;
021    
022    import org.omg.CORBA.ORB;
023    import org.omg.CSI.EstablishContext;
024    import org.omg.CSIIOP.CompoundSecMech;
025    import org.omg.IOP.Codec;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    import org.apache.geronimo.corba.security.SASException;
030    import org.apache.geronimo.corba.security.config.ConfigUtil;
031    
032    
033    /**
034     * @version $Rev: 503274 $ $Date: 2007-02-03 10:19:18 -0800 (Sat, 03 Feb 2007) $
035     */
036    public class TSSCompoundSecMechConfig implements Serializable {
037    
038        private final static Log log = LogFactory.getLog(TSSCompoundSecMechConfig.class);
039        private TSSTransportMechConfig transport_mech;
040        private TSSASMechConfig as_mech;
041        private TSSSASMechConfig sas_mech;
042    
043        public TSSTransportMechConfig getTransport_mech() {
044            return transport_mech;
045        }
046    
047        public void setTransport_mech(TSSTransportMechConfig transport_mech) {
048            this.transport_mech = transport_mech;
049        }
050    
051        public TSSASMechConfig getAs_mech() {
052            return as_mech;
053        }
054    
055        public void setAs_mech(TSSASMechConfig as_mech) {
056            this.as_mech = as_mech;
057        }
058    
059        public TSSSASMechConfig getSas_mech() {
060            return sas_mech;
061        }
062    
063        public void setSas_mech(TSSSASMechConfig sas_mech) {
064            this.sas_mech = sas_mech;
065        }
066    
067        public short getSupports() {
068            short result = 0;
069    
070            if (transport_mech != null) result |= transport_mech.getSupports();
071            if (as_mech != null) result |= as_mech.getSupports();
072            if (sas_mech != null) result |= sas_mech.getSupports();
073    
074            return result;
075        }
076    
077        public short getRequires() {
078            short result = 0;
079    
080            if (transport_mech != null) result |= transport_mech.getRequires();
081            if (as_mech != null) result |= as_mech.getRequires();
082            if (sas_mech != null) result |= sas_mech.getRequires();
083    
084            return result;
085        }
086    
087        public CompoundSecMech encodeIOR(ORB orb, Codec codec) throws Exception {
088            CompoundSecMech result = new CompoundSecMech();
089    
090            result.target_requires = 0;
091    
092            // transport mechanism
093            result.transport_mech = transport_mech.encodeIOR(orb, codec);
094            result.target_requires |= transport_mech.getRequires();
095            if (log.isDebugEnabled()) {
096                log.debug("transport adds supported: " + ConfigUtil.flags(transport_mech.getSupports()));
097                log.debug("transport adds required: " + ConfigUtil.flags(transport_mech.getRequires()));
098            }
099    
100            // AS_ContextSec
101            result.as_context_mech = as_mech.encodeIOR(orb, codec);
102            result.target_requires |= as_mech.getRequires();
103            if (log.isDebugEnabled()) {
104                log.debug("AS adds supported: " + ConfigUtil.flags(as_mech.getSupports()));
105                log.debug("AS adds required: " + ConfigUtil.flags(as_mech.getRequires()));
106            }
107    
108            // SAS_ContextSec
109            result.sas_context_mech = sas_mech.encodeIOR(orb, codec);
110            result.target_requires |= sas_mech.getRequires();
111            if (log.isDebugEnabled()) {
112                log.debug("SAS adds supported: " + ConfigUtil.flags(sas_mech.getSupports()));
113                log.debug("SAS adds required: " + ConfigUtil.flags(sas_mech.getRequires()));
114    
115                log.debug("REQUIRES: " + ConfigUtil.flags(result.target_requires));
116            }
117    
118    
119            return result;
120        }
121    
122        public static TSSCompoundSecMechConfig decodeIOR(Codec codec, CompoundSecMech compoundSecMech) throws Exception {
123            TSSCompoundSecMechConfig result = new TSSCompoundSecMechConfig();
124    
125            result.setTransport_mech(TSSTransportMechConfig.decodeIOR(codec, compoundSecMech.transport_mech));
126            result.setAs_mech(TSSASMechConfig.decodeIOR(compoundSecMech.as_context_mech));
127            result.setSas_mech(new TSSSASMechConfig(compoundSecMech.sas_context_mech));
128    
129            return result;
130        }
131    
132        public Subject check(EstablishContext msg) throws SASException {
133            Subject asSubject = as_mech.check(msg);
134            Subject sasSubject = sas_mech.check(msg);
135    
136            if (sasSubject != null) return sasSubject;
137    
138            return asSubject;
139        }
140    
141        public String toString() {
142            StringBuffer buf = new StringBuffer();
143            toString("", buf);
144            return buf.toString();
145        }
146    
147        void toString(String spaces, StringBuffer buf) {
148            String moreSpaces = spaces + "  ";
149            buf.append(spaces).append("TSSCompoundSecMechConfig: [\n");
150            buf.append(moreSpaces).append("SUPPORTS (aggregate): ").append(ConfigUtil.flags(getSupports())).append("\n");
151            buf.append(moreSpaces).append("REQUIRES (aggregate): ").append(ConfigUtil.flags(getRequires())).append("\n");
152            if (transport_mech != null) {
153                transport_mech.toString(moreSpaces, buf);
154            }
155            if (as_mech != null) {
156                as_mech.toString(moreSpaces, buf);
157            }
158            if (sas_mech != null) {
159                sas_mech.toString(moreSpaces, buf);
160            }
161            buf.append(spaces).append("]\n");
162        }
163    
164    
165    }