KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > http11 > Http11Protocol


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

17
18 package org.apache.coyote.http11;
19
20 import java.net.InetAddress JavaDoc;
21 import java.net.Socket JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.concurrent.Executor JavaDoc;
26
27 import javax.management.MBeanRegistration JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.apache.coyote.ActionCode;
32 import org.apache.coyote.ActionHook;
33 import org.apache.coyote.Adapter;
34 import org.apache.coyote.ProtocolHandler;
35 import org.apache.coyote.RequestGroupInfo;
36 import org.apache.coyote.RequestInfo;
37 import org.apache.tomcat.util.modeler.Registry;
38 import org.apache.tomcat.util.net.JIoEndpoint;
39 import org.apache.tomcat.util.net.SSLImplementation;
40 import org.apache.tomcat.util.net.ServerSocketFactory;
41 import org.apache.tomcat.util.net.JIoEndpoint.Handler;
42 import org.apache.tomcat.util.res.StringManager;
43
44
45 /**
46  * Abstract the protocol implementation, including threading, etc.
47  * Processor is single threaded and specific to stream-based protocols,
48  * will not fit Jk protocols like JNI.
49  *
50  * @author Remy Maucherat
51  * @author Costin Manolache
52  * @deprecated
53  */

54 public class Http11Protocol
55     implements ProtocolHandler, MBeanRegistration JavaDoc {
56
57
58     protected static org.apache.commons.logging.Log log
59         = org.apache.commons.logging.LogFactory.getLog(Http11Protocol.class);
60
61     /**
62      * The string manager for this package.
63      */

64     protected static StringManager sm =
65         StringManager.getManager(Constants.Package);
66
67
68     // ------------------------------------------------------------ Constructor
69

70
71     public Http11Protocol() {
72         setSoLinger(Constants.DEFAULT_CONNECTION_LINGER);
73         setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT);
74         //setServerSoTimeout(Constants.DEFAULT_SERVER_SOCKET_TIMEOUT);
75
setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY);
76     }
77
78     
79     // ----------------------------------------------------------------- Fields
80

81
82     protected Http11ConnectionHandler cHandler = new Http11ConnectionHandler(this);
83     protected JIoEndpoint endpoint = new JIoEndpoint();
84
85
86     // *
87
protected ObjectName JavaDoc tpOname = null;
88     // *
89
protected ObjectName JavaDoc rgOname = null;
90
91
92     protected ServerSocketFactory socketFactory = null;
93     protected SSLImplementation sslImplementation = null;
94
95
96     // ----------------------------------------- ProtocolHandler Implementation
97
// *
98

99
100     protected HashMap JavaDoc<String JavaDoc, Object JavaDoc> attributes = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
101
102     
103     /**
104      * Set a property.
105      */

106     public void setProperty(String JavaDoc name, String JavaDoc value) {
107         setAttribute(name, value);
108     }
109
110     /**
111      * Get a property
112      */

113     public String JavaDoc getProperty(String JavaDoc name) {
114         return (String JavaDoc)getAttribute(name);
115     }
116
117     /**
118      * Pass config info
119      */

120     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
121         if (log.isTraceEnabled()) {
122             log.trace(sm.getString("http11protocol.setattribute", name, value));
123         }
124         attributes.put(name, value);
125     }
126
127     public Object JavaDoc getAttribute(String JavaDoc key) {
128         return attributes.get(key);
129     }
130
131     public Iterator JavaDoc getAttributeNames() {
132         return attributes.keySet().iterator();
133     }
134
135
136     /**
137      * The adapter, used to call the connector.
138      */

139     protected Adapter adapter;
140     public void setAdapter(Adapter adapter) { this.adapter = adapter; }
141     public Adapter getAdapter() { return adapter; }
142
143
144     public void init() throws Exception JavaDoc {
145         endpoint.setName(getName());
146         endpoint.setHandler(cHandler);
147
148         // Verify the validity of the configured socket factory
149
try {
150             if (isSSLEnabled()) {
151                 sslImplementation =
152                     SSLImplementation.getInstance(sslImplementationName);
153                 socketFactory = sslImplementation.getServerSocketFactory();
154                 endpoint.setServerSocketFactory(socketFactory);
155             } else if (socketFactoryName != null) {
156                 socketFactory = (ServerSocketFactory) Class.forName(socketFactoryName).newInstance();
157                 endpoint.setServerSocketFactory(socketFactory);
158             }
159         } catch (Exception JavaDoc ex) {
160             log.error(sm.getString("http11protocol.socketfactory.initerror"),
161                       ex);
162             throw ex;
163         }
164
165         if (socketFactory!=null) {
166             Iterator JavaDoc<String JavaDoc> attE = attributes.keySet().iterator();
167             while( attE.hasNext() ) {
168                 String JavaDoc key = attE.next();
169                 Object JavaDoc v=attributes.get(key);
170                 socketFactory.setAttribute(key, v);
171             }
172         }
173         
174         try {
175             endpoint.init();
176         } catch (Exception JavaDoc ex) {
177             log.error(sm.getString("http11protocol.endpoint.initerror"), ex);
178             throw ex;
179         }
180         if (log.isInfoEnabled())
181             log.info(sm.getString("http11protocol.init", getName()));
182
183     }
184
185     public void start() throws Exception JavaDoc {
186         if (this.domain != null) {
187             try {
188                 tpOname = new ObjectName JavaDoc
189                     (domain + ":" + "type=ThreadPool,name=" + getName());
190                 Registry.getRegistry(null, null)
191                     .registerComponent(endpoint, tpOname, null );
192             } catch (Exception JavaDoc e) {
193                 log.error("Can't register endpoint");
194             }
195             rgOname=new ObjectName JavaDoc
196                 (domain + ":type=GlobalRequestProcessor,name=" + getName());
197             Registry.getRegistry(null, null).registerComponent
198                 ( cHandler.global, rgOname, null );
199         }
200
201         try {
202             endpoint.start();
203         } catch (Exception JavaDoc ex) {
204             log.error(sm.getString("http11protocol.endpoint.starterror"), ex);
205             throw ex;
206         }
207         if (log.isInfoEnabled())
208             log.info(sm.getString("http11protocol.start", getName()));
209     }
210
211     public void pause() throws Exception JavaDoc {
212         try {
213             endpoint.pause();
214         } catch (Exception JavaDoc ex) {
215             log.error(sm.getString("http11protocol.endpoint.pauseerror"), ex);
216             throw ex;
217         }
218         if (log.isInfoEnabled())
219             log.info(sm.getString("http11protocol.pause", getName()));
220     }
221
222     public void resume() throws Exception JavaDoc {
223         try {
224             endpoint.resume();
225         } catch (Exception JavaDoc ex) {
226             log.error(sm.getString("http11protocol.endpoint.resumeerror"), ex);
227             throw ex;
228         }
229         if (log.isInfoEnabled())
230             log.info(sm.getString("http11protocol.resume", getName()));
231     }
232
233     public void destroy() throws Exception JavaDoc {
234         if (log.isInfoEnabled())
235             log.info(sm.getString("http11protocol.stop", getName()));
236         endpoint.destroy();
237         if (tpOname!=null)
238             Registry.getRegistry(null, null).unregisterComponent(tpOname);
239         if (rgOname != null)
240             Registry.getRegistry(null, null).unregisterComponent(rgOname);
241     }
242
243     
244     // ------------------------------------------------------------- Properties
245

246     
247     // *
248
/**
249      * This field indicates if the protocol is secure from the perspective of
250      * the client (= https is used).
251      */

252     protected boolean secure;
253     public boolean getSecure() { return secure; }
254     public void setSecure(boolean b) { secure = b; }
255
256     protected boolean SSLEnabled = false;
257     public boolean isSSLEnabled() { return SSLEnabled;}
258     public void setSSLEnabled(boolean SSLEnabled) {this.SSLEnabled = SSLEnabled;}
259     
260     /**
261      * Name of the socket factory.
262      */

263     protected String JavaDoc socketFactoryName = null;
264     public String JavaDoc getSocketFactory() { return socketFactoryName; }
265     public void setSocketFactory(String JavaDoc valueS) { socketFactoryName = valueS; }
266     
267     
268     /**
269      * Name of the SSL implementation.
270      */

271     protected String JavaDoc sslImplementationName=null;
272     public String JavaDoc getSSLImplementation() { return sslImplementationName; }
273     public void setSSLImplementation( String JavaDoc valueS) {
274         sslImplementationName = valueS;
275         setSecure(true);
276     }
277     
278     
279     // HTTP
280
/**
281      * Maximum number of requests which can be performed over a keepalive
282      * connection. The default is the same as for Apache HTTP Server.
283      */

284     protected int maxKeepAliveRequests = 100;
285     public int getMaxKeepAliveRequests() { return maxKeepAliveRequests; }
286     public void setMaxKeepAliveRequests(int mkar) { maxKeepAliveRequests = mkar; }
287
288
289     // HTTP
290
/**
291      * This timeout represents the socket timeout which will be used while
292      * the adapter execution is in progress, unless disableUploadTimeout
293      * is set to true. The default is the same as for Apache HTTP Server
294      * (300 000 milliseconds).
295      */

296     protected int timeout = 300000;
297     public int getTimeout() { return timeout; }
298     public void setTimeout(int timeout) { this.timeout = timeout; }
299
300
301     // *
302
/**
303      * Maximum size of the post which will be saved when processing certain
304      * requests, such as a POST.
305      */

306     protected int maxSavePostSize = 4 * 1024;
307     public int getMaxSavePostSize() { return maxSavePostSize; }
308     public void setMaxSavePostSize(int valueI) { maxSavePostSize = valueI; }
309
310
311     // HTTP
312
/**
313      * Maximum size of the HTTP message header.
314      */

315     protected int maxHttpHeaderSize = 8 * 1024;
316     public int getMaxHttpHeaderSize() { return maxHttpHeaderSize; }
317     public void setMaxHttpHeaderSize(int valueI) { maxHttpHeaderSize = valueI; }
318
319
320     // HTTP
321
/**
322      * If true, the regular socket timeout will be used for the full duration
323      * of the connection.
324      */

325     protected boolean disableUploadTimeout = true;
326     public boolean getDisableUploadTimeout() { return disableUploadTimeout; }
327     public void setDisableUploadTimeout(boolean isDisabled) { disableUploadTimeout = isDisabled; }
328
329
330     // HTTP
331
/**
332      * Integrated compression support.
333      */

334     protected String JavaDoc compression = "off";
335     public String JavaDoc getCompression() { return compression; }
336     public void setCompression(String JavaDoc valueS) { compression = valueS; }
337     
338     
339     // HTTP
340
protected String JavaDoc noCompressionUserAgents = null;
341     public String JavaDoc getNoCompressionUserAgents() { return noCompressionUserAgents; }
342     public void setNoCompressionUserAgents(String JavaDoc valueS) { noCompressionUserAgents = valueS; }
343
344     
345     // HTTP
346
protected String JavaDoc compressableMimeTypes = "text/html,text/xml,text/plain";
347     public String JavaDoc getCompressableMimeType() { return compressableMimeTypes; }
348     public void setCompressableMimeType(String JavaDoc valueS) { compressableMimeTypes = valueS; }
349     
350     
351     // HTTP
352
protected int compressionMinSize = 2048;
353     public int getCompressionMinSize() { return compressionMinSize; }
354     public void setCompressionMinSize(int valueI) { compressionMinSize = valueI; }
355
356
357     // HTTP
358
/**
359      * User agents regular expressions which should be restricted to HTTP/1.0 support.
360      */

361     protected String JavaDoc restrictedUserAgents = null;
362     public String JavaDoc getRestrictedUserAgents() { return restrictedUserAgents; }
363     public void setRestrictedUserAgents(String JavaDoc valueS) { restrictedUserAgents = valueS; }
364     
365     
366     // HTTP
367
/**
368      * Server header.
369      */

370     protected String JavaDoc server;
371     public void setServer( String JavaDoc server ) { this.server = server; }
372     public String JavaDoc getServer() { return server; }
373
374
375     // --------------------------------------------------------- Public methods
376

377     // *
378
public Executor JavaDoc getExecutor() {
379         return endpoint.getExecutor();
380     }
381     
382     // *
383
public void setExecutor(Executor JavaDoc executor) {
384         endpoint.setExecutor(executor);
385     }
386     
387     // *
388
public int getMaxThreads() {
389         return endpoint.getMaxThreads();
390     }
391
392     // *
393
public void setMaxThreads( int maxThreads ) {
394         endpoint.setMaxThreads(maxThreads);
395     }
396
397     // *
398
public void setThreadPriority(int threadPriority) {
399         endpoint.setThreadPriority(threadPriority);
400     }
401
402     // *
403
public int getThreadPriority() {
404         return endpoint.getThreadPriority();
405     }
406
407     // *
408
public int getBacklog() {
409         return endpoint.getBacklog();
410     }
411
412     // *
413
public void setBacklog( int i ) {
414         endpoint.setBacklog(i);
415     }
416
417     // *
418
public int getPort() {
419         return endpoint.getPort();
420     }
421
422     // *
423
public void setPort( int port ) {
424         endpoint.setPort(port);
425     }
426
427     // *
428
public InetAddress JavaDoc getAddress() {
429         return endpoint.getAddress();
430     }
431
432     // *
433
public void setAddress(InetAddress JavaDoc ia) {
434         endpoint.setAddress( ia );
435     }
436
437     // *
438
public String JavaDoc getName() {
439         String JavaDoc encodedAddr = "";
440         if (getAddress() != null) {
441             encodedAddr = "" + getAddress();
442             if (encodedAddr.startsWith("/"))
443                 encodedAddr = encodedAddr.substring(1);
444             encodedAddr = URLEncoder.encode(encodedAddr) + "-";
445         }
446         return ("http-" + encodedAddr + endpoint.getPort());
447     }
448
449     // *
450
public boolean getTcpNoDelay() {
451         return endpoint.getTcpNoDelay();
452     }
453
454     // *
455
public void setTcpNoDelay( boolean b ) {
456         endpoint.setTcpNoDelay( b );
457     }
458
459     // *
460
public int getSoLinger() {
461         return endpoint.getSoLinger();
462     }
463
464     // *
465
public void setSoLinger( int i ) {
466         endpoint.setSoLinger( i );
467     }
468
469     // *
470
public int getSoTimeout() {
471         return endpoint.getSoTimeout();
472     }
473
474     // *
475
public void setSoTimeout( int i ) {
476         endpoint.setSoTimeout(i);
477     }
478
479     // HTTP
480
/**
481      * Return the Keep-Alive policy for the connection.
482      */

483     public boolean getKeepAlive() {
484         return ((maxKeepAliveRequests != 0) && (maxKeepAliveRequests != 1));
485     }
486
487     // HTTP
488
/**
489      * Set the keep-alive policy for this connection.
490      */

491     public void setKeepAlive(boolean keepAlive) {
492         if (!keepAlive) {
493             setMaxKeepAliveRequests(1);
494         }
495     }
496
497     /*
498      * Note: All the following are JSSE/java.io specific attributes.
499      */

500     
501     public String JavaDoc getKeystore() {
502         return (String JavaDoc) getAttribute("keystore");
503     }
504
505     public void setKeystore( String JavaDoc k ) {
506         setAttribute("keystore", k);
507     }
508
509     public String JavaDoc getKeypass() {
510         return (String JavaDoc) getAttribute("keypass");
511     }
512
513     public void setKeypass( String JavaDoc k ) {
514         attributes.put("keypass", k);
515         //setAttribute("keypass", k);
516
}
517
518     public String JavaDoc getKeytype() {
519         return (String JavaDoc) getAttribute("keystoreType");
520     }
521
522     public void setKeytype( String JavaDoc k ) {
523         setAttribute("keystoreType", k);
524     }
525
526     public String JavaDoc getClientauth() {
527         return (String JavaDoc) getAttribute("clientauth");
528     }
529
530     public void setClientauth( String JavaDoc k ) {
531         setAttribute("clientauth", k);
532     }
533
534     public String JavaDoc getProtocols() {
535         return (String JavaDoc) getAttribute("protocols");
536     }
537
538     public void setProtocols(String JavaDoc k) {
539         setAttribute("protocols", k);
540     }
541
542     public String JavaDoc getAlgorithm() {
543         return (String JavaDoc) getAttribute("algorithm");
544     }
545
546     public void setAlgorithm( String JavaDoc k ) {
547         setAttribute("algorithm", k);
548     }
549
550     public String JavaDoc getCiphers() {
551         return (String JavaDoc) getAttribute("ciphers");
552     }
553
554     public void setCiphers(String JavaDoc ciphers) {
555         setAttribute("ciphers", ciphers);
556     }
557
558     public String JavaDoc getKeyAlias() {
559         return (String JavaDoc) getAttribute("keyAlias");
560     }
561
562     public void setKeyAlias(String JavaDoc keyAlias) {
563         setAttribute("keyAlias", keyAlias);
564     }
565
566     // ----------------------------------- Http11ConnectionHandler Inner Class
567

568     protected static class Http11ConnectionHandler implements Handler {
569         protected Http11Protocol protocol;
570         protected static int count = 0;
571         protected RequestGroupInfo global = new RequestGroupInfo();
572         protected ThreadLocal JavaDoc<Http11Processor> localProcessor = new ThreadLocal JavaDoc<Http11Processor>();
573
574         Http11ConnectionHandler(Http11Protocol proto) {
575             this.protocol = proto;
576         }
577
578         public boolean process(Socket JavaDoc socket) {
579             Http11Processor processor = null;
580             try {
581                 processor = localProcessor.get();
582                 if (processor == null) {
583                     processor =
584                         new Http11Processor(protocol.maxHttpHeaderSize, protocol.endpoint);
585                     processor.setAdapter(protocol.adapter);
586                     processor.setMaxKeepAliveRequests(protocol.maxKeepAliveRequests);
587                     processor.setTimeout(protocol.timeout);
588                     processor.setDisableUploadTimeout(protocol.disableUploadTimeout);
589                     processor.setCompression(protocol.compression);
590                     processor.setCompressionMinSize(protocol.compressionMinSize);
591                     processor.setNoCompressionUserAgents(protocol.noCompressionUserAgents);
592                     processor.setCompressableMimeTypes(protocol.compressableMimeTypes);
593                     processor.setRestrictedUserAgents(protocol.restrictedUserAgents);
594                     processor.setMaxSavePostSize(protocol.maxSavePostSize);
595                     processor.setServer(protocol.server);
596                     localProcessor.set(processor);
597                     if (protocol.getDomain() != null) {
598                         synchronized (this) {
599                             try {
600                                 RequestInfo rp = processor.getRequest().getRequestProcessor();
601                                 rp.setGlobalProcessor(global);
602                                 ObjectName JavaDoc rpName = new ObjectName JavaDoc
603                                 (protocol.getDomain() + ":type=RequestProcessor,worker="
604                                         + protocol.getName() + ",name=HttpRequest" + count++);
605                                 Registry.getRegistry(null, null).registerComponent(rp, rpName, null);
606                             } catch (Exception JavaDoc e) {
607                                 log.warn("Error registering request");
608                             }
609                         }
610                     }
611                 }
612
613                 if (processor instanceof ActionHook) {
614                     ((ActionHook) processor).action(ActionCode.ACTION_START, null);
615                 }
616
617                 if (protocol.secure && (protocol.sslImplementation != null)) {
618                     processor.setSSLSupport
619                         (protocol.sslImplementation.getSSLSupport(socket));
620                 } else {
621                     processor.setSSLSupport(null);
622                 }
623                 
624                 processor.process(socket);
625                 return false;
626
627             } catch(java.net.SocketException JavaDoc e) {
628                 // SocketExceptions are normal
629
Http11Protocol.log.debug
630                     (sm.getString
631                      ("http11protocol.proto.socketexception.debug"), e);
632             } catch (java.io.IOException JavaDoc e) {
633                 // IOExceptions are normal
634
Http11Protocol.log.debug
635                     (sm.getString
636                      ("http11protocol.proto.ioexception.debug"), e);
637             }
638             // Future developers: if you discover any other
639
// rare-but-nonfatal exceptions, catch them here, and log as
640
// above.
641
catch (Throwable JavaDoc e) {
642                 // any other exception or error is odd. Here we log it
643
// with "ERROR" level, so it will show up even on
644
// less-than-verbose logs.
645
Http11Protocol.log.error
646                     (sm.getString("http11protocol.proto.error"), e);
647             } finally {
648                 // if(proto.adapter != null) proto.adapter.recycle();
649
// processor.recycle();
650

651                 if (processor instanceof ActionHook) {
652                     ((ActionHook) processor).action(ActionCode.ACTION_STOP, null);
653                 }
654             }
655             return false;
656         }
657     }
658
659
660     // -------------------- JMX related methods --------------------
661

662     // *
663
protected String JavaDoc domain;
664     protected ObjectName JavaDoc oname;
665     protected MBeanServer JavaDoc mserver;
666
667     public ObjectName JavaDoc getObjectName() {
668         return oname;
669     }
670
671     public String JavaDoc getDomain() {
672         return domain;
673     }
674
675     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server,
676                                   ObjectName JavaDoc name) throws Exception JavaDoc {
677         oname=name;
678         mserver=server;
679         domain=name.getDomain();
680         return name;
681     }
682
683     public void postRegister(Boolean JavaDoc registrationDone) {
684     }
685
686     public void preDeregister() throws Exception JavaDoc {
687     }
688
689     public void postDeregister() {
690     }
691 }
692
Popular Tags