Example usage for org.apache.http.client.methods HttpHead HttpHead

List of usage examples for org.apache.http.client.methods HttpHead HttpHead

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpHead HttpHead.

Prototype

public HttpHead(final String uri) 

Source Link

Usage

From source file:ro.zg.netcell.datasources.executors.http.HttpCommandExecutor.java

public HttpCommandResponse executeCommand(CommandContext commandContext) throws Exception {
    HttpClient httpClient = (HttpClient) commandContext.getConnectionManager().getConnection();
    ScriptDaoCommand command = commandContext.getCommand();
    String method = (String) command.getArgument("method");
    String url = (String) command.getArgument("url");
    /* encode the url passed on the http request */
    // URI requestUri = new URI(url);
    // requestUri = URIUtils.createURI(requestUri.getScheme(), requestUri.getHost(), requestUri.getPort(),
    // requestUri.getPath(), URLEncoder.encode(requestUri.getQuery(),HTTP.DEFAULT_PROTOCOL_CHARSET),
    // requestUri.getFragment());
    String encodedUrl = URLEncoder.encode(url, HTTP.DEFAULT_PROTOCOL_CHARSET);
    boolean returnHeaders = false;
    Object rh = command.getArgument("returnHeaders");
    if (rh != null) {
        returnHeaders = (Boolean) rh;
    }//from w ww  . j a  va  2 s.  c  o  m

    HttpRequestBase request = null;
    if ("GET".equals(method)) {
        request = new HttpGet(encodedUrl);
    } else if ("POST".equals(method)) {
        HttpPost post = new HttpPost(encodedUrl);
        String content = (String) command.getArgument("content");
        if (content != null) {
            post.setEntity(new StringEntity(content));
        }
        request = post;
    } else if ("HEAD".equals(method)) {
        request = new HttpHead(encodedUrl);
    }

    Map<String, String> requestHeaders = (Map) command.getArgument("requestHeaders");
    if (requestHeaders != null) {
        for (Map.Entry<String, String> entry : requestHeaders.entrySet()) {
            request.setHeader(entry.getKey(), entry.getValue());
        }
    }
    HttpContext localContext = new BasicHttpContext();
    HttpEntity responseEntity = null;
    HttpCommandResponse commandResponse = new HttpCommandResponse();
    try {
        HttpResponse response = httpClient.execute(request, localContext);
        responseEntity = response.getEntity();
        StatusLine statusLine = response.getStatusLine();

        commandResponse.setStatusCode(statusLine.getStatusCode());
        commandResponse.setProtocol(statusLine.getProtocolVersion().getProtocol());
        commandResponse.setReasonPhrase(statusLine.getReasonPhrase());
        commandResponse.setRequestUrl(url);
        HttpRequest actualRequest = (HttpRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
        HttpHost targetHost = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        commandResponse.setTargetUrl(targetHost.toURI() + actualRequest.getRequestLine().getUri());

        if (returnHeaders) {
            Map<String, String> headers = new HashMap<String, String>();
            for (Header h : response.getAllHeaders()) {
                headers.put(h.getName().toLowerCase(), h.getValue().toLowerCase());
            }
            commandResponse.setHeaders(headers);
        }
        if (responseEntity != null) {
            long responseLength = responseEntity.getContentLength();
            String responseContent = EntityUtils.toString(responseEntity, HTTP.UTF_8);
            if (responseLength == -1) {
                responseLength = responseContent.length();
            }
            commandResponse.setLength(responseLength);
            commandResponse.setContent(responseContent);
        }
    } finally {
        if (responseEntity != null) {
            responseEntity.consumeContent();
        } else {
            request.abort();
        }
    }

    return commandResponse;
}

From source file:org.androidx.frames.libs.volley.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from  w  ww . ja  v a  2s  . c om*/
/* protected */
static HttpUriRequest createHttpRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for
        // backwards compatibility.
        // If the request's post body is null, then the assumption is
        // that the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:com.feedeo.rest.client.AbstractRestClient.java

protected ClientHttpRequestFactory createClientHttpRequestFactory(HttpClient httpClient) {
    return new HttpComponentsClientHttpRequestFactory(httpClient) {
        @Override// w ww .  j a v a 2 s.  c o m
        protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
            switch (httpMethod) {
            case GET:
                return new HttpGet(uri);
            case DELETE:
                return new HttpEntityEnclosingDeleteRequest(uri);
            case HEAD:
                return new HttpHead(uri);
            case OPTIONS:
                return new HttpOptions(uri);
            case POST:
                return new HttpPost(uri);
            case PUT:
                return new HttpPut(uri);
            case TRACE:
                return new HttpTrace(uri);
            case PATCH:
                return new HttpPatch(uri);
            default:
                throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
            }
        }
    };
}

From source file:com.griddynamics.jagger.invoker.http.HttpInvoker.java

private HttpRequestBase createMethod(HttpQuery query, URI uri) throws UnsupportedEncodingException {
    HttpRequestBase method;/*from  ww  w. j av a2 s. co  m*/
    switch (query.getMethod()) {
    case POST:
        method = new HttpPost(uri);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        for (Map.Entry<String, String> methodParam : query.getMethodParams().entrySet()) {
            nameValuePairs.add(new BasicNameValuePair(methodParam.getKey(), methodParam.getValue()));
        }
        ((HttpPost) method).setEntity(new UrlEncodedFormEntity(nameValuePairs));
        break;
    case PUT:
        method = new HttpPut(uri);
        break;
    case GET:
        method = new HttpGet(uri);
        break;
    case DELETE:
        method = new HttpDelete(uri);
        break;
    case TRACE:
        method = new HttpTrace(uri);
        break;
    case HEAD:
        method = new HttpHead(uri);
        break;
    case OPTIONS:
        method = new HttpOptions(uri);
        break;
    default:
        throw new UnsupportedOperationException(
                "Invoker does not support \"" + query.getMethod() + "\" HTTP request.");
    }
    return method;
}

From source file:de.adorsys.forge.plugin.curl.CurlPlugin.java

private HttpUriRequest createRequest(final String url, RequestMethod command, String headers) {
    HttpUriRequest request;/*from  ww  w .j  ava  2s  . c  o  m*/

    switch (command) {

    case HEAD:
        request = new HttpHead(url);
        break;

    case OPTIONS:
        request = new HttpOptions(url);
        break;

    case POST:
        request = new HttpPost(url);
        break;

    case PUT:
        request = new HttpPut(url);
        break;

    default:
        request = new HttpGet(url);
        break;
    }

    setHeaders(request, headers);

    return request;
}

From source file:com.github.tomakehurst.wiremock.http.HttpClientFactory.java

public static HttpUriRequest getHttpRequestFor(RequestMethod method, String url) {
    notifier().info("Proxying: " + method + " " + url);

    if (method.equals(GET))
        return new HttpGet(url);
    else if (method.equals(POST))
        return new HttpPost(url);
    else if (method.equals(PUT))
        return new HttpPut(url);
    else if (method.equals(DELETE))
        return new HttpDelete(url);
    else if (method.equals(HEAD))
        return new HttpHead(url);
    else if (method.equals(OPTIONS))
        return new HttpOptions(url);
    else if (method.equals(TRACE))
        return new HttpTrace(url);
    else if (method.equals(PATCH))
        return new HttpPatch(url);
    else/* w w w.j a  va  2s. com*/
        return new GenericHttpUriRequest(method.toString(), url);
}

From source file:com.lumata.lib.lupa.internal.ProxiedReadableResource.java

private void doHttpHeadToReadContentTypeFromHeader() {
    String url = getUrl().toString();
    LOG.debug("Invoking HTTP HEAD on {}", url);
    HttpHead head = new HttpHead(url);
    try {/*  w w w. ja va2  s  . c o m*/
        HttpResponse response = client.execute(head);
        if (response.getStatusLine().getStatusCode() < 300) {
            setContentTypeFromHeader(response);
        }
    } catch (IOException e) {
        LOG.error("Unable to fetch content type using HTTP HEAD", e);
    }
}

From source file:net.gcolin.httpquery.HttpHandlerImpl.java

@Override
public Request head(String uri) {
    return new RequestImpl(new HttpHead(uri));
}

From source file:org.apache.geode.rest.internal.web.GeodeRestClient.java

public HttpResponse doHEAD(String query, String username, String password) throws Exception {
    HttpHead httpHead = new HttpHead(CONTEXT + query);
    return doRequest(httpHead, username, password);
}