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.warc; 019 020import org.jwat.common.Digest; 021 022/** 023 * This class represents the parsed and format validated information provided 024 * from a WARC digest header value. 025 * 026 * @author nicl 027 */ 028public class WarcDigest extends Digest { 029 030 /** 031 * Package level constructor. 032 */ 033 protected WarcDigest() { 034 } 035 036 /** 037 * Construct an object with the supplied parameters. 038 * @param algorithm digest algorithm 039 * @param digestValue digest value in encoded format. (base16/32/64) 040 */ 041 protected WarcDigest(String algorithm, String digestValue) { 042 this.algorithm = algorithm; 043 this.digestString = digestValue; 044 } 045 046 /** 047 * Parse and validate the format of a WARC digest header value. 048 * @param labelledDigest WARC digest header value 049 * @return <code>WarcDigest</code> object or <code>null</code> 050 */ 051 public static WarcDigest parseWarcDigest(String labelledDigest) { 052 if (labelledDigest == null || labelledDigest.length() == 0) { 053 return null; 054 } 055 String algorithm; 056 String digestValue; 057 int cIdx = labelledDigest.indexOf(':'); 058 if (cIdx != -1) { 059 algorithm = labelledDigest.substring(0, cIdx).trim().toLowerCase(); 060 digestValue = labelledDigest.substring(cIdx + 1).trim(); 061 if (algorithm.length() > 0 && digestValue.length() > 0) { 062 return new WarcDigest(algorithm, digestValue); 063 } 064 } 065 return null; 066 } 067 068 /** 069 * Create an object with the supplied parameters. 070 * @param algorithm digest algorithm 071 * @param digestBytes digest in byte form 072 * @param encoding encoding used 073 * @param digestValue digest value in encoded form. 074 * @return the warc digest 075 */ 076 public static WarcDigest createWarcDigest(String algorithm, byte[] digestBytes, String encoding, String digestValue) { 077 if (algorithm == null || algorithm.length() == 0) { 078 throw new IllegalArgumentException("'algorithm' is empty or null"); 079 } 080 if (digestBytes == null || digestBytes.length == 0) { 081 throw new IllegalArgumentException("'digestBytes' is empty or null"); 082 } 083 if (encoding == null || encoding.length() == 0) { 084 throw new IllegalArgumentException("'encoding' is empty or null"); 085 } 086 if (digestValue == null || digestValue.length() == 0) { 087 throw new IllegalArgumentException("'digestValue' is empty or null"); 088 } 089 WarcDigest digest = new WarcDigest(); 090 digest.algorithm = algorithm.toLowerCase(); 091 digest.digestBytes = digestBytes; 092 digest.encoding = encoding.toLowerCase(); 093 digest.digestString = digestValue; 094 return digest; 095 } 096 097 /** 098 * Returns a header representation of the class state. 099 * The string includes: algorithm and digest string. 100 * @return header representation of the class state 101 */ 102 @Override 103 public String toString() { 104 return (algorithm + ":" + digestString); 105 } 106 107 /** 108 * Returns a full textual string representation of the class state. 109 * The string includes: algorithm, encoding and digest string. 110 * @return a full textual string representation of the class state 111 */ 112 public String toStringFull() { 113 return (algorithm + ":" + encoding + ":" + digestString); 114 } 115 116}