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:org.apache.kylin.engine.mr.common.HadoopStatusGetter.java

private String getHttpResponse(String url) throws IOException {
    HttpClient client = new HttpClient();

    String response = null;//from   w  w w  .  j ava2s.c om
    while (response == null) { // follow redirects via 'refresh'
        if (url.startsWith("https://")) {
            registerEasyHttps();
        }
        if (url.contains("anonymous=true") == false) {
            url += url.contains("?") ? "&" : "?";
            url += "anonymous=true";
        }

        HttpMethod get = new GetMethod(url);
        get.addRequestHeader("accept", "application/json");

        try {
            client.executeMethod(get);

            String redirect = null;
            Header h = get.getResponseHeader("Location");
            if (h != null) {
                redirect = h.getValue();
                if (isValidURL(redirect) == false) {
                    logger.info("Get invalid redirect url, skip it: " + redirect);
                    Thread.sleep(1000L);
                    continue;
                }
            } else {
                h = get.getResponseHeader("Refresh");
                if (h != null) {
                    String s = h.getValue();
                    int cut = s.indexOf("url=");
                    if (cut >= 0) {
                        redirect = s.substring(cut + 4);

                        if (isValidURL(redirect) == false) {
                            logger.info("Get invalid redirect url, skip it: " + redirect);
                            Thread.sleep(1000L);
                            continue;
                        }
                    }
                }
            }

            if (redirect == null) {
                response = get.getResponseBodyAsString();
                logger.debug("Job " + mrJobId + " get status check result.\n");
            } else {
                url = redirect;
                logger.debug("Job " + mrJobId + " check redirect url " + url + ".\n");
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            logger.error(e.getMessage());
        } finally {
            get.releaseConnection();
        }
    }

    return response;
}

From source file:org.apache.kylin.job.tools.HadoopStatusGetter.java

private String getHttpResponse(String url) throws IOException {
    HttpClient client = new HttpClient();

    String response = null;//from www  . j  av  a 2 s. com
    while (response == null) { // follow redirects via 'refresh'
        if (url.startsWith("https://")) {
            registerEasyHttps();
        }
        if (url.contains("anonymous=true") == false) {
            url += url.contains("?") ? "&" : "?";
            url += "anonymous=true";
        }

        HttpMethod get = new GetMethod(url);
        try {
            client.executeMethod(get);

            String redirect = null;
            Header h = get.getResponseHeader("Refresh");
            if (h != null) {
                String s = h.getValue();
                int cut = s.indexOf("url=");
                if (cut >= 0) {
                    redirect = s.substring(cut + 4);
                }
            }

            if (redirect == null) {
                response = get.getResponseBodyAsString();
                log.debug("Job " + mrJobId + " get status check result.\n");
            } else {
                url = redirect;
                log.debug("Job " + mrJobId + " check redirect url " + url + ".\n");
            }
        } finally {
            get.releaseConnection();
        }
    }

    return response;
}

From source file:org.apache.maven.doxia.linkcheck.validation.OnlineHTTPLinkValidator.java

/**
 * Checks the given link./*  w  ww.  j ava  2s  .  c om*/
 *
 * @param link the link to check.
 * @param nbRedirect the number of current redirects.
 * @return HttpMethod
 * @throws IOException if something goes wrong.
 */
private HttpMethod checkLink(String link, int nbRedirect) throws IOException {
    int max = MAX_NB_REDIRECT;
    if (this.http.getHttpClientParameters() != null
            && this.http.getHttpClientParameters().get(HttpClientParams.MAX_REDIRECTS) != null) {
        try {
            max = Integer
                    .valueOf(this.http.getHttpClientParameters().get(HttpClientParams.MAX_REDIRECTS).toString())
                    .intValue();
        } catch (NumberFormatException e) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("HttpClient parameter '" + HttpClientParams.MAX_REDIRECTS
                        + "' is not a number. Ignoring!");
            }
        }
    }
    if (nbRedirect > max) {
        throw new HttpException("Maximum number of redirections (" + max + ") exceeded");
    }

    HttpMethod hm;
    if (HEAD_METHOD.equalsIgnoreCase(this.http.getMethod())) {
        hm = new HeadMethod(link);
    } else if (GET_METHOD.equalsIgnoreCase(this.http.getMethod())) {
        hm = new GetMethod(link);
    } else {
        if (LOG.isErrorEnabled()) {
            LOG.error("Unsupported method: " + this.http.getMethod() + ", using 'get'.");
        }
        hm = new GetMethod(link);
    }

    // Default
    hm.setFollowRedirects(this.http.isFollowRedirects());

    try {
        URL url = new URL(link);

        cl.getHostConfiguration().setHost(url.getHost(), url.getPort(), url.getProtocol());

        cl.executeMethod(hm);

        StatusLine sl = hm.getStatusLine();
        if (sl == null) {
            if (LOG.isErrorEnabled()) {
                LOG.error("Unknown error validating link : " + link);
            }

            return null;
        }

        if (hm.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
                || hm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY
                || hm.getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT) {
            Header locationHeader = hm.getResponseHeader("location");

            if (locationHeader == null) {
                LOG.error("Site sent redirect, but did not set Location header");

                return hm;
            }

            String newLink = locationHeader.getValue();

            // Be careful to absolute/relative links
            if (!newLink.startsWith("http://") && !newLink.startsWith("https://")) {
                if (newLink.startsWith("/")) {
                    URL oldUrl = new URL(link);

                    newLink = oldUrl.getProtocol() + "://" + oldUrl.getHost()
                            + (oldUrl.getPort() > 0 ? ":" + oldUrl.getPort() : "") + newLink;
                } else {
                    newLink = link + newLink;
                }
            }

            HttpMethod oldHm = hm;

            if (LOG.isDebugEnabled()) {
                LOG.debug("[" + link + "] is redirected to [" + newLink + "]");
            }

            oldHm.releaseConnection();

            hm = checkLink(newLink, nbRedirect + 1);

            // Restore the hm to "Moved permanently" | "Moved temporarily" | "Temporary redirect"
            // if the new location is found to allow us to report it
            if (hm.getStatusCode() == HttpStatus.SC_OK && nbRedirect == 0) {
                return oldHm;
            }
        }

    } finally {
        hm.releaseConnection();
    }

    return hm;
}

From source file:org.apache.maven.proxy.config.HttpRepoConfiguration.java

private static long getLastModified(HttpMethod method) {
    Header lastModifiedHeader = method.getResponseHeader("Last-Modified");
    if (lastModifiedHeader == null) {
        return -1;
    }//from  www.j  a  va  2 s. co m
    String lastModifiedString = lastModifiedHeader.getValue();

    try {
        return DateParser.parseDate(lastModifiedString).getTime();
    } catch (DateParseException e) {
        LOGGER.warn("Unable to parse Last-Modified header : " + lastModifiedString);
        return System.currentTimeMillis();
    }
}

From source file:org.apache.ode.axis2.httpbinding.HttpHelper.java

/**
 * Build a "details" element that looks like this:
 *
 * @param method/*from   w  w w .j av a2  s . co  m*/
 * @return
 * @throws IOException
 */
public static Element prepareDetailsElement(HttpMethod method) {
    Header h = method.getResponseHeader("Content-Type");
    String receivedType = h != null ? h.getValue() : null;
    boolean bodyIsXml = receivedType != null && HttpUtils.isXml(receivedType);

    Document doc = DOMUtils.newDocument();
    Element detailsEl = doc.createElementNS(null, "details");
    Element statusLineEl = statusLineToElement(doc, method.getStatusLine());
    detailsEl.appendChild(statusLineEl);

    // set the body if any
    try {
        final String body = method.getResponseBodyAsString();
        if (StringUtils.isNotEmpty(body)) {
            Element bodyEl = doc.createElementNS(null, "responseBody");
            detailsEl.appendChild(bodyEl);
            // first, try to parse the body as xml
            // if it fails, put it as string in the body element
            boolean exceptionDuringParsing = false;
            if (bodyIsXml) {
                try {
                    Element parsedBodyEl = DOMUtils.stringToDOM(body);
                    bodyEl.appendChild(doc.importNode(parsedBodyEl, true));
                } catch (Exception e) {
                    String errmsg = "Unable to parse the response body as xml. Body will be inserted as string.";
                    if (log.isDebugEnabled())
                        log.debug(errmsg, e);
                    exceptionDuringParsing = true;
                }
            }
            if (!bodyIsXml || exceptionDuringParsing) {
                bodyEl.setTextContent(body);
            }
        }
    } catch (IOException e) {
        if (log.isWarnEnabled())
            log.warn("Exception while loading response body", e);
    }
    return detailsEl;
}

From source file:org.apache.ode.axis2.httpbinding.HttpMethodConverter.java

/**
 * Process the HTTP Response Headers.//w ww . ja v  a2 s.c o  m
 * <p/>
 * First go through the list of {@linkplain Namespaces.ODE_HTTP_EXTENSION_NS}{@code :header} elements included in the output binding.
 * For each of them, set the header value as the value of the message part.
 * <p/>
 * Then add all HTTP headers as header part in the message. The name of the header would be the part name.
 * <p/>
 * Finally, insert a header names 'Status-Line'. This header contains an element as returned by {@link HttpHelper#statusLineToElement(String)} .
 *
 * @param odeMessage
 * @param method
 * @param operationDef
 */
public void extractHttpResponseHeaders(org.apache.ode.bpel.iapi.Message odeMessage, HttpMethod method,
        Operation operationDef) {
    Message messageDef = operationDef.getOutput().getMessage();

    BindingOutput outputBinding = binding.getBindingOperation(operationDef.getName(),
            operationDef.getInput().getName(), operationDef.getOutput().getName()).getBindingOutput();
    Collection<UnknownExtensibilityElement> headerBindings = WsdlUtils
            .getHttpHeaders(outputBinding.getExtensibilityElements());

    // iterate through the list of header bindings
    // and set the message parts accordingly
    for (Iterator<UnknownExtensibilityElement> iterator = headerBindings.iterator(); iterator.hasNext();) {
        Element binding = iterator.next().getElement();
        String partName = binding.getAttribute("part");
        String headerName = binding.getAttribute("name");

        Part part = messageDef.getPart(partName);
        if (StringUtils.isNotEmpty(partName)) {
            Header responseHeader = method.getResponseHeader(headerName);
            // if the response header is not set, just skip it. no need to fail.
            if (responseHeader != null) {
                odeMessage.setPart(partName, createPartElement(part, responseHeader.getValue()));
            }
        } else {
            String errMsg = "Invalid binding: missing required attribute! Part name: "
                    + new QName(Namespaces.ODE_HTTP_EXTENSION_NS, "part");
            if (log.isErrorEnabled())
                log.error(errMsg);
            throw new RuntimeException(errMsg);
        }
    }

    // add all HTTP response headers (in their condensed form) into the message as header parts
    Set<String> headerNames = new HashSet<String>();
    for (Header header : method.getResponseHeaders())
        headerNames.add(header.getName());
    for (String hname : headerNames)
        odeMessage.setHeaderPart(hname, method.getResponseHeader(hname).getValue());

    // make the status line information available as a single element
    odeMessage.setHeaderPart("Status-Line", HttpHelper.statusLineToElement(method.getStatusLine()));
}

From source file:org.apache.ode.axis2.httpbinding.HttpMethodConverter.java

public void parseHttpResponse(org.apache.ode.bpel.iapi.Message odeResponse, HttpMethod method, Operation opDef)
        throws SAXException, IOException {
    BindingOperation opBinding = binding.getBindingOperation(opDef.getName(), opDef.getInput().getName(),
            opDef.getOutput().getName());
    /* process headers */
    extractHttpResponseHeaders(odeResponse, method, opDef);

    /* process the body if any */

    // assumption is made that a response may have at most one body. HttpBindingValidator checks this.
    MIMEContent outputContent = WsdlUtils
            .getMimeContent(opBinding.getBindingOutput().getExtensibilityElements());
    int status = method.getStatusCode();

    boolean xmlExpected = outputContent != null && HttpUtils.isXml(outputContent.getType());
    // '202/Accepted' and '204/No Content' status codes explicitly state that there is no body, so we should not fail even if a part is bound to the body response
    boolean isBodyExpected = outputContent != null;
    boolean isBodyMandatory = isBodyExpected && bodyAllowed(status) && status != _202_ACCEPTED;
    final String body;
    try {//from   www.  j  a  v  a 2 s  . c o  m
        body = method.getResponseBodyAsString();
    } catch (IOException e) {
        throw new RuntimeException("Unable to get the request body : " + e.getMessage());
    }

    final boolean emptyBody = StringUtils.isEmpty(body);
    if (emptyBody) {
        if (isBodyMandatory) {
            throw new RuntimeException("Response body is mandatory but missing!");
        }
    } else {
        if (isBodyExpected) {
            Part partDef = opDef.getOutput().getMessage().getPart(outputContent.getPart());
            Element partElement;

            if (xmlExpected) {

                Header h = method.getResponseHeader("Content-Type");
                String receivedType = h != null ? h.getValue() : null;
                boolean contentTypeSet = receivedType != null;
                boolean xmlReceived = contentTypeSet && HttpUtils.isXml(receivedType);

                // a few checks
                if (!contentTypeSet) {
                    if (log.isDebugEnabled())
                        log.debug("Received Response with a body but no 'Content-Type' header!");
                } else if (!xmlReceived) {
                    if (log.isDebugEnabled())
                        log.debug("Xml type was expected but non-xml type received! Expected Content-Type="
                                + outputContent.getType() + " Received Content-Type=" + receivedType);
                }

                // parse the body and create the message part
                Element bodyElement = DOMUtils.stringToDOM(body);
                partElement = createPartElement(partDef, bodyElement);
            } else {
                // if not xml, process it as text
                partElement = createPartElement(partDef, body);
            }

            // set the part
            odeResponse.setPart(partDef.getName(), partElement);

        } else {
            // the body was not expected but we don't know how to deal with it
            if (log.isDebugEnabled())
                log.debug("Body received but not mapped to any part! Body=\n" + body);
        }
    }
}

From source file:org.apache.ode.axis2.httpbinding.HttpMethodConverter.java

public Object[] parseFault(PartnerRoleMessageExchange odeMex, HttpMethod method) {
    Operation opDef = odeMex.getOperation();
    BindingOperation opBinding = binding.getBindingOperation(opDef.getName(), opDef.getInput().getName(),
            opDef.getOutput().getName());

    final String body;
    try {/*w  w w . java  2  s.c om*/
        body = method.getResponseBodyAsString();
    } catch (IOException e) {
        throw new RuntimeException("Unable to get the request body : " + e.getMessage(), e);
    }
    Header h = method.getResponseHeader("Content-Type");
    String receivedType = h != null ? h.getValue() : null;
    if (opDef.getFaults().isEmpty()) {
        throw new RuntimeException("Operation [" + opDef.getName() + "] has no fault. This "
                + method.getStatusCode() + " error will be considered as a failure.");
    } else if (opBinding.getBindingFaults().isEmpty()) {
        throw new RuntimeException(
                "No fault binding. This " + method.getStatusCode() + " error will be considered as a failure.");
    } else if (StringUtils.isEmpty(body)) {
        throw new RuntimeException("No body in the response. This " + method.getStatusCode()
                + " error will be considered as a failure.");
    } else if (receivedType != null && !HttpUtils.isXml(receivedType)) {
        throw new RuntimeException("Response Content-Type [" + receivedType
                + "] does not describe XML entities. Faults must be XML. This " + method.getStatusCode()
                + " error will be considered as a failure.");
    } else {

        if (receivedType == null) {
            if (log.isWarnEnabled())
                log.warn("[Service: " + serviceName + ", Port: " + portName + ", Operation: " + opDef.getName()
                        + "] Received Response with a body but no 'Content-Type' header! Will try to parse nevertheless.");
        }

        // try to parse body
        final Element bodyElement;
        try {
            bodyElement = DOMUtils.stringToDOM(body);
        } catch (Exception e) {
            throw new RuntimeException("Unable to parse the response body as xml. This "
                    + method.getStatusCode() + " error will be considered as a failure.", e);
        }

        // Guess which fault it is
        QName bodyName = new QName(bodyElement.getNamespaceURI(), bodyElement.getNodeName());
        Fault faultDef = WsdlUtils.inferFault(opDef, bodyName);

        if (faultDef == null) {
            throw new RuntimeException("Unknown Fault Type [" + bodyName + "] This " + method.getStatusCode()
                    + " error will be considered as a failure.");
        } else if (!WsdlUtils.isOdeFault(opBinding.getBindingFault(faultDef.getName()))) {
            // is this fault bound with ODE extension?
            throw new RuntimeException("Fault [" + bodyName + "] is not bound with "
                    + new QName(Namespaces.ODE_HTTP_EXTENSION_NS, "fault") + ". This " + method.getStatusCode()
                    + " error will be considered as a failure.");
        } else {
            // a fault has only one part
            Part partDef = (Part) faultDef.getMessage().getParts().values().iterator().next();

            QName faultName = new QName(definition.getTargetNamespace(), faultDef.getName());
            QName faultType = faultDef.getMessage().getQName();

            // create the ODE Message now that we know the fault
            org.apache.ode.bpel.iapi.Message response = odeMex.createMessage(faultType);

            // build the element to be sent back
            Element partElement = createPartElement(partDef, bodyElement);
            response.setPart(partDef.getName(), partElement);

            // extract and set headers
            extractHttpResponseHeaders(response, method, opDef);
            return new Object[] { faultName, response };
        }
    }
}

From source file:org.apache.sling.discovery.impl.topology.connector.TopologyRequestValidator.java

/**
 * Get the value of a response header./*  w w  w  .ja v  a2  s .  c  o  m*/
 *
 * @param method the method
 * @param name the name of the response header.
 * @return the value of the response header, null if none.
 */
private String getResponseHeader(HttpMethod method, String name) {
    Header h = method.getResponseHeader(name);
    if (h == null) {
        return null;
    }
    return h.getValue();
}

From source file:org.apache.sling.discovery.impl.topology.connector.TopologyRequestValidator.java

/**
 * @param method the response method.// w w  w  . j  a va2s.co  m
 * @return the body of the response from the server.
 * @throws IOException
 */
private String getResponseBody(HttpMethod method) throws IOException {
    final Header contentEncoding = method.getResponseHeader("Content-Encoding");
    if (contentEncoding != null && contentEncoding.getValue() != null
            && contentEncoding.getValue().contains("gzip")) {
        // then the server sent gzip - treat it so:
        final GZIPInputStream gzipIn = new GZIPInputStream(method.getResponseBodyAsStream());
        final String gunzippedEncodedJson = IOUtils.toString(gzipIn);
        gzipIn.close();
        return gunzippedEncodedJson;
    } else {
        // otherwise the server sent plaintext:
        if (method instanceof HttpMethodBase) {
            return ((HttpMethodBase) method).getResponseBodyAsString(16 * 1024 * 1024);
        }
        return method.getResponseBodyAsString();
    }
}