Search in sources :

Example 1 with HttpHead

use of org.apache.http.client.methods.HttpHead in project elasticsearch by elastic.

the class RestClientSingleHostTests method performRandomRequest.

private HttpUriRequest performRandomRequest(String method) throws Exception {
    String uriAsString = "/" + randomStatusCode(getRandom());
    URIBuilder uriBuilder = new URIBuilder(uriAsString);
    final Map<String, String> params = new HashMap<>();
    boolean hasParams = randomBoolean();
    if (hasParams) {
        int numParams = randomIntBetween(1, 3);
        for (int i = 0; i < numParams; i++) {
            String paramKey = "param-" + i;
            String paramValue = randomAsciiOfLengthBetween(3, 10);
            params.put(paramKey, paramValue);
            uriBuilder.addParameter(paramKey, paramValue);
        }
    }
    if (randomBoolean()) {
        //randomly add some ignore parameter, which doesn't get sent as part of the request
        String ignore = Integer.toString(randomFrom(RestClientTestUtil.getAllErrorStatusCodes()));
        if (randomBoolean()) {
            ignore += "," + Integer.toString(randomFrom(RestClientTestUtil.getAllErrorStatusCodes()));
        }
        params.put("ignore", ignore);
    }
    URI uri = uriBuilder.build();
    HttpUriRequest request;
    switch(method) {
        case "DELETE":
            request = new HttpDeleteWithEntity(uri);
            break;
        case "GET":
            request = new HttpGetWithEntity(uri);
            break;
        case "HEAD":
            request = new HttpHead(uri);
            break;
        case "OPTIONS":
            request = new HttpOptions(uri);
            break;
        case "PATCH":
            request = new HttpPatch(uri);
            break;
        case "POST":
            request = new HttpPost(uri);
            break;
        case "PUT":
            request = new HttpPut(uri);
            break;
        case "TRACE":
            request = new HttpTrace(uri);
            break;
        default:
            throw new UnsupportedOperationException("method not supported: " + method);
    }
    HttpEntity entity = null;
    boolean hasBody = request instanceof HttpEntityEnclosingRequest && getRandom().nextBoolean();
    if (hasBody) {
        entity = new StringEntity(randomAsciiOfLengthBetween(10, 100), ContentType.APPLICATION_JSON);
        ((HttpEntityEnclosingRequest) request).setEntity(entity);
    }
    Header[] headers = new Header[0];
    final Set<String> uniqueNames = new HashSet<>();
    if (randomBoolean()) {
        headers = RestClientTestUtil.randomHeaders(getRandom(), "Header");
        for (Header header : headers) {
            request.addHeader(header);
            uniqueNames.add(header.getName());
        }
    }
    for (Header defaultHeader : defaultHeaders) {
        // request level headers override default headers
        if (uniqueNames.contains(defaultHeader.getName()) == false) {
            request.addHeader(defaultHeader);
        }
    }
    try {
        if (hasParams == false && hasBody == false && randomBoolean()) {
            restClient.performRequest(method, uriAsString, headers);
        } else if (hasBody == false && randomBoolean()) {
            restClient.performRequest(method, uriAsString, params, headers);
        } else {
            restClient.performRequest(method, uriAsString, params, entity, headers);
        }
    } catch (ResponseException e) {
    //all good
    }
    return request;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) HttpOptions(org.apache.http.client.methods.HttpOptions) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HashSet(java.util.HashSet) URIBuilder(org.apache.http.client.utils.URIBuilder) HttpTrace(org.apache.http.client.methods.HttpTrace) Header(org.apache.http.Header)

Example 2 with HttpHead

use of org.apache.http.client.methods.HttpHead in project elasticsearch by elastic.

the class RestClient method createHttpRequest.

private static HttpRequestBase createHttpRequest(String method, URI uri, HttpEntity entity) {
    switch(method.toUpperCase(Locale.ROOT)) {
        case HttpDeleteWithEntity.METHOD_NAME:
            return addRequestBody(new HttpDeleteWithEntity(uri), entity);
        case HttpGetWithEntity.METHOD_NAME:
            return addRequestBody(new HttpGetWithEntity(uri), entity);
        case HttpHead.METHOD_NAME:
            return addRequestBody(new HttpHead(uri), entity);
        case HttpOptions.METHOD_NAME:
            return addRequestBody(new HttpOptions(uri), entity);
        case HttpPatch.METHOD_NAME:
            return addRequestBody(new HttpPatch(uri), entity);
        case HttpPost.METHOD_NAME:
            HttpPost httpPost = new HttpPost(uri);
            addRequestBody(httpPost, entity);
            return httpPost;
        case HttpPut.METHOD_NAME:
            return addRequestBody(new HttpPut(uri), entity);
        case HttpTrace.METHOD_NAME:
            return addRequestBody(new HttpTrace(uri), entity);
        default:
            throw new UnsupportedOperationException("http method not supported: " + method);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpTrace(org.apache.http.client.methods.HttpTrace) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpHead(org.apache.http.client.methods.HttpHead) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut)

Example 3 with HttpHead

use of org.apache.http.client.methods.HttpHead in project OpenAttestation by OpenAttestation.

the class SslUtil method getServerCertificates.

public static X509Certificate[] getServerCertificates(URL url) throws NoSuchAlgorithmException, KeyManagementException, IOException {
    if (!"https".equals(url.getProtocol())) {
        throw new IllegalArgumentException("URL scheme must be https");
    }
    int port = url.getPort();
    if (port == -1) {
        port = 443;
    }
    X509HostnameVerifier hostnameVerifier = new NopX509HostnameVerifierApache();
    CertificateStoringX509TrustManager trustManager = new CertificateStoringX509TrustManager();
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new X509TrustManager[] { trustManager }, null);
    SSLSocketFactory sf = new SSLSocketFactory(sslcontext, hostnameVerifier);
    Scheme https = new Scheme("https", port, sf);
    SchemeRegistry sr = new SchemeRegistry();
    sr.register(https);
    BasicClientConnectionManager connectionManager = new BasicClientConnectionManager(sr);
    HttpParams httpParams = new BasicHttpParams();
    httpParams.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    HttpClient httpClient = new DefaultHttpClient(connectionManager, httpParams);
    log.debug("Saving certificates from server URL: {}", url.toExternalForm());
    HttpHead request = new HttpHead(url.toExternalForm());
    HttpResponse response = httpClient.execute(request);
    log.debug("Server status line: {} {} ({})", new String[] { response.getProtocolVersion().getProtocol(), response.getStatusLine().getReasonPhrase(), String.valueOf(response.getStatusLine().getStatusCode()) });
    httpClient.getConnectionManager().shutdown();
    return trustManager.getStoredCertificates();
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) HttpResponse(org.apache.http.HttpResponse) SSLContext(javax.net.ssl.SSLContext) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpHead(org.apache.http.client.methods.HttpHead) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicClientConnectionManager(org.apache.http.impl.conn.BasicClientConnectionManager) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 4 with HttpHead

use of org.apache.http.client.methods.HttpHead in project lucene-solr by apache.

the class SolrCLI method attemptHttpHead.

/**
   * Tries a simple HEAD request and throws SolrException in case of Authorization error
   * @param url the url to do a HEAD request to
   * @param httpClient the http client to use (make sure it has authentication optinos set)
   * @return the HTTP response code
   * @throws SolrException if auth/autz problems
   * @throws IOException if connection failure
   */
private static int attemptHttpHead(String url, HttpClient httpClient) throws SolrException, IOException {
    HttpResponse response = httpClient.execute(new HttpHead(url), HttpClientUtil.createNewHttpClientRequestContext());
    int code = response.getStatusLine().getStatusCode();
    if (code == UNAUTHORIZED.code || code == FORBIDDEN.code) {
        throw new SolrException(SolrException.ErrorCode.getErrorCode(code), "Solr requires authentication for " + url + ". Please supply valid credentials. HTTP code=" + code);
    }
    return code;
}
Also used : HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead) SolrException(org.apache.solr.common.SolrException)

Example 5 with HttpHead

use of org.apache.http.client.methods.HttpHead in project lucene-solr by apache.

the class CacheHeaderTestBase method getUpdateMethod.

protected HttpRequestBase getUpdateMethod(String method, String... params) throws URISyntaxException {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    HttpRequestBase m = null;
    ArrayList<BasicNameValuePair> qparams = new ArrayList<>();
    for (int i = 0; i < params.length / 2; i++) {
        qparams.add(new BasicNameValuePair(params[i * 2], params[i * 2 + 1]));
    }
    URI uri = URI.create(client.getBaseURL() + "/update?" + URLEncodedUtils.format(qparams, StandardCharsets.UTF_8));
    if ("GET".equals(method)) {
        m = new HttpGet(uri);
    } else if ("POST".equals(method)) {
        m = new HttpPost(uri);
    } else if ("HEAD".equals(method)) {
        m = new HttpHead(uri);
    }
    return m;
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)102 HttpResponse (org.apache.http.HttpResponse)41 HttpGet (org.apache.http.client.methods.HttpGet)28 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)27 IOException (java.io.IOException)24 Test (org.junit.Test)24 URI (java.net.URI)22 HttpPut (org.apache.http.client.methods.HttpPut)22 Header (org.apache.http.Header)21 HttpPost (org.apache.http.client.methods.HttpPost)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)15 HttpDelete (org.apache.http.client.methods.HttpDelete)13 InputStream (java.io.InputStream)12 HttpEntity (org.apache.http.HttpEntity)10 File (java.io.File)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpOptions (org.apache.http.client.methods.HttpOptions)8 URISyntaxException (java.net.URISyntaxException)6 URL (java.net.URL)6