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:org.codegist.crest.io.http.HttpClientHttpChannelFactory.java

/**
 * @inheritDoc//from w  w w .ja v  a2  s . c  om
 */
public HttpChannel open(MethodType methodType, String url, Charset charset) {
    HttpUriRequest request;
    switch (methodType) {
    case GET:
        request = new HttpGet(url);
        break;
    case POST:
        request = new HttpPost(url);
        break;
    case PUT:
        request = new HttpPut(url);
        break;
    case DELETE:
        request = new HttpDelete(url);
        break;
    case OPTIONS:
        request = new HttpOptions(url);
        break;
    case HEAD:
        request = new HttpHead(url);
        break;
    default:
        throw new IllegalArgumentException("Method " + methodType + " not supported");
    }

    HttpProtocolParams.setContentCharset(request.getParams(), charset.displayName());

    return new HttpClientHttpChannel(client, request);
}

From source file:tools.devnull.boteco.client.rest.impl.DefaultRestClient.java

@Override
public RestConfiguration head(URI uri) {
    return execute(new HttpHead(uri));
}

From source file:com.cibuddy.jenkins.JenkinsServer.java

public JenkinsServer(String source, String serverName, URI serverUri, AuthenticationToken token) {
    try {/*from  w  w  w .  j  av  a 2  s . com*/
        this.serverUri = serverUri;
        this.name = serverName;
        this.source = source;
        LOG.debug("New Jenkins Server to include under" + serverUri);

        // check if Jenkins is reachable and in fact a jenkins server

        HttpClient httpclient = new DefaultHttpClient();
        HttpHead head = new HttpHead(serverUri);
        HttpResponse response = httpclient.execute(head);
        HeaderIterator it = response.headerIterator(JENKINS_HEADER);
        while (it.hasNext()) {
            jenkins_version = ((BufferedHeader) it.next()).getValue();
            LOG.debug("Jenkins Version identified: " + jenkins_version);
        }
    } catch (Exception ex) {
        // do nothing
        LOG.warn("Problem occured while adding Jenkins Server to configuration", ex);
    }
}

From source file:org.jclouds.http.apachehc.ApacheHCUtils.java

public HttpUriRequest convertToApacheRequest(HttpRequest request) {
    HttpUriRequest apacheRequest;/* ww w . ja v a 2s.  c  o  m*/
    if (request.getMethod().equals(HttpMethod.HEAD)) {
        apacheRequest = new HttpHead(request.getEndpoint());
    } else if (request.getMethod().equals(HttpMethod.GET)) {
        apacheRequest = new HttpGet(request.getEndpoint());
    } else if (request.getMethod().equals(HttpMethod.DELETE)) {
        apacheRequest = new HttpDelete(request.getEndpoint());
    } else if (request.getMethod().equals(HttpMethod.PUT)) {
        apacheRequest = new HttpPut(request.getEndpoint());
        apacheRequest.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
    } else if (request.getMethod().equals(HttpMethod.POST)) {
        apacheRequest = new HttpPost(request.getEndpoint());
    } else {
        final String method = request.getMethod();
        if (request.getPayload() != null)
            apacheRequest = new HttpEntityEnclosingRequestBase() {

                @Override
                public String getMethod() {
                    return method;
                }

            };
        else
            apacheRequest = new HttpRequestBase() {

                @Override
                public String getMethod() {
                    return method;
                }

            };
        HttpRequestBase.class.cast(apacheRequest).setURI(request.getEndpoint());
    }
    Payload payload = request.getPayload();

    // Since we may remove headers, ensure they are added to the apache
    // request after this block
    if (apacheRequest instanceof HttpEntityEnclosingRequest) {
        if (payload != null) {
            addEntityForContent(HttpEntityEnclosingRequest.class.cast(apacheRequest), payload);
        }
    } else {
        apacheRequest.addHeader(HttpHeaders.CONTENT_LENGTH, "0");
    }

    for (Map.Entry<String, String> entry : request.getHeaders().entries()) {
        String header = entry.getKey();
        // apache automatically tries to add content length header
        if (!header.equals(HttpHeaders.CONTENT_LENGTH))
            apacheRequest.addHeader(header, entry.getValue());
    }
    apacheRequest.addHeader(HttpHeaders.USER_AGENT, USER_AGENT);
    return apacheRequest;
}

From source file:org.openremote.java.console.controller.connector.SingleThreadHttpConnector.java

@Override
protected void doRequest(String url, final ControllerCallback callback, Integer timeout) {
    boolean doHead = false;

    if (callback.command == Command.GET_RESOURCE) {
        // Determine if we should load data if not do a head request
        Object[] data = (Object[]) callback.data;
        boolean loadData = (Boolean) data[2];
        if (!loadData) {
            doHead = true;/*  ww w.j a v  a2s. c  om*/
        }
    }

    HttpUriRequest http;

    if (doHead) {
        HttpHead httpHead = new HttpHead(url);
        httpHead.setConfig(RequestConfig.custom().setSocketTimeout(timeout).setConnectionRequestTimeout(timeout)
                .setConnectTimeout(timeout).build());
        http = httpHead;
    } else {
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Accept", "application/json");
        httpGet.setConfig(RequestConfig.custom().setSocketTimeout(timeout).setConnectionRequestTimeout(timeout)
                .setConnectTimeout(timeout).build());
        http = httpGet;
    }

    try {
        HttpResponse response = client.execute(http);
        byte[] responseData = null;

        if (response.getEntity() != null) {
            InputStream is = response.getEntity().getContent();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int read = 0;
            while ((read = is.read(buffer, 0, buffer.length)) != -1) {
                baos.write(buffer, 0, read);
            }
            baos.flush();
            is.close();
            responseData = baos.toByteArray();
        }

        // java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        // String responseStr = s.hasNext() ? s.next() : "";

        if (callback.command == Command.LOGOUT) {
            creds.clear();
        }

        handleResponse(callback, response.getStatusLine().getStatusCode(), response.getAllHeaders(),
                responseData);
    } catch (Exception e) {
        if (callback.command == Command.DO_SENSOR_POLLING && e instanceof SocketTimeoutException) {
            callback.callback.onSuccess(null);
            return;
        }
        callback.callback.onFailure(ControllerResponseCode.UNKNOWN_ERROR);
    }
}

From source file:com.okta.sdk.impl.http.httpclient.HttpClientRequestFactory.java

/**
 * Creates an HttpClient method object based on the specified request and
 * populates any parameters, headers, etc. from the original request.
 *
 * @param request        The request to convert to an HttpClient method object.
 * @param previousEntity The optional, previous HTTP entity to reuse in the new request.
 * @return The converted HttpClient method object with any parameters,
 *         headers, etc. from the original request set.
 *//*w  w  w .  j av a 2  s .c o  m*/
HttpRequestBase createHttpClientRequest(Request request, HttpEntity previousEntity) {

    HttpMethod method = request.getMethod();
    URI uri = getFullyQualifiedUri(request);
    InputStream body = request.getBody();
    long contentLength = request.getHeaders().getContentLength();

    HttpRequestBase base;

    switch (method) {
    case DELETE:
        base = new HttpDelete(uri);
        break;
    case GET:
        base = new HttpGet(uri);
        break;
    case HEAD:
        base = new HttpHead(uri);
        break;
    case POST:
        base = new HttpPost(uri);
        ((HttpEntityEnclosingRequestBase) base).setEntity(new RepeatableInputStreamEntity(request));
        break;
    case PUT:
        base = new HttpPut(uri);

        // Enable 100-continue support for PUT operations, since this is  where we're potentially uploading
        // large amounts of data and want to find out as early as possible if an operation will fail. We
        // don't want to do this for all operations since it will cause extra latency in the network
        // interaction.
        base.setConfig(RequestConfig.copy(defaultRequestConfig).setExpectContinueEnabled(true).build());

        if (previousEntity != null) {
            ((HttpEntityEnclosingRequestBase) base).setEntity(previousEntity);
        } else if (body != null) {
            HttpEntity entity = new RepeatableInputStreamEntity(request);
            if (contentLength < 0) {
                entity = newBufferedHttpEntity(entity);
            }
            ((HttpEntityEnclosingRequestBase) base).setEntity(entity);
        }
        break;
    default:
        throw new IllegalArgumentException("Unrecognized HttpMethod: " + method);
    }

    base.setProtocolVersion(HttpVersion.HTTP_1_1);

    applyHeaders(base, request);

    return base;
}

From source file:org.kymjs.kjframe.http.HttpClientStack.java

static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) {
    switch (request.getMethod()) {
    case HttpMethod.GET:
        return new HttpGet(request.getUrl());
    case HttpMethod.DELETE:
        return new HttpDelete(request.getUrl());
    case HttpMethod.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }//from ww w  .  ja  va 2  s .c o m
    case HttpMethod.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case HttpMethod.HEAD:
        return new HttpHead(request.getUrl());
    case HttpMethod.OPTIONS:
        return new HttpOptions(request.getUrl());
    case HttpMethod.TRACE:
        return new HttpTrace(request.getUrl());
    case HttpMethod.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.scut.easyfe.network.kjFrame.http.HttpClientStack.java

static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) {
    switch (request.getMethod()) {
    case Request.HttpMethod.GET:
        return new HttpGet(request.getUrl());
    case Request.HttpMethod.DELETE:
        return new HttpDelete(request.getUrl());
    case Request.HttpMethod.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }/*from w w w .  jav a  2  s. com*/
    case Request.HttpMethod.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Request.HttpMethod.HEAD:
        return new HttpHead(request.getUrl());
    case Request.HttpMethod.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Request.HttpMethod.TRACE:
        return new HttpTrace(request.getUrl());
    case Request.HttpMethod.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:net.duckling.probe.ui.cron.AccessURLJob.java

@SuppressWarnings("finally")
public WatchedUrl HttpTest() throws ClientProtocolException, IOException {
    WatchedUrl watchedUrl = new WatchedUrl();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    long start = System.currentTimeMillis();
    try {/*from   w ww. j av  a  2  s.c  om*/
        switch (method) {
        case "GET":
            HttpGet httpget = new HttpGet(url);
            response = httpclient.execute(httpget);
            break;
        case "POST":
            HttpPost httppost = new HttpPost(url);
            response = httpclient.execute(httppost);
            break;
        case "HEAD":
            HttpHead httphead = new HttpHead(url);
            response = httpclient.execute(httphead);
            break;
        }
    } finally {
        int responseTime = (int) (System.currentTimeMillis() - start);
        if (response != null) {
            watchedUrl.setStatusCode(response.getStatusLine().getStatusCode());
            if (response.getStatusLine().getStatusCode() == 200
                    || response.getStatusLine().getStatusCode() == 302) {
                watchedUrl.setAvailability(true);
                alertServiceImpl.conutSuccess(collector);
            } else {
                watchedUrl.setAvailability(false);
                alertServiceImpl.countFail(collector);
            }

        } else {
            watchedUrl.setAvailability(false);
            alertServiceImpl.countFail(collector);
        }
        watchedUrl.setResponseTime(responseTime);
        watchedUrl.setCollectorId(collector.getId());
        watchedUrl.setProductId(collector.getProductId());
        watchedUrl.setMethod(method);
        watchedUrl.setWatchTime(new Date());
        httpclient.close();
        return watchedUrl;
    }

}

From source file:tools.devnull.boteco.client.rest.impl.DefaultRestClient.java

@Override
public RestConfiguration head(String url) {
    return execute(new HttpHead(url));
}