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

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

Introduction

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

Prototype

public abstract Header getResponseHeader(String paramString);

Source Link

Usage

From source file:com.taobao.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * RP_NO_CHANGE<br>//from  ww  w.j av  a  2  s  .  c  o  m
 * 1.MD5MD5<br>
 * 2.MD5NULL<br>
 */
private String getNotModified(String dataId, CacheData cacheData, HttpMethod httpMethod) {
    Header md5Header = httpMethod.getResponseHeader(Constants.CONTENT_MD5);
    if (null == md5Header) {
        throw new RuntimeException("RP_NO_CHANGEMD5");
    }
    String md5 = md5Header.getValue();
    if (!cacheData.getMd5().equals(md5)) {
        String lastMd5 = cacheData.getMd5();
        cacheData.setMd5(Constants.NULL);
        cacheData.setLastModifiedHeader(Constants.NULL);
        throw new RuntimeException("MD5,DataID:[" + dataId + "]MD5:[" + lastMd5
                + "]MD5:[" + md5 + "]");
    }

    cacheData.setMd5(md5);
    changeSpacingInterval(httpMethod);
    if (log.isInfoEnabled()) {
        log.info("DataId: " + dataId + ", configInfo");
    }
    return null;
}

From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * RP_NO_CHANGE?<br>/*from  w  w  w  .  jav  a 2 s  . c o  m*/
 * 1.MD5?MD5?????<br>
 * 2.MD5?NULL<br>
 */
private String getNotModified(String dataId, CacheData cacheData, HttpMethod httpMethod) {
    Header md5Header = httpMethod.getResponseHeader(Constants.CONTENT_MD5);
    if (null == md5Header) {
        throw new RuntimeException("RP_NO_CHANGEMD5?");
    }
    String md5 = md5Header.getValue();
    if (!cacheData.getMd5().equals(md5)) {
        String lastMd5 = cacheData.getMd5();
        cacheData.setMd5(Constants.NULL);
        cacheData.setLastModifiedHeader(Constants.NULL);
        throw new RuntimeException("MD5?,DataID:[" + dataId + "]MD5:[" + lastMd5
                + "]MD5:[" + md5 + "]");
    }

    cacheData.setMd5(md5);
    changeSpacingInterval(httpMethod);
    if (log.isInfoEnabled()) {
        log.info("DataId: " + dataId + ", configInfo?");
    }
    return null;
}

From source file:is.hi.bok.deduplicator.DeDuplicator.java

protected void doTimestampAnalysis(CrawlURI curi, Document urlHit, Statistics currHostStats,
        boolean isDuplicate) {

    HttpMethod method = (HttpMethod) curi.getObject(CoreAttributeConstants.A_HTTP_TRANSACTION);

    // Compare datestamps (last-modified versus the indexed date)
    Date lastModified = null;//w  w  w. j a va  2s. c  o  m
    if (method.getResponseHeader("last-modified") != null) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
        try {
            lastModified = sdf.parse(method.getResponseHeader("last-modified").getValue());
        } catch (ParseException e) {
            logger.log(Level.INFO, "Exception parsing last modified of " + curi.toString(), e);
            return;
        }
    } else {
        stats.timestampMissing++;
        if (statsPerHost) {
            currHostStats.timestampMissing++;
            logger.finest("Missing timestamp on " + curi.toString());
        }
        return;
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    Date lastFetch = null;
    try {
        lastFetch = sdf.parse(urlHit.get(DigestIndexer.FIELD_TIMESTAMP));
    } catch (ParseException e) {
        logger.log(Level.WARNING, "Exception parsing indexed date for " + urlHit.get(DigestIndexer.FIELD_URL),
                e);
        return;
    }

    if (lastModified.after(lastFetch)) {
        // Header predicts change
        if (isDuplicate) {
            // But the DeDuplicator did not notice a change.
            stats.timestampChangeFalse++;
            if (statsPerHost) {
                currHostStats.timestampChangeFalse++;
            }
            logger.finest("Last-modified falsly predicts change on " + curi.toString());
        } else {
            stats.timestampChangeCorrect++;
            if (statsPerHost) {
                currHostStats.timestampChangeCorrect++;
            }
            logger.finest("Last-modified correctly predicts change on " + curi.toString());
        }
    } else {
        // Header does not predict change.
        if (isDuplicate) {
            // And the DeDuplicator verifies that no change had occurred
            stats.timestampNoChangeCorrect++;
            if (statsPerHost) {
                currHostStats.timestampNoChangeCorrect++;
            }
            logger.finest("Last-modified correctly predicts no-change on " + curi.toString());
        } else {
            // As this is particularly bad we'll log the URL at INFO level
            logger.log(Level.INFO,
                    "Last-modified incorrectly indicated " + "no-change on " + curi.toString() + " "
                            + curi.getContentType() + ". last-modified: " + lastModified + ". Last fetched: "
                            + lastFetch);
            stats.timestampNoChangeFalse++;
            if (statsPerHost) {
                currHostStats.timestampNoChangeFalse++;
            }
        }
    }

}

From source file:com.linkedin.pinot.common.utils.webhdfs.WebHdfsV1Client.java

public synchronized boolean uploadSegment(String webHdfsPath, String localFilePath) {
    // Step 1: Submit a HTTP PUT request without automatically following
    // redirects and without sending the file data.
    String firstPutReqString = String.format(WEB_HDFS_UPLOAD_PATH_TEMPLATE, _protocol, _host, _port,
            webHdfsPath, _overwrite, _permission);
    HttpMethod firstPutReq = new PutMethod(firstPutReqString);
    try {/*from w ww .java2s .c  om*/
        LOGGER.info("Trying to send request: {}.", firstPutReqString);
        int firstResponseCode = _httpClient.executeMethod(firstPutReq);
        if (firstResponseCode != 307) {
            LOGGER.error(String.format(
                    "Failed to execute the first PUT request to upload segment to webhdfs: %s. "
                            + "Expected response code 307, but get %s. Response body: %s",
                    firstPutReqString, firstResponseCode, firstPutReq.getResponseBodyAsString()));
            return false;
        }
    } catch (Exception e) {
        LOGGER.error(String.format("Failed to execute the first request to upload segment to webhdfs: %s.",
                firstPutReqString), e);
        return false;
    } finally {
        firstPutReq.releaseConnection();
    }
    // Step 2: Submit another HTTP PUT request using the URL in the Location
    // header with the file data to be written.
    String redirectedReqString = firstPutReq.getResponseHeader(LOCATION).getValue();
    PutMethod redirectedReq = new PutMethod(redirectedReqString);
    File localFile = new File(localFilePath);
    RequestEntity requestEntity = new FileRequestEntity(localFile, "application/binary");
    redirectedReq.setRequestEntity(requestEntity);

    try {
        LOGGER.info("Trying to send request: {}.", redirectedReqString);
        int redirectedResponseCode = _httpClient.executeMethod(redirectedReq);
        if (redirectedResponseCode != 201) {
            LOGGER.error(String.format(
                    "Failed to execute the redirected PUT request to upload segment to webhdfs: %s. "
                            + "Expected response code 201, but get %s. Response: %s",
                    redirectedReqString, redirectedResponseCode, redirectedReq.getResponseBodyAsString()));
        }
        return true;
    } catch (IOException e) {
        LOGGER.error(String.format("Failed to execute the redirected request to upload segment to webhdfs: %s.",
                redirectedReqString), e);
        return false;
    } finally {
        redirectedReq.releaseConnection();
    }
}

From source file:eu.alefzero.webdav.WebdavClient.java

@Override
public int executeMethod(HttpMethod method) throws IOException, HttpException {
    boolean customRedirectionNeeded = false;
    try {/*from  www .java 2s  . c o m*/
        method.setFollowRedirects(mFollowRedirects);
    } catch (Exception e) {
        if (mFollowRedirects)
            Log_OC.d(TAG, "setFollowRedirects failed for " + method.getName()
                    + " method, custom redirection will be used");
        customRedirectionNeeded = mFollowRedirects;
    }
    if (mSsoSessionCookie != null && mSsoSessionCookie.length() > 0) {
        method.setRequestHeader("Cookie", mSsoSessionCookie);
    }
    int status = super.executeMethod(method);
    int redirectionsCount = 0;
    while (customRedirectionNeeded && redirectionsCount < MAX_REDIRECTIONS_COUNT
            && (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY
                    || status == HttpStatus.SC_TEMPORARY_REDIRECT)) {

        Header location = method.getResponseHeader("Location");
        if (location != null) {
            Log_OC.d(TAG, "Location to redirect: " + location.getValue());
            method.setURI(new URI(location.getValue(), true));
            status = super.executeMethod(method);
            redirectionsCount++;

        } else {
            Log_OC.d(TAG, "No location to redirect!");
            status = HttpStatus.SC_NOT_FOUND;
        }
    }

    return status;
}

From source file:com.taobao.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * RP_OK<br>//w  w w  .  j  ava  2 s .  c  om
 * 1.<br>
 * 2.MD5<br>
 * 3.LastModifiedMD5<br>
 */
private String getSuccess(String dataId, String group, CacheData cacheData, HttpMethod httpMethod) {
    String configInfo = Constants.NULL;
    configInfo = getContent(httpMethod);
    if (null == configInfo) {
        throw new RuntimeException("RP_OK");
    }

    Header md5Header = httpMethod.getResponseHeader(Constants.CONTENT_MD5);
    if (null == md5Header) {
        throw new RuntimeException("RP_OKMD5, " + configInfo);
    }
    String md5 = md5Header.getValue();
    if (!checkContent(configInfo, md5)) {
        throw new RuntimeException("MD5,DataID:[" + dataId + "]:[" + configInfo
                + "]MD5:[" + md5 + "]");
    }

    Header lastModifiedHeader = httpMethod.getResponseHeader(Constants.LAST_MODIFIED);
    if (null == lastModifiedHeader) {
        throw new RuntimeException("RP_OKlastModifiedHeader");
    }
    String lastModified = lastModifiedHeader.getValue();

    cacheData.setMd5(md5);
    cacheData.setLastModifiedHeader(lastModified);

    changeSpacingInterval(httpMethod);

    // cache
    String key = makeCacheKey(dataId, group);
    contentCache.put(key, configInfo);

    // 
    StringBuilder buf = new StringBuilder();
    buf.append("dataId=").append(dataId);
    buf.append(" ,group=").append(group);
    buf.append(" ,content=").append(configInfo);
    dataLog.info(buf.toString());

    return configInfo;
}

From source file:cn.leancloud.diamond.client.impl.DefaultDiamondSubscriber.java

/**
 * RP_OK?<br>/*from w  w w . j  a  v  a 2 s .c  o  m*/
 * 1.??????<br>
 * 2.????MD5???????<br>
 * 3.?LastModified?MD5???????<br>
 */
private String getSuccess(String dataId, String group, CacheData cacheData, HttpMethod httpMethod) {
    String configInfo = Constants.NULL;
    configInfo = getContent(httpMethod);
    if (null == configInfo) {
        throw new RuntimeException("RP_OK???");
    }

    Header md5Header = httpMethod.getResponseHeader(Constants.CONTENT_MD5);
    if (null == md5Header) {
        throw new RuntimeException("RP_OKMD5?, " + configInfo);
    }
    String md5 = md5Header.getValue();
    if (!checkContent(configInfo, md5)) {
        throw new RuntimeException("??MD5?,DataID:[" + dataId
                + "]??:[" + configInfo + "]MD5:[" + md5 + "]");
    }

    Header lastModifiedHeader = httpMethod.getResponseHeader(Constants.LAST_MODIFIED);
    if (null == lastModifiedHeader) {
        throw new RuntimeException("RP_OKlastModifiedHeader");
    }
    String lastModified = lastModifiedHeader.getValue();

    cacheData.setMd5(md5);
    cacheData.setLastModifiedHeader(lastModified);

    changeSpacingInterval(httpMethod);

    // cache
    String key = makeCacheKey(dataId, group);
    contentCache.put(key, configInfo);

    // ?
    StringBuilder buf = new StringBuilder();
    buf.append("dataId=").append(dataId);
    buf.append(" ,group=").append(group);
    buf.append(" ,content=").append(configInfo);
    dataLog.info(buf.toString());

    return configInfo;
}

From source file:com.foglyn.fogbugz.Request.java

private <T> T request(String url, HttpMethod method, IProgressMonitor monitor, ResponseProcessor<T> processor)
        throws FogBugzException {
    Utils.checkCancellation(monitor);//w ww.ja  v a2  s .  c o  m

    HostConfiguration hostConfiguration = WebUtil.createHostConfiguration(httpClient, repositoryLocation,
            monitor);

    if (allowGzip) {
        method.addRequestHeader("Accept-Encoding", "gzip");
    }

    InputStream responseStream = null;
    CancellableInputStream cancellableStream = null;
    try {
        log.debug("Sending request to server");
        int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor);

        log.debug("Got " + code + " response");

        if (!processor.checkHttpStatus(code)) {
            Map<String, String> headers = Utils.convertHeadersToMap(method);

            method.abort();

            throw unexpectedStatus(code, url, headers);
        }

        log.debug("Downloading data");

        responseStream = method.getResponseBodyAsStream();

        InputStream processed = responseStream;

        // may be null, for example for HEAD request
        if (processed != null) {
            Header contentEncoding = method.getResponseHeader("Content-Encoding");
            if (allowGzip && contentEncoding != null && "gzip".equals(contentEncoding.getValue())) {
                processed = new GZIPInputStream(processed);
            }

            cancellableStream = new CancellableInputStream(processed, monitor, threadFactory);
            processed = cancellableStream;
        }

        log.debug("Processing response");

        return processor.processResponse(url, method, processed);
    } catch (RuntimeException e) {
        // also catches OperationCanceledException

        // we don't know what happened to method, so we better abort processing
        method.abort();

        log.error("Error while executing request", e);

        throw e;
    } catch (IOException e) {
        // we don't know what happened... better abort connection
        method.abort();

        log.error("IO Error while executing request", e);

        throw ioError(e, url);
    } finally {
        if (cancellableStream != null) {
            cancellableStream.shutdownBackgroundThread();
        }

        // don't use cancellable stream to close responseStream -- because in case of cancelled monitor, it would ignore close request 
        Utils.close(responseStream);

        method.releaseConnection();
    }
}

From source file:com.jackbe.mapreduce.EMMLRestRunner.java

protected String executeRESTCall(String scriptName, String encodedValue) {

    // "http://localhost:8080/presto/edge/api/rest/StockQuoteMapper/runMashup?x-presto-resultFormat=xml&value=<encodedValue>"
    HttpMethod httpMethod = null;
    String result = null;//  ww  w  . j  av a  2 s .  c  o  m

    if (encodedValue != null) {
        httpMethod = new GetMethod(protocol + host + ":" + port + path + scriptName + "/" + operation + "?"
                + format + "&value=" + encodedValue);
        log.debug("Invoking REST service: " + protocol + host + ":" + port + path + scriptName + "/" + operation
                + "?" + format + "&value=" + encodedValue);
    } else {
        httpMethod = new GetMethod(
                protocol + host + ":" + port + path + scriptName + "/" + operation + "?" + format);
        log.debug("Invoking REST service: " + protocol + host + ":" + port + path + scriptName + "/" + operation
                + "?" + format);
    }

    try {
        client.executeMethod(httpMethod);

        if (httpMethod.getStatusCode() != HttpStatus.SC_OK) {
            log.error("HTTP Error status connecting to Presto: " + httpMethod.getStatusCode());
            log.error("HTTP Error message connecting to Presto: " + httpMethod.getStatusText());
            return null;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Status code: " + httpMethod.getStatusCode());
                Header contentTypeHeader = httpMethod.getResponseHeader("content-type");
                log.debug("Mimetype: " + contentTypeHeader.getValue());
            }
        }

        result = httpMethod.getResponseBodyAsString();
        // log.debug(httpMethod.getStatusText());
        if (log.isDebugEnabled())
            log.debug("Response: " + result);
    } catch (Exception e) {
        log.error("Exception executing REST call: " + e, e);
    } finally {
        httpMethod.releaseConnection();
    }

    return result;

}

From source file:com.groupon.odo.Proxy.java

/**
 * Execute a redirected request/*from w ww. ja  v a 2 s .com*/
 *
 * @param stringStatusCode
 * @param httpMethodProxyRequest
 * @param httpServletRequest
 * @param httpServletResponse
 * @throws Exception
 */
private void processRedirect(String stringStatusCode, HttpMethod httpMethodProxyRequest,
        HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
    // Check if the proxy response is a redirect
    // The following code is adapted from
    // org.tigris.noodle.filters.CheckForRedirect
    // Hooray for open source software

    String stringLocation = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue();
    if (stringLocation == null) {
        throw new ServletException("Received status code: " + stringStatusCode + " but no "
                + STRING_LOCATION_HEADER + " header was found in the response");
    }
    // Modify the redirect to go to this proxy servlet rather than the proxied host
    String stringMyHostName = httpServletRequest.getServerName();
    if (httpServletRequest.getServerPort() != 80) {
        stringMyHostName += ":" + httpServletRequest.getServerPort();
    }
    stringMyHostName += httpServletRequest.getContextPath();
    httpServletResponse.sendRedirect(
            stringLocation.replace(getProxyHostAndPort() + this.getProxyPath(), stringMyHostName));
}