001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.geronimo.tomcat.connector;
020    
021    import java.util.Map;
022    
023    import javax.net.ssl.KeyManagerFactory;
024    
025    import org.apache.geronimo.gbean.GBeanInfo;
026    import org.apache.geronimo.gbean.GBeanInfoBuilder;
027    import org.apache.geronimo.management.geronimo.WebManager;
028    import org.apache.geronimo.system.serverinfo.ServerInfo;
029    import org.apache.geronimo.tomcat.TomcatContainer;
030    
031    public abstract class AbstractHttp11ConnectorGBean extends BaseHttp11ConnectorGBean implements Http11Protocol {
032        private String keystoreFileName;
033    
034        private String truststoreFileName;
035    
036        private String algorithm;
037        
038        public AbstractHttp11ConnectorGBean(String name, Map initParams, String tomcatProtocol, String host, int port, TomcatContainer container, ServerInfo serverInfo) throws Exception {
039            super(name, initParams, tomcatProtocol, host, port, container, serverInfo);
040        }
041    
042        @Override
043        public int getDefaultPort() {
044            return 80;
045        }
046    
047        @Override
048        public String getGeronimoProtocol() {
049            return WebManager.PROTOCOL_HTTP;
050        }
051        
052        // Generic SSL
053        public String getAlgorithm() {
054            return algorithm;
055        }
056    
057        public String getCiphers() {
058            return (String) connector.getAttribute("ciphers");
059        }
060    
061        public boolean getClientAuth() {
062            Object value = connector.getAttribute("clientAuth");
063            return value == null ? false : new Boolean(value.toString()).booleanValue();
064        }
065    
066        public String getKeyAlias() {
067            return (String) connector.getAttribute("keyAlias");
068        }
069    
070        public String getKeystoreFile() {
071            return keystoreFileName;
072        }
073    
074        public String getKeystoreType() {
075            return (String) connector.getAttribute("keystoreType");
076        }
077    
078        public String getSslProtocol() {
079            return (String) connector.getAttribute("sslProtocol");
080        }
081    
082        public String getTruststoreFile() {
083            return truststoreFileName;
084        }
085    
086        public String getTruststoreType() {
087            return (String) connector.getAttribute("truststoreType");
088        }
089    
090        public String getTruststorePass() {
091            return (String) connector.getAttribute("truststorePass");
092        }
093        
094        public String getKeystorePass() {
095            return (String) connector.getAttribute("keystorePass");
096        }
097        
098        public void setAlgorithm(String algorithm) {
099            this.algorithm = algorithm;
100            if ("default".equalsIgnoreCase(algorithm)) {
101                algorithm = KeyManagerFactory.getDefaultAlgorithm();
102            }
103            connector.setAttribute("algorithm", algorithm);
104        }
105    
106        public void setCiphers(String ciphers) {
107            connector.setAttribute("ciphers", ciphers);
108        }
109    
110        public void setClientAuth(boolean clientAuth) {
111            connector.setAttribute("clientAuth", new Boolean(clientAuth));
112        }
113    
114        public void setKeyAlias(String keyAlias) {
115            if (keyAlias.equals(""))
116                keyAlias = null;
117            connector.setAttribute("keyAlias", keyAlias);
118        }
119    
120        public void setKeystoreFile(String keystoreFile) {
121            if (keystoreFile!= null && keystoreFile.equals("")) 
122                keystoreFile = null;
123            keystoreFileName = keystoreFile;
124            if (keystoreFileName == null)
125                connector.setAttribute("keystoreFile", null);
126            else
127                connector.setAttribute("keystoreFile", serverInfo.resolveServerPath(keystoreFileName));
128        }
129    
130        public void setKeystorePass(String keystorePass) {
131            if (keystorePass!= null && keystorePass.equals("")) 
132                keystorePass = null;
133            connector.setAttribute("keystorePass", keystorePass);
134        }
135    
136        public void setKeystoreType(String keystoreType) {
137            if (keystoreType!= null && keystoreType.equals("")) 
138                keystoreType = null;
139            connector.setAttribute("keystoreType", keystoreType);
140        }
141    
142        public void setSslProtocol(String sslProtocol) {
143            if (sslProtocol!= null && sslProtocol.equals("")) 
144                sslProtocol = null;
145            connector.setAttribute("sslProtocol", sslProtocol);
146        }
147    
148        public void setTruststoreFile(String truststoreFile) {
149            if (truststoreFile!= null && truststoreFile.equals("")) 
150                truststoreFile = null;
151            truststoreFileName = truststoreFile;
152            if (truststoreFileName == null)
153                connector.setAttribute("truststoreFile", null);
154            else
155                connector.setAttribute("truststoreFile", serverInfo.resolveServerPath(truststoreFileName));
156        }
157    
158        public void setTruststorePass(String truststorePass) {
159            if (truststorePass!= null && truststorePass.equals("")) 
160                truststorePass = null;
161            connector.setAttribute("truststorePass", truststorePass);
162        }
163    
164        public void setTruststoreType(String truststoreType) {
165            if (truststoreType!= null && truststoreType.equals("")) 
166                truststoreType = null;
167            connector.setAttribute("truststoreType", truststoreType);
168        }
169    
170        public static final GBeanInfo GBEAN_INFO;
171    
172        static {
173            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Tomcat Connector", AbstractHttp11ConnectorGBean.class, BaseHttp11ConnectorGBean.GBEAN_INFO);
174            infoFactory.addInterface(Http11Protocol.class, 
175                    new String[] {
176                        //SSL Attributes
177                        "algorithm",
178                        "clientAuth",
179                        "keystoreFile",
180                        "keystorePass",
181                        "keystoreType",
182                        "sslProtocol",
183                        "ciphers",
184                        "keyAlias",
185                        "truststoreFile",
186                        "truststorePass",
187                        "truststoreType"
188                    },
189                    new String[] {
190                        //SSL Attributes
191                        "algorithm",
192                        "clientAuth",
193                        "keystoreFile",
194                        "keystorePass",
195                        "keystoreType",
196                        "sslProtocol",
197                        "ciphers",
198                        "keyAlias",
199                        "truststoreFile",
200                        "truststorePass",
201                        "truststoreType"
202                    }
203            );
204            infoFactory.setConstructor(new String[] { "name", "initParams", "tomcatProtocol", "host", "port", "TomcatContainer", "ServerInfo"});
205            GBEAN_INFO = infoFactory.getBeanInfo();
206        }
207        
208        public static GBeanInfo getGBeanInfo() {
209            return GBEAN_INFO;
210        }
211        
212    }