Search in sources :

Example 91 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project athenz by yahoo.

the class CloudStoreTest method testGetMetaDataEmptyResponse.

@Test
public void testGetMetaDataEmptyResponse() throws InterruptedException, ExecutionException, TimeoutException {
    CloudStore store = new CloudStore(null);
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(response.getContentAsString()).thenReturn("");
    store.setHttpClient(httpClient);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/iam-info")).thenReturn(response);
    assertNull(store.getMetaData("/iam-info"));
    store.close();
}
Also used : CloudStore(com.yahoo.athenz.zts.store.CloudStore) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.testng.annotations.Test)

Example 92 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project smarthome by eclipse.

the class SecureHttpClientFactoryTest method testGetClient.

@Test
public void testGetClient() throws Exception {
    secureHttpClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
    HttpClient client = secureHttpClientFactory.getCommonHttpClient();
    assertThat(client, is(notNullValue()));
    secureHttpClientFactory.deactivate();
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Example 93 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project smarthome by eclipse.

the class FSInternetRadioHandlerJavaTest method setUpClass.

@BeforeClass
public static void setUpClass() throws Exception {
    ServletHolder holder = new ServletHolder(radioServiceDummy);
    server = new TestServer(DEFAULT_CONFIG_PROPERTY_IP, DEFAULT_CONFIG_PROPERTY_PORT, TIMEOUT, holder);
    setTheChannelsMap();
    server.startServer();
    httpClient = new HttpClient();
    httpClient.start();
}
Also used : ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) BeforeClass(org.junit.BeforeClass)

Example 94 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project apache-kafka-on-k8s by banzaicloud.

the class RestClient method httpRequest.

/**
 * Sends HTTP request to remote REST server
 *
 * @param url             HTTP connection will be established with this url.
 * @param method          HTTP method ("GET", "POST", "PUT", etc.)
 * @param requestBodyData Object to serialize as JSON and send in the request body.
 * @param responseFormat  Expected format of the response to the HTTP request.
 * @param <T>             The type of the deserialized response to the HTTP request.
 * @return The deserialized response to the HTTP request, or null if no data is expected.
 */
public static <T> HttpResponse<T> httpRequest(String url, String method, Object requestBodyData, TypeReference<T> responseFormat, WorkerConfig config) {
    HttpClient client;
    if (url.startsWith("https://")) {
        client = new HttpClient(SSLUtils.createSslContextFactory(config, true));
    } else {
        client = new HttpClient();
    }
    client.setFollowRedirects(false);
    try {
        client.start();
    } catch (Exception e) {
        log.error("Failed to start RestClient: ", e);
        throw new ConnectRestException(Response.Status.INTERNAL_SERVER_ERROR, "Failed to start RestClient: " + e.getMessage(), e);
    }
    try {
        String serializedBody = requestBodyData == null ? null : JSON_SERDE.writeValueAsString(requestBodyData);
        log.trace("Sending {} with input {} to {}", method, serializedBody, url);
        Request req = client.newRequest(url);
        req.method(method);
        req.accept("application/json");
        req.agent("kafka-connect");
        if (serializedBody != null) {
            req.content(new StringContentProvider(serializedBody, StandardCharsets.UTF_8), "application/json");
        }
        ContentResponse res = req.send();
        int responseCode = res.getStatus();
        log.debug("Request's response code: {}", responseCode);
        if (responseCode == HttpStatus.NO_CONTENT_204) {
            return new HttpResponse<>(responseCode, convertHttpFieldsToMap(res.getHeaders()), null);
        } else if (responseCode >= 400) {
            ErrorMessage errorMessage = JSON_SERDE.readValue(res.getContentAsString(), ErrorMessage.class);
            throw new ConnectRestException(responseCode, errorMessage.errorCode(), errorMessage.message());
        } else if (responseCode >= 200 && responseCode < 300) {
            T result = JSON_SERDE.readValue(res.getContentAsString(), responseFormat);
            return new HttpResponse<>(responseCode, convertHttpFieldsToMap(res.getHeaders()), result);
        } else {
            throw new ConnectRestException(Response.Status.INTERNAL_SERVER_ERROR, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Unexpected status code when handling forwarded request: " + responseCode);
        }
    } catch (IOException | InterruptedException | TimeoutException | ExecutionException e) {
        log.error("IO error forwarding REST request: ", e);
        throw new ConnectRestException(Response.Status.INTERNAL_SERVER_ERROR, "IO Error trying to forward REST request: " + e.getMessage(), e);
    } finally {
        if (client != null)
            try {
                client.stop();
            } catch (Exception e) {
                log.error("Failed to stop HTTP client", e);
            }
    }
}
Also used : StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) HttpClient(org.eclipse.jetty.client.HttpClient) ErrorMessage(org.apache.kafka.connect.runtime.rest.entities.ErrorMessage) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 95 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project rpki-validator-3 by RIPE-NCC.

the class BgpRisDownloader method postConstruct.

@PostConstruct
public void postConstruct() throws Exception {
    httpClient = new HttpClient();
    httpClient.start();
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) PostConstruct(javax.annotation.PostConstruct)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)197 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)102 Test (org.junit.Test)94 Request (org.eclipse.jetty.client.api.Request)53 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)40 Test (org.testng.annotations.Test)34 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)24 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)19 CloudStore (com.yahoo.athenz.zts.store.CloudStore)17 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)16 HttpCertSigner (com.yahoo.athenz.zts.cert.impl.HttpCertSigner)14 HttpCertSignerFactory (com.yahoo.athenz.zts.cert.impl.HttpCertSignerFactory)14 URI (java.net.URI)11 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)11 ExecutionException (java.util.concurrent.ExecutionException)8 HttpProxy (org.eclipse.jetty.client.HttpProxy)8 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7