KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > ara > algorithms > ContextRootAlgorithm


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web.ara.algorithms;
25
26 import com.sun.enterprise.web.connector.grizzly.StreamAlgorithm;
27 import com.sun.enterprise.web.connector.grizzly.Handler;
28
29 import java.nio.ByteBuffer JavaDoc;
30 import java.nio.BufferUnderflowException JavaDoc;
31 import java.nio.channels.SocketChannel JavaDoc;
32
33
34 /**
35  * Parse the request bytes and seek for the context-root value of the
36  * HTTP method.
37  *
38  * @author Jeanfrancois Arcand
39  */

40 public class ContextRootAlgorithm implements StreamAlgorithm{
41  
42     private int port = 8080;
43     
44     private SocketChannel JavaDoc socketChannel;
45     
46     public ContextRootAlgorithm() {
47     }
48
49     
50     /**
51      * Allocate a <code>ByteBuffer</code>
52      * @param useDirect allocate a direct <code>ByteBuffer</code>.
53      * @param useView allocate a view <code>ByteBuffer</code>.
54      * @return a new <code>ByteBuffer</code>
55      */

56     public ByteBuffer JavaDoc allocate(boolean useDirect, boolean useView) {
57         throw new UnsupportedOperationException JavaDoc();
58     }
59
60     
61     /**
62      * Return the stream content-length. If the content-length wasn't parsed,
63      * return -1.
64      */

65     public int contentLength() {
66         throw new UnsupportedOperationException JavaDoc();
67     }
68
69     
70     /**
71      * Return the stream header length. The header length is the length between
72      * the start of the stream and the first occurance of character '\r\n' .
73      */

74     public int headerLength() {
75         throw new UnsupportedOperationException JavaDoc();
76     }
77
78     
79     /**
80      * Parse the request line in search of the context-root bytes of the HTTP
81      * Method. The <code>ByteBuffer</code> position and limit refer
82      * respectively to the start and the end of the context root.
83      * @param byteBuffer The byteBuffer containing the requests bytes
84      * @return true if the context-root has been found.
85      */

86     public boolean parse(ByteBuffer JavaDoc byteBuffer) {
87         boolean isFound = false;
88                           
89         int curPosition = byteBuffer.position();
90         int curLimit = byteBuffer.limit();
91       
92         // Rule a - If nothing, return to the Selector.
93
if (byteBuffer.position() == 0)
94             return false;
95        
96         byteBuffer.position(0);
97         byteBuffer.limit(curPosition);
98         int state =0;
99         int start =0;
100         int end = 0;
101         
102         try {
103             byte c;
104             
105             // Rule b - try to determine the context-root
106
while(byteBuffer.hasRemaining()) {
107                 c = byteBuffer.get();
108
109                 // State Machine
110
// 0 - Search for the first SPACE ' ' between the method and the
111
// the request URI
112
// 1 - Search for the second SPACE ' ' between the request URI
113
// and the method
114
switch(state) {
115                     case 0: // Search for first ' '
116
if (c == 0x20){
117                             state = 1;
118                             start = byteBuffer.position() + 1;
119                         }
120                         break;
121                     case 1: // Search for next ' '
122
if (c == 0x20){
123                             end = byteBuffer.position() - 1;
124                             return true;
125                         }
126                         break;
127                     default:
128                         throw new IllegalArgumentException JavaDoc("Unexpected state");
129                 }
130             }
131             return false;
132         } catch (BufferUnderflowException JavaDoc bue) {
133             return false;
134         } finally {
135             if ( end > 0 ){
136                 byteBuffer.position(start);
137                 byteBuffer.limit(end);
138             } else {
139                 byteBuffer.limit(curLimit);
140                 byteBuffer.position(curPosition);
141             }
142         }
143     }
144
145
146     /**
147      * After parsing the bytes, post process the <code>ByteBuffer</code>
148      * @param byteBuffer the <code>ByteBuffer</code> used by this algorithm
149      * @return <code>ByteBuffer</code> used by this algorithm
150      */

151     public ByteBuffer JavaDoc postParse(ByteBuffer JavaDoc byteBuffer) {
152         throw new UnsupportedOperationException JavaDoc();
153     }
154     
155     
156     /**
157      * Before parsing the bytes, initialize and prepare the algorithm.
158      * @param byteBuffer the <code>ByteBuffer</code> used by this algorithm
159      * @return <code>ByteBuffer</code> used by this algorithm
160      */

161     public ByteBuffer JavaDoc preParse(ByteBuffer JavaDoc byteBuffer) {
162         throw new UnsupportedOperationException JavaDoc();
163     }
164
165     
166     /**
167      * Recycle the algorithm.
168      */

169     public void recycle() {
170     }
171
172     
173     /**
174      * Rollback the <code>ByteBuffer</code> to its previous state in case
175      * an error as occured.
176      */

177     public ByteBuffer JavaDoc rollbackParseState(ByteBuffer JavaDoc byteBuffer) {
178         throw new UnsupportedOperationException JavaDoc();
179     }
180     
181     
182     // ----------------------------------------------------- Util -----------//
183

184     
185     /**
186      * Dump the ByteBuffer content. This is used only for debugging purpose.
187      */

188     private String JavaDoc dump(ByteBuffer JavaDoc byteBuffer){
189         ByteBuffer JavaDoc dd = byteBuffer.duplicate();
190         dd.flip();
191         
192         int length = dd.limit();
193         byte[] dump = new byte[length];
194         dd.get(dump,0,length);
195         return(new String JavaDoc(dump) + "\n----------------------------" + dd);
196     }
197
198
199     /**
200      * Set the <code>SocketChannel</code> used by this class.
201      */

202     public void setSocketChannel(SocketChannel JavaDoc socketChannel){
203         this.socketChannel = socketChannel;
204     }
205     
206
207     /**
208      * Return null as handler aren't required.
209      */

210     public Handler getHandler(){
211         return null;
212     }
213    
214     /**
215      * Set the port
216      */

217     public void setPort(int port){
218         this.port = port;
219     }
220     
221     
222     /**
223      * Return the port
224      */

225     public int getPort(){
226         return port;
227     }
228 }
229
Popular Tags