Example usage for org.apache.commons.httpclient Header toString

List of usage examples for org.apache.commons.httpclient Header toString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient Header toString.

Prototype

public String toString() 

Source Link

Usage

From source file:org.apache.hadoop.fs.swift.TestSwiftFileSystemPartitionedUploads.java

/**
 * tests functionality for big files ( > 5Gb) upload
 *///w  w  w . j a v a2s  . c  o m
@Test(timeout = SWIFT_BULK_IO_TEST_TIMEOUT)
public void testFilePartUpload() throws Throwable {

    final Path path = new Path("/test/testFilePartUpload");

    int len = 8192;
    final byte[] src = SwiftTestUtils.dataset(len, 32, 144);
    FSDataOutputStream out = fs.create(path, false, getBufferSize(), (short) 1, BLOCK_SIZE);

    try {
        int totalPartitionsToWrite = len / PART_SIZE_BYTES;
        assertPartitionsWritten("Startup", out, 0);
        //write 2048
        int firstWriteLen = 2048;
        out.write(src, 0, firstWriteLen);
        //assert
        long expected = getExpectedPartitionsWritten(firstWriteLen, PART_SIZE_BYTES, false);
        SwiftUtils.debug(LOG, "First write: predict %d partitions written", expected);
        assertPartitionsWritten("First write completed", out, expected);
        //write the rest
        int remainder = len - firstWriteLen;
        SwiftUtils.debug(LOG, "remainder: writing: %d bytes", remainder);

        out.write(src, firstWriteLen, remainder);
        expected = getExpectedPartitionsWritten(len, PART_SIZE_BYTES, false);
        assertPartitionsWritten("Remaining data", out, expected);
        out.close();
        expected = getExpectedPartitionsWritten(len, PART_SIZE_BYTES, true);
        assertPartitionsWritten("Stream closed", out, expected);

        Header[] headers = fs.getStore().getObjectHeaders(path, true);
        for (Header header : headers) {
            LOG.info(header.toString());
        }

        byte[] dest = readDataset(fs, path, len);
        LOG.info("Read dataset from " + path + ": data length =" + len);
        //compare data
        SwiftTestUtils.compareByteArrays(src, dest, len);
        FileStatus status;

        final Path qualifiedPath = path.makeQualified(fs);
        status = fs.getFileStatus(qualifiedPath);
        //now see what block location info comes back.
        //This will vary depending on the Swift version, so the results
        //aren't checked -merely that the test actually worked
        BlockLocation[] locations = fs.getFileBlockLocations(status, 0, len);
        assertNotNull("Null getFileBlockLocations()", locations);
        assertTrue("empty array returned for getFileBlockLocations()", locations.length > 0);

        //last bit of test -which seems to play up on partitions, which we download
        //to a skip
        try {
            validatePathLen(path, len);
        } catch (AssertionError e) {
            //downgrade to a skip
            throw new AssumptionViolatedException(e, null);
        }

    } finally {
        IOUtils.closeStream(out);
    }
}

From source file:org.apache.hadoop.fs.swift.TestSwiftFileSystemPartitionedUploads.java

/**
 * tests functionality for big files ( > 5Gb) upload
 *///  w  w  w . j a  v a  2 s .c  om
@Test(timeout = SWIFT_BULK_IO_TEST_TIMEOUT)
public void testFilePartUploadNoLengthCheck() throws IOException, URISyntaxException {

    final Path path = new Path("/test/testFilePartUploadLengthCheck");

    int len = 8192;
    final byte[] src = SwiftTestUtils.dataset(len, 32, 144);
    FSDataOutputStream out = fs.create(path, false, getBufferSize(), (short) 1, BLOCK_SIZE);

    try {
        int totalPartitionsToWrite = len / PART_SIZE_BYTES;
        assertPartitionsWritten("Startup", out, 0);
        //write 2048
        int firstWriteLen = 2048;
        out.write(src, 0, firstWriteLen);
        //assert
        long expected = getExpectedPartitionsWritten(firstWriteLen, PART_SIZE_BYTES, false);
        SwiftUtils.debug(LOG, "First write: predict %d partitions written", expected);
        assertPartitionsWritten("First write completed", out, expected);
        //write the rest
        int remainder = len - firstWriteLen;
        SwiftUtils.debug(LOG, "remainder: writing: %d bytes", remainder);

        out.write(src, firstWriteLen, remainder);
        expected = getExpectedPartitionsWritten(len, PART_SIZE_BYTES, false);
        assertPartitionsWritten("Remaining data", out, expected);
        out.close();
        expected = getExpectedPartitionsWritten(len, PART_SIZE_BYTES, true);
        assertPartitionsWritten("Stream closed", out, expected);

        Header[] headers = fs.getStore().getObjectHeaders(path, true);
        for (Header header : headers) {
            LOG.info(header.toString());
        }

        byte[] dest = readDataset(fs, path, len);
        LOG.info("Read dataset from " + path + ": data length =" + len);
        //compare data
        SwiftTestUtils.compareByteArrays(src, dest, len);
        FileStatus status = fs.getFileStatus(path);

        //now see what block location info comes back.
        //This will vary depending on the Swift version, so the results
        //aren't checked -merely that the test actually worked
        BlockLocation[] locations = fs.getFileBlockLocations(status, 0, len);
        assertNotNull("Null getFileBlockLocations()", locations);
        assertTrue("empty array returned for getFileBlockLocations()", locations.length > 0);
    } finally {
        IOUtils.closeStream(out);
    }
}

From source file:org.apache.jmeter.protocol.http.sampler.HTTPHC3Impl.java

/**
 * Calculate response headers size/*w ww  . jav a 2  s  . co m*/
 * 
 * @return the size response headers (in bytes)
 */
private static int calculateHeadersSize(HttpMethodBase httpMethod) {
    int headerSize = httpMethod.getStatusLine().toString().length() + 2; // add a \r\n
    Header[] rh = httpMethod.getResponseHeaders();
    for (Header responseHeader : rh) {
        headerSize += responseHeader.toString().length(); // already include the \r\n
    }
    headerSize += 2; // last \r\n before response data
    return headerSize;
}

From source file:org.apache.webdav.lib.methods.XMLResponseMethodBase.java

/**
 * Return the length (in bytes) of my request body, suitable for use in a
 * <tt>Content-Length</tt> header.
 *
 * <p>//from w  w  w . ja va 2  s .c  o m
 * Return <tt>-1</tt> when the content-length is unknown.
 * </p>
 *
 * <p>
 * This implementation returns <tt>0</tt>, indicating that the request has
 * no body.
 * </p>
 *
 * @return <tt>0</tt>, indicating that the request has no body.
 */
protected int getRequestContentLength() {
    if (!isRequestContentAlreadySet()) {
        String contents = generateRequestBody();
        // be nice - allow overriding functions to return null or empty
        // strings for no content.
        if (contents == null)
            contents = "";

        setRequestBody(contents);

        if (debug > 0) {
            System.out.println("\n>>>>>>>  to  server  ---------------------------------------------------");
            System.out.println(getName() + " " + getPath()
                    + (getQueryString() != null ? "?" + getQueryString() : "") + " " + "HTTP/1.1");

            Header[] headers = getRequestHeaders();
            for (int i = 0; i < headers.length; i++) {
                Header header = headers[i];
                System.out.print(header.toString());
            }
            System.out.println("Content-Length: " + super.getRequestContentLength());

            if (this instanceof DepthSupport) {
                System.out.println("Depth: " + ((DepthSupport) this).getDepth());
            }

            System.out.println();
            xo.print(contents);
            System.out.println("------------------------------------------------------------------------");
        }

    }

    return super.getRequestContentLength();
}

From source file:org.apache.webdav.lib.methods.XMLResponseMethodBase.java

protected void parseXMLResponse(InputStream input) throws IOException, HttpException {

    if (builder == null) {
        try {/*from www. j  av  a  2  s  .  com*/
            // TODO: avoid the newInstance call for each method instance for performance reasons.
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new HttpException("XML Parser Configuration error: " + e.getMessage());
        }
    }

    try {

        // avoid ugly printlns from the default error handler.
        builder.setErrorHandler(new DummyErrorHandler());
        responseDocument = builder.parse(new InputSource(input));

        if (debug > 0) {
            System.out.println("\n<<<<<<< from server  ---------------------------------------------------");
            System.out.println(getStatusLine());

            Header[] headers = getResponseHeaders();
            for (int i = 0; i < headers.length; i++) {
                Header header = headers[i];
                System.out.print(header.toString());
            }

            System.out.println();

            xo.print(responseDocument);
            System.out.println("------------------------------------------------------------------------");
        }

    } catch (Exception e) {
        throw new IOException("XML parsing error; response stream is not valid XML: " + e.getMessage());
    }

    // init the response table to display the responses during debugging
    /*if (debug > 10) {
    //if (log.isDebugEnabled()) {
    initResponseHashtable();
    }*/

}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

protected void innerProcess(final CrawlURI curi) throws InterruptedException {
    if (!canFetch(curi)) {
        // Cannot fetch this, due to protocol, retries, or other problems
        return;/*ww w. j a v a  2 s  . co  m*/
    }

    HttpClient http = this.getClient();
    setLocalIP(http);

    this.curisHandled++;

    // Note begin time
    curi.putLong(A_FETCH_BEGAN_TIME, System.currentTimeMillis());

    // Get a reference to the HttpRecorder that is set into this ToeThread.
    HttpRecorder rec = HttpRecorder.getHttpRecorder();

    // Shall we get a digest on the content downloaded?
    boolean digestContent = ((Boolean) getUncheckedAttribute(curi, ATTR_DIGEST_CONTENT)).booleanValue();
    String algorithm = null;
    if (digestContent) {
        algorithm = ((String) getUncheckedAttribute(curi, ATTR_DIGEST_ALGORITHM));
        rec.getRecordedInput().setDigest(algorithm);
    } else {
        // clear
        rec.getRecordedInput().setDigest((MessageDigest) null);
    }

    // Below we do two inner classes that add check of midfetch
    // filters just as we're about to receive the response body.
    String curiString = curi.getUURI().toString();
    HttpMethodBase method = null;
    if (curi.isPost()) {
        method = new HttpRecorderPostMethod(curiString, rec) {
            protected void readResponseBody(HttpState state, HttpConnection conn)
                    throws IOException, HttpException {
                addResponseContent(this, curi);
                if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) {
                    doAbort(curi, this, MIDFETCH_ABORT_LOG);
                } else {
                    super.readResponseBody(state, conn);
                }
            }
        };
    } else {
        method = new HttpRecorderGetMethod(curiString, rec) {
            protected void readResponseBody(HttpState state, HttpConnection conn)
                    throws IOException, HttpException {
                addResponseContent(this, curi);
                if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) {
                    doAbort(curi, this, MIDFETCH_ABORT_LOG);
                } else {
                    super.readResponseBody(state, conn);
                }
            }
        };
    }

    HostConfiguration customConfigOrNull = configureMethod(curi, method);

    // Set httpRecorder into curi. Subsequent code both here and later
    // in extractors expects to find the HttpRecorder in the CrawlURI.
    curi.setHttpRecorder(rec);

    // Populate credentials. Set config so auth. is not automatic.
    boolean addedCredentials = populateCredentials(curi, method);
    method.setDoAuthentication(addedCredentials);

    // set hardMax on bytes (if set by operator)
    long hardMax = getMaxLength(curi);
    // set overall timeout (if set by operator)
    long timeoutMs = 1000 * getTimeout(curi);
    // Get max fetch rate (bytes/ms). It comes in in KB/sec
    long maxRateKBps = getMaxFetchRate(curi);
    rec.getRecordedInput().setLimits(hardMax, timeoutMs, maxRateKBps);

    try {
        http.executeMethod(customConfigOrNull, method);
    } catch (RecorderTooMuchHeaderException ex) {
        // when too much header material, abort like other truncations
        doAbort(curi, method, HEADER_TRUNC);
    } catch (IOException e) {
        failedExecuteCleanup(method, curi, e);
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        // For weird windows-only ArrayIndex exceptions in native
        // code... see
        // http://forum.java.sun.com/thread.jsp?forum=11&thread=378356
        // treating as if it were an IOException
        failedExecuteCleanup(method, curi, e);
        return;
    }

    // set softMax on bytes to get (if implied by content-length) 
    long softMax = method.getResponseContentLength();

    try {
        if (!curi.isSeed() && curi.getFetchStatus() == HttpStatus.SC_NOT_MODIFIED) {
            logger.debug(curi.getUURI().toString() + " is not modify");
            curi.skipToProcessorChain(getController().getPostprocessorChain());
        } else if (!method.isAborted()) {
            // Force read-to-end, so that any socket hangs occur here,
            // not in later modules.
            rec.getRecordedInput().readFullyOrUntil(softMax);
        }
    } catch (RecorderTimeoutException ex) {
        doAbort(curi, method, TIMER_TRUNC);
    } catch (RecorderLengthExceededException ex) {
        doAbort(curi, method, LENGTH_TRUNC);
    } catch (IOException e) {
        cleanup(curi, e, "readFully", S_CONNECT_LOST);
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        // For weird windows-only ArrayIndex exceptions from native code
        // see http://forum.java.sun.com/thread.jsp?forum=11&thread=378356
        // treating as if it were an IOException
        cleanup(curi, e, "readFully", S_CONNECT_LOST);
        return;
    } finally {
        // ensure recording has stopped
        rec.closeRecorders();
        logger.debug("cloase backup file.&uri= " + curi.getCrawlURIString());
        if (!method.isAborted()) {
            method.releaseConnection();
        }
        // Note completion time
        curi.putLong(A_FETCH_COMPLETED_TIME, System.currentTimeMillis());
        // Set the response charset into the HttpRecord if available.
        setCharacterEncoding(rec, method);
        setSizes(curi, rec);
    }

    if (digestContent) {
        curi.setContentDigest(algorithm, rec.getRecordedInput().getDigestValue());
    }

    logger.info((curi.isPost() ? "POST" : "GET") + " " + curi.getUURI().toString() + " "
            + method.getStatusCode() + " " + rec.getRecordedInput().getSize() + " " + curi.getContentType());

    if (curi.isSuccess() && addedCredentials) {
        // Promote the credentials from the CrawlURI to the CrawlServer
        // so they are available for all subsequent CrawlURIs on this
        // server.
        promoteCredentials(curi);
        if (logger.isDebugEnabled()) {
            // Print out the cookie.  Might help with the debugging.
            Header setCookie = method.getResponseHeader("set-cookie");
            if (setCookie != null) {
                logger.debug(setCookie.toString().trim());
            }
        }
    } else if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        // 401 is not 'success'.
        handle401(method, curi);
    }

    if (rec.getRecordedInput().isOpen()) {
        logger.error(curi.toString() + " RIS still open. Should have" + " been closed by method release: "
                + Thread.currentThread().getName());
        try {
            rec.getRecordedInput().close();
        } catch (IOException e) {
            logger.error("second-chance RIS close failed", e);
        }
    }
}

From source file:org.exoplatform.calendar.service.impl.RemoteCalendarServiceImpl.java

@Override
public boolean isValidRemoteUrl(String url, String type, String remoteUser, String remotePassword)
        throws IOException, UnsupportedOperationException {
    try {/*from   w  w w  .  j  a va 2  s.co  m*/
        HttpClient client = new HttpClient();
        HostConfiguration hostConfig = new HostConfiguration();
        String host = new URL(url).getHost();
        if (StringUtils.isEmpty(host))
            host = url;
        hostConfig.setHost(host);
        client.setHostConfiguration(hostConfig);
        Credentials credentials = null;
        client.setHostConfiguration(hostConfig);
        if (!StringUtils.isEmpty(remoteUser)) {
            credentials = new UsernamePasswordCredentials(remoteUser, remotePassword);
            client.getState().setCredentials(new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                    credentials);
        }

        if (CalendarService.ICALENDAR.equals(type)) {
            GetMethod get = new GetMethod(url);
            client.executeMethod(get);
            int statusCode = get.getStatusCode();
            get.releaseConnection();
            return (statusCode == HttpURLConnection.HTTP_OK);
        } else {
            if (CalendarService.CALDAV.equals(type)) {
                OptionsMethod options = new OptionsMethod(url);
                client.executeMethod(options);
                Header header = options.getResponseHeader("DAV");
                options.releaseConnection();
                if (header == null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Cannot connect to remoter server or not support WebDav access");
                    }
                    return false;
                }
                Boolean support = header.toString().contains("calendar-access");
                options.releaseConnection();
                if (!support) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Remote server does not support CalDav access");
                    }
                    throw new UnsupportedOperationException("Remote server does not support CalDav access");
                }
                return support;
            }
            return false;
        }
    } catch (MalformedURLException e) {
        if (logger.isDebugEnabled())
            logger.debug(e.getMessage(), e);
        throw new IOException("URL is invalid. Maybe no legal protocol or URl could not be parsed");
    } catch (IOException e) {
        if (logger.isDebugEnabled())
            logger.debug(e.getMessage(), e);
        throw new IOException("Error occurs when connecting to remote server");
    }
}

From source file:org.jabox.cis.hudson.HudsonConnector.java

/**
 * Implementation inspired by groovy code here:
 * http://wiki.hudson-ci.org/display/HUDSON/Authenticating+scripted+clients
 *///from ww  w .j a va  2  s.  co  m
public boolean addProject(final Project project, final CISConnectorConfig dc) throws IOException, SAXException {
    HudsonConnectorConfig hcc = (HudsonConnectorConfig) dc;
    String url = dc.getServer().getUrl();

    HttpClient client = new HttpClient();
    client.getState().setCredentials(null, null,
            new UsernamePasswordCredentials(hcc.getUsername(), hcc.getPassword()));

    // Hudson does not do any authentication negotiation,
    // ie. it does not return a 401 (Unauthorized)
    // but immediately a 403 (Forbidden)
    client.getState().setAuthenticationPreemptive(true);

    String uri = url + "createItem?name=" + project.getName();
    PostMethod post = new PostMethod(uri);
    post.setDoAuthentication(true);

    post.setRequestHeader("Content-type", "text/xml; charset=UTF-8");

    InputStream is = getConfigXMLStream(project);

    String body = parseInputStream(is, project);
    post.setRequestBody(body);
    try {
        int result = client.executeMethod(post);
        LOGGER.info("Return code: " + result);
        for (Header header : post.getResponseHeaders()) {
            LOGGER.info(header.toString().trim());
        }
        LOGGER.info(post.getResponseBodyAsString());
    } finally {
        post.releaseConnection();
    }

    // Trigger the Hudson build
    PostMethod triggerBuild = new PostMethod(url + "/job/" + project.getName() + "/build");
    client.executeMethod(triggerBuild);
    return true;
}

From source file:org.jabox.cis.jenkins.JenkinsConnector.java

/**
 * Implementation inspired by groovy code here:
 * http://wiki.jenkins-ci.org/display//ww  w  . ja v a  2s  .c o  m
 * /JENKINS/Authenticating+scripted+clients
 */
public boolean addProject(final Project project, final CISConnectorConfig dc) throws IOException, SAXException {
    JenkinsConnectorConfig hcc = (JenkinsConnectorConfig) dc;
    String url = dc.getServer().getUrl();

    HttpClient client = new HttpClient();
    client.getState().setCredentials(null, null,
            new UsernamePasswordCredentials(hcc.getUsername(), hcc.getPassword()));

    // Jenkins does not do any authentication negotiation,
    // ie. it does not return a 401 (Unauthorized)
    // but immediately a 403 (Forbidden)
    client.getState().setAuthenticationPreemptive(true);

    String uri = url + "createItem?name=" + project.getName();
    PostMethod post = new PostMethod(uri);
    post.setDoAuthentication(true);

    post.setRequestHeader("Content-type", "text/xml; charset=UTF-8");

    InputStream is = getConfigXMLStream(project);

    String body = parseInputStream(is, project);
    post.setRequestBody(body);
    try {
        int result = client.executeMethod(post);
        LOGGER.info("Return code: " + result);
        for (Header header : post.getResponseHeaders()) {
            LOGGER.info(header.toString().trim());
        }
        LOGGER.info(post.getResponseBodyAsString());
    } finally {
        post.releaseConnection();
    }

    // Trigger the Jenkins build
    PostMethod triggerBuild = new PostMethod(url + "/job/" + project.getName() + "/build");
    client.executeMethod(triggerBuild);
    return true;
}

From source file:org.jboss.mod_cluster.ManagerClient.java

public boolean isApacheHttpd() throws Exception {
    GetMethod gm = new GetMethod(URL);
    try {/* ww w  . j a va 2 s .c om*/
        httpResponseCode = httpClient.executeMethod(gm);
    } catch (HttpException e) {
        System.out.println("error: " + e);
        throw (e);
    }
    gm.releaseConnection();
    Header head = gm.getResponseHeader("Server");
    if (head != null)
        return head.toString().contains("Apache/2");
    return false;
}