001/** 002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC 003 * and GZip files. (http://jwat.org/) 004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/) 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.jwat.archive; 019 020import java.io.ByteArrayInputStream; 021import java.io.File; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.OutputStream; 025import java.io.RandomAccessFile; 026import java.nio.ByteBuffer; 027import java.security.MessageDigest; 028import java.security.NoSuchAlgorithmException; 029import java.util.concurrent.ConcurrentLinkedQueue; 030import java.util.concurrent.Semaphore; 031 032import org.jwat.arc.ArcRecordBase; 033import org.jwat.common.ByteArrayIOStream; 034import org.jwat.common.ByteCountingPushBackInputStream; 035import org.jwat.common.HttpHeader; 036import org.jwat.common.Payload; 037import org.jwat.common.PayloadWithHeaderAbstract; 038import org.jwat.common.RandomAccessFileInputStream; 039import org.jwat.warc.WarcRecord; 040 041/** 042 * Class used to buffer a record payload so it can be read multiple times. 043 * Either buffers in memory or on temporary storage depending on the payload size. 044 * 045 * @author nicl 046 */ 047public class ManagedPayload { 048 049 public static final int T_NONE = 0; 050 public static final int T_MEMORY_BUFFERED = 1; 051 public static final int T_FILE_BUFFERED = 2; 052 053 public static final int DEFAULT_COPY_BUFFER_SIZE = 8192; 054 055 public static final int DEFAULT_IN_MEMORY_BUFFER_SIZE = 10*1024*1024; 056 057 public static final String DEFAULT_BLOCK_DIGEST_ALGO = "SHA1"; 058 059 public static final String DEFAULT_PAYLOAD_DIGEST_ALGO = "SHA1"; 060 061 protected static Semaphore queueLock = new Semaphore(1); 062 063 protected static ConcurrentLinkedQueue<ManagedPayload> managedPayloadQueue = new ConcurrentLinkedQueue<ManagedPayload>(); 064 065 public static ManagedPayload checkout() { 066 ManagedPayload managedPayload = null; 067 queueLock.acquireUninterruptibly(); 068 managedPayload = managedPayloadQueue.poll(); 069 if (managedPayload == null) { 070 managedPayload = new ManagedPayload(DEFAULT_COPY_BUFFER_SIZE, DEFAULT_IN_MEMORY_BUFFER_SIZE); 071 } 072 if (!managedPayload.lock.tryAcquire()) { 073 throw new IllegalStateException(); 074 } 075 queueLock.release(); 076 return managedPayload; 077 } 078 079 public void checkin() { 080 queueLock.acquireUninterruptibly(); 081 lock.release(); 082 managedPayloadQueue.add(this); 083 queueLock.release(); 084 } 085 086 protected Semaphore lock = new Semaphore(1); 087 088 protected MessageDigest blockDigestObj; 089 090 protected MessageDigest payloadDigestObj; 091 092 public byte[] blockDigestBytes; 093 094 public byte[] payloadDigestBytes; 095 096 public int type = T_NONE; 097 098 protected byte[] copyBuf; 099 100 protected File tmpfile; 101 102 protected ByteArrayIOStream baios; 103 104 protected RandomAccessFile tmpfile_raf; 105 106 public long payloadLength; 107 108 public HttpHeader httpHeader; 109 110 public byte[] httpHeaderBytes; 111 112 public long httpHeaderLength; 113 114 protected ManagedPayload(int copyBufferSize, int inMemorybufferSize) { 115 this(copyBufferSize, inMemorybufferSize, DEFAULT_BLOCK_DIGEST_ALGO, DEFAULT_PAYLOAD_DIGEST_ALGO); 116 } 117 118 protected ManagedPayload(int copyBufferSize, int inMemorybufferSize, String blockDigestAlgo, String payloadDigestAlgo) { 119 try { 120 blockDigestObj = MessageDigest.getInstance(blockDigestAlgo); 121 payloadDigestObj = MessageDigest.getInstance(payloadDigestAlgo); 122 } catch (NoSuchAlgorithmException e) { 123 throw new IllegalArgumentException("Digest algorithm not supported!", e); 124 } 125 copyBuf = new byte[copyBufferSize]; 126 baios = new ByteArrayIOStream(inMemorybufferSize); 127 } 128 129 public void close() { 130 if (tmpfile_raf != null) { 131 try { 132 tmpfile_raf.close(); 133 tmpfile_raf = null; 134 } catch (IOException e) { 135 } 136 } 137 if (tmpfile != null && tmpfile.exists()) { 138 tmpfile.delete(); 139 tmpfile = null; 140 } 141 } 142 143 public void manageVersionBlock(ArcRecordBase arcRecord, boolean bDigest) throws IOException { 144 blockDigestObj.reset(); 145 blockDigestBytes = null; 146 httpHeader = null; 147 httpHeaderBytes = null; 148 httpHeaderLength = 0; 149 payloadLength = 0; 150 151 OutputStream out; 152 type = T_NONE; 153 154 Payload payload = arcRecord.getPayload(); 155 InputStream payload_in = null; 156 int read; 157 if ((arcRecord.header.headerBytes.length + arcRecord.getArchiveLength()) <= baios.getLength()) { 158 type = T_MEMORY_BUFFERED; 159 out = baios.getOutputStream(); 160 if (bDigest) { 161 blockDigestObj.update(arcRecord.header.headerBytes); 162 } 163 out.write(arcRecord.header.headerBytes); 164 if (payload != null) { 165 payload_in = payload.getInputStreamComplete(); 166 while((read = payload_in.read(copyBuf)) != -1) { 167 if (bDigest) { 168 blockDigestObj.update(copyBuf, 0, read); 169 } 170 out.write(copyBuf, 0, read); 171 } 172 } 173 out.close(); 174 payloadLength = (long)baios.getLimit(); 175 } else { 176 type = T_FILE_BUFFERED; 177 if (tmpfile == null) { 178 tmpfile = File.createTempFile("JWAT-", "-ARC2WARC"); 179 } 180 if (tmpfile_raf == null) { 181 tmpfile_raf = new RandomAccessFile(tmpfile, "rw"); 182 } 183 tmpfile_raf.seek(0L); 184 tmpfile_raf.setLength(0L); 185 if (bDigest) { 186 blockDigestObj.update(arcRecord.header.headerBytes); 187 } 188 tmpfile_raf.write(arcRecord.header.headerBytes); 189 if (payload != null) { 190 payload_in = payload.getInputStreamComplete(); 191 while((read = payload_in.read(copyBuf)) != -1) { 192 if (bDigest) { 193 blockDigestObj.update(copyBuf, 0, read); 194 } 195 tmpfile_raf.write(copyBuf, 0, read); 196 } 197 } 198 tmpfile_raf.seek(0L); 199 payloadLength = tmpfile_raf.length(); 200 tmpfile_raf.close(); 201 tmpfile_raf = null; 202 } 203 if (bDigest) { 204 blockDigestBytes = blockDigestObj.digest(); 205 } 206 if (payload_in != null) { 207 payload_in.close(); 208 } 209 if (payload != null) { 210 payload.close(); 211 } 212 } 213 214 public void manageArcRecord(ArcRecordBase arcRecord, boolean bDigest) throws IOException { 215 manageRecord(arcRecord.getPayload(), bDigest); 216 } 217 218 public void manageWarcRecord(WarcRecord warcRecord, boolean bDigest) throws IOException { 219 manageRecord(warcRecord.getPayload(), bDigest); 220 } 221 222 public void manageRecord(Payload payload, boolean bDigest) throws IOException { 223 blockDigestObj.reset(); 224 payloadDigestObj.reset(); 225 blockDigestBytes = null; 226 payloadDigestBytes = null; 227 httpHeader = null; 228 httpHeaderBytes = null; 229 httpHeaderLength = 0; 230 payloadLength = 0; 231 232 OutputStream out; 233 type = T_NONE; 234 235 PayloadWithHeaderAbstract payloadHeaderWrapped; 236 InputStream payload_in = null; 237 int read; 238 if (payload != null) { 239 payloadHeaderWrapped = payload.getPayloadHeaderWrapped(); 240 if (payloadHeaderWrapped instanceof HttpHeader) { 241 httpHeader = (HttpHeader)payloadHeaderWrapped; 242 } 243 if (httpHeader != null && httpHeader.isValid()) { 244 httpHeaderBytes = httpHeader.getHeader(); 245 if (bDigest) { 246 blockDigestObj.update(httpHeaderBytes); 247 } 248 httpHeaderLength = httpHeaderBytes.length; 249 } 250 if (payload.getRemaining() <= baios.getLength()) { 251 type = T_MEMORY_BUFFERED; 252 out = baios.getOutputStream(); 253 payload_in = payload.getInputStream(); 254 while((read = payload_in.read(copyBuf)) != -1) { 255 if (bDigest) { 256 blockDigestObj.update(copyBuf, 0, read); 257 payloadDigestObj.update(copyBuf, 0, read); 258 } 259 out.write(copyBuf, 0, read); 260 } 261 out.close(); 262 payloadLength = (long)baios.getLimit(); 263 } else { 264 type = T_FILE_BUFFERED; 265 if (tmpfile == null) { 266 tmpfile = File.createTempFile("JWAT-", "-ARC2WARC"); 267 } 268 if (tmpfile_raf == null) { 269 tmpfile_raf = new RandomAccessFile(tmpfile, "rw"); 270 } 271 tmpfile_raf.seek(0L); 272 tmpfile_raf.setLength(0L); 273 payload_in = payload.getInputStream(); 274 while((read = payload_in.read(copyBuf)) != -1) { 275 if (bDigest) { 276 blockDigestObj.update(copyBuf, 0, read); 277 payloadDigestObj.update(copyBuf, 0, read); 278 } 279 tmpfile_raf.write(copyBuf, 0, read); 280 } 281 tmpfile_raf.seek(0L); 282 payloadLength = tmpfile_raf.length(); 283 tmpfile_raf.close(); 284 tmpfile_raf = null; 285 } 286 } 287 if (bDigest) { 288 blockDigestBytes = blockDigestObj.digest(); 289 payloadDigestBytes = payloadDigestObj.digest(); 290 } 291 if (payload_in != null) { 292 payload_in.close(); 293 } 294 if (payload != null) { 295 payload.close(); 296 } 297 } 298 299 public void managedHttp(byte[] bytes, boolean bDigest) throws IOException { 300 blockDigestObj.reset(); 301 payloadDigestObj.reset(); 302 blockDigestBytes = null; 303 payloadDigestBytes = null; 304 305 httpHeader = HttpHeader.processPayload(HttpHeader.HT_RESPONSE, new ByteCountingPushBackInputStream(new ByteArrayInputStream(bytes), 16384), bytes.length, null); 306 if (httpHeader == null || !httpHeader.isValid()) { 307 throw new IllegalStateException(); 308 } 309 httpHeaderBytes = httpHeader.getHeader(); 310 if (bDigest) { 311 blockDigestObj.update(httpHeaderBytes); 312 } 313 httpHeaderLength = httpHeaderBytes.length; 314 } 315 316 /** 317 * Closes the input stream after usage. 318 * @param in 319 * @param len 320 * @param bDigest 321 * @throws IOException 322 */ 323 public void managePayloadInputStream(InputStream in, long len, boolean bDigest) throws IOException { 324 payloadLength = 0; 325 326 OutputStream out; 327 type = T_NONE; 328 329 int read; 330 if (len <= baios.getLength()) { 331 type = T_MEMORY_BUFFERED; 332 out = baios.getOutputStream(); 333 while((read = in.read(copyBuf)) != -1) { 334 if (bDigest) { 335 blockDigestObj.update(copyBuf, 0, read); 336 payloadDigestObj.update(copyBuf, 0, read); 337 } 338 out.write(copyBuf, 0, read); 339 } 340 out.close(); 341 payloadLength = (long)baios.getLimit(); 342 } else { 343 type = T_FILE_BUFFERED; 344 if (tmpfile == null) { 345 tmpfile = File.createTempFile("JWAT-", "-ARC2WARC"); 346 } 347 if (tmpfile_raf == null) { 348 tmpfile_raf = new RandomAccessFile(tmpfile, "rw"); 349 } 350 tmpfile_raf.seek(0L); 351 tmpfile_raf.setLength(0L); 352 while((read = in.read(copyBuf)) != -1) { 353 if (bDigest) { 354 blockDigestObj.update(copyBuf, 0, read); 355 payloadDigestObj.update(copyBuf, 0, read); 356 } 357 tmpfile_raf.write(copyBuf, 0, read); 358 } 359 tmpfile_raf.seek(0L); 360 payloadLength = tmpfile_raf.length(); 361 tmpfile_raf.close(); 362 tmpfile_raf = null; 363 } 364 if (bDigest) { 365 blockDigestBytes = blockDigestObj.digest(); 366 payloadDigestBytes = payloadDigestObj.digest(); 367 } 368 in.close(); 369 if (payloadLength != len) { 370 throw new IllegalStateException(); 371 } 372 } 373 374 public ByteBuffer getBuffer() { 375 return baios.getByteBuffer(); 376 } 377 378 public File getFile() { 379 return tmpfile; 380 } 381 382 public InputStream getHttpHeaderStream() { 383 if (httpHeaderBytes != null) { 384 return new ByteArrayInputStream(httpHeaderBytes); 385 } else { 386 return null; 387 } 388 } 389 390 public InputStream getPayloadStream() throws IOException { 391 switch (type) { 392 case T_MEMORY_BUFFERED: 393 return baios.getInputStream(); 394 case T_FILE_BUFFERED: 395 if (tmpfile_raf == null) { 396 tmpfile_raf = new RandomAccessFile(tmpfile, "rw"); 397 } 398 tmpfile_raf.seek(0L); 399 return new RandomAccessFileInputStream(tmpfile_raf); 400 default: 401 return null; 402 } 403 } 404 405}