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

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

Introduction

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

Prototype

public HeaderElement[] getValues() throws HttpException 

Source Link

Usage

From source file:de.innovationgate.igutils.pingback.PingBackClient.java

private String determinePingBackURI(String targetURI) throws PingBackException {
    HttpClient client = WGFactory.getHttpClientFactory().createHttpClient();
    GetMethod pingbackTargetGET = new GetMethod(targetURI);
    pingbackTargetGET.setFollowRedirects(false);
    try {/*from   ww  w  .  j a va 2  s.co  m*/
        int responseCode = client.executeMethod(pingbackTargetGET);
        if (responseCode != HttpURLConnection.HTTP_OK) {
            if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
                throw new PingBackException(PingBackException.ERROR_ACCESS_DENIED,
                        "Access denied on target '" + targetURI + "'.");
            } else if (responseCode == HttpURLConnection.HTTP_BAD_GATEWAY) {
                throw new PingBackException(PingBackException.ERROR_UPSTREAM_SERVER_COMMUNICATION_ERROR,
                        "Unable to determine ping back target for post. Get request on '" + targetURI
                                + "' returned '" + responseCode + "'.");
            } else {
                throw new PingBackException(PingBackException.ERROR_TARGET_URI_CANNOT_BE_USED_AS_TARGET,
                        "Unable to determine ping back target for post. Get request on '" + targetURI
                                + "' returned '" + responseCode + "'.");
            }
        }

        Header header = pingbackTargetGET.getResponseHeader("X-Pingback");
        if (header != null && header.getValues().length > 0) {
            // retrieve ping back url from header
            HeaderElement headerElement = header.getValues()[0];
            return headerElement.getName();
        } else {
            // retrieve ping back url from link tag

            // check for textual content
            checkTextualContentType(pingbackTargetGET);

            // retrieve input reader an try to find link tag
            InputStream sourceIn = pingbackTargetGET.getResponseBodyAsStream();
            String searchTerm = "<link rel=\"pingback\" href=\"";
            if (sourceIn == null) {
                throw new PingBackException(PingBackException.ERROR_TARGET_URI_CANNOT_BE_USED_AS_TARGET,
                        "TargetURL '" + targetURI + "' cannot be parsed for link tag.");
            } else {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        pingbackTargetGET.getResponseBodyAsStream(), pingbackTargetGET.getResponseCharSet()));
                String line = reader.readLine();
                while (line != null) {
                    String orgLine = line;
                    line = line.toLowerCase();
                    int start = line.indexOf(searchTerm);
                    if (start != -1) {
                        if (start + searchTerm.length() <= line.length()) {
                            start = start + searchTerm.length();
                            int end = line.indexOf("\"", start);
                            if (end != -1) {
                                String href = orgLine.substring(start, end);
                                href = href.replaceAll("&amp;", "&");
                                href = href.replaceAll("&lt;", "<");
                                href = href.replaceAll("&gt;", ">");
                                href = href.replaceAll("&quot;", "\"");
                                return href;
                            } else {
                                throw new PingBackException(
                                        PingBackException.ERROR_TARGET_URI_CANNOT_BE_USED_AS_TARGET,
                                        "TargetURL '" + targetURI + "' returned an unparsable link-tag");
                            }
                        } else {
                            throw new PingBackException(
                                    PingBackException.ERROR_TARGET_URI_CANNOT_BE_USED_AS_TARGET,
                                    "TargetURL '" + targetURI + "' returned an unparsable link-tag");
                        }
                    }
                    // if endof head reached - cancel search
                    if (line.indexOf("</head>") != -1 || line.indexOf("<body>") != -1) {
                        throw new PingBackException(PingBackException.ERROR_TARGET_URI_CANNOT_BE_USED_AS_TARGET,
                                "TargetURL '" + targetURI + "' is not pingback-enabled.");
                    }
                    line = reader.readLine();
                }
                throw new PingBackException(PingBackException.ERROR_TARGET_URI_CANNOT_BE_USED_AS_TARGET,
                        "TargetURL '" + targetURI + "' is not pingback-enabled.");
            }
        }
    } catch (HttpException e) {
        throw new PingBackException(PingBackException.ERROR_GENERIC,
                "Unable to determine ping back target for post.", e);
    } catch (IOException e) {
        throw new PingBackException(PingBackException.ERROR_GENERIC,
                "Unable to determine ping back target for post.", e);
    }
}

From source file:edu.umd.cs.buildServer.BuildServerDaemon.java

/**
 * Get a required header value. If the header value isn't specified in the
 * server response, returns null.//from   w w  w  .  j a v  a  2  s . c o  m
 *
 * @param method
 *            the HttpMethod representing the request/response
 * @param headerName
 *            the name of the header
 * @return the value of the header, or null if the header isn't present
 * @throws HttpException
 */
private String getRequiredHeaderValue(HttpMethod method, String headerName) throws HttpException {
    Header header = method.getResponseHeader(headerName);
    if (header == null || header.getValues().length != 1) {
        getLog().error("Internal error: Missing header " + headerName + " in submit server response");
        for (Header h : method.getResponseHeaders()) {
            getLog().error("  have header " + h.getName());
        }
        return null;
    }
    return header.getValue();
}

From source file:edu.umd.cs.buildServer.BuildServerDaemon.java

/**
 * Get a required header value. If the header value isn't specified in the
 * server response, returns null.//w w w  .  j  a va  2  s  . com
 *
 * @param method
 *            the HttpMethod representing the request/response
 * @param headerName
 *            the name of the header
 * @return the value of the header, or null if the header isn't present
 * @throws HttpException
 */
private String getRequiredHeaderValue(HttpMethod method, String headerName, String alternativeHeaderName)
        throws HttpException {
    Header header = method.getResponseHeader(headerName);
    if (header == null || header.getValues().length != 1)
        header = method.getResponseHeader(alternativeHeaderName);
    if (header == null || header.getValues().length != 1) {
        getLog().error("Internal error: Missing header " + headerName + " in submit server response");
        for (Header h : method.getResponseHeaders()) {
            getLog().error("  have header " + h.getName());
        }
        return null;
    }
    return header.getValue();
}