package dk.statsbiblioteket.archeaderextractor;

/*
 * #%L
 * ARC Header Extractor
 * %%
 * Copyright (C) 2013 State and University Library, Denmark
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import org.jwat.arc.ArcRecordBase;
import org.jwat.common.HeaderLine;
import org.jwat.common.HttpHeader;
import org.jwat.common.Payload;
import org.jwat.common.UriProfile;
import org.jwat.gzip.GzipEntry;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class HeaderExtractorFile implements ArchiveParserCallback {

    protected String fileName;

    protected int recordNr = 1;

    protected byte[] tmpBuf = new byte[8192];

    public HeaderExtractorFile() {
    }

    public void processFile(File file, File outputDirectory) {
        fileName = file.getName();
        ArchiveParser archiveParser = new ArchiveParser();
        archiveParser.uriProfile = UriProfile.RFC3986_ABS_16BIT_LAX;
        archiveParser.bBlockDigestEnabled = true;
        archiveParser.bPayloadDigestEnabled = true;
        long consumed = archiveParser.parse(file, outputDirectory, this);
    }

    @Override
    public void apcFileId(File file, int fileId) {
    }

    @Override
    public void apcGzipEntryStart(GzipEntry gzipEntry, long startOffset) {
    }

    @Override
    public void apcArcRecordStart(ArcRecordBase arcRecord, long startOffset, boolean compressed, File outputDirectory) throws IOException {
        Payload payload = arcRecord.getPayload();

        HttpHeader httpHeader = null;
        if (payload != null) {
            httpHeader = arcRecord.getHttpHeader();
            if (httpHeader != null) {
                FileWriter headerFile = new FileWriter(
                        outputDirectory.getAbsolutePath() + "/" + fileName
                                + "-"
                                + arcRecord.getStartOffset()
                                + "-"
                                + recordNr);

                PrintWriter hout = new PrintWriter(headerFile);

                hout.println("URL: " + arcRecord.getUrlStr());
                hout.println("IP:  " + arcRecord.getIpAddress());

                hout.println("ProtocolVersion: " + httpHeader.getProtocolVersion());
                hout.println("ProtocolStatusCode: " + httpHeader.getProtocolStatusCodeStr());
                hout.println("ProtocolContentType: " + httpHeader.getProtocolContentType());
                hout.println("TotalLength: " + httpHeader.getTotalLength());

                for (HeaderLine hl : httpHeader.getHeaderList()) {
                    hout.println(hl.name + ": " + hl.value);
                }

                hout.println("Filename: " + fileName);
                hout.println("Offset: " + arcRecord.getStartOffset());

                hout.close();
            }
        }
        if (httpHeader != null) {
            httpHeader.close();
        }
        if (payload != null) {
            payload.close();
        }
        arcRecord.close();
        ++recordNr;
    }

    @Override
    public void apcUpdateConsumed(long consumed) {
    }

    @Override
    public void apcRuntimeError(Throwable t, long offset, long consumed) {
    }

    @Override
    public void apcDone() {
    }

}

@@ -943,95 +943,9 @@
 .io.
-File;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0Aimport java.io.PrintWriter
+*
 ;%0A%0Ap
@@ -2127,367 +2127,53 @@
     
-FileWriter headerFile = new FileWriter(%0A                        outputDirectory.getAbsolutePath() + %22/%22 + fileName%0A                                + %22-%22%0A                                + arcRecord.getStartOffset()%0A                                + %22-%22%0A                                + recordNr);%0A%0A                PrintWriter hout
+StringWriter headerString
  = new 
-Print
+String
 Writer(
-headerFile
 );%0A%0A
@@ -2189,27 +2189,33 @@
        h
-out.println
+eaderString.write
 (%22URL: %22
@@ -2234,24 +2234,31 @@
 .getUrlStr()
+ + %22%5Cn%22
 );%0A         
@@ -2265,27 +2265,33 @@
        h
-out.println
+eaderString.write
 (%22IP:  %22
@@ -2317,16 +2317,23 @@
 ddress()
+ + %22%5Cn%22
 );%0A%0A    
@@ -2341,35 +2341,41 @@
            h
-out.println
+eaderString.write
 (%22ProtocolVe
@@ -2416,16 +2416,23 @@
 ersion()
+ + %22%5Cn%22
 );%0A     
@@ -2439,35 +2439,41 @@
            h
-out.println
+eaderString.write
 (%22ProtocolSt
@@ -2523,16 +2523,23 @@
 odeStr()
+ + %22%5Cn%22
 );%0A     
@@ -2550,27 +2550,33 @@
        h
-out.println
+eaderString.write
 (%22Protoc
@@ -2629,16 +2629,23 @@
 ntType()
+ + %22%5Cn%22
 );%0A     
@@ -2656,27 +2656,33 @@
        h
-out.println
+eaderString.write
 (%22TotalL
@@ -2719,16 +2719,23 @@
 Length()
+ + %22%5Cn%22
 );%0A%0A    
@@ -2818,27 +2818,33 @@
        h
-out.println
+eaderString.write
 (hl.name
@@ -2861,16 +2861,23 @@
 hl.value
+ + %22%5Cn%22
 );%0A     
@@ -2907,27 +2907,33 @@
        h
-out.println
+eaderString.write
 (%22Filena
@@ -2948,16 +2948,23 @@
 fileName
+ + %22%5Cn%22
 );%0A     
@@ -2979,19 +2979,25 @@
    h
-out.println
+eaderString.write
 (%22Of
@@ -3032,16 +3032,23 @@
 Offset()
+ + %22%5Cn%22
 );%0A%0A    
@@ -3063,12 +3063,391 @@
     
-hout
+FileWriter headerFile = new FileWriter(%0A                        outputDirectory.getAbsolutePath() + %22/%22 + fileName%0A                                + %22-%22%0A                                + arcRecord.getStartOffset()%0A                                + %22-%22%0A                                + recordNr);%0A%0A                headerFile.write(headerString.toString());%0A                headerFile
 .clo

Use StringWriter to temporarily store the output text before writing it to disk