KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
20
21 import org.apache.coyote.ActionCode;
22 import org.apache.tomcat.util.buf.ByteChunk;
23 import org.apache.tomcat.util.buf.MessageBytes;
24 import org.apache.tomcat.core.BaseInterceptor;
25
26 /** The Request to connect with Coyote.
27  * This class handles the I/O requirements and transferring the request
28  * line and Mime headers between Coyote and Tomcat.
29  *
30  * @author Bill Barker
31  * @author Costin Manolache
32  */

33 public class Tomcat3Request extends org.apache.tomcat.core.Request {
34
35     org.apache.coyote.Request coyoteRequest=null;
36     BaseInterceptor connector = null;
37
38     // For SSL attributes we need to call an ActionHook to get
39
// info from the protocol handler.
40
// SSLSupport sslSupport=null;
41

42     ByteChunk readChunk = new ByteChunk(8096);
43     int pos=-1;
44     int end=-1;
45     byte [] readBuffer = null;
46
47
48     public Tomcat3Request() {
49         super();
50         remoteAddrMB.recycle();
51         remoteHostMB.recycle();
52     }
53
54     public void recycle() {
55     super.recycle();
56     if( coyoteRequest != null) coyoteRequest.recycle();
57
58         remoteAddrMB.recycle();
59         remoteHostMB.recycle();
60     readChunk.recycle();
61
62     readBuffer=null;
63     pos=-1;
64     end=-1;
65     }
66
67     public org.apache.coyote.Request getCoyoteRequest() {
68         return coyoteRequest;
69     }
70     
71     /** Attach the Coyote Request to this Request.
72      * This is currently set pre-request to allow copying the request
73      * attributes to the Tomcat attributes.
74      */

75     public void setCoyoteRequest(org.apache.coyote.Request cReq) {
76         coyoteRequest=cReq;
77
78         // The CoyoteRequest/Tomcat3Request are bound togheter, they
79
// don't change. That means we can use the same field ( which
80
// doesn't change as well.
81
schemeMB = coyoteRequest.scheme();
82         methodMB = coyoteRequest.method();
83         uriMB = coyoteRequest.requestURI();
84         queryMB = coyoteRequest.query();
85         protoMB = coyoteRequest.protocol();
86
87     headers = coyoteRequest.getMimeHeaders();
88     scookies.setHeaders(headers);
89     params.setHeaders(headers);
90         params.setQuery( queryMB );
91         
92         remoteAddrMB = coyoteRequest.remoteAddr();
93     remoteHostMB = coyoteRequest.remoteHost();
94     serverNameMB = coyoteRequest.serverName();
95     }
96
97     /**
98      * Set the Connector that this request services
99      */

100     void setConnector(BaseInterceptor conn) {
101     connector = conn;
102     }
103
104     /**
105      * Get the Connector that this request services
106      */

107     BaseInterceptor getConnector() {
108     return connector;
109     }
110     
111     /** Read a single character from the request body.
112      */

113     public int doRead() throws IOException JavaDoc {
114     if( available == 0 )
115         return -1;
116     // #3745
117
// if available == -1: unknown length, we'll read until end of stream.
118
if( available!= -1 )
119         available--;
120     if(pos >= end) {
121         if(readBytes() < 0)
122         return -1;
123     }
124     return readBuffer[pos++] & 0xFF;
125     }
126
127     /** Read a chunk from the request body.
128      */

129     public int doRead(byte[] b, int off, int len) throws IOException JavaDoc {
130     if( available == 0 )
131         return -1;
132     // if available == -1: unknown length, we'll read until end of stream.
133
if(pos >= end) {
134         if(readBytes() <= 0)
135         return -1;
136     }
137     int rd = -1;
138     if((end - pos) > len) {
139         rd = len;
140     } else {
141         rd = end - pos;
142     }
143
144         System.arraycopy(readBuffer, pos, b, off, rd);
145     pos += rd;
146     if( available!= -1 )
147         available -= rd;
148
149     return rd;
150     }
151     
152     /**
153      * Read bytes to the read chunk buffer.
154      */

155     protected int readBytes()
156         throws IOException JavaDoc {
157
158         int result = coyoteRequest.doRead(readChunk);
159         if (result > 0) {
160             readBuffer = readChunk.getBytes();
161             end = readChunk.getEnd();
162             pos = readChunk.getStart();
163         } else if( result < 0 ) {
164             throw new IOException JavaDoc( "Read bytes failed " + result );
165         }
166         return result;
167
168     }
169
170     // -------------------- override special methods
171

172     public MessageBytes remoteAddr() {
173     if( remoteAddrMB.isNull() ) {
174         coyoteRequest.action( ActionCode.ACTION_REQ_HOST_ADDR_ATTRIBUTE, coyoteRequest );
175     }
176     return remoteAddrMB;
177     }
178
179     public MessageBytes remoteHost() {
180     if( remoteHostMB.isNull() ) {
181         coyoteRequest.action( ActionCode.ACTION_REQ_HOST_ATTRIBUTE, coyoteRequest );
182     }
183     return remoteHostMB;
184     }
185
186     public String JavaDoc getLocalHost() {
187     return localHost;
188     }
189
190     public MessageBytes serverName(){
191         // That's set by protocol in advance, it's needed for mapping anyway,
192
// no need to do lazy eval.
193
return coyoteRequest.serverName();
194     }
195
196     public int getServerPort(){
197         return coyoteRequest.getServerPort();
198     }
199     
200     public void setServerPort(int i ) {
201     coyoteRequest.setServerPort( i );
202     }
203
204
205     public void setRemoteUser( String JavaDoc s ) {
206     super.setRemoteUser(s);
207     coyoteRequest.getRemoteUser().setString(s);
208     }
209
210     public String JavaDoc getRemoteUser() {
211     String JavaDoc s=coyoteRequest.getRemoteUser().toString();
212     if( s == null )
213         s=super.getRemoteUser();
214     return s;
215     }
216
217     public String JavaDoc getAuthType() {
218     return coyoteRequest.getAuthType().toString();
219     }
220     
221     public void setAuthType(String JavaDoc s ) {
222     coyoteRequest.getAuthType().setString(s);
223     }
224
225     public String JavaDoc getJvmRoute() {
226     return coyoteRequest.instanceId().toString();
227     }
228     
229     public void setJvmRoute(String JavaDoc s ) {
230     coyoteRequest.instanceId().setString(s);
231     }
232
233     public boolean isSecure() {
234     return "https".equalsIgnoreCase( coyoteRequest.scheme().toString());
235     }
236 }
237
Popular Tags