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.fcrepo.integration.http.api.AbstractResourceIT.java

protected static void assertDeleted(final String id) {
    final String location = serverAddress + id;
    assertThat("Expected object to be deleted", getStatus(new HttpHead(location)), is(GONE.getStatusCode()));
    assertThat("Expected object to be deleted", getStatus(new HttpGet(location)), is(GONE.getStatusCode()));
}

From source file:org.eweb4j.spiderman.plugin.util.PageFetcherImpl.java

/**
 * /*from w  w  w . j  a va2  s. c  o m*/
 * @date 2013-1-7 ?11:08:54
 * @param toFetchURL
 * @return
 */
public FetchResult request(FetchRequest req) throws Exception {
    FetchResult fetchResult = new FetchResult();
    HttpUriRequest request = null;
    HttpEntity entity = null;
    String toFetchURL = req.getUrl();
    boolean isPost = false;
    try {
        if (Http.Method.GET.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpGet(toFetchURL);
        else if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())) {
            request = new HttpPost(toFetchURL);
            isPost = true;
        } else if (Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpPut(toFetchURL);
        else if (Http.Method.HEAD.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpHead(toFetchURL);
        else if (Http.Method.OPTIONS.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpOptions(toFetchURL);
        else if (Http.Method.DELETE.equalsIgnoreCase(req.getHttpMethod()))
            request = new HttpDelete(toFetchURL);
        else
            throw new Exception("Unknown http method name");

        //???,??
        // TODO ?delay?
        synchronized (mutex) {
            //??
            long now = (new Date()).getTime();
            //?Host??
            if (now - lastFetchTime < config.getPolitenessDelay())
                Thread.sleep(config.getPolitenessDelay() - (now - lastFetchTime));
            //????HOST??URL
            lastFetchTime = (new Date()).getTime();
        }

        //GZIP???GZIP?
        request.addHeader("Accept-Encoding", "gzip");
        for (Iterator<Entry<String, String>> it = headers.entrySet().iterator(); it.hasNext();) {
            Entry<String, String> entry = it.next();
            request.addHeader(entry.getKey(), entry.getValue());
        }

        //?
        Header[] headers = request.getAllHeaders();
        for (Header h : headers) {
            Map<String, List<String>> hs = req.getHeaders();
            String key = h.getName();
            List<String> val = hs.get(key);
            if (val == null)
                val = new ArrayList<String>();
            val.add(h.getValue());

            hs.put(key, val);
        }
        req.getCookies().putAll(this.cookies);
        fetchResult.setReq(req);

        HttpEntity reqEntity = null;
        if (Http.Method.POST.equalsIgnoreCase(req.getHttpMethod())
                || Http.Method.PUT.equalsIgnoreCase(req.getHttpMethod())) {
            if (!req.getFiles().isEmpty()) {
                reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                for (Iterator<Entry<String, List<File>>> it = req.getFiles().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<File>> e = it.next();
                    String paramName = e.getKey();
                    for (File file : e.getValue()) {
                        // For File parameters
                        ((MultipartEntity) reqEntity).addPart(paramName, new FileBody(file));
                    }
                }

                for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<Object>> e = it.next();
                    String paramName = e.getKey();
                    for (Object paramValue : e.getValue()) {
                        // For usual String parameters
                        ((MultipartEntity) reqEntity).addPart(paramName, new StringBody(
                                String.valueOf(paramValue), "text/plain", Charset.forName("UTF-8")));
                    }
                }
            } else {
                List<NameValuePair> params = new ArrayList<NameValuePair>(req.getParams().size());
                for (Iterator<Entry<String, List<Object>>> it = req.getParams().entrySet().iterator(); it
                        .hasNext();) {
                    Entry<String, List<Object>> e = it.next();
                    String paramName = e.getKey();
                    for (Object paramValue : e.getValue()) {
                        params.add(new BasicNameValuePair(paramName, String.valueOf(paramValue)));
                    }
                }
                reqEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
            }

            if (isPost)
                ((HttpPost) request).setEntity(reqEntity);
            else
                ((HttpPut) request).setEntity(reqEntity);
        }

        //??
        HttpResponse response = httpClient.execute(request);
        headers = response.getAllHeaders();
        for (Header h : headers) {
            Map<String, List<String>> hs = fetchResult.getHeaders();
            String key = h.getName();
            List<String> val = hs.get(key);
            if (val == null)
                val = new ArrayList<String>();
            val.add(h.getValue());

            hs.put(key, val);
        }
        //URL
        fetchResult.setFetchedUrl(toFetchURL);
        String uri = request.getURI().toString();
        if (!uri.equals(toFetchURL))
            if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL))
                fetchResult.setFetchedUrl(uri);

        entity = response.getEntity();
        //???
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            if (statusCode != HttpStatus.SC_NOT_FOUND) {
                Header locationHeader = response.getFirstHeader("Location");
                //301?302?URL??
                if (locationHeader != null && (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                        || statusCode == HttpStatus.SC_MOVED_TEMPORARILY))
                    fetchResult.setMovedToUrl(
                            URLCanonicalizer.getCanonicalURL(locationHeader.getValue(), toFetchURL));
            }
            //???OKURLstatusCode??
            //???
            if (this.site.getSkipStatusCode() != null && this.site.getSkipStatusCode().trim().length() > 0) {
                String[] scs = this.site.getSkipStatusCode().split(",");
                for (String code : scs) {
                    int c = CommonUtil.toInt(code);
                    //????entity
                    if (statusCode == c) {
                        assemPage(fetchResult, entity);
                        break;
                    }
                }
            }
            fetchResult.setStatusCode(statusCode);
            return fetchResult;
        }

        //??
        if (entity != null) {
            fetchResult.setStatusCode(statusCode);
            assemPage(fetchResult, entity);
            return fetchResult;
        }
    } catch (Throwable e) {
        fetchResult.setFetchedUrl(e.toString());
        fetchResult.setStatusCode(Status.INTERNAL_SERVER_ERROR.ordinal());
        return fetchResult;
    } finally {
        try {
            if (entity == null && request != null)
                request.abort();
        } catch (Exception e) {
            throw e;
        }
    }

    fetchResult.setStatusCode(Status.UNSPECIFIED_ERROR.ordinal());
    return fetchResult;
}

From source file:com.sat.vcse.automation.utils.http.HttpClient.java

/**
 * HEAD request to a server/*  w  w  w  .j  a  v  a2  s .c o m*/
 * @return CoreResponse
 */
public CoreResponse head() {
    final String METHOD_NAME = "head(): ";

    try (CloseableHttpClient client = getClient();) {
        // Instantiate a Head Request and define the Target URL
        HttpRequestBase httpRequestBase = new HttpHead(this.targetURL);
        // Build the HTTP request header from the HeadeMap
        addHeader(httpRequestBase);
        return executeRequest(client, httpRequestBase);

    } catch (IOException exp) {
        LogHandler.error(CLASS_NAME + METHOD_NAME + "Exception: " + exp.getMessage());
        throw new CoreRuntimeException(exp, CLASS_NAME + METHOD_NAME + exp.getMessage());
    }
}

From source file:qhindex.controller.SearchAuthorWorksController.java

private String resolvePublisher(String urlCitationWork, String publisherNameIncomplete) throws IOException {
    String publisher = publisherNameIncomplete;
    if (urlCitationWork.contains(".pdf") == false) {
        // Get the header and determine if the resource is in text format (html or plain)
        // to be able to extract the publisher name
        final RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(AppHelper.connectionTimeOut)
                .setConnectionRequestTimeout(AppHelper.connectionTimeOut)
                .setSocketTimeout(AppHelper.connectionTimeOut).setStaleConnectionCheckEnabled(true).build();
        final CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
                .build();/*from   w  w  w . ja v a 2  s.c  o m*/

        HttpHead httpHead = new HttpHead(urlCitationWork);
        try {
            CloseableHttpResponse responseHead = httpclient.execute(httpHead);
            StatusLine statusLineHead = responseHead.getStatusLine();
            responseHead.close();
            String contentType = responseHead.getFirstHeader("Content-Type").toString().toLowerCase();

            if (statusLineHead.getStatusCode() < 300 && contentType.contains("text/html")
                    || contentType.contains("text/plain")) {
                HttpGet httpGet = new HttpGet(urlCitationWork);

                CloseableHttpResponse responsePost = httpclient.execute(httpGet);
                StatusLine statusLine = responsePost.getStatusLine();

                if (statusLine.getStatusCode() < 300) {
                    //AppHelper.waitBeforeNewRequest();
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader((responsePost.getEntity().getContent())));
                    String content = new String();
                    String line;
                    while ((line = br.readLine()) != null) {
                        content += line;
                    }

                    int bodyStartIndex = content.indexOf("<body");
                    if (bodyStartIndex < 0)
                        bodyStartIndex = 0;

                    try {
                        publisherNameIncomplete = formatRegExSpecialCharsInString(publisherNameIncomplete);
                        Pattern pattern = Pattern.compile(publisherNameIncomplete + "(\\w|\\d|-|\\s)+");
                        Matcher matcher = pattern.matcher(content);
                        if (matcher.find(bodyStartIndex)) {
                            publisher = content.substring(matcher.start(), matcher.end());
                        } else {
                            publisher = publisherNameIncomplete;
                        }
                    } catch (Exception ex) {
                        Debug.print(
                                "Exception while resolving publisher for citing work - extrating pattern from citation web resource: "
                                        + ex.toString());
                        resultsMsg += "Exception while resolving publisher for citing work - extrating pattern from citation web resource.\n";
                    }
                }
                responsePost.close();

            }
        } catch (IOException ioEx) {
            Debug.print("Exception while resolving publisher for citing work: " + ioEx.toString());
            resultsMsg += "Exception while resolving publisher for citing work.\n";
        }
    }
    publisher = publisher.trim();
    return publisher;
}

From source file:org.frontcache.tests.base.CommonTests.java

@Test
public void testCacheForHTTPMethod() throws Exception {

    webClient.addRequestHeader(FCHeaders.X_FRONTCACHE_TRACE, "true");

    // the first request - response should be cached
    HtmlPage page = webClient.getPage(getFrontCacheBaseURLDomainFC1() + "common/methods/a.jsp");
    assertEquals("a", page.getPage().asText());

    WebResponse webResponse = page.getWebResponse();

    assertEquals(false, TestUtils.isRequestFromCache(
            webResponse.getResponseHeaderValue(FCHeaders.X_FRONTCACHE_TRACE_REQUEST + ".0")));

    // second request - the same request - response should be from the cache now
    page = webClient.getPage(getFrontCacheBaseURLDomainFC1() + "common/methods/a.jsp");
    assertEquals("a", page.getPage().asText());
    webResponse = page.getWebResponse();

    assertEquals(true, TestUtils.isRequestFromCache(
            webResponse.getResponseHeaderValue(FCHeaders.X_FRONTCACHE_TRACE_REQUEST + ".0")));

    // third request (HEAD METHOD) - the same request - response should be from the cache now

    String urlStr = getFrontCacheBaseURLDomainFC1() + "common/methods/a.jsp";
    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpResponse response = null;/*from   www . j  av a2s. c  om*/

    HttpHost httpHost = FCUtils.getHttpHost(new URL(urlStr));
    HttpRequest httpRequest = new HttpHead(FCUtils.buildRequestURI(urlStr));
    httpRequest.addHeader(FCHeaders.ACCEPT, "text/html");
    httpRequest.addHeader(FCHeaders.X_FRONTCACHE_TRACE, "true");

    response = httpclient.execute(httpHost, httpRequest);

    assertEquals(true, TestUtils.isRequestFromCache(
            response.getFirstHeader(FCHeaders.X_FRONTCACHE_TRACE_REQUEST + ".0").getValue()));

    ((CloseableHttpResponse) response).close();

    return;
}

From source file:org.coding.git.api.CodingNetConnection.java

@NotNull
private CloseableHttpResponse doREST(@NotNull final String uri, @Nullable final String requestBody,
        @NotNull final Collection<Header> headers, @NotNull final HttpVerb verb) throws IOException {
    HttpRequestBase request;//from ww w.j a va2s  .co  m
    switch (verb) {
    case POST:
        request = new HttpPost(uri);
        if (requestBody != null) {
            ((HttpPost) request).setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
        }
        break;
    case PATCH:
        request = new HttpPatch(uri);
        if (requestBody != null) {
            ((HttpPatch) request).setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
        }
        break;
    case GET:
        request = new HttpGet(uri);
        break;
    case DELETE:
        request = new HttpDelete(uri);
        break;
    case HEAD:
        request = new HttpHead(uri);
        break;
    default:
        throw new IllegalStateException("Unknown HttpVerb: " + verb.toString());
    }

    for (Header header : headers) {
        request.addHeader(header);
    }

    myRequest = request;
    return myClient.execute(request);
}

From source file:org.fao.geonet.util.XslUtil.java

/**
 * Returns the HTTP code  or error message if error occurs during URL connection.
 *
 * @param url       The URL to ckeck./*from  www.  j  a v a2  s  . c  o  m*/
 * @param tryNumber the number of remaining tries.
 */
public static String getUrlStatus(String url, int tryNumber) {
    if (tryNumber < 1) {
        // protect against redirect loops
        return "ERR_TOO_MANY_REDIRECTS";
    }
    HttpHead head = new HttpHead(url);
    GeonetHttpRequestFactory requestFactory = ApplicationContextHolder.get()
            .getBean(GeonetHttpRequestFactory.class);
    ClientHttpResponse response = null;
    try {
        response = requestFactory.execute(head, new Function<HttpClientBuilder, Void>() {
            @Nullable
            @Override
            public Void apply(@Nullable HttpClientBuilder originalConfig) {
                RequestConfig.Builder config = RequestConfig.custom().setConnectTimeout(1000)
                        .setConnectionRequestTimeout(3000).setSocketTimeout(5000);
                RequestConfig requestConfig = config.build();
                originalConfig.setDefaultRequestConfig(requestConfig);

                return null;
            }
        });
        //response = requestFactory.execute(head);
        if (response.getRawStatusCode() == HttpStatus.SC_BAD_REQUEST
                || response.getRawStatusCode() == HttpStatus.SC_METHOD_NOT_ALLOWED
                || response.getRawStatusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            // the website doesn't support HEAD requests. Need to do a GET...
            response.close();
            HttpGet get = new HttpGet(url);
            response = requestFactory.execute(get);
        }

        if (response.getStatusCode().is3xxRedirection() && response.getHeaders().containsKey("Location")) {
            // follow the redirects
            return getUrlStatus(response.getHeaders().getFirst("Location"), tryNumber - 1);
        }

        return String.valueOf(response.getRawStatusCode());
    } catch (IOException e) {
        Log.error(Geonet.GEONETWORK, "IOException validating  " + url + " URL. " + e.getMessage(), e);
        return e.getMessage();
    } finally {
        if (response != null) {
            response.close();
        }
    }
}

From source file:com.google.wireless.speed.speedometer.measurements.PingTask.java

/** 
 * Use the HTTP Head method to emulate ping. The measurement from this method can be 
 * substantially (2x) greater than the first two methods and inaccurate. This is because, 
 * depending on the implementing of the destination web server, either a quick HTTP
 * response is replied or some actual heavy lifting will be done in preparing the response
 * *//*from   w  w w . j a  v  a  2  s.  c o  m*/
private MeasurementResult executeHttpPingTask() throws MeasurementError {
    long pingStartTime = 0;
    long pingEndTime = 0;
    ArrayList<Double> rrts = new ArrayList<Double>();
    PingDesc pingTask = (PingDesc) this.measurementDesc;
    String errorMsg = "";
    MeasurementResult result = null;

    try {
        long totalPingDelay = 0;

        HttpClient client = AndroidHttpClient.newInstance(Util.prepareUserAgent(this.parent));
        HttpHead headMethod = new HttpHead("http://" + targetIp);
        headMethod.addHeader(new BasicHeader("Connection", "close"));
        headMethod.setParams(new BasicHttpParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000));

        int timeOut = (int) (1000 * (double) pingTask.pingTimeoutSec / Config.PING_COUNT_PER_MEASUREMENT);
        HttpConnectionParams.setConnectionTimeout(headMethod.getParams(), timeOut);

        for (int i = 0; i < Config.PING_COUNT_PER_MEASUREMENT; i++) {
            pingStartTime = System.currentTimeMillis();
            HttpResponse response = client.execute(headMethod);
            pingEndTime = System.currentTimeMillis();
            rrts.add((double) (pingEndTime - pingStartTime));
            this.progress = 100 * i / Config.PING_COUNT_PER_MEASUREMENT;
            broadcastProgressForUser(progress);
        }
        Log.i(SpeedometerApp.TAG, "HTTP get ping succeeds");
        double packetLoss = 1 - ((double) rrts.size() / (double) Config.PING_COUNT_PER_MEASUREMENT);
        result = constructResult(rrts, packetLoss, Config.PING_COUNT_PER_MEASUREMENT);
    } catch (MalformedURLException e) {
        Log.e(SpeedometerApp.TAG, e.getMessage());
        errorMsg += e.getMessage() + "\n";
    } catch (IOException e) {
        Log.e(SpeedometerApp.TAG, e.getMessage());
        errorMsg += e.getMessage() + "\n";
    }
    if (result != null) {
        return result;
    } else {
        Log.i(SpeedometerApp.TAG, "HTTP get ping fails");
        throw new MeasurementError(errorMsg);
    }
}