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.wafersystems.util.HttpUtil.java

/**
 * ??//from   w ww.  j  av a2s . c o m
 * 
 * @param ip         IP?
 * @param port         ??
 * @param hClient      HttpClient
 * @param method      HttpMethod
 * @param lastResult   
 * 
 * @return
 */
public static int checkRedirect(String ip, int port, HttpClient hClient, HttpMethod method, int lastResult)
        throws Exception {
    int result = HttpStatus.SC_OK;

    if ((lastResult == HttpStatus.SC_MOVED_TEMPORARILY) || (lastResult == HttpStatus.SC_MOVED_PERMANENTLY)
            || (lastResult == HttpStatus.SC_SEE_OTHER) || (lastResult == HttpStatus.SC_TEMPORARY_REDIRECT)) {
        //????URL?
        Header header = method.getResponseHeader("location");
        if (header != null) {
            String newURI = header.getValue();
            if (StrUtil.isEmptyStr(newURI))
                newURI = "http://" + ip + ":" + port + "/";

            //URL??IP??
            if (!newURI.startsWith("http"))
                newURI = "http://" + ip + ":" + port + newURI;
            logger.warn("??" + newURI);
            method = new GetMethod(newURI);
            result = hClient.executeMethod(method);
        }
    }

    return result;
}

From source file:com.intellij.tasks.impl.httpclient.ResponseUtil.java

public static String getResponseContentAsString(@Nonnull HttpMethod response) throws IOException {
    // Sometimes servers don't specify encoding and HttpMethod#getResponseBodyAsString
    // by default decodes from Latin-1, so we got to read byte stream and decode it from UTF-8
    // manually//from   w  ww . j  av a  2 s .  com
    //if (!response.hasBeenUsed()) {
    //  return "";
    //}
    org.apache.commons.httpclient.Header header = response.getResponseHeader(HTTP.CONTENT_TYPE);
    if (header != null && header.getValue().contains("charset")) {
        // ISO-8859-1 if charset wasn't specified in response
        return StringUtil.notNullize(response.getResponseBodyAsString());
    } else {
        InputStream stream = response.getResponseBodyAsStream();
        return stream == null ? "" : StreamUtil.readText(stream, DEFAULT_CHARSET);
    }
}

From source file:fr.dutra.confluence2wordpress.util.UrlUtils.java

private static URI followRedirectsInternal(URI url, int maxRedirections) {
    URI response = url;//  ww  w. j  a  v  a  2s. c o m
    HttpClient client = new HttpClient();
    DefaultHttpMethodRetryHandler handler = new DefaultHttpMethodRetryHandler(1, false);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, handler);
    HttpMethod method = new HeadMethod(url.toASCIIString());
    //method.setRequestHeader("Accept-Language", locale.getLanguage() + ",en");
    method.setFollowRedirects(false);
    try {
        int statusCode = client.executeMethod(method);
        if ((statusCode == HttpStatus.SC_MOVED_PERMANENTLY) | (statusCode == HttpStatus.SC_MOVED_TEMPORARILY)) {
            if (maxRedirections > 0) {
                Header location = method.getResponseHeader("Location");
                if (!location.getValue().equals("")) {
                    // recursively check URL until it's not redirected any more
                    // locations can be relative to previous URL
                    URI target = url.resolve(location.getValue());
                    response = followRedirectsInternal(target, maxRedirections - 1);
                }
            }
        }
    } catch (Exception e) {
        //HttpClient can also throw IllegalArgumentException when URLs are malformed
    }
    return response;
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

private static String getLocationValue(HttpMethod method) throws URIException {
    String locationValue = null;/*w w  w.j a v a  2 s.co m*/
    Header location = method.getResponseHeader("Location");
    if (location != null && isRedirect(method.getStatusCode())) {
        locationValue = location.getValue();
        // Novell iChain workaround
        if (locationValue.indexOf('"') >= 0) {
            locationValue = URIUtil.encodePath(locationValue);
        }
        // workaround for invalid relative location
        if (locationValue.startsWith("./")) {
            locationValue = locationValue.substring(1);
        }
    }
    return locationValue;
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Test method header for supported authentication mode,
 * return true if Basic authentication is not available
 *
 * @param getMethod http method//from w  w  w  .j  a  v  a  2  s  .  com
 * @return true if only NTLM is enabled
 */
public static boolean acceptsNTLMOnly(HttpMethod getMethod) {
    Header authenticateHeader = null;
    if (getMethod.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        authenticateHeader = getMethod.getResponseHeader("WWW-Authenticate");
    } else if (getMethod.getStatusCode() == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
        authenticateHeader = getMethod.getResponseHeader("Proxy-Authenticate");
    }
    if (authenticateHeader == null) {
        return false;
    } else {
        boolean acceptBasic = false;
        boolean acceptNTLM = false;
        HeaderElement[] headerElements = authenticateHeader.getElements();
        for (HeaderElement headerElement : headerElements) {
            if ("NTLM".equalsIgnoreCase(headerElement.getName())) {
                acceptNTLM = true;
            }
            if ("Basic realm".equalsIgnoreCase(headerElement.getName())) {
                acceptBasic = true;
            }
        }
        return acceptNTLM && !acceptBasic;

    }
}

From source file:com.kodokux.github.api.GithubApiUtil.java

@NotNull
public static Collection<String> getTokenScopes(@NotNull GithubAuthData auth) throws IOException {
    HttpMethod method = null;
    try {/*from w  ww  .j  a va  2 s . c  o m*/
        String uri = GithubUrlUtil.getApiUrl(auth.getHost()) + "/user";
        method = doREST(auth, uri, null, Collections.<Header>emptyList(), HttpVerb.HEAD);

        checkStatusCode(method);

        Header header = method.getResponseHeader("X-OAuth-Scopes");
        if (header == null) {
            throw new HttpException("No scopes header");
        }

        Collection<String> scopes = new ArrayList<String>();
        for (HeaderElement elem : header.getElements()) {
            scopes.add(elem.getName());
        }
        return scopes;
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.zimbra.cs.store.triton.MozyServerToken.java

/**
 * Set token value based on TDS response header contained in HttpMethod
 *///from   w  ww . j ava 2  s  . c om
public void setToken(HttpMethod method) {
    this.token = method.getResponseHeader(TritonHeaders.SERVER_TOKEN).getValue();
}

From source file:com.discursive.jccook.httpclient.ConditionalGetExample.java

private String retrieveHeader(HttpMethod method, String name) throws HttpException {
    HeaderElement[] header = method.getResponseHeader(name).getElements();
    String value = "";
    if (header.length > 0) {
        value = header[0].getName();//  w w  w.j a va 2s .c om
    }
    return value;
}

From source file:com.dtolabs.client.utils.WebserviceFormAuthenticator.java

boolean isValidLoginRedirect(final HttpMethod method) {
    final Header locHeader = method.getResponseHeader("Location");
    if (locHeader == null) {
        return false;
    }/*from  w  w w  .  j  a v  a2 s  . co  m*/
    final String location = locHeader.getValue();
    return location.matches(REGEX_BASE + getBasePath() + LOGIN_REGEX);
}

From source file:com.dtolabs.client.utils.WebserviceFormAuthenticator.java

boolean isLoginError(final HttpMethod method) {
    final Header locHeader = method.getResponseHeader("Location");
    if (locHeader == null) {
        return true;
    }// w w w . j a va  2  s.c  o m
    final String location = locHeader.getValue();

    return location.matches(REGEX_BASE + getBasePath() + LOGIN_ERROR_REGEX);
}