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
020/**
021 * Class containing all relevant ARC constants and structures.
022 * Including but not limited to field names and mime-types.
023 *
024 * @author lbihanic, selghissassi, nicl
025 */
026public final class ArcConstants {
027
028    /**
029     * No constructor for this utility class, static access only.
030     */
031    protected ArcConstants() {
032    }
033
034    /** Invalid ARC file property. */
035    protected static final String ARC_FILE = "ARC file";
036
037    /** Invalid ARC record property. */
038    protected static final String ARC_RECORD = "ARC record";
039
040    /** Invalid ARC version block property. */
041    protected static final String ARC_VERSION_BLOCK = "ARC version block";
042
043    /** Maximum payload size in bytes of unwanted trailing new lines in
044     * V1.0/V2.0 version block. */
045    public static final int ARC_VB_MAX_TRAILING_NEWLINES = 1024*1024;
046
047    /** Trailing newlines after each record as per the ARC documentation. */
048    public static final int ARC_RECORD_TRAILING_NEWLINES = 1;
049
050    /** An ARC version block starts with this string. */
051    public static final String ARC_MAGIC_HEADER = "filedesc:";
052
053    /** Arc file URL URI scheme. */
054    public static final String ARC_SCHEME = "filedesc";
055
056    /** End mark used after each record consisting of one line feed. */
057    protected static byte[] endMark = "\n".getBytes();
058
059    /** Text plain content type. */
060    public static final String CONTENT_TYPE_TEXT_PLAIN = "text/plain";
061
062    /** Special content-type for none. */
063    public static final String CONTENT_TYPE_NO_TYPE = "no-type";
064
065    /** Version block preferred content-type. */
066    public static final String VERSION_BLOCK_CONTENT_TYPE = "text";
067
068    /** Version block preferred media-type. */
069    public static final String VERSION_BLOCK_MEDIA_TYPE = "plain";
070
071    /** ARC date format string as specified in the ARC documentation. */
072    public static final String ARC_DATE_FORMAT = "yyyyMMddHHmmss";
073
074    /** Content-type format string as specified in RFC2616.*/
075    public static final String CONTENT_TYPE_FORMAT = "<type>/<sub-type>(; <argument>=<value>)*";
076
077    /**
078     * Record block fields.
079     */
080
081    /** Url field name. */
082    public static final String FN_URL = "URL";
083    /** Ip-Address field name. */
084    public static final String FN_IP_ADDRESS = "IP-address";
085    /** Date field name. */
086    public static final String FN_ARCHIVE_DATE = "Archive-date";
087    /** Content-Type field name. */
088    public static final String FN_CONTENT_TYPE = "Content-type";
089    /** Result-Code field name. */
090    public static final String FN_RESULT_CODE = "Result-code";
091    /** Checksum field name. */
092    public static final String FN_CHECKSUM = "Checksum";
093    /** Location field name. */
094    public static final String FN_LOCATION = "Location";
095    /** Offset field name. */
096    public static final String FN_OFFSET = "Offset";
097    /** Filename field name. */
098    public static final String FN_FILENAME = "Filename";
099    /** Length field name. */
100    public static final String FN_ARCHIVE_LENGTH = "Archive-length";
101
102    /** URL record field index. */
103    public static final int FN_IDX_URL = 0;
104    /** Ip address record field index. */
105    public static final int FN_IDX_IP_ADDRESS = 1;
106    /** Archive date record field index. */
107    public static final int FN_IDX_ARCHIVE_DATE = 2;
108    /** Content-type record field index. */
109    public static final int FN_IDX_CONTENT_TYPE = 3;
110    /** Result code record field index.  */
111    public static final int FN_IDX_RESULT_CODE = 4;
112    /** Checksum record field index. */
113    public static final int FN_IDX_CHECKSUM = 5;
114    /** Location record field index. */
115    public static final int FN_IDX_LOCATION = 6;
116    /** Offset record field index. */
117    public static final int FN_IDX_OFFSET = 7;
118    /** Filename record field index. */
119    public static final int FN_IDX_FILENAME = 8;
120
121    /**
122     * Version block fields.
123     */
124
125    /** Version field name. */
126    public static final String FN_VERSION_NUMBER = "Version-number";
127    /** Reserved field name. */
128    public static final String FN_RESERVED = "Reserved";
129    /** Origin field name. */
130    public static final String FN_ORIGIN_CODE = "Origin-code";
131
132    /** Version number version field index. */
133    public static final int FN_IDX_VERSION_NUMBER = 0;
134    /** Reserved version field index. */
135    public static final int FN_IDX_RESERVED = 1;
136    /** Origin code version field index. */
137    public static final int FN_IDX_ORIGIN_CODE = 2;
138
139    /** Version-1-block fields. */
140    public static final String[] VERSION_1_BLOCK_FIELDS = {
141            FN_URL, FN_IP_ADDRESS, FN_ARCHIVE_DATE, FN_CONTENT_TYPE,
142            FN_ARCHIVE_LENGTH};
143
144    /** Version-2-block fields. */
145    public static final String[] VERSION_2_BLOCK_FIELDS = {
146            FN_URL, FN_IP_ADDRESS, FN_ARCHIVE_DATE, FN_CONTENT_TYPE,
147            FN_RESULT_CODE, FN_CHECKSUM, FN_LOCATION,
148            FN_OFFSET, FN_FILENAME,
149            FN_ARCHIVE_LENGTH };
150
151    /** Number of fields in a version 1 block. */
152    public static final int VERSION_1_BLOCK_NUMBER_FIELDS = VERSION_1_BLOCK_FIELDS.length;
153
154    /** Number of fields in a version 2 block. */
155    public static final int VERSION_2_BLOCK_NUMBER_FIELDS = VERSION_2_BLOCK_FIELDS.length;
156
157    /** Version description fields. */
158    public static final String[] VERSION_DESC_FIELDS = {
159        FN_VERSION_NUMBER, FN_RESERVED, FN_ORIGIN_CODE };
160
161    /** Version 1 description string. */
162    public static final String VERSION_1_BLOCK_DEF =
163                    join(' ', VERSION_1_BLOCK_FIELDS);
164
165    /** Version 2 description string. */
166    public static final String VERSION_2_BLOCK_DEF =
167                    join(' ', VERSION_2_BLOCK_FIELDS);
168
169    /**
170     * Helper method for converting an array in a string using a specified
171     * separator.
172     * @param sep desired separator
173     * @param elts array of string to join
174     * @return an array joined into a string
175     */
176    public static String join(char sep, String... elts) {
177        StringBuilder buf = new StringBuilder();
178        for (String s : elts) {
179            buf.append(s).append(sep);
180        }
181        buf.setLength(buf.length() - 1);
182        return buf.toString();
183    }
184
185}