View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/GetMethod.java,v 1.24 2003/04/29 22:18:08 olegk Exp $ 3 * $Revision: 1.24 $ 4 * $Date: 2003/04/29 22:18:08 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient.methods; 65 66 import java.io.File; 67 import java.io.FileInputStream; 68 import java.io.FileOutputStream; 69 import java.io.IOException; 70 import java.io.InputStream; 71 import java.io.OutputStream; 72 import java.net.URLEncoder; 73 74 import org.apache.commons.httpclient.HttpConnection; 75 import org.apache.commons.httpclient.HttpException; 76 import org.apache.commons.httpclient.HttpMethodBase; 77 import org.apache.commons.httpclient.HttpState; 78 import org.apache.commons.logging.Log; 79 import org.apache.commons.logging.LogFactory; 80 81 /*** 82 * Implements the HTTP GET specification. 83 * <p> 84 * The HTTP GET method is defined in section 8.1 of 85 * <a href="http://www.ietf.org/rfc/rfc1945.txt">RFC1945</a>: 86 * <blockquote> 87 * The GET method means retrieve whatever information (in the form of an 88 * entity) is identified by the Request-URI. If the Request-URI refers 89 * to a data-producing process, it is the produced data which shall be 90 * returned as the entity in the response and not the source text of the 91 * process, unless that text happens to be the output of the process. 92 * </blockquote> 93 * </p> 94 * <p> 95 * GetMethods will follow redirect requests from the http server by default. 96 * This behavour can be disabled by calling setFollowRedirects(false).</p> 97 * <p> 98 * The useDisk methods have been deprecated. Disk I/O is the responsibility 99 * of the client. If you need to write a response body to a file, you 100 * can use the following as an example: 101 * <pre> 102 * out = new FileOutputStream(myFile); 103 * InputStream in = getResponseBodyAsStream(); 104 * byte[] buffer = new byte[10000]; 105 * int len ; 106 * while ((len = in.read(buffer)) > 0) { 107 * out.write(buffer, 0, len); 108 * } 109 * in.close(); 110 * out.close(); 111 * </pre> 112 * </p> 113 * 114 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> 115 * @author Sung-Gu Park 116 * @author Sean C. Sullivan 117 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 118 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 119 * 120 * @version $Revision: 1.24 $ 121 * @since 1.0 122 */ 123 public class GetMethod extends HttpMethodBase { 124 125 // -------------------------------------------------------------- Constants 126 127 /*** Log object for this class. */ 128 private static final Log LOG = LogFactory.getLog(GetMethod.class); 129 130 /*** 131 * Temporary directory. 132 * @deprecated the client is responsible for disk I/O 133 */ 134 private static final String TEMP_DIR = "temp/"; 135 136 137 // ----------------------------------------------------- Instance Variables 138 139 /*** 140 * File which contains the buffered data. 141 * @deprecated the client is responsible for disk I/O 142 */ 143 private File fileData; 144 145 /*** 146 * Temporary directory to use. 147 * @deprecated the client is responsible for disk I/O 148 */ 149 private String tempDir = TEMP_DIR; 150 151 /*** 152 * Temporary file to use. 153 * @deprecated the client is responsible for disk I/O 154 */ 155 private String tempFile = null; 156 157 /*** 158 * By default, the get method will buffer read data to the memory. 159 * @deprecated the client is responsible for disk I/O 160 */ 161 private boolean useDisk = false; 162 163 164 // ----------------------------------------------------------- Constructors 165 166 /*** 167 * No-arg constructor. 168 * 169 * @since 1.0 170 */ 171 public GetMethod() { 172 setFollowRedirects(true); 173 } 174 175 /*** 176 * Constructor specifying a URI. 177 * 178 * @param uri either an absolute or relative URI 179 * 180 * @since 1.0 181 */ 182 public GetMethod(String uri) { 183 super(uri); 184 LOG.trace("enter GetMethod(String)"); 185 setFollowRedirects(true); 186 } 187 188 /*** 189 * Constructor. 190 * 191 * @param path the path to request 192 * @param tempDir the directory in which to store temporary files 193 * 194 * @deprecated the client is responsible for disk I/O 195 * @since 1.0 196 */ 197 public GetMethod(String path, String tempDir) { 198 super(path); 199 LOG.trace("enter GetMethod(String, String)"); 200 setUseDisk(true); 201 setTempDir(tempDir); 202 setFollowRedirects(true); 203 } 204 205 /*** 206 * Constructor. 207 * 208 * @param path the path to request 209 * @param tempDir the directory in which to store temporary files 210 * @param tempFile the file (under tempDir) to buffer contents to 211 * 212 * @deprecated the client is responsible for disk I/O 213 * @since 1.0 214 */ 215 public GetMethod(String path, String tempDir, String tempFile) { 216 super(path); 217 LOG.trace("enter GetMethod(String, String, String)"); 218 setUseDisk(true); 219 setTempDir(tempDir); 220 setTempFile(tempFile); 221 setFollowRedirects(true); 222 } 223 224 /*** 225 * Constructor. 226 * 227 * @param path the path to request 228 * @param fileData the file to buffer contents to 229 * 230 * @deprecated the client is responsible for disk I/O 231 * @since 1.0 232 */ 233 public GetMethod(String path, File fileData) { 234 this(path); 235 LOG.trace("enter GetMethod(String, File)"); 236 useDisk = true; 237 this.fileData = fileData; 238 setFollowRedirects(true); 239 } 240 241 //~ Methods ���������������������������������������������������������������� 242 243 /*** 244 * File data setter. 245 * 246 * @param fileData the file to buffer data to 247 * 248 * @deprecated the client is responsible for disk I/O 249 * @since 1.0 250 */ 251 public void setFileData(File fileData) { 252 checkNotUsed(); 253 this.fileData = fileData; 254 } 255 256 /*** 257 * File data getter. 258 * 259 * @return the file being used for buffering data 260 * 261 * @deprecated the client is responsible for disk I/O 262 * @since 1.0 263 */ 264 public File getFileData() { 265 return fileData; 266 } 267 268 // --------------------------------------------------------- Public Methods 269 270 /*** 271 * Returns <tt>"GET"</tt>. 272 * 273 * @return <tt>"GET"</tt> 274 * 275 * @since 2.0 276 */ 277 public String getName() { 278 return "GET"; 279 } 280 281 /*** 282 * Temporary directory setter. 283 * 284 * @param tempDir New value of tempDir 285 * 286 * @deprecated the client is responsible for disk I/O 287 * @since 1.0 288 */ 289 public void setTempDir(String tempDir) { 290 checkNotUsed(); 291 this.tempDir = tempDir; 292 setUseDisk(true); 293 } 294 295 /*** 296 * Temporary directory getter. 297 * 298 * @return the current temporary directory 299 * 300 * @deprecated the client is responsible for disk I/O 301 * @since 1.0 302 */ 303 public String getTempDir() { 304 return tempDir; 305 } 306 307 /*** 308 * Temporary file setter. 309 * 310 * @param tempFile New value of tempFile 311 * 312 * @deprecated the client is responsible for disk I/O 313 * @since 1.0 314 */ 315 public void setTempFile(String tempFile) { 316 checkNotUsed(); 317 this.tempFile = tempFile; 318 } 319 320 /*** 321 * Temporary file getter. 322 * 323 * @return the current temporary file 324 * 325 * @deprecated the client is responsible for disk I/O 326 * @since 1.0 327 */ 328 public String getTempFile() { 329 return tempFile; 330 } 331 332 // ------------------------------------------------------------- Properties 333 334 /*** 335 * Buffer the response in a file or not. The default is false. 336 * 337 * @param useDisk If true the entire response will be buffered in a 338 * temporary file. 339 * 340 * @deprecated the client is responsible for disk I/O 341 * @since 1.0 342 */ 343 public void setUseDisk(boolean useDisk) { 344 checkNotUsed(); 345 this.useDisk = useDisk; 346 } 347 348 /*** 349 * Tells if the response will be buffered in a file. 350 * 351 * @return true if the response will be buffered 352 * 353 * @deprecated the client is responsible for disk I/O 354 * @since 1.0 355 */ 356 public boolean getUseDisk() { 357 return useDisk; 358 } 359 360 /*** 361 * Override recycle to reset redirects default. 362 * 363 * @since 1.0 364 */ 365 public void recycle() { 366 LOG.trace("enter GetMethod.recycle()"); 367 368 super.recycle(); 369 this.fileData = null; 370 setFollowRedirects(true); 371 } 372 373 // ----------------------------------------------------- HttpMethod Methods 374 375 /*** 376 * Overrides method in {@link HttpMethodBase} to write data to the 377 * appropriate buffer. 378 * 379 * @param state the shared http state 380 * @param conn the connection to read data from 381 * 382 * @throws IOException when there are problems reading from the connection 383 * @throws HttpException when a protocol error occurs or state is invalid 384 * @since 2.0 385 */ 386 protected void readResponseBody(HttpState state, HttpConnection conn) 387 throws IOException, HttpException { 388 LOG.trace("enter GetMethod.readResponseBody(HttpState, HttpConnection)"); 389 390 super.readResponseBody(state, conn); 391 392 OutputStream out = null; 393 if (useDisk) { 394 out = new FileOutputStream(createTempFile()); 395 InputStream in = getResponseBodyAsStream(); 396 byte[] buffer = new byte[10000]; 397 int len ; 398 while ((len = in.read(buffer)) > 0) { 399 out.write(buffer, 0, len); 400 } 401 in.close(); 402 out.close(); 403 setResponseStream(new FileInputStream(createTempFile())); 404 } 405 } 406 407 /*** 408 * Returns the file buffer, creating it if necessary. The created file is 409 * deleted when the VM exits. 410 * @return Temporary file to hold the data buffer. 411 * 412 * @deprecated the client is responsible for disk I/O 413 */ 414 private File createTempFile() { 415 if (fileData == null) { 416 // Create a temporary file on the HD 417 File dir = new File(tempDir); 418 dir.deleteOnExit(); 419 dir.mkdirs(); 420 String tempFileName = null; 421 if (tempFile == null) { 422 String encodedPath = URLEncoder.encode(getPath()); 423 int length = encodedPath.length(); 424 if (length > 200) { 425 encodedPath = 426 encodedPath.substring(length - 190, length); 427 } 428 tempFileName = System.currentTimeMillis() + "-" 429 + encodedPath + ".tmp"; 430 } else { 431 tempFileName = tempFile; 432 } 433 fileData = new File(tempDir, tempFileName); 434 435 fileData = new File(tempDir, tempFileName); 436 fileData.deleteOnExit(); 437 } 438 return fileData; 439 } 440 }

This page was automatically generated by Maven