KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > mplex > HttpStreamSelector


1 package org.sapia.ubik.net.mplex;
2
3
4 /**
5  * Implements a selection logic for HTTP requests. By default this selector will choose
6  * all HTTP request, as it looks for the token "HTTP/" as specified by the HTTP specification.
7  * This selector also gives the functionaly to select only a given HTTP method (like POST or
8  * GET) and/or to specify a pattern for the HTTP request path. If specify this request pattern
9  * must be the first characters of the request path to select (without any starts or regex
10  * expression). The current logic to determine if the request pattern matches is done using
11  * the <code>startsWith()</code> method of the <code>java.lang.String</code> object.
12  *
13  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">
16  * Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
17  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
18  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a>
19  * at the Sapia OSS web site</dd></dt>
20  * </dl>
21  */

22 public class HttpStreamSelector implements StreamSelector {
23   /** The specific type of HTTP method to select, if provided */
24   private String JavaDoc _theMethod;
25
26   /** A simple selection pattern of the request path of the HTTP header, if provided */
27   private String JavaDoc _theRequestPatern;
28
29   /**
30    * Creates a new HtpStreamSelector instance that will select any HTTP request.
31    */

32   public HttpStreamSelector() {
33   }
34
35   /**
36    * Creates a new HtpStreamSelector instance that will select an HTTP request
37    * based on the criterias passed in.
38    *
39    * @param aMethod The specific HTTP method to discrimate on, or <code>null</code>
40    * for any type of HTTP method.
41    * @param aRequestPattern The simple pattern that defines the starting request path
42    * of the HTTP request. For example the value "/sapia/example" would select the
43    * path "/sapia/example/index.html" and also "/sapia/example/service/myWebService".
44    * Passing <code>null</code> will make this selector choose any request path.
45    */

46   public HttpStreamSelector(String JavaDoc aMethod, String JavaDoc aRequestPattern) {
47     _theMethod = aMethod;
48     _theRequestPatern = aRequestPattern;
49   }
50
51   /**
52    * Selects or not a stream by analyzing the header of the stream passed in.
53    *
54    * @param header The first 64 bytes of the stream.
55    * @return True if the header is accepted by this selector, false otherwise.
56    */

57   public boolean selectStream(byte[] header) {
58     String JavaDoc aStringValue = new String JavaDoc(header);
59
60     int firstSpace = aStringValue.indexOf(" ");
61
62     if (firstSpace > 0) {
63       // Looking for the HTTP method
64
if (_theMethod != null) {
65         if (!aStringValue.substring(0, firstSpace).equals(_theMethod)) {
66           return false;
67         }
68       }
69
70       int secondSpace = aStringValue.indexOf(" ", firstSpace + 1);
71
72       if (secondSpace > firstSpace) {
73         // Looking for the HTTP request URI
74
if (_theRequestPatern != null) {
75           if (!aStringValue.substring(firstSpace + 1, secondSpace).startsWith(_theRequestPatern)) {
76             return false;
77           }
78         }
79
80         // Looking for the HTTP version
81
if (aStringValue.substring(secondSpace + 1, secondSpace + 6).equals("HTTP/")) {
82           return true;
83         } else {
84           return false;
85         }
86       } else {
87         return false;
88       }
89     } else {
90       return false;
91     }
92   }
93 }
94
Popular Tags