⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 experimentalwarcwriterprocessor.java

📁 Heritrix是一个开源,可扩展的web爬虫项目。Heritrix设计成严格按照robots.txt文件的排除指示和META robots标签。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* $Id: ExperimentalWARCWriterProcessor.java 4935 2007-02-23 00:27:24Z gojomo $ * * Created on August 1st, 2006. * * Copyright (C) 2006 Internet Archive. * * This file is part of the Heritrix web crawler (crawler.archive.org). * * Heritrix is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * any later version. * * Heritrix is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Lesser Public License for more details. * * You should have received a copy of the GNU Lesser Public License * along with Heritrix; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */package org.archive.crawler.writer;import java.io.ByteArrayInputStream;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import java.util.Collection;import java.util.HashMap;import java.util.Map;import java.util.concurrent.atomic.AtomicInteger;import java.util.logging.Level;import java.util.logging.Logger;import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HttpMethodBase;import org.apache.commons.httpclient.HttpStatus;import org.archive.crawler.datamodel.CoreAttributeConstants;import org.archive.crawler.datamodel.CrawlURI;import org.archive.crawler.datamodel.FetchStatusCodes;import org.archive.crawler.deciderules.recrawl.IdenticalDigestDecideRule;import org.archive.crawler.event.CrawlStatusListener;import org.archive.crawler.extractor.Link;import org.archive.crawler.framework.WriterPoolProcessor;import org.archive.crawler.settings.SimpleType;import org.archive.crawler.settings.Type;import org.archive.io.ReplayInputStream;import org.archive.io.WriterPoolMember;import org.archive.io.WriterPoolSettings;import org.archive.io.warc.ExperimentalWARCWriter;import org.archive.io.warc.WARCConstants;import org.archive.io.warc.WARCWriterPool;import org.archive.uid.GeneratorFactory;import org.archive.util.ArchiveUtils;import org.archive.util.anvl.ANVLRecord;/** * Experimental WARCWriterProcessor. * Goes against the pending release of 0.12 of the WARC specification, the * "Marcel Marceau" release. See <a href="https://archive-access.svn.sourceforge.net/svnroot/archive-access/branches/gjm_warc_0_12/warc/warc_file_format.html">latest revision</a> * for current state.  The 0.10 WARC implemenation has been moved to * {@link ExperimentalV10WARCWriterProcessor}. *  * <p>TODO: Remove ANVLRecord. Rename NameValue or use RFC822 * (commons-httpclient?) or find something else. *  * @author stack */public class ExperimentalWARCWriterProcessor extends WriterPoolProcessorimplements CoreAttributeConstants, CrawlStatusListener,WriterPoolSettings, FetchStatusCodes, WARCConstants {    private static final long serialVersionUID = 6182850087635847443L;    private final Logger logger = Logger.getLogger(this.getClass().getName());        /**     * Key for whether to write 'request' type records where possible     */    public static final String ATTR_WRITE_REQUESTS =        "write-requests";        /**     * Key for whether to write 'metadata' type records where possible     */    public static final String ATTR_WRITE_METADATA =        "write-metadata";        /**     * Key for whether to write 'revisit' type records when     * consecutive identical digest     */    public static final String ATTR_WRITE_REVISIT_FOR_IDENTICAL_DIGESTS =        "write-revisit-for-identical-digests";        /**     * Key for whether to write 'revisit' type records for server     * "304 not modified" responses     */    public static final String ATTR_WRITE_REVISIT_FOR_NOT_MODIFIED =        "write-revisit-for-not-modified";        /**     * Default path list.     */    private static final String [] DEFAULT_PATH = {"warcs"};    protected String [] getDefaultPath() {        return DEFAULT_PATH;    }        /**     * @param name Name of this writer.     */    public ExperimentalWARCWriterProcessor(final String name) {        super(name, "Experimental WARCWriter processor (Version 0.12)");        Type e = addElementToDefinition(                new SimpleType(ATTR_WRITE_REQUESTS,                "Whether to write 'request' type records. " +                "Default is true.", new Boolean(true)));        e.setOverrideable(true);        e.setExpertSetting(true);        e = addElementToDefinition(                new SimpleType(ATTR_WRITE_METADATA,                "Whether to write 'metadata' type records. " +                "Default is true.", new Boolean(true)));        e.setOverrideable(true);        e.setExpertSetting(true);        e = addElementToDefinition(                new SimpleType(ATTR_WRITE_REVISIT_FOR_IDENTICAL_DIGESTS,                "Whether to write 'revisit' type records when a URI's " +                "history indicates the previous fetch had an identical " +                "content digest. " +                "Default is true.", new Boolean(true)));        e.setOverrideable(true);        e.setExpertSetting(true);        e = addElementToDefinition(                new SimpleType(ATTR_WRITE_REVISIT_FOR_NOT_MODIFIED,                "Whether to write 'revisit' type records when a " +                "304-Not Modified response is received. " +                "Default is true.", new Boolean(true)));        e.setOverrideable(true);        e.setExpertSetting(true);    }    protected void setupPool(final AtomicInteger serialNo) {		setPool(new WARCWriterPool(serialNo, this, getPoolMaximumActive(),            getPoolMaximumWait()));    }        /**     * Writes a CrawlURI and its associated data to store file.     *      * Currently this method understands the following uri types: dns, http, and     * https.     *      * @param curi CrawlURI to process.     *      */    protected void innerProcess(CrawlURI curi) {        // If failure, or we haven't fetched the resource yet, return        if (curi.getFetchStatus() <= 0) {            return;        }                // If no recorded content at all, don't write record.        long recordLength = curi.getContentSize();        if (recordLength <= 0) {            // getContentSize() should be > 0 if any material (even just            // HTTP headers with zero-length body) is available.         	return;        }                String scheme = curi.getUURI().getScheme().toLowerCase();        try {            if (shouldWrite(curi)) {                write(scheme, curi);            } else {                logger.info("This writer does not write out scheme " +                        scheme + " content");            }        } catch (IOException e) {            curi.addLocalizedError(this.getName(), e, "WriteRecord: " +                curi.toString());            logger.log(Level.SEVERE, "Failed write of Record: " +                curi.toString(), e);        }    }        protected void write(final String lowerCaseScheme, final CrawlURI curi)    throws IOException {        WriterPoolMember writer = getPool().borrowFile();        long position = writer.getPosition();        // See if we need to open a new file because we've exceeed maxBytes.        // Call to checkFileSize will open new file if we're at maximum for        // current file.        writer.checkSize();        if (writer.getPosition() != position) {            // We just closed the file because it was larger than maxBytes.            // Add to the totalBytesWritten the size of the first record            // in the file, if any.            setTotalBytesWritten(getTotalBytesWritten() +            	(writer.getPosition() - position));            position = writer.getPosition();        }                ExperimentalWARCWriter w = (ExperimentalWARCWriter)writer;        try {            // Write a request, response, and metadata all in the one            // 'transaction'.            final URI baseid = getRecordID();            final String timestamp =                ArchiveUtils.getLog14Date(curi.getLong(A_FETCH_BEGAN_TIME));            if (lowerCaseScheme.startsWith("http")) {                // Add named fields for ip, checksum, and relate the metadata                // and request to the resource field.                // TODO: Use other than ANVL (or rename ANVL as NameValue or                // use RFC822 (commons-httpclient?).                ANVLRecord headers = new ANVLRecord(5);                if (curi.getContentDigest() != null) {                    headers.addLabelValue(HEADER_KEY_CHECKSUM,                        curi.getContentDigestSchemeString());                }                headers.addLabelValue(HEADER_KEY_IP, getHostAddress(curi));                URI rid;                                if (IdenticalDigestDecideRule.hasIdenticalDigest(curi) &&                         ((Boolean)getUncheckedAttribute(curi,                                 ATTR_WRITE_REVISIT_FOR_IDENTICAL_DIGESTS))) {                    rid = writeRevisitDigest(w, timestamp, HTTP_RESPONSE_MIMETYPE,                            baseid, curi, headers);                } else if (curi.getFetchStatus() == HttpStatus.SC_NOT_MODIFIED &&                         ((Boolean)getUncheckedAttribute(curi,                                 ATTR_WRITE_REVISIT_FOR_NOT_MODIFIED))) {                    rid = writeRevisitNotModified(w, timestamp,                            baseid, curi, headers);                } else {                    if (curi.isTruncatedFetch()) {                        String value = curi.isTimeTruncatedFetch()?                            NAMED_FIELD_TRUNCATED_VALUE_TIME:                            curi.isLengthTruncatedFetch()?

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -