de.l3s.concatgz.data.WarcRecord.java Source code

Java tutorial

Introduction

Here is the source code for de.l3s.concatgz.data.WarcRecord.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Helge Holzmann (L3S)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package de.l3s.concatgz.data;

import org.apache.commons.io.IOUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.util.EntityUtils;
import org.archive.format.http.HttpHeader;
import org.archive.format.http.HttpResponse;
import org.archive.format.http.HttpResponseParser;
import org.archive.io.ArchiveReader;
import org.archive.io.ArchiveReaderFactory;
import org.archive.io.ArchiveRecord;
import org.archive.io.ArchiveRecordHeader;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class WarcRecord {
    public static final String MIME_TYPE_HTTP_HEADER = "Content-Type";
    public static final String DEFAULT_HTTP_STRING_CHARSET = "UTF-8";

    public static WarcRecord get(String filename, InputStream stream) throws IOException {
        ArchiveReader reader = null;
        try {
            reader = ArchiveReaderFactory.get(filename, stream, false);
            ArchiveRecord record = reader.get();
            return new WarcRecord(reader, record);
        } catch (Exception readException) {
            readException.printStackTrace();
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception closeException) {
                    closeException.printStackTrace();
                }
            }
            return null;
        }
    }

    private ArchiveReader reader;
    private ArchiveRecord record;
    private boolean isHttp = true;
    private HttpResponse http = null;
    private Map<String, String> httpHeaders = null;
    private Map<String, String> mergedHeaders = null;
    private byte[] httpBody = null;

    private WarcRecord(ArchiveReader reader, ArchiveRecord record) {
        this.reader = reader;
        this.record = record;
    }

    public ArchiveRecord getRecord() {
        return record;
    }

    public ArchiveRecordHeader getHeader() {
        return record.getHeader();
    }

    private void readHttp() {
        if (isHttp && http == null) {
            HttpResponseParser parser = new HttpResponseParser();
            try {
                http = parser.parse(record);
            } catch (Exception e) {
                isHttp = false;
            }
        }
    }

    public HttpResponse getHttpResponse() {
        readHttp();
        return http;
    }

    public boolean isHttp() {
        readHttp();
        return isHttp;
    }

    public Map<String, String> getHttpHeaders() {
        if (httpHeaders != null)
            return httpHeaders;

        httpHeaders = new HashMap<>();

        readHttp();
        if (http != null) {
            for (HttpHeader header : http.getHeaders()) {
                httpHeaders.put(header.getName(), header.getValue());
            }
        }

        return httpHeaders;
    }

    public String getHttpMimeType() {
        Map<String, String> headers = getHttpHeaders();
        String type = headers.get(MIME_TYPE_HTTP_HEADER);
        if (type == null)
            return null;
        String[] split = type.split(";");
        return split.length == 0 ? "" : split[0].trim();
    }

    public byte[] getHttpBody() throws IOException {
        if (httpBody != null)
            return httpBody;

        readHttp();
        if (http != null)
            httpBody = IOUtils.toByteArray(http);
        return httpBody;
    }

    public String getHttpStringBody() throws IOException {
        Map<String, String> headers = getHttpHeaders();
        String charset = DEFAULT_HTTP_STRING_CHARSET;
        String type = headers.get(MIME_TYPE_HTTP_HEADER);
        if (type != null) {
            String[] split = type.split("[ =]");
            for (int i = 0; i < split.length - 1; i++) {
                if (split[i].equalsIgnoreCase("charset")) {
                    charset = split[i + 1];
                    break;
                }
            }
        }
        return EntityUtils.toString(new ByteArrayEntity(getHttpBody()), charset);
    }

    public void close() {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}