KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > tomcat3 > CoyoteInterceptor2


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.coyote.tomcat3;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 import org.apache.coyote.ActionCode;
23 import org.apache.coyote.ProtocolHandler;
24 import org.apache.tomcat.core.BaseInterceptor;
25 import org.apache.tomcat.core.Context;
26 import org.apache.tomcat.core.ContextManager;
27 import org.apache.tomcat.core.TomcatException;
28 import org.apache.tomcat.util.IntrospectionUtils;
29 import org.apache.tomcat.util.net.SSLSupport;
30
31 /** Standalone http.
32  *
33  * Connector properties:
34  * <ul>
35  * <li> secure - will load a SSL socket factory and act as https server</li>
36  * </ul>
37  *
38  * Properties passed to the net layer:
39  * <ul>
40  * <li>timeout</li>
41  * <li>backlog</li>
42  * <li>address</li>
43  * <li>port</li>
44  * </ul>
45  * Thread pool properties:
46  * <ul>
47  * <li>minSpareThreads</li>
48  * <li>maxSpareThreads</li>
49  * <li>maxThreads</li>
50  * <li>poolOn</li>
51  * </ul>
52  * Properties for HTTPS:
53  * <ul>
54  * <li>keystore - certificates - default to ~/.keystore</li>
55  * <li>keypass - password</li>
56  * <li>clientauth - true if the server should authenticate the client using certs</li>
57  * </ul>
58  * Properties for HTTP:
59  * <ul>
60  * <li>reportedname - name of server sent back to browser (security purposes)</li>
61  * </ul>
62  * <ul>
63  * <li>compression - use gzip compression in HTTP 1.1 (on/off) - def off</li>
64  * </ul>
65  * <ul>
66  * <li>compressionMinSize - minimum size content to use gzip compression in HTTP 1.1 - def 2048</li>
67  * </ul>
68  * <ul>
69  * <li>noCompressionUserAgents - comma separated list of userAgents who didn't support gzip</li>
70  * </ul>
71  * <ul>
72  * <li>restrictedUserAgents - comma separated list of userAgents who didn't support HTTP 1.1 (use HTTP 1.0)</li>
73  * </ul>
74  * <ul>
75  * <li>compressableMimeTypes - comma separated list of mime types supported for compression - def text/html,text/xml,text/plain</li>
76  * </ul>
77  */

78 public class CoyoteInterceptor2 extends BaseInterceptor
79 {
80     public static final String JavaDoc REDIRECT_PORT_ATTR =
81     "org.apache.tomcat.request.redirectPort";
82     private String JavaDoc processorClassName="org.apache.coyote.http11.Http11Protocol";
83     Tomcat3Adapter adapter;
84     ProtocolHandler proto;
85     int protocolNote;
86     int redirectPort = -1;
87     
88     public CoyoteInterceptor2() {
89     super();
90     // defaults:
91
this.setAttribute( "port", "8080" );
92         this.setAttribute( "soLinger", "-1" );
93     }
94
95     // -------------------- PoolTcpConnector --------------------
96

97     /** Set the class of the processor to use.
98      */

99     public void setProcessorClassName(String JavaDoc pcn) {
100     processorClassName = pcn;
101     }
102
103     // -------------------- Start/stop --------------------
104
Hashtable JavaDoc attributes=new Hashtable JavaDoc();
105     
106     public void setAttribute( String JavaDoc prop, Object JavaDoc value) {
107     attributes.put( translateAttributeName(prop), value );
108     }
109
110
111     public void setProperty( String JavaDoc prop, String JavaDoc value ) {
112         setAttribute( prop, value );
113     }
114
115     /**
116      * Set the redirect port.
117      */

118     public void setRedirectPort(int rp) {
119     redirectPort = rp;
120     setAttribute("redirectPort", new Integer JavaDoc(rp));
121     }
122
123     /**
124      * Get the redirect port.
125      */

126     public int getRedirectPort() {
127     return redirectPort;
128     }
129
130     /** Called when the ContextManger is started
131      */

132     public void engineInit(ContextManager cm) throws TomcatException {
133     super.engineInit( cm );
134
135         protocolNote = cm.getNoteId(ContextManager.MODULE_NOTE,
136                     "coyote.protocol");
137         adapter=new Tomcat3Adapter(cm, this);
138         try {
139             Class JavaDoc c=Class.forName(processorClassName);
140             proto=(ProtocolHandler)c.newInstance();
141         setNote(protocolNote, proto);
142         } catch( Exception JavaDoc ex ) {
143             ex.printStackTrace();
144         }
145         
146         this.setAttribute("jkHome", cm.getHome());
147
148         proto.setAdapter( adapter );
149         try {
150             Enumeration JavaDoc keys=attributes.keys();
151             while( keys.hasMoreElements() ) {
152                 String JavaDoc k=(String JavaDoc)keys.nextElement();
153                 Object JavaDoc o=attributes.get(k);
154                 if( o instanceof String JavaDoc )
155                     IntrospectionUtils.setProperty( proto, k, (String JavaDoc)o );
156                 else
157                     IntrospectionUtils.setAttribute( proto, k, o );
158             }
159         proto.init();
160         } catch( Exception JavaDoc ex ) {
161             throw new TomcatException( "Error setting protocol properties ", ex );
162         }
163     }
164
165     /** Called when the ContextManger is started
166      */

167     public void engineStart(ContextManager cm) throws TomcatException {
168     try {
169             proto.start();
170     } catch( Exception JavaDoc ex ) {
171             ex.printStackTrace();
172         throw new TomcatException( ex );
173     }
174     }
175
176     public void engineShutdown(ContextManager cm) throws TomcatException {
177     try {
178         proto.destroy();
179         } catch( Exception JavaDoc ex ) {
180         throw new TomcatException( ex );
181     }
182     }
183
184     // -------------------- Handler implementation --------------------
185

186     /** Handle HTTP expectations.
187      */

188     public int preService(org.apache.tomcat.core.Request request,
189                           org.apache.tomcat.core.Response response) {
190     if(response instanceof Tomcat3Response) {
191         try {
192         ((Tomcat3Response)response).sendAcknowledgement();
193         } catch(Exception JavaDoc ex) {
194         log("Can't send ACK", ex);
195         }
196     }
197     return 0;
198     }
199
200     public int postRequest(org.apache.tomcat.core.Request request,
201                            org.apache.tomcat.core.Response response) {
202     if(request instanceof Tomcat3Request) {
203         try {
204                 Tomcat3Request httpReq=(Tomcat3Request)request;
205                 org.apache.coyote.Request cReq = httpReq.getCoyoteRequest();
206                 cReq.action( ActionCode.ACTION_POST_REQUEST , null);
207         } catch(Exception JavaDoc ex) {
208         log("Can't send ACK", ex);
209         }
210     }
211         return 0;
212     }
213     
214     /**
215        getInfo calls for SSL data
216        
217        @return the requested data
218     */

219     public Object JavaDoc getInfo( Context ctx, org.apache.tomcat.core.Request request,
220                            int id, String JavaDoc key ) {
221         if( ! ( request instanceof Tomcat3Request ) )
222             return null;
223
224         Tomcat3Request httpReq=(Tomcat3Request)request;
225
226         if( httpReq == null || httpReq.getConnector() != this ) {
227         return null;
228     }
229
230         if(key!=null ){
231             org.apache.coyote.Request cReq = httpReq.getCoyoteRequest();
232             Object JavaDoc info = cReq.getAttribute(key);
233             if( info != null)
234                 return info;
235             // XXX Should use MsgContext, pass the attribute we need.
236
// This will extract both
237
if(isSSLAttribute(key)) {
238                 cReq.action(ActionCode.ACTION_REQ_SSL_ATTRIBUTE,
239                             httpReq.getCoyoteRequest() );
240         // Only allowed a single cert under the 2.2 Spec.
241
Object JavaDoc [] value = (Object JavaDoc []) cReq.getAttribute(SSLSupport.CERTIFICATE_KEY);
242         if( value != null ) {
243             cReq.setAttribute(SSLSupport.CERTIFICATE_KEY, value[0]);
244         }
245         
246                 return cReq.getAttribute(key);
247             } else if(key.equals(REDIRECT_PORT_ATTR)) {
248         return new Integer JavaDoc(redirectPort);
249         }
250
251             return cReq.getAttribute( key );
252         }
253         return super.getInfo(ctx,request,id,key);
254     }
255
256     public int setInfo( Context ctx, org.apache.tomcat.core.Request request,
257                          int id, String JavaDoc key, String JavaDoc object ) {
258         if( ! ( request instanceof Tomcat3Request ) )
259             return DECLINED;
260         
261         Tomcat3Request httpReq=(Tomcat3Request)request;
262         
263         if(key!=null && httpReq!=null ){
264             org.apache.coyote.Request cReq = httpReq.getCoyoteRequest();
265             cReq.setAttribute(key, object);
266         return OK;
267         }
268     return super.setInfo(ctx, request, id, key, object);
269     }
270
271     /**
272      * Check if a string is a reserved SSL attribute key.
273      */

274     public static boolean isSSLAttribute(String JavaDoc key) {
275     return SSLSupport.CIPHER_SUITE_KEY.equals(key) ||
276         SSLSupport.KEY_SIZE_KEY.equals(key) ||
277         SSLSupport.CERTIFICATE_KEY.equals(key) ||
278         SSLSupport.SESSION_ID_KEY.equals(key);
279     }
280
281     private String JavaDoc translateAttributeName(String JavaDoc name) {
282          if ("clientAuth".equals(name)) {
283              return "clientauth";
284          } else if ("keystoreFile".equals(name)) {
285              return "keystore";
286          } else if ("randomFile".equals(name)) {
287              return "randomfile";
288          } else if ("rootFile".equals(name)) {
289              return "rootfile";
290          } else if ("keystorePass".equals(name)) {
291              return "keypass";
292          } else if ("keystoreType".equals(name)) {
293              return "keytype";
294          } else if ("sslProtocol".equals(name)) {
295              return "protocol";
296          } else if ("sslProtocols".equals(name)) {
297              return "protocols";
298          }
299          return name;
300     }
301  
302 }
303
304
Popular Tags