1 25 package org.archive.util; 26 27 import java.io.BufferedInputStream ; 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 35 import org.archive.io.RecordingInputStream; 36 import org.archive.io.RecordingOutputStream; 37 import org.archive.io.ReplayCharSequence; 38 import org.archive.io.ReplayInputStream; 39 40 41 52 public class HttpRecorder { 53 protected static Logger logger = 54 Logger.getLogger("org.archive.util.HttpRecorder"); 55 56 private static final int DEFAULT_OUTPUT_BUFFER_SIZE = 4096; 57 private static final int DEFAULT_INPUT_BUFFER_SIZE = 65536; 58 59 private RecordingInputStream ris = null; 60 private RecordingOutputStream ros = null; 61 62 67 private String backingFileBasename = null; 68 69 72 private static final String RECORDING_OUTPUT_STREAM_SUFFIX = ".ros"; 73 74 77 private static final String RECORDING_INPUT_STREAM_SUFFIX = ".ris"; 78 79 82 private String characterEncoding = null; 83 84 89 protected HttpRecorder() { 90 super(); 91 } 92 93 104 public HttpRecorder(File tempDir, String backingFilenameBase, 105 int outBufferSize, int inBufferSize) { 106 super(); 107 tempDir.mkdirs(); 108 this.backingFileBasename = 109 (new File (tempDir.getPath(), backingFilenameBase)) 110 .getAbsolutePath(); 111 this.ris = new RecordingInputStream(inBufferSize, 112 this.backingFileBasename + RECORDING_INPUT_STREAM_SUFFIX); 113 this.ros = new RecordingOutputStream(outBufferSize, 114 this.backingFileBasename + RECORDING_OUTPUT_STREAM_SUFFIX); 115 } 116 117 128 public HttpRecorder(File tempDir, String backingFilenameBase) { 129 this(tempDir, backingFilenameBase, DEFAULT_INPUT_BUFFER_SIZE, 130 DEFAULT_OUTPUT_BUFFER_SIZE); 131 } 132 133 145 public InputStream inputWrap(InputStream is) 146 throws IOException { 147 logger.fine(Thread.currentThread().getName() + " wrapping input"); 148 this.ris.open(is); 149 return this.ris; 150 } 151 152 164 public OutputStream outputWrap(OutputStream os) 165 throws IOException { 166 this.ros.open(os); 167 return this.ros; 168 } 169 170 173 public void close() { 174 logger.fine(Thread.currentThread().getName() + " closing"); 175 try { 176 this.ris.close(); 177 } catch (IOException e) { 178 DevUtils.logger.log(Level.SEVERE, "close() ris" + 181 DevUtils.extraInfo(), e); 182 } 183 try { 184 this.ros.close(); 185 } catch (IOException e) { 186 DevUtils.logger.log(Level.SEVERE, "close() ros" + 187 DevUtils.extraInfo(), e); 188 } 189 } 190 191 196 public RecordingInputStream getRecordedInput() { 197 return this.ris; 198 } 199 200 203 public RecordingOutputStream getRecordedOutput() { 204 return this.ros; 205 } 206 207 210 public void markContentBegin() { 211 this.ris.markContentBegin(); 212 } 213 214 public long getResponseContentLength() { 215 return this.ris.getResponseContentLength(); 216 } 217 218 225 public void closeRecorders() { 226 try { 227 this.ris.closeRecorder(); 228 this.ros.closeRecorder(); 229 } catch (IOException e) { 230 DevUtils.warnHandle(e, "Convert to runtime exception?"); 231 } 232 } 233 234 240 public void cleanup() { 241 this.close(); 242 this.delete(this.backingFileBasename + RECORDING_OUTPUT_STREAM_SUFFIX); 243 this.delete(this.backingFileBasename + RECORDING_INPUT_STREAM_SUFFIX); 244 } 245 246 251 private void delete(String name) { 252 File f = new File (name); 253 if (f.exists()) { 254 f.delete(); 255 } 256 } 257 258 264 public static HttpRecorder getHttpRecorder() { 265 HttpRecorder recorder = null; 266 Thread thread = Thread.currentThread(); 267 if (thread instanceof HttpRecorderMarker) { 268 recorder = ((HttpRecorderMarker)thread).getHttpRecorder(); 269 } 270 return recorder; 271 } 272 273 276 public void setCharacterEncoding(String characterEncoding) { 277 this.characterEncoding = characterEncoding; 278 } 279 280 283 public String getCharacterEncoding() { 284 return this.characterEncoding; 285 } 286 287 294 public ReplayCharSequence getReplayCharSequence() throws IOException { 295 return getRecordedInput(). 296 getReplayCharSequence(this.characterEncoding); 297 } 298 299 303 public ReplayInputStream getReplayInputStream() throws IOException { 304 return getRecordedInput().getReplayInputStream(); 305 } 306 307 318 public static HttpRecorder wrapInputStreamWithHttpRecord(File dir, 319 String basename, InputStream in, String encoding) 320 throws IOException { 321 HttpRecorder rec = new HttpRecorder(dir, basename); 322 if (encoding != null && encoding.length() > 0) { 323 rec.setCharacterEncoding(encoding); 324 } 325 InputStream is = rec.inputWrap(new BufferedInputStream (in)); 328 final int BUFFER_SIZE = 1024 * 4; 329 byte [] buffer = new byte[BUFFER_SIZE]; 330 while(true) { 331 int x = is.read(buffer); 333 if (x == -1) { 334 break; 335 } 336 } 337 is.close(); 338 return rec; 339 } 340 } 341 | Popular Tags |