KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > methods > HttpRequestBodyMethodBase


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

22
23 package org.apache.webdav.lib.methods;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.net.URL JavaDoc;
33 import org.apache.commons.httpclient.ChunkedOutputStream;
34 import org.apache.commons.httpclient.HttpConnection;
35 import org.apache.commons.httpclient.HttpConstants;
36 import org.apache.commons.httpclient.HttpException;
37 import org.apache.commons.httpclient.HttpMethodBase;
38 import org.apache.commons.httpclient.HttpState;
39 import org.apache.commons.httpclient.HttpStatus;
40
41
42 /**
43  * PUT Method.
44  *
45  *
46  * @since 1.0
47  */

48 public abstract class HttpRequestBodyMethodBase extends HttpMethodBase {
49
50
51     // ----------------------------------------------------------- Constructors
52

53
54     /**
55      * Default constructor.
56      */

57     public HttpRequestBodyMethodBase() {
58         super();
59     }
60
61
62
63     /**
64      * URI-setting constructor.
65      *
66      * @param uri the URI to request. The URI is expected
67      * to be already URL encoded. It may be either an absolute or
68      * server relative path.
69      *
70      * @since 1.0
71      */

72     public HttpRequestBodyMethodBase(String JavaDoc uri) {
73         super(uri);
74     }
75
76
77     // ------------------------------------------------------- Instance Methods
78

79
80     /**
81      * Request body content to be sent.
82      */

83     private byte[] data = null;
84
85
86     /**
87      * Request body content to be sent.
88      */

89     private File JavaDoc file = null;
90
91
92     /**
93      * Request body content to be sent.
94      */

95     private URL JavaDoc url = null;
96
97
98     // --------------------------------------------------------- Public Methods
99

100
101     /**
102      * Set my request body content to the contents of a file.
103      *
104      * @since 2.0
105      */

106     public void setRequestBody(File JavaDoc file) throws IOException JavaDoc {
107         checkNotUsed();
108         this.file = file;
109     }
110
111     /**
112      * Set my request body content to the resource at the specified URL.
113      *
114      * @since 2.0
115      */

116     public void setRequestBody(URL JavaDoc url) throws IOException JavaDoc {
117         checkNotUsed();
118         this.url = url;
119     }
120
121     /**
122      * Set my request body content to the contents of a byte array.
123      *
124      * @since 2.0
125      */

126     public void setRequestBody(byte[] bodydata) {
127         checkNotUsed();
128         this.data = bodydata;
129     }
130
131     /**
132      * Set my request body content to the contents of a string.
133      *
134      * @since 2.0
135      */

136     public void setRequestBody(String JavaDoc bodydata) {
137         checkNotUsed();
138         setRequestBody(HttpConstants.getContentBytes(bodydata, getRequestCharSet()));
139     }
140
141     /**
142      * Set my request body content to the contents of an input stream.
143      * The contents will be buffered into
144      * memory. To upload large entities, it is recommended to first buffer the
145      * data into a temporary file, and then send that file.
146      *
147      * @since 2.0
148      */

149     public void setRequestBody(InputStream JavaDoc is)
150         throws IOException JavaDoc {
151
152         checkNotUsed();
153         byte[] buffer = new byte[4096];
154         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
155         int nb = 0;
156         while (true) {
157             nb = is.read(buffer);
158             if (nb == -1) {
159                 break;
160             }
161             os.write(buffer, 0, nb);
162         }
163         data = os.toByteArray();
164     }
165
166     /**
167      * Returns true if <tt>100 Continue</tt> status code
168      * is found.
169      *
170      * @since 2.0
171      */

172     public boolean readContinueCode() {
173         if (getStatusLine() == null) {
174             return false;
175         }
176         if(null != getRequestHeader("expect") &&
177            getStatusLine().getStatusCode() != HttpStatus.SC_CONTINUE) {
178             return false;
179         }
180         return true;
181     }
182
183     /**
184      * Do write the request body.
185      * Override the method of {@link HttpMethodBase}
186      * if the method should wait until a <tt>100 Continue</tt> status code
187      * is expected (@link readContinueCode)
188      *
189      * @since 2.0
190      */

191     protected boolean writeRequestBody(HttpState state, HttpConnection conn)
192         throws IOException JavaDoc, HttpException {
193         OutputStream JavaDoc out = conn.getRequestOutputStream();
194         if (isHttp11() && (null == getRequestHeader("Content-Length"))) {
195             out = new ChunkedOutputStream(out);
196         }
197
198         InputStream JavaDoc inputStream = null;
199         if (file != null && file.exists()) {
200             inputStream = new FileInputStream JavaDoc(file);
201         } else if (url != null) {
202             inputStream = url.openConnection().getInputStream();
203         } else if(data != null){
204             inputStream = new ByteArrayInputStream JavaDoc(data);
205         } else {
206             return true;
207         }
208
209         byte[] buffer = new byte[4096];
210         int nb = 0;
211         while (true) {
212             nb = inputStream.read(buffer);
213             if (nb == -1) {
214                 break;
215             }
216             out.write(buffer, 0, nb);
217         }
218         out.flush();
219         return true;
220     }
221
222     /**
223      * Override the method of {@link HttpMethodBase}
224      * to return the appropriate content length.
225      *
226      * @since 2.0
227      */

228     protected int getRequestContentLength() {
229         if(null != data) {
230             return data.length;
231         } else if(null != file && file.exists()) {
232             return (int)(file.length());
233         } else if(url != null) {
234             return -1;
235         } else {
236             return 0;
237         }
238     }
239
240
241     /**
242      * return true, if the method setRequestContent has been called (with a null parameter)
243      *
244      * @since 2.0
245      */

246     protected boolean isRequestContentAlreadySet() {
247         return (data != null) || (file != null) || (url != null);
248     }
249
250     /**
251      *
252      * @since 1.0
253      */

254     public void recycle() {
255         super.recycle();
256         data = null;
257         url = null;
258         file = null;
259     }
260 }
261
262
Popular Tags