1 31 32 package org.apache.commons.httpclient.methods; 33 34 import java.io.File ; 35 import java.io.FileInputStream ; 36 import java.io.FileOutputStream ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.OutputStream ; 40 import java.net.URLEncoder ; 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 91 public class GetMethod extends HttpMethodBase { 92 93 95 96 private static final Log LOG = LogFactory.getLog(GetMethod.class); 97 98 102 private static final String TEMP_DIR = "temp/"; 103 104 105 107 111 private File fileData; 112 113 117 private String tempDir = TEMP_DIR; 118 119 123 private String tempFile = null; 124 125 129 private boolean useDisk = false; 130 131 132 134 139 public GetMethod() { 140 setFollowRedirects(true); 141 } 142 143 150 public GetMethod(String uri) { 151 super(uri); 152 LOG.trace("enter GetMethod(String)"); 153 setFollowRedirects(true); 154 } 155 156 165 public GetMethod(String path, String tempDir) { 166 super(path); 167 LOG.trace("enter GetMethod(String, String)"); 168 setUseDisk(true); 169 setTempDir(tempDir); 170 setFollowRedirects(true); 171 } 172 173 183 public GetMethod(String path, String tempDir, String 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 201 public GetMethod(String path, File fileData) { 202 this(path); 203 LOG.trace("enter GetMethod(String, File)"); 204 useDisk = true; 205 this.fileData = fileData; 206 setFollowRedirects(true); 207 } 208 209 211 219 public void setFileData(File fileData) { 220 checkNotUsed(); 221 this.fileData = fileData; 222 } 223 224 232 public File getFileData() { 233 return fileData; 234 } 235 236 238 245 public String getName() { 246 return "GET"; 247 } 248 249 257 public void setTempDir(String tempDir) { 258 checkNotUsed(); 259 this.tempDir = tempDir; 260 setUseDisk(true); 261 } 262 263 271 public String getTempDir() { 272 return tempDir; 273 } 274 275 283 public void setTempFile(String tempFile) { 284 checkNotUsed(); 285 this.tempFile = tempFile; 286 } 287 288 296 public String getTempFile() { 297 return tempFile; 298 } 299 300 302 311 public void setUseDisk(boolean useDisk) { 312 checkNotUsed(); 313 this.useDisk = useDisk; 314 } 315 316 324 public boolean getUseDisk() { 325 return useDisk; 326 } 327 328 341 public void recycle() { 342 LOG.trace("enter GetMethod.recycle()"); 343 344 super.recycle(); 345 this.fileData = null; 346 setFollowRedirects(true); 347 } 348 349 351 370 protected void readResponseBody(HttpState state, HttpConnection conn) 371 throws IOException , HttpException { 372 LOG.trace("enter GetMethod.readResponseBody(HttpState, HttpConnection)"); 373 374 super.readResponseBody(state, conn); 375 376 OutputStream out = null; 377 if (useDisk) { 378 out = new FileOutputStream (createTempFile()); 379 InputStream 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 (createTempFile())); 388 } 389 } 390 391 398 private File createTempFile() { 399 if (fileData == null) { 400 File dir = new File (tempDir); 402 dir.deleteOnExit(); 403 dir.mkdirs(); 404 String tempFileName = null; 405 if (tempFile == null) { 406 String 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 (tempDir, tempFileName); 418 419 fileData = new File (tempDir, tempFileName); 420 fileData.deleteOnExit(); 421 } 422 return fileData; 423 } 424 } 425 | Popular Tags |