Search in sources :

Example 86 with HttpHead

use of org.apache.http.client.methods.HttpHead in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method sendHttpHEADRequest.

/**
 * Send HTTP HEAD request to test the endpoint url
 *
 * @param urlVal url for which the HEAD request is sent
 * @return ApiEndpointValidationResponseDTO Response DTO containing validity information of the HEAD request made
 * to test the endpoint url
 */
public static ApiEndpointValidationResponseDTO sendHttpHEADRequest(String urlVal) throws APIManagementException {
    ApiEndpointValidationResponseDTO apiEndpointValidationResponseDTO = new ApiEndpointValidationResponseDTO();
    org.apache.http.client.HttpClient client = APIUtil.getHttpClient(urlVal);
    HttpHead method = new HttpHead(urlVal);
    try {
        HttpResponse response = client.execute(method);
        apiEndpointValidationResponseDTO.setStatusCode(response.getStatusLine().getStatusCode());
        apiEndpointValidationResponseDTO.setStatusMessage(HttpStatus.getStatusText(response.getStatusLine().getStatusCode()));
    } catch (UnknownHostException e) {
        log.error("UnknownHostException occurred while sending the HEAD request to the given endpoint url:", e);
        apiEndpointValidationResponseDTO.setError("Unknown Host");
    } catch (IOException e) {
        log.error("Error occurred while sending the HEAD request to the given endpoint url:", e);
        apiEndpointValidationResponseDTO.setError("Connection error");
    } finally {
        method.releaseConnection();
    }
    return apiEndpointValidationResponseDTO;
}
Also used : UnknownHostException(java.net.UnknownHostException) HttpResponse(org.apache.http.HttpResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ApiEndpointValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ApiEndpointValidationResponseDTO) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead)

Example 87 with HttpHead

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

the class DefaultStoreValidator method availableSslRemoteRepository.

private ArtifactStoreValidateData availableSslRemoteRepository(CountDownLatch httpRequestsLatch, Optional<URL> remoteUrl, RemoteRepository remoteRepository) throws InterruptedException, ExecutionException, URISyntaxException {
    HashMap<String, String> errors = new HashMap<>();
    // Execute HTTP GET & HEAD requests in separate thread pool from executor service
    Future<Integer> httpGetStatus = executeGetHttp(new HttpGet(remoteUrl.get().toURI()), httpRequestsLatch);
    Future<Integer> httpHeadStatus = executeHeadHttp(new HttpHead(remoteUrl.get().toURI()), httpRequestsLatch);
    // Waiting for Http GET & HEAD Request Executor tasks to finish
    httpRequestsLatch.await();
    if (!remoteUrl.get().getProtocol().equalsIgnoreCase(StoreValidationConstants.HTTPS)) {
        errors.put(StoreValidationConstants.HTTP_PROTOCOL, remoteUrl.get().getProtocol());
    }
    // Check for Sucessfull Validation for only one http call to be successfull...
    if (httpGetStatus.get() < 400 || httpHeadStatus.get() < 400) {
        LOGGER.warn("=> Success HTTP GET and HEAD Response from Remote Repository: " + remoteUrl.get());
        errors.put(StoreValidationConstants.HTTP_GET_STATUS, httpGetStatus.get().toString());
        errors.put(StoreValidationConstants.HTTP_HEAD_STATUS, httpHeadStatus.get().toString());
        return new ArtifactStoreValidateData.Builder(remoteRepository.getKey()).setRepositoryUrl(remoteUrl.get().toExternalForm()).setValid(true).setErrors(errors).build();
    } else {
        LOGGER.warn("=> Failure @ HTTP GET and HEAD Response from Remote Repository: " + remoteUrl.get());
        errors.put(StoreValidationConstants.HTTP_GET_STATUS, httpGetStatus.get().toString());
        errors.put(StoreValidationConstants.HTTP_HEAD_STATUS, httpHeadStatus.get().toString());
        return new ArtifactStoreValidateData.Builder(remoteRepository.getKey()).setRepositoryUrl(remoteUrl.get().toExternalForm()).setValid(false).setErrors(errors).build();
    }
}
Also used : HashMap(java.util.HashMap) HttpGet(org.apache.http.client.methods.HttpGet) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpHead(org.apache.http.client.methods.HttpHead)

Example 88 with HttpHead

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

the class ElasticsearchIndexTest method indexExists.

private boolean indexExists(String name) throws IOException {
    final CloseableHttpResponse response = httpClient.execute(host, new HttpHead(name));
    final boolean exists = response.getStatusLine().getStatusCode() == 200;
    IOUtils.closeQuietly(response);
    return exists;
}
Also used : CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpHead(org.apache.http.client.methods.HttpHead)

Example 89 with HttpHead

use of org.apache.http.client.methods.HttpHead in project undertow by undertow-io.

the class HeadBlockingExchangeTestCase method sendHttpHead.

@Test
public void sendHttpHead() throws IOException {
    HttpHead head = new HttpHead(DefaultServer.getDefaultServerURL());
    TestHttpClient client = new TestHttpClient();
    try {
        HttpResponse result = client.execute(head);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("", HttpClientUtils.readResponse(result));
        Assert.assertEquals("100", result.getFirstHeader("Content-Length").getValue());
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 90 with HttpHead

use of org.apache.http.client.methods.HttpHead in project undertow by undertow-io.

the class HeadTestCase method sendHttpHead.

@Test
public void sendHttpHead() throws IOException {
    connection = null;
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
    HttpHead head = new HttpHead(DefaultServer.getDefaultServerURL() + "/path");
    TestHttpClient client = new TestHttpClient();
    try {
        generateMessage(1);
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(message, HttpClientUtils.readResponse(result));
        result = client.execute(head);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("", HttpClientUtils.readResponse(result));
        generateMessage(1000);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(message, HttpClientUtils.readResponse(result));
        result = client.execute(head);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals("", HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) HttpHead(org.apache.http.client.methods.HttpHead) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

HttpHead (org.apache.http.client.methods.HttpHead)104 HttpResponse (org.apache.http.HttpResponse)42 HttpGet (org.apache.http.client.methods.HttpGet)30 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)27 IOException (java.io.IOException)26 HttpPut (org.apache.http.client.methods.HttpPut)24 Test (org.junit.Test)24 HttpPost (org.apache.http.client.methods.HttpPost)23 URI (java.net.URI)22 Header (org.apache.http.Header)21 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)16 HttpDelete (org.apache.http.client.methods.HttpDelete)14 InputStream (java.io.InputStream)13 HttpEntity (org.apache.http.HttpEntity)11 File (java.io.File)9 HttpOptions (org.apache.http.client.methods.HttpOptions)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpPatch (org.apache.http.client.methods.HttpPatch)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6