Example usage for org.apache.commons.httpclient HttpMethodBase getResponseHeader

List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseHeader

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase getResponseHeader.

Prototype

@Override
public Header getResponseHeader(String headerName) 

Source Link

Document

Gets the response header associated with the given name.

Usage

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;//w ww.ja  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.deegree.portal.owswatch.validator.AbstractValidator.java

/**
 * Validates the HttpMethodBase and checks if the execution was successful or not
 *
 * @param method//from ww w.ja v  a2 s  .com
 *            the httpmethod after executing it
 * @return an instance of ValidatorResponse with the necessary information after validation
 */
protected ValidatorResponse validateXml(HttpMethodBase method) {

    String lastMessage = null;
    Status status = null;

    String contentType = method.getResponseHeader("Content-Type").getValue();
    if (!contentType.contains("xml")) {
        status = Status.RESULT_STATE_UNEXPECTED_CONTENT;
        lastMessage = StringTools.concat(100, "Error: Response Content is ", contentType, " not xml");
        return new ValidatorResponse(lastMessage, status);
    }

    String xml = null;
    try {
        InputStream stream = copyStream(method.getResponseBodyAsStream());
        stream.reset();
        xml = parseStream(stream);
    } catch (IOException e) {
        status = Status.RESULT_STATE_BAD_RESPONSE;
        lastMessage = status.getStatusMessage();
        return new ValidatorResponse(lastMessage, status);
    }

    if (xml.length() == 0) {
        status = Status.RESULT_STATE_BAD_RESPONSE;
        lastMessage = "Error: XML Response is empty";
        return new ValidatorResponse(lastMessage, status);
    }
    if (xml.contains("ServiceException")) {
        return validateXmlServiceException(method);
    }
    // If its an xml, and there's no service exception, then don't really parse the xml,
    // we assume that its well formed, since there might be huge xmls, which would take time to be parsed
    status = Status.RESULT_STATE_AVAILABLE;
    lastMessage = status.getStatusMessage();
    return new ValidatorResponse(lastMessage, status);
}

From source file:org.deegree.portal.owswatch.validator.CSWGetRecordsValidator.java

@Override
public ValidatorResponse validateAnswer(HttpMethodBase method, int statusCode) {

    String contentType = method.getResponseHeader("Content-Type").getValue();
    String lastMessage = null;/*from w ww  . j a v a2s . co  m*/
    Status status = null;

    if (!contentType.contains("xml")) {
        status = Status.RESULT_STATE_UNEXPECTED_CONTENT;
        lastMessage = StringTools.concat(100, "Error: Response Content is ", contentType, " not xml");
        return new ValidatorResponse(lastMessage, status);
    }

    String xml = null;
    try {
        InputStream stream = method.getResponseBodyAsStream();
        stream.reset();
        xml = parseStream(stream);
    } catch (IOException e) {
        status = Status.RESULT_STATE_BAD_RESPONSE;
        lastMessage = status.getStatusMessage();
        return new ValidatorResponse(lastMessage, status);
    }

    if (xml.length() == 0) {
        status = Status.RESULT_STATE_BAD_RESPONSE;
        lastMessage = "Error: XML Response is empty";
        return new ValidatorResponse(lastMessage, status);
    }

    if (xml.contains("ExceptionReport")) {
        validateXmlServiceException(method);
        return new ValidatorResponse(lastMessage, status);
    }
    // If its an xml, and there's no service exception, then don't really parse the xml,
    // we assume that its well formed, since there might be huge xmls, which would take time to be parsed
    status = Status.RESULT_STATE_AVAILABLE;
    lastMessage = status.getStatusMessage();
    return new ValidatorResponse(lastMessage, status);
}

From source file:org.deegree.portal.owswatch.validator.WCSGetCoverageValidator.java

@Override
public ValidatorResponse validateAnswer(HttpMethodBase method, int statusCode) {

    String lastMessage = null;/*ww  w .  ja va 2s.  com*/
    Status status = null;
    String contentType = method.getResponseHeader("Content-Type").getValue();
    if (contentType.contains("image")) {
        try {
            InputStream stream = copyStream(method.getResponseBodyAsStream());
            stream.reset();
            ImageIO.read(stream);
            status = Status.RESULT_STATE_AVAILABLE;
            lastMessage = status.getStatusMessage();
            return new ValidatorResponse(lastMessage, status);
        } catch (Exception e) {
            status = Status.RESULT_STATE_SERVICE_UNAVAILABLE;
            lastMessage = e.getLocalizedMessage();
            return new ValidatorResponse(lastMessage, status);
        }
    } else if (contentType.contains("xml")) {
        return validateXml(method);
    } else {
        StringBuilder builder = new StringBuilder("Response content is: ");
        builder.append(contentType);
        status = Status.RESULT_STATE_SERVICE_UNAVAILABLE;
        lastMessage = Messages.getMessage("ERROR_INCORRECT_RESP_CONTENT2", contentType, "image", "xml");
        return new ValidatorResponse(lastMessage, status);
    }
}

From source file:org.deegree.portal.owswatch.validator.WMSGetMapValidator.java

@Override
public ValidatorResponse validateAnswer(HttpMethodBase method, int statusCode) {

    String contentType = method.getResponseHeader("Content-Type").getValue();
    String lastMessage = null;//from   ww  w.  j  a v a 2 s  .  com
    Status status = null;

    if (!contentType.contains("image")) {
        if (!contentType.contains("xml")) {
            status = Status.RESULT_STATE_UNEXPECTED_CONTENT;
            lastMessage = StringTools.concat(100, "Error: Response Content is ", contentType, " not image");
            return new ValidatorResponse(lastMessage, status);
        } else {
            return validateXmlServiceException(method);
        }
    }

    try {
        InputStream stream = copyStream(method.getResponseBodyAsStream());
        stream.reset();
        ImageIO.read(stream);
        status = Status.RESULT_STATE_AVAILABLE;
        lastMessage = status.getStatusMessage();
        return new ValidatorResponse(lastMessage, status);
    } catch (Exception e) {
        status = Status.RESULT_STATE_SERVICE_UNAVAILABLE;
        lastMessage = e.getLocalizedMessage();
        return new ValidatorResponse(lastMessage, status);
    }
}

From source file:org.deri.pipes.utils.HttpResponseCache.java

/**
 * @param data/*  ww w . ja  va 2 s . c  om*/
 * @param headMethod
 */
private static void setExpires(HttpResponseData data, HttpMethodBase method) {
    long expires = System.currentTimeMillis() + MINIMUM_CACHE_TIME_MILLIS;
    Header expiresHeader = method.getResponseHeader(EXPIRES_HEADER);
    if (expiresHeader != null) {
        try {
            Date expiresDate = DateUtil.parseDate(expiresHeader.getValue());
            if (expiresDate.getTime() > expires) {
                logger.info("Setting cache time according to expiresHeader=[" + expiresHeader.getValue() + "]");
                expires = expiresDate.getTime();
            } else {
                logger.debug("Ignoring expires header [" + expiresHeader.getValue() + "]");
            }
        } catch (Exception e) {
            logger.debug("Problem parsing expires header [" + expiresHeader.getValue() + "]");
        }
    }
    data.setExpires(expires);

}

From source file:org.deri.pipes.utils.HttpResponseCache.java

private static HttpResponseData getDataFromRequest(HttpClient client, String location,
        Map<String, String> requestHeaders) throws IOException, HttpException {
    HttpMethodBase method = new GetMethod(location);
    method.setFollowRedirects(true);//from  w w  w  .  j  a v a  2  s.c o  m
    try {
        if (location.length() > 2000 && location.indexOf('?') >= 0) {
            logger.info("Using post method because request location is very long");
            PostMethod postMethod = new PostMethod(location.substring(0, location.indexOf('?')));
            String urlDecoded = URLDecoder.decode(location.substring(location.indexOf('?') + 1), "UTF-8");
            String[] parts = urlDecoded.split("\\&");
            for (String part : parts) {
                String[] keyval = part.split("=", 2);
                if (keyval.length == 2) {
                    postMethod.addParameter(keyval[0], keyval[1]);
                } else {
                    postMethod.addParameter(keyval[0], "");
                }
            }
            method = postMethod;
        }
        addRequestHeaders(method, requestHeaders);
        int response = client.executeMethod(method);
        HttpResponseData data = new HttpResponseData();
        setExpires(data, method);
        data.setResponse(response);
        data.setCharSet(method.getResponseCharSet());
        Header lastModifiedHeader = method.getResponseHeader(HEADER_LAST_MODIFIED);
        if (lastModifiedHeader != null) {
            data.setLastModified(lastModifiedHeader.getValue());
        }
        Header contentTypeHeader = method.getResponseHeader(HEADER_CONTENT_TYPE);
        if (contentTypeHeader != null) {
            data.setContentType(contentTypeHeader.getValue());
        }
        data.setBody(method.getResponseBody(MAX_CONTENT_SIZE));

        return data;
    } finally {
        method.releaseConnection();
    }
}

From source file:org.eclipse.mylyn.internal.bugzilla.core.BugzillaClient.java

private boolean isZippedReply(HttpMethodBase method) {
    // content-encoding:gzip can be set by a dedicated perl script or mod_gzip
    boolean zipped = (null != method.getResponseHeader("Content-encoding") && method.getResponseHeader( //$NON-NLS-1$
            "Content-encoding").getValue().equals(IBugzillaConstants.CONTENT_ENCODING_GZIP)) //$NON-NLS-1$
            ||/*from  ww w  .ja v a 2s  . c om*/
            // content-type: application/x-gzip can be set by any apache after 302 redirect, based on .gz suffix
            (null != method.getResponseHeader("Content-Type") && method.getResponseHeader("Content-Type") //$NON-NLS-1$ //$NON-NLS-2$
                    .getValue().equals("application/x-gzip")); //$NON-NLS-1$
    return zipped;
}

From source file:org.eclipse.mylyn.internal.bugzilla.core.BugzillaClient.java

public boolean getSearchHits(IRepositoryQuery query, TaskDataCollector collector, TaskAttributeMapper mapper,
        IProgressMonitor monitor) throws IOException, CoreException {
    HttpMethodBase postMethod = null;

    try {//from ww w  . ja v  a 2  s.co m
        authenticate(new SubProgressMonitor(monitor, 1));
        String queryUrl = query.getUrl();
        int start = queryUrl.indexOf('?');

        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        if (start != -1) {
            queryUrl = queryUrl.substring(start + 1);
            String[] result = queryUrl.split("&"); //$NON-NLS-1$
            if (result.length > 0) {
                for (String string : result) {
                    String[] nameValue = string.split("="); //$NON-NLS-1$
                    if (nameValue.length == 1) {
                        pairs.add(new NameValuePair(nameValue[0].trim(), "")); //$NON-NLS-1$
                    } else if (nameValue.length == 2 && nameValue[0] != null && nameValue[1] != null) {

                        //Hack around bugzilla's change of attribute name for comment search field bug#289155
                        if (nameValue[0].startsWith("long_desc")) { //$NON-NLS-1$
                            pairs.add(new NameValuePair(nameValue[0].replace("long_desc", "longdesc"), //$NON-NLS-1$ //$NON-NLS-2$
                                    URLDecoder.decode(nameValue[1].trim(), getCharacterEncoding())));
                        }

                        pairs.add(new NameValuePair(nameValue[0].trim(),
                                URLDecoder.decode(nameValue[1].trim(), getCharacterEncoding())));
                    }
                }
            }
        }

        NameValuePair ctypePair = new NameValuePair("ctype", "rdf"); //$NON-NLS-1$ //$NON-NLS-2$
        // Test that we don't specify content type twice.
        if (!pairs.contains(ctypePair)) {
            pairs.add(ctypePair);
        }

        try {
            postMethod = postFormData(IBugzillaConstants.URL_BUGLIST,
                    pairs.toArray(new NameValuePair[pairs.size()]), monitor);
        } catch (RedirectException r) {
            // Handle one redirect (Bugzilla 3.4 provides a redirect upon query submission via post)
            postMethod = getConnectGzip(r.getMessage(), monitor, null);
        }

        if (postMethod != null && postMethod.getResponseHeader("Content-Type") != null) { //$NON-NLS-1$
            Header responseTypeHeader = postMethod.getResponseHeader("Content-Type"); //$NON-NLS-1$
            for (String type : VALID_CONFIG_CONTENT_TYPES) {
                if (responseTypeHeader.getValue().toLowerCase(Locale.ENGLISH).contains(type)) {
                    InputStream stream = getResponseStream(postMethod, monitor);
                    try {
                        RepositoryQueryResultsFactory queryFactory = getQueryResultsFactory(stream);
                        int count = queryFactory.performQuery(repositoryUrl.toString(), collector, mapper,
                                TaskDataCollector.MAX_HITS);
                        return count > 0;
                    } finally {
                        stream.close();
                    }
                }
            }
        }
        // because html is not a valid config content type it is save to get the response here
        throw new CoreException(parseHtmlError(getResponseStream(postMethod, monitor)));
    } finally {
        if (postMethod != null) {
            WebUtil.releaseConnection(postMethod, monitor);
        }
    }
}

From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.java

private int authenticateForm(AuthenticationCredentials credentials, IProgressMonitor monitor)
        throws IOException, GerritException {
    // try standard basic/digest/ntlm authentication first
    String repositoryUrl = getUrl();
    AuthScope authScope = new AuthScope(WebUtil.getHost(repositoryUrl), WebUtil.getPort(repositoryUrl), null,
            AuthScope.ANY_SCHEME);/*from   w ww  .ja v a2  s  . c o m*/
    Credentials httpCredentials = WebUtil.getHttpClientCredentials(credentials, WebUtil.getHost(repositoryUrl));
    httpClient.getState().setCredentials(authScope, httpCredentials);

    HttpMethodBase[] methods = getFormAuthMethods(repositoryUrl, credentials);
    for (HttpMethodBase method : methods) {
        int code;
        try {
            code = WebUtil.execute(httpClient, hostConfiguration, method, monitor);
            if (code == HttpStatus.SC_METHOD_NOT_ALLOWED) {
                continue; // try next http method
            } else if (needsReauthentication(code, monitor)) {
                return -1;
            } else if (code == HttpStatus.SC_MOVED_TEMPORARILY) {
                Header locationHeader = method.getResponseHeader("Location"); //$NON-NLS-1$
                if (locationHeader != null) {
                    if (locationHeader.getValue()
                            .endsWith("SignInFailure,SIGN_IN,Session cookie not available.")) { //$NON-NLS-1$
                        // try different authentication method
                        return HttpStatus.SC_NOT_FOUND;
                    }
                }
            } else if (code == HttpStatus.SC_OK) {
                // try different authentication method as the server maybe using development mode authentication
                return HttpStatus.SC_NOT_FOUND;
            } else if (code != HttpStatus.SC_NOT_FOUND) {
                throw new GerritHttpException(code);
            }
            return code;
        } finally {
            WebUtil.releaseConnection(method, monitor);
        }
    }
    return HttpStatus.SC_NOT_FOUND;
}