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;
021import java.io.UnsupportedEncodingException;
022
023import org.jwat.common.ByteCountingPushBackInputStream;
024import org.jwat.common.Diagnosis;
025import org.jwat.common.DiagnosisType;
026import org.jwat.common.Diagnostics;
027import org.jwat.common.MaxLengthRecordingInputStream;
028import org.jwat.common.PayloadWithHeaderAbstract;
029
030/**
031 * This class can be used to post process a payload object to parse and
032 * validate a suspected ARC version block header. Any metadata present is
033 * preserved and exposed as payload of the ARC version block object.
034 *
035 * @author nicl
036 */
037public class ArcVersionHeader extends PayloadWithHeaderAbstract {
038
039    /** Buffer size used by <code>PushbackInputStream</code>. */
040    public static final int PUSHBACK_BUFFER_SIZE = 32;
041
042    /** ARC field parser used.
043     * Must be set prior to calling the various methods. */
044    protected ArcFieldParsers fieldParsers;
045
046    /** Is the version format valid. */
047    public boolean bValidVersionFormat;
048
049    /** Version string. */
050    public String versionStr;
051
052    /** Did we find a valid version number. */
053    protected boolean isVersionValid;
054
055    /** Did we recognize the block description line. */
056    public boolean isValidBlockdDesc;
057
058    /** Version of the block description. Last line in the version block. */
059    public int blockDescVersion;
060
061    /** Block description line. */
062    public String blockDesc;
063
064    /*
065     * Fields.
066     */
067
068    /** Version description field string value. */
069    public String versionNumberStr;
070
071    /** Version description field integer value. */
072    public Integer versionNumber;
073
074    /** Reserved field value string, used for version 1.1. */
075    public String reservedStr;
076
077    /** Reserved field value, used for version 1.1. */
078    public Integer reserved;
079
080    /** Version block origin code. */
081    public String originCode;
082
083    /** ARC record version. */
084    public ArcVersion version;
085
086    /**
087     * Create a version header and initialize it using the version and origin
088     * code arguments.
089     * @param version version to initialize this header according to
090     * @param originCode origin code
091     * @return an initialized version header object ready for writing
092     */
093    public static ArcVersionHeader create(ArcVersion version, String originCode) {
094        if (version == null) {
095            throw new IllegalArgumentException("'version' argument is null!");
096        }
097        if (originCode == null || originCode.length() == 0) {
098            throw new IllegalArgumentException("'originCode' argument is null or empty!");
099        }
100        ArcVersionHeader versionHeader = new ArcVersionHeader();
101        versionHeader.versionNumber = version.major;
102        versionHeader.reserved = version.minor;
103        versionHeader.originCode = originCode;
104        switch (version) {
105        case VERSION_1:
106        case VERSION_1_1:
107            versionHeader.blockDescVersion = 1;
108            versionHeader.blockDesc = ArcConstants.VERSION_1_BLOCK_DEF;
109            break;
110        case VERSION_2:
111            versionHeader.blockDescVersion = 2;
112            versionHeader.blockDesc = ArcConstants.VERSION_2_BLOCK_DEF;
113            break;
114        }
115        versionHeader.bValidVersionFormat = true;
116        versionHeader.isVersionValid = true;
117        versionHeader.isValidBlockdDesc = true;
118        return versionHeader;
119    }
120
121    /**
122     * Use the information contained in this object to rebuild the byte array
123     * representation of the version header.
124     * (@see getHeader())
125     * @throws UnsupportedEncodingException if an encoding error occurs
126     */
127    public void rebuild() throws UnsupportedEncodingException {
128        StringBuilder sb = new StringBuilder();
129        sb.append(Integer.toString(versionNumber));
130        sb.append(" ");
131        sb.append(Integer.toString(reserved));
132        sb.append(" ");
133        sb.append(originCode);
134        sb.append("\n");
135        sb.append(blockDesc);
136        sb.append("\n");
137        header = sb.toString().getBytes("ISO8859-1");
138    }
139
140    /**
141     * Method called to parse and validate an ARC version block.
142     * This method never returns null so validity and extra state information
143     * is kept even in failure.
144     * @param pbin payload input stream
145     * @param length length of payload
146     * @param digestAlgorithm digest algorithm for payload minus header
147     * @param fieldParsers parser used for the different field types
148     * @param diagnostics object used to report errors and/or warnings
149     * @return <code>ArcVersionHeader</code> object
150     * @throws IOException if an I/O exception occurs while parsing the version block
151     */
152    public static ArcVersionHeader processPayload(ByteCountingPushBackInputStream pbin,
153            long length, String digestAlgorithm, ArcFieldParsers fieldParsers,
154            Diagnostics<Diagnosis> diagnostics) throws IOException {
155        if (pbin == null) {
156            throw new IllegalArgumentException(
157                    "The inputstream 'pbin' is null");
158        }
159        if (length < 0) {
160            throw new IllegalArgumentException(
161                    "The 'length' is less than zero: " + length);
162        }
163        if (fieldParsers == null) {
164            throw new IllegalArgumentException(
165                    "'fieldParsers' is null");
166        }
167        if (diagnostics == null) {
168            throw new IllegalArgumentException(
169                    "'diagnostics' is null");
170        }
171        ArcVersionHeader avh = new ArcVersionHeader();
172        avh.in_pb = pbin;
173        avh.totalLength = length;
174        avh.digestAlgorithm = digestAlgorithm;
175        avh.fieldParsers = fieldParsers;
176        avh.diagnostics = diagnostics;
177        avh.initProcess();
178        return avh;
179    }
180
181    @Override
182    protected boolean readHeader(MaxLengthRecordingInputStream in,
183            long payloadLength) throws IOException {
184        ByteCountingPushBackInputStream pbin = new ByteCountingPushBackInputStream(in, PUSHBACK_BUFFER_SIZE);
185        String versionLine = pbin.readLine();
186        String blockDescLine = pbin.readLine();
187        // debug
188        //System.out.println(versionLine);
189        //System.out.println(blockDescLine);
190        /*
191         * Check for version and parse if present.
192         */
193        if (versionLine != null && versionLine.length() > 0) {
194            String[] versionArr = versionLine.split(" ", -1);
195            if (versionArr.length != ArcConstants.VERSION_DESC_FIELDS.length) {
196                diagnostics.addError(new Diagnosis(DiagnosisType.INVALID,
197                        ArcConstants.ARC_VERSION_BLOCK,
198                        "Invalid version description"));
199            }
200            /*
201             * Get version and origin.
202             */
203            switch (versionArr.length) {
204            default:
205            case 3:
206                originCode = versionArr[ArcConstants.FN_IDX_ORIGIN_CODE];
207                originCode = fieldParsers.parseString(
208                            originCode, ArcConstants.FN_ORIGIN_CODE, false);
209            case 2:
210                reservedStr = versionArr[ArcConstants.FN_IDX_RESERVED];
211                reserved = fieldParsers.parseInteger(
212                            reservedStr, ArcConstants.FN_RESERVED, false);
213            case 1:
214                versionNumberStr = versionArr[ArcConstants.FN_IDX_VERSION_NUMBER];
215                versionNumber = fieldParsers.parseInteger(
216                            versionNumberStr, ArcConstants.FN_VERSION_NUMBER, false);
217            case 0:
218                break;
219            }
220            /*
221             *  Check version.
222             */
223            version = null;
224            if (versionNumber != null && reserved != null) {
225                bValidVersionFormat = true;
226                versionStr = Integer.toString(versionNumber) + "." + Integer.toString(reserved);
227                // Check ARC version number
228                version = ArcVersion.fromValues(versionNumber.intValue(),
229                        reserved.intValue());
230            }
231            isVersionValid = (version != null);
232            if (!isVersionValid) {
233                // Add validation error
234                diagnostics.addError(new Diagnosis(DiagnosisType.INVALID,
235                        ArcConstants.ARC_VERSION_BLOCK,
236                        "Invalid version: [version number: " + versionNumber
237                        + ", reserved: " + reserved +']'));
238            }
239        } else {
240            diagnostics.addError(new Diagnosis(DiagnosisType.ERROR,
241                    ArcConstants.ARC_VERSION_BLOCK,
242                    "Version line empty"));
243        }
244        /*
245         * Identify block description.
246         */
247        if (blockDescLine != null && blockDescLine.length() > 0) {
248            if (ArcConstants.VERSION_1_BLOCK_DEF.equals(blockDescLine)) {
249                isValidBlockdDesc = true;
250                blockDescVersion = 1;
251            } else if (ArcConstants.VERSION_2_BLOCK_DEF.equals(blockDescLine)) {
252                isValidBlockdDesc = true;
253                blockDescVersion = 2;
254            } else {
255                diagnostics.addError(new Diagnosis(DiagnosisType.INVALID,
256                        ArcConstants.ARC_VERSION_BLOCK,
257                        "Unsupported version block definition"));
258            }
259        } else {
260            diagnostics.addError(new Diagnosis(DiagnosisType.ERROR,
261                    ArcConstants.ARC_VERSION_BLOCK,
262                    "Block definition empty"));
263        }
264        boolean bIsValidVersionBlock = (version != null) && (blockDescVersion > 0);
265        if (bIsValidVersionBlock) {
266            switch (blockDescVersion) {
267            case 1:
268                if (version != ArcVersion.VERSION_1 && version != ArcVersion.VERSION_1_1) {
269                    bIsValidVersionBlock = false;
270                }
271                break;
272            case 2:
273                if (version != ArcVersion.VERSION_2) {
274                    bIsValidVersionBlock = false;
275                }
276                break;
277            }
278            if (!bIsValidVersionBlock) {
279                diagnostics.addError(new Diagnosis(DiagnosisType.INVALID,
280                        ArcConstants.ARC_VERSION_BLOCK,
281                        "Version number does not match the block definition"));
282            }
283        }
284        return bIsValidVersionBlock;
285    }
286
287    @Override
288    public String toString() {
289        StringBuilder builder = new StringBuilder(256);
290        builder.append("\nArcVersionHeader : [\n");
291        builder.append("isValid:");
292        builder.append(isValid());
293        builder.append(',');
294        builder.append("versionNumber:");
295        if(versionNumber != null){
296            builder.append(versionNumber);
297        }
298        builder.append(',');
299        builder.append("reserved:");
300        if(reserved != null){
301            builder.append(reserved);
302        }
303        builder.append(',');
304        builder.append("originCode:");
305        if(originCode != null){
306            builder.append(originCode);
307        }
308        builder.append(',');
309        builder.append("blockDescVersion:");
310        builder.append(blockDescVersion);
311        builder.append("]\n");
312        return builder.toString();
313    }
314
315}