KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > logger > XHttpServletRequestFacade


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/logger/XHttpServletRequestFacade.java,v 1.6 2004/07/28 09:32:36 ib Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/07/28 09:32:36 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.logger;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29
30 import javax.servlet.ServletInputStream JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
33
34 import org.apache.slide.common.Domain;
35
36 /**
37  * This class supports additional set-methods and a re-readable
38  * inputstream to the interface javax.servlet.http.HttpServletRequest.
39  *
40  * Christopher Lenz (cmlenz at apache.org)
41  *
42  * @version 0.1
43  *
44  * @invariant (inputStream != null)
45  *
46  * @see javax.servlet.http.HttpServletRequest
47  *
48  */

49 public class XHttpServletRequestFacade extends HttpServletRequestWrapper JavaDoc {
50         
51             
52     public static final String JavaDoc DEFAULT_CHAR_ENCODING = "8859_1";
53
54     /**
55      * the inputstream to re-read.
56      *
57      * @see java.io.BufferedInputStream
58      */

59     private XServletInputStreamFacade inStreamFacade = null;
60             
61     /**
62      * buffered reader which uses the input stream
63      *
64      * @see java.io.BufferedReader
65      */

66     private BufferedReader JavaDoc reader = null;
67         
68     /**
69      * true - if inputstream can read using a reader; false otherwise.
70      */

71     private boolean usingReader = false;
72
73     /**
74      * true - if inputstream can read using a stream; false otherwise.
75      */

76     private boolean usingStream = false;
77         
78         
79     /**
80      * This constructor creates an re-readable HttpServletRequest.
81      *
82      * @pre (req != null)
83      * @post
84      *
85      * @param req HttpServletRequest
86      *
87      * @time
88      * @space
89      */

90     public XHttpServletRequestFacade (HttpServletRequest JavaDoc request) {
91         super(request);
92         Domain.debug("Create XHttpServletRequestFacade");
93     }
94
95     /**
96      * Retrieves the body of the request as binary data using a ServletInputStream.
97      *
98      * @param none
99      *
100      * @return Returns the body of the request
101      */

102     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
103         Domain.debug("ENTER: XHttpServletRequestFacade:getInputStream()");
104         if (usingReader) {
105             throw new IllegalStateException JavaDoc("getReader() method has been called on this request");
106         }
107         usingStream = true;
108         
109         if ( inStreamFacade == null ) {
110             inStreamFacade = new XServletInputStreamFacade( super.getInputStream());
111         }
112             
113         Domain.debug("LEAVE: XHttpServletRequestFacade:getInputStream()");
114         return ( inStreamFacade );
115     }
116
117         
118     /**
119      * Reads the next byte of data from the input stream.
120      *
121      * @param none
122      *
123      * @return the next byte of data, or -1 if the end of the stream is reached
124      */

125     public int doRead() throws IOException JavaDoc {
126         return inStreamFacade.read();
127     }
128
129     /**
130      * Reads up to len bytes of data from the input stream into an array of bytes.
131      *
132      * @param b - the buffer into which the data is read.
133      * @param off - the start offset in array b at which the data is written.
134      * @param len - the maximum number of bytes to read.
135      *
136      * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
137      */

138     public int doRead( byte b[], int off, int len ) throws IOException JavaDoc {
139         return inStreamFacade.read( b, off, len );
140     }
141
142         
143     /**
144      * Retrieves the body of the request as character data using a BufferedReader.
145      *
146      * @param none
147      *
148      * @return Returns a BufferedReader
149      */

150     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
151         Domain.debug("ENTER: XHttpServletRequestFacade:getReader()");
152         if (usingStream) {
153             throw new IllegalStateException JavaDoc("getInputStream() method has been called on this request");
154         }
155         usingReader = true;
156
157         if ( inStreamFacade == null ) {
158             inStreamFacade = new XServletInputStreamFacade( super.getInputStream());
159         }
160
161         if( reader != null ) {
162             Domain.debug("LEAVE: XHttpServletRequestFacade:getReader() - reader != null");
163             return reader; // method already called
164
}
165
166         String JavaDoc encoding = super.getCharacterEncoding();
167         if (encoding == null) {
168             encoding = DEFAULT_CHAR_ENCODING;
169         }
170     
171         InputStreamReader JavaDoc r = new InputStreamReader JavaDoc(inStreamFacade, encoding);
172         reader= new BufferedReader JavaDoc(r);
173         Domain.debug("LEAVE: XHttpServletRequestFacade:getReader() - new BufferedReader");
174         return reader;
175     }
176
177     /**
178      * Returns the content of the buffered input stream.
179      *
180      * @return copy of input stream as string.
181      */

182     public String JavaDoc getRequestBody() throws IOException JavaDoc {
183         if ( usingStream ){
184             Domain.debug("XHttpServletRequestFacade:getRequestBody() - usingStream");
185             return inStreamFacade.getBufferContent();
186         } else if ( usingReader ) {
187             Domain.debug("XHttpServletRequestFacade:getRequestBody() - usingReader");
188             return reader.toString();
189         } else {
190             Domain.debug("XHttpServletRequestFacade:getRequestBody() - nor Reader nor Stream - do nothing");
191             return "";
192         }
193     }
194 }
195
Popular Tags