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.arc; 019 020import java.io.IOException; 021 022import org.jwat.common.ByteCountingPushBackInputStream; 023import org.jwat.common.Diagnosis; 024import org.jwat.common.DiagnosisType; 025import org.jwat.common.Diagnostics; 026import org.jwat.common.HttpHeader; 027import org.jwat.common.Payload; 028 029/** 030 * This class represents an ARC record and header including possible 031 * validation and format warnings/errors encountered in the process. 032 * This class also contains the specific ARC record payload processing. 033 * The payload of the ARC record is accessible through a wrapped payload 034 * object. 035 * 036 * @author lbihanic, selghissassi, nicl 037 */ 038public class ArcRecord extends ArcRecordBase { 039 040 /** Buffer size used in toString(). */ 041 public static final int TOSTRING_BUFFER_SIZE = 256; 042 043 /** 044 * Protected constructor to force instantiation of record header 045 * from stream. 046 */ 047 protected ArcRecord() { 048 } 049 050 /** 051 * Create and initialize a new <code>ArcRecord</code> for writing. 052 * @param writer writer which shall be used 053 * @return an <code>ArcRecord</code> prepared for writing 054 */ 055 public static ArcRecord createRecord(ArcWriter writer) { 056 ArcRecord ar = new ArcRecord(); 057 ar.trailingNewLines = 1; 058 ar.diagnostics = new Diagnostics<Diagnosis>(); 059 ar.header = ArcHeader.initHeader(writer, ar.diagnostics); 060 writer.fieldParsers.diagnostics = ar.diagnostics; 061 return ar; 062 } 063 064 /** 065 * Creates a new <code>ArcRecord</code> based on the supplied header and 066 * starts processing the payload, if present. 067 * @param reader <code>ArcReader</code> used, with access to user defined 068 * options 069 * @param diagnostics diagnostics used to report errors and/or warnings 070 * @param header record header that has already been processed 071 * @param in <code>InputStream</code> used to read possible payload 072 * @return an <code>ArcRecord</code> 073 * @throws IOException I/O exception while processing possible payload 074 */ 075 public static ArcRecord parseArcRecord(ArcReader reader, 076 Diagnostics<Diagnosis> diagnostics, 077 ArcHeader header, ByteCountingPushBackInputStream in) 078 throws IOException { 079 ArcRecord ar = new ArcRecord(); 080 ar.recordType = RT_ARC_RECORD; 081 ar.reader = reader; 082 ar.diagnostics = diagnostics; 083 ar.header = header; 084 ar.in = in; 085 ar.processPayload(in, reader); 086 ar.consumed = in.getConsumed() - ar.header.startOffset; 087 return ar; 088 } 089 090 @Override 091 protected void processPayload(ByteCountingPushBackInputStream in, 092 ArcReader reader) throws IOException { 093 payload = null; 094 if (header.archiveLength != null && header.archiveLength > 0L) { 095 String digestAlgorithm = null; 096 if (reader.bBlockDigest) { 097 digestAlgorithm = reader.blockDigestAlgorithm; 098 } 099 payload = Payload.processPayload(in, header.archiveLength.longValue(), 100 reader.payloadHeaderMaxSize, digestAlgorithm); 101 payload.setOnClosedHandler(this); 102 // HttpHeader. 103 if (HttpHeader.isSupported(header.urlScheme)) { 104 // Never! -> && !ArcConstants.CONTENT_TYPE_NO_TYPE.equals(header.contentTypeStr) 105 digestAlgorithm = null; 106 if (reader.bPayloadDigest) { 107 digestAlgorithm = reader.payloadDigestAlgorithm; 108 } 109 // Try to read a valid HTTP response header from the payload. 110 httpHeader = HttpHeader.processPayload(HttpHeader.HT_RESPONSE, 111 payload.getInputStream(), header.archiveLength.longValue(), 112 digestAlgorithm); 113 if (httpHeader != null) { 114 if (httpHeader.isValid()) { 115 payload.setPayloadHeaderWrapped(httpHeader); 116 } else { 117 diagnostics.addError( 118 new Diagnosis(DiagnosisType.ERROR, 119 "http header", 120 "Unable to parse http header!")); 121 } 122 } 123 } 124 } else if (HttpHeader.isSupported(header.urlScheme)) { 125 // Never! -> && !ArcConstants.CONTENT_TYPE_NO_TYPE.equals(header.contentTypeStr) 126 diagnostics.addError(new Diagnosis(DiagnosisType.ERROR_EXPECTED, 127 ArcConstants.ARC_FILE, 128 "Expected payload not found in the record block")); 129 } 130 return; 131 } 132 133 @Override 134 public String toString() { 135 StringBuilder builder = new StringBuilder(TOSTRING_BUFFER_SIZE); 136 builder.append("\nArcRecord ["); 137 builder.append(super.toString()); 138 builder.append(']'); 139 if (httpHeader != null) { 140 builder.append(httpHeader.toString()); 141 } 142 return builder.toString(); 143 } 144 145}