KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > methods > GetMethod


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/GetMethod.java,v 1.24.2.3 2004/06/13 20:24:49 olegk Exp $
3  * $Revision: 1.24.2.3 $
4  * $Date: 2004/06/13 20:24:49 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 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  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.methods;
33
34 import java.io.File JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.FileOutputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.net.URLEncoder JavaDoc;
41
42 import org.apache.commons.httpclient.HttpConnection;
43 import org.apache.commons.httpclient.HttpException;
44 import org.apache.commons.httpclient.HttpMethodBase;
45 import org.apache.commons.httpclient.HttpState;
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48
49 /**
50  * Implements the HTTP GET method.
51  * <p>
52  * The HTTP GET method is defined in section 9.3 of
53  * <a HREF="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
54  * <blockquote>
55  * The GET method means retrieve whatever information (in the form of an
56  * entity) is identified by the Request-URI. If the Request-URI refers
57  * to a data-producing process, it is the produced data which shall be
58  * returned as the entity in the response and not the source text of the
59  * process, unless that text happens to be the output of the process.
60  * </blockquote>
61  * </p>
62  * <p>
63  * GetMethods will follow redirect requests from the http server by default.
64  * This behavour can be disabled by calling setFollowRedirects(false).</p>
65  * <p>
66  * The useDisk methods have been deprecated. Disk I/O is the responsibility
67  * of the client. If you need to write a response body to a file, you
68  * can use the following as an example:
69  * <pre>
70  * out = new FileOutputStream(myFile);
71  * InputStream in = getResponseBodyAsStream();
72  * byte[] buffer = new byte[10000];
73  * int len ;
74  * while ((len = in.read(buffer)) > 0) {
75  * out.write(buffer, 0, len);
76  * }
77  * in.close();
78  * out.close();
79  * </pre>
80  * </p>
81  *
82  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
83  * @author Sung-Gu Park
84  * @author Sean C. Sullivan
85  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
86  * @author <a HREF="mailto:jsdever@apache.org">Jeff Dever</a>
87  *
88  * @version $Revision: 1.24.2.3 $
89  * @since 1.0
90  */

91 public class GetMethod extends HttpMethodBase {
92
93     // -------------------------------------------------------------- Constants
94

95     /** Log object for this class. */
96     private static final Log LOG = LogFactory.getLog(GetMethod.class);
97
98     /**
99      * Temporary directory.
100      * @deprecated the client is responsible for disk I/O
101      */

102     private static final String JavaDoc TEMP_DIR = "temp/";
103
104
105     // ----------------------------------------------------- Instance Variables
106

107     /**
108      * File which contains the buffered data.
109      * @deprecated the client is responsible for disk I/O
110      */

111     private File JavaDoc fileData;
112
113     /**
114      * Temporary directory to use.
115      * @deprecated the client is responsible for disk I/O
116      */

117     private String JavaDoc tempDir = TEMP_DIR;
118
119     /**
120      * Temporary file to use.
121      * @deprecated the client is responsible for disk I/O
122      */

123     private String JavaDoc tempFile = null;
124
125     /**
126      * By default, the get method will buffer read data to the memory.
127      * @deprecated the client is responsible for disk I/O
128      */

129     private boolean useDisk = false;
130
131
132     // ----------------------------------------------------------- Constructors
133

134     /**
135      * No-arg constructor.
136      *
137      * @since 1.0
138      */

139     public GetMethod() {
140         setFollowRedirects(true);
141     }
142
143     /**
144      * Constructor specifying a URI.
145      *
146      * @param uri either an absolute or relative URI
147      *
148      * @since 1.0
149      */

150     public GetMethod(String JavaDoc uri) {
151         super(uri);
152         LOG.trace("enter GetMethod(String)");
153         setFollowRedirects(true);
154     }
155
156     /**
157      * Constructor.
158      *
159      * @param path the path to request
160      * @param tempDir the directory in which to store temporary files
161      *
162      * @deprecated the client is responsible for disk I/O
163      * @since 1.0
164      */

165     public GetMethod(String JavaDoc path, String JavaDoc tempDir) {
166         super(path);
167         LOG.trace("enter GetMethod(String, String)");
168         setUseDisk(true);
169         setTempDir(tempDir);
170         setFollowRedirects(true);
171     }
172
173     /**
174      * Constructor.
175      *
176      * @param path the path to request
177      * @param tempDir the directory in which to store temporary files
178      * @param tempFile the file (under tempDir) to buffer contents to
179      *
180      * @deprecated the client is responsible for disk I/O
181      * @since 1.0
182      */

183     public GetMethod(String JavaDoc path, String JavaDoc tempDir, String JavaDoc tempFile) {
184         super(path);
185         LOG.trace("enter GetMethod(String, String, String)");
186         setUseDisk(true);
187         setTempDir(tempDir);
188         setTempFile(tempFile);
189         setFollowRedirects(true);
190     }
191
192     /**
193      * Constructor.
194      *
195      * @param path the path to request
196      * @param fileData the file to buffer contents to
197      *
198      * @deprecated the client is responsible for disk I/O
199      * @since 1.0
200      */

201     public GetMethod(String JavaDoc path, File JavaDoc fileData) {
202         this(path);
203         LOG.trace("enter GetMethod(String, File)");
204         useDisk = true;
205         this.fileData = fileData;
206         setFollowRedirects(true);
207     }
208
209     //~ Methods ииииииииииииииииииииииииииииииииииииииииииииииииииииииииииииииии
210

211     /**
212      * File data setter.
213      *
214      * @param fileData the file to buffer data to
215      *
216      * @deprecated the client is responsible for disk I/O
217      * @since 1.0
218      */

219     public void setFileData(File JavaDoc fileData) {
220         checkNotUsed();
221         this.fileData = fileData;
222     }
223
224     /**
225      * File data getter.
226      *
227      * @return the file being used for buffering data
228      *
229      * @deprecated the client is responsible for disk I/O
230      * @since 1.0
231      */

232     public File JavaDoc getFileData() {
233         return fileData;
234     }
235
236     // --------------------------------------------------------- Public Methods
237

238     /**
239      * Returns <tt>"GET"</tt>.
240      *
241      * @return <tt>"GET"</tt>
242      *
243      * @since 2.0
244      */

245     public String JavaDoc getName() {
246         return "GET";
247     }
248
249     /**
250      * Temporary directory setter.
251      *
252      * @param tempDir New value of tempDir
253      *
254      * @deprecated the client is responsible for disk I/O
255      * @since 1.0
256      */

257     public void setTempDir(String JavaDoc tempDir) {
258         checkNotUsed();
259         this.tempDir = tempDir;
260         setUseDisk(true);
261     }
262
263     /**
264      * Temporary directory getter.
265      *
266      * @return the current temporary directory
267      *
268      * @deprecated the client is responsible for disk I/O
269      * @since 1.0
270      */

271     public String JavaDoc getTempDir() {
272         return tempDir;
273     }
274
275     /**
276      * Temporary file setter.
277      *
278      * @param tempFile New value of tempFile
279      *
280      * @deprecated the client is responsible for disk I/O
281      * @since 1.0
282      */

283     public void setTempFile(String JavaDoc tempFile) {
284         checkNotUsed();
285         this.tempFile = tempFile;
286     }
287
288     /**
289      * Temporary file getter.
290      *
291      * @return the current temporary file
292      *
293      * @deprecated the client is responsible for disk I/O
294      * @since 1.0
295      */

296     public String JavaDoc getTempFile() {
297         return tempFile;
298     }
299
300     // ------------------------------------------------------------- Properties
301

302     /**
303      * Buffer the response in a file or not. The default is false.
304      *
305      * @param useDisk If true the entire response will be buffered in a
306      * temporary file.
307      *
308      * @deprecated the client is responsible for disk I/O
309      * @since 1.0
310      */

311     public void setUseDisk(boolean useDisk) {
312         checkNotUsed();
313         this.useDisk = useDisk;
314     }
315
316     /**
317      * Tells if the response will be buffered in a file.
318      *
319      * @return true if the response will be buffered
320      *
321      * @deprecated the client is responsible for disk I/O
322      * @since 1.0
323      */

324     public boolean getUseDisk() {
325         return useDisk;
326     }
327
328     /**
329      * Recycles the HTTP method so that it can be used again.
330      * Note that all of the instance variables will be reset
331      * once this method has been called. This method will also
332      * release the connection being used by this HTTP method.
333      *
334      * @see #releaseConnection()
335      *
336      * @since 1.0
337      *
338      * @deprecated no longer supported and will be removed in the future
339      * version of HttpClient
340      */

341     public void recycle() {
342         LOG.trace("enter GetMethod.recycle()");
343
344         super.recycle();
345         this.fileData = null;
346         setFollowRedirects(true);
347     }
348
349     // ----------------------------------------------------- HttpMethod Methods
350

351     /**
352      * Overrides method in {@link HttpMethodBase} to write data to the
353      * appropriate buffer.
354      *
355      * @param state the {@link HttpState state} information associated with this method
356      * @param conn the {@link HttpConnection connection} used to execute
357      * this HTTP method
358      *
359      * @throws IOException if an I/O (transport) error occurs
360      * @throws HttpException if a protocol exception occurs.
361      * @throws HttpRecoverableException if a recoverable transport error occurs.
362      * Usually this kind of exceptions can be recovered from by
363      * retrying the HTTP method
364      *
365      * @see #readResponse
366      * @see #processResponseBody
367      *
368      * @since 2.0
369      */

370     protected void readResponseBody(HttpState state, HttpConnection conn)
371     throws IOException JavaDoc, HttpException {
372         LOG.trace("enter GetMethod.readResponseBody(HttpState, HttpConnection)");
373
374         super.readResponseBody(state, conn);
375
376         OutputStream JavaDoc out = null;
377         if (useDisk) {
378             out = new FileOutputStream JavaDoc(createTempFile());
379             InputStream JavaDoc in = getResponseBodyAsStream();
380             byte[] buffer = new byte[10000];
381             int len ;
382             while ((len = in.read(buffer)) > 0) {
383                 out.write(buffer, 0, len);
384             }
385             in.close();
386             out.close();
387             setResponseStream(new FileInputStream JavaDoc(createTempFile()));
388         }
389     }
390
391     /**
392      * Returns the file buffer, creating it if necessary. The created file is
393      * deleted when the VM exits.
394      * @return Temporary file to hold the data buffer.
395      *
396      * @deprecated the client is responsible for disk I/O
397      */

398     private File JavaDoc createTempFile() {
399         if (fileData == null) {
400             // Create a temporary file on the HD
401
File JavaDoc dir = new File JavaDoc(tempDir);
402             dir.deleteOnExit();
403             dir.mkdirs();
404             String JavaDoc tempFileName = null;
405             if (tempFile == null) {
406                 String JavaDoc encodedPath = URLEncoder.encode(getPath());
407                 int length = encodedPath.length();
408                 if (length > 200) {
409                     encodedPath =
410                         encodedPath.substring(length - 190, length);
411                 }
412                 tempFileName = System.currentTimeMillis() + "-"
413                     + encodedPath + ".tmp";
414             } else {
415                 tempFileName = tempFile;
416             }
417             fileData = new File JavaDoc(tempDir, tempFileName);
418  
419             fileData = new File JavaDoc(tempDir, tempFileName);
420             fileData.deleteOnExit();
421         }
422         return fileData;
423     }
424 }
425
Popular Tags