org.eclipse.jetty.client.HttpClient Java Examples

The following examples show how to use org.eclipse.jetty.client.HttpClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloudStoreTest.java    From athenz with Apache License 2.0 7 votes vote down vote up
@Test
public void testFetchRoleCredentialInvalidCreds() throws InterruptedException, ExecutionException, TimeoutException {
    
    CloudStore store = new CloudStore();
    store.awsRole = "athenz.zts";
    
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(response.getContentAsString()).thenReturn("invalid-creds");

    store.setHttpClient(httpClient);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/meta-data/iam/security-credentials/athenz.zts")).thenReturn(response);
    
    assertFalse(store.fetchRoleCredentials());
    store.close();
}
 
Example #2
Source File: CloudStoreTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testGetMetaDataExceptions() throws InterruptedException, ExecutionException, TimeoutException {
    
    CloudStore store = new CloudStore();
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    store.setHttpClient(httpClient);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/exc1")).thenThrow(InterruptedException.class);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/exc2")).thenThrow(ExecutionException.class);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/exc3")).thenThrow(TimeoutException.class);
    
    assertNull(store.getMetaData("/exc1"));
    assertNull(store.getMetaData("/exc2"));
    assertNull(store.getMetaData("/exc3"));
    store.close();
}
 
Example #3
Source File: CloudStoreTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testFetchRoleCredential() throws InterruptedException, ExecutionException, TimeoutException {

    CloudStore store = new CloudStore();
    store.awsRole = "athenz.zts";

    HttpClient httpClient = Mockito.mock(HttpClient.class);
    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(response.getContentAsString()).thenReturn("{\"AccessKeyId\":\"id\",\"SecretAccessKey\":\"key\",\"Token\":\"token\"}");

    store.setHttpClient(httpClient);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/meta-data/iam/security-credentials/athenz.zts")).thenReturn(response);

    assertTrue(store.fetchRoleCredentials());
    store.close();
}
 
Example #4
Source File: WebClientFactoryImplTest.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
@Ignore("connecting to the outside world makes this test flaky")
public void testCommonClientUsesExtensibleTrustManager() throws Exception {
    ArgumentCaptor<X509Certificate[]> certificateChainCaptor = ArgumentCaptor.forClass(X509Certificate[].class);
    ArgumentCaptor<SSLEngine> sslEngineCaptor = ArgumentCaptor.forClass(SSLEngine.class);
    HttpClient httpClient = webClientFactory.getCommonHttpClient();

    ContentResponse response = httpClient.GET(TEST_URL);
    if (response.getStatus() != 200) {
        fail("Statuscode != 200");
    }

    verify(extensibleTrustManager).checkServerTrusted(certificateChainCaptor.capture(), anyString(),
            sslEngineCaptor.capture());
    verifyNoMoreInteractions(extensibleTrustManager);
    assertThat(sslEngineCaptor.getValue().getPeerHost(), is("www.eclipse.org"));
    assertThat(sslEngineCaptor.getValue().getPeerPort(), is(443));
    assertThat(certificateChainCaptor.getValue()[0].getSubjectX500Principal().getName(),
            containsString("eclipse.org"));
}
 
Example #5
Source File: HttpCertSignerTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateX509CertificateResponseNull() throws Exception {

    HttpClient httpClient = Mockito.mock(HttpClient.class);

    HttpCertSignerFactory certFactory = new HttpCertSignerFactory();
    HttpCertSigner certSigner = (HttpCertSigner) certFactory.create();
    certSigner.setHttpClient(httpClient);

    Request request = Mockito.mock(Request.class);
    Mockito.when(httpClient.POST("https://localhost:443/certsign/v2/x509")).thenReturn(request);

    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(request.send()).thenReturn(response);
    Mockito.when(response.getStatus()).thenReturn(201);
    Mockito.when(response.getContentAsString()).thenReturn(null);

    assertNull(certSigner.generateX509Certificate("csr", null, 0));
    certSigner.close();
}
 
Example #6
Source File: DynRealmITCase.java    From syncope with Apache License 2.0 6 votes vote down vote up
private static ArrayNode fetchDynRealmsFromElasticsearch(final String userKey) throws Exception {
    String body =
        '{'
            + "    \"query\": {"
            + "        \"match\": {\"_id\": \"" + userKey + "\"}"
            + "    }"
            + '}';

    HttpClient httpClient = new HttpClient();
    httpClient.start();
    ContentResponse response = httpClient.newRequest("http://localhost:9200/master_user/_search").
            method(HttpMethod.GET).
            header(HttpHeader.CONTENT_TYPE, MediaType.APPLICATION_JSON).
            content(new InputStreamContentProvider(IOUtils.toInputStream(body))).
            send();
    assertEquals(HttpStatus.OK_200, response.getStatus());

    return (ArrayNode) OBJECT_MAPPER.readTree(response.getContent()).
            get("hits").get("hits").get(0).get("_source").get("dynRealms");
}
 
Example #7
Source File: HttpServerTest.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Test
public void requireThatJdiscLocalPortPropertyIsNotOverriddenByProxyProtocol() throws Exception {
    Path privateKeyFile = tmpFolder.newFile().toPath();
    Path certificateFile = tmpFolder.newFile().toPath();
    generatePrivateKeyAndCertificate(privateKeyFile, certificateFile);
    AccessLogMock accessLogMock = new AccessLogMock();
    TestDriver driver = createSslWithProxyProtocolTestDriver(certificateFile, privateKeyFile, accessLogMock, /*mixedMode*/false);
    HttpClient client = createJettyHttpClient(certificateFile);

    String proxiedRemoteAddress = "192.168.0.100";
    int proxiedRemotePort = 12345;
    String proxyLocalAddress = "10.0.0.10";
    int proxyLocalPort = 23456;
    V2.Tag v2Tag = new V2.Tag(V2.Tag.Command.PROXY, null, V2.Tag.Protocol.STREAM,
                              proxiedRemoteAddress, proxiedRemotePort, proxyLocalAddress, proxyLocalPort, null);
    ContentResponse response = sendJettyClientRequest(driver, client, v2Tag);
    client.stop();
    assertThat(driver.close(), is(true));

    int clientPort = Integer.parseInt(response.getHeaders().get("Jdisc-Local-Port"));
    assertNotEquals(proxyLocalPort, clientPort);
}
 
Example #8
Source File: RegistrationMonitorWorkerIntegrationTest.java    From ja-micro with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
    HttpClient httpClient = mock(HttpClient.class);
    response = mock(ContentResponse.class);
    when(response.getStatus()).thenReturn(200);
    when(response.getContentAsString()).thenReturn(healthInfo);
    HttpFields headers = new HttpFields();
    headers.add(CONSUL_INDEX, "42");
    when(response.getHeaders()).thenReturn(headers);
    Request request = mock(Request.class);
    when(httpClient.newRequest(anyString())).thenReturn(request);
    when(request.send()).thenReturn(response);
    props = new ServiceProperties();
    props.addProperty(ServiceProperties.REGISTRY_SERVER_KEY, "localhost:1234");
    worker = new RegistrationMonitorWorker(httpClient, props, mock(ServiceDependencyHealthCheck.class));
    worker.setServiceName("foobar");
}
 
Example #9
Source File: HttpCertSignerTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCACertificateResponseEmpty() throws Exception {

    HttpClient httpClient = Mockito.mock(HttpClient.class);

    HttpCertSignerFactory certFactory = new HttpCertSignerFactory();
    HttpCertSigner certSigner = (HttpCertSigner) certFactory.create();
    certSigner.setHttpClient(httpClient);

    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(httpClient.GET("https://localhost:443/certsign/v2/x509")).thenReturn(response);
    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(response.getContentAsString()).thenReturn("");

    assertNull(certSigner.getCACertificate());
    certSigner.close();
}
 
Example #10
Source File: RandomServiceIntegrationTest.java    From ja-micro with Apache License 2.0 6 votes vote down vote up
@Test
public void testHealthCheckOutputFormat() throws Exception {
    String failureMessage = "I'm a failure!";
    SetHealthCheckStatusCommand command = SetHealthCheckStatusCommand.newBuilder()
            .setStatus(HealthCheck.Status.FAIL.toString()).setMessage(failureMessage).build();
    //we have to work around the normal communication channels, as the load-balancer
    //won't let us talk to a failing instance
    LoadBalancer loadBalancer = ServiceIntegrationTestSuite.testService.getLoadBalancer();
    ServiceEndpoint endpoint = loadBalancer.getHealthyInstance();
    ServiceIntegrationTestSuite.testService.sendRequest("TestService.SetHealthCheckStatus", command);
    String url = "http://" + endpoint.getHostAndPort() + "/health";
    HttpClient httpClient = new HttpClient();
    httpClient.start();
    String response = httpClient.GET(url).getContentAsString();
    assertThat(response).contains("{\"name\":\"test_servlet\",\"status\":\"CRITICAL\",\"reason\":\"I'm a failure!\"}");
}
 
Example #11
Source File: HttpServerTest.java    From vespa with Apache License 2.0 6 votes vote down vote up
private ContentResponse sendJettyClientRequest(TestDriver testDriver, HttpClient client, Object tag)
        throws InterruptedException, TimeoutException {
    int maxAttempts = 3;
    for (int attempt = 0; attempt < maxAttempts; attempt++) {
        try {
            ContentResponse response = client.newRequest(URI.create("https://localhost:" + testDriver.server().getListenPort() + "/"))
                    .tag(tag)
                    .send();
            assertEquals(200, response.getStatus());
            return response;
        } catch (ExecutionException e) {
            // Retry when the server closes the connection before the TLS handshake is completed. This have been observed in CI.
            // We have been unable to reproduce this locally. The cause is therefor currently unknown.
            log.log(Level.WARNING, String.format("Attempt %d failed: %s", attempt, e.getMessage()), e);
            Thread.sleep(10);
        }
    }
    throw new AssertionError("Failed to send request, see log for details");
}
 
Example #12
Source File: CloudStoreTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadBootMetaDataInvalidIamInfoGet() throws InterruptedException, ExecutionException, TimeoutException {
    
    CloudStore store = new CloudStore();
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    
    ContentResponse responseDoc = Mockito.mock(ContentResponse.class);
    Mockito.when(responseDoc.getStatus()).thenReturn(200);
    Mockito.when(responseDoc.getContentAsString()).thenReturn(AWS_INSTANCE_DOCUMENT);
    
    ContentResponse responseSig = Mockito.mock(ContentResponse.class);
    Mockito.when(responseSig.getStatus()).thenReturn(200);
    Mockito.when(responseSig.getContentAsString()).thenReturn("pkcs7-signature");
    
    ContentResponse responseInfo = Mockito.mock(ContentResponse.class);
    Mockito.when(responseInfo.getStatus()).thenReturn(404);
    
    store.setHttpClient(httpClient);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/dynamic/instance-identity/document")).thenReturn(responseDoc);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/dynamic/instance-identity/pkcs7")).thenReturn(responseSig);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/meta-data/iam/info")).thenReturn(responseInfo);

    assertFalse(store.loadBootMetaData());
    store.close();
}
 
Example #13
Source File: HttpCertSignerTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCACertificateInvalidStatus() throws Exception {

    HttpClient httpClient = Mockito.mock(HttpClient.class);

    HttpCertSignerFactory certFactory = new HttpCertSignerFactory();
    HttpCertSigner certSigner = (HttpCertSigner) certFactory.create();
    certSigner.setHttpClient(httpClient);

    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(httpClient.GET("https://localhost:443/certsign/v2/x509")).thenReturn(response);
    Mockito.when(response.getStatus()).thenReturn(400);

    assertNull(certSigner.getCACertificate());
    certSigner.close();
}
 
Example #14
Source File: HttpCertSignerTest.java    From athenz with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCACertificate() throws Exception {

    HttpClient httpClient = Mockito.mock(HttpClient.class);

    HttpCertSignerFactory certFactory = new HttpCertSignerFactory();
    HttpCertSigner certSigner = (HttpCertSigner) certFactory.create();
    certSigner.setHttpClient(httpClient);

    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(httpClient.GET("https://localhost:443/certsign/v2/x509")).thenReturn(response);
    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(response.getContentAsString()).thenReturn("{\"pem\": \"pem-value\"}");

    String pem = certSigner.getCACertificate();
    assertEquals(pem, "pem-value");
    certSigner.close();
}
 
Example #15
Source File: CloudStoreTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMetaDataValidResponse() throws InterruptedException, ExecutionException, TimeoutException {
    
    CloudStore store = new CloudStore();
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(response.getContentAsString()).thenReturn("json-document");
    store.setHttpClient(httpClient);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/iam-info")).thenReturn(response);

    assertEquals(store.getMetaData("/iam-info"), "json-document");
    store.close();
}
 
Example #16
Source File: StreamClientImpl.java    From DroidDLNA with GNU General Public License v3.0 5 votes vote down vote up
public HttpContentExchange(StreamClientConfigurationImpl configuration,
                           HttpClient client,
                           StreamRequestMessage requestMessage) {
    super(true);
    this.configuration = configuration;
    this.client = client;
    this.requestMessage = requestMessage;
    applyRequestURLMethod();
    applyRequestHeaders();
    applyRequestBody();
}
 
Example #17
Source File: CloudStoreTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMetaDataEmptyResponse() throws InterruptedException, ExecutionException, TimeoutException {
    
    CloudStore store = new CloudStore();
    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();
}
 
Example #18
Source File: JettyClientFactory.java    From moon-api-gateway with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(jettyClientConfig.getThreadCount());
    httpClient = new HttpClient();
    try {
        httpClient.setMaxConnectionsPerDestination(jettyClientConfig.getMaxConnection());
        httpClient.setConnectTimeout(jettyClientConfig.getTimeout());
        httpClient.setExecutor(executor);
        httpClient.start();
    } catch(Exception e) {
        e.printStackTrace();
    }
}
 
Example #19
Source File: CloudStoreTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadBootMetaDataInvalidDocumentGet() throws InterruptedException, ExecutionException, TimeoutException {
    
    CloudStore store = new CloudStore();
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    ContentResponse response = Mockito.mock(ContentResponse.class);
    Mockito.when(response.getStatus()).thenReturn(404);
    store.setHttpClient(httpClient);
    Mockito.when(httpClient.GET("http://169.254.169.254/latest/dynamic/instance-identity/document")).thenReturn(response);
    
    assertFalse(store.loadBootMetaData());
    store.close();
}
 
Example #20
Source File: WebClientFactoryTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGetHttpClientWithEndpoint() throws Exception {
    when(trustmanagerProvider.getTrustManagers("https://www.heise.de")).thenReturn(Stream.empty());

    HttpClient httpClient = webClientFactory.createHttpClient("consumer", TEST_URL);

    assertThat(httpClient, is(notNullValue()));
    verify(trustmanagerProvider).getTrustManagers(TEST_URL);
    httpClient.stop();
}
 
Example #21
Source File: CloudStore.java    From athenz with Apache License 2.0 5 votes vote down vote up
void setupHttpClient(HttpClient client) {

        client.setFollowRedirects(false);
        client.setStopTimeout(1000);
        try {
            client.start();
        } catch (Exception ex) {
            LOGGER.error("CloudStore: unable to start http client", ex);
            throw new ResourceException(ResourceException.INTERNAL_SERVER_ERROR,
                    "Http client not available");
        }
    }
 
Example #22
Source File: HttpOperatorFactory.java    From digdag with Apache License 2.0 5 votes vote down vote up
void stop(HttpClient httpClient)
{
    try {
        httpClient.stop();
    }
    catch (Exception e) {
        logger.warn("Failed to stop http client", e);
    }
}
 
Example #23
Source File: HttpCertSigner.java    From athenz with Apache License 2.0 5 votes vote down vote up
void setupHttpClient(HttpClient client, long requestTimeout, long connectTimeout) {

        client.setFollowRedirects(false);
        client.setConnectTimeout(connectTimeout);
        client.setStopTimeout(TimeUnit.MILLISECONDS.convert(requestTimeout, TimeUnit.SECONDS));
        try {
            client.start();
        } catch (Exception ex) {
            LOGGER.error("HttpCertSigner: unable to start http client", ex);
            throw new ResourceException(ResourceException.INTERNAL_SERVER_ERROR,
                    "Http client not available");
        }
    }
 
Example #24
Source File: HomematicGatewayFactory.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Loads some metadata about the type of the Homematic gateway.
 */
private static void loadGatewayInfo(HomematicConfig config, String id, HttpClient httpClient) throws IOException {
    RpcClient<String> rpcClient = new XmlRpcClient(config, httpClient);
    try {
        config.setGatewayInfo(rpcClient.getGatewayInfo(id));
    } finally {
        rpcClient.dispose();
    }
}
 
Example #25
Source File: ProxyServlet.java    From secrets-proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Async http client used to connect to <b>proxyTo</b> server. TLS config is usually done here.
 *
 * @return {@link HttpClient}
 */
@Override
protected HttpClient newHttpClient() {
  SslContextFactory sslFactory = new SslContextFactory();
  sslFactory.setTrustAll(trustAllCerts);
  return new HttpClient(sslFactory);
}
 
Example #26
Source File: HttpRpcEndpoint.java    From nutzcloud with Apache License 2.0 5 votes vote down vote up
public void init() throws Exception {
    client = new HttpClient(new SslContextFactory(true));
    client.setFollowRedirects(false);
    client.setCookieStore(new HttpCookieStore.Empty());

    executor = new QueuedThreadPool(conf.getInt(PRE + ".maxThreads", 256));
    client.setExecutor(executor);
    client.setMaxConnectionsPerDestination(conf.getInt(PRE + ".maxConnections", 256));
    client.setIdleTimeout(conf.getLong(PRE + ".idleTimeout", 30000));

    client.setConnectTimeout(conf.getLong(PRE + ".connectTime", 1000));

    if (conf.has(PRE + "requestBufferSize"))
        client.setRequestBufferSize(conf.getInt(PRE + "requestBufferSize"));

    if (conf.has(PRE + "responseBufferSize"))
        client.setResponseBufferSize(conf.getInt(PRE + "responseBufferSize"));

    client.start();

    // Content must not be decoded, otherwise the client gets confused.
    client.getContentDecoderFactories().clear();

    // Pass traffic to the client, only intercept what's necessary.
    ProtocolHandlers protocolHandlers = client.getProtocolHandlers();
    protocolHandlers.clear();
}
 
Example #27
Source File: LoadBalancerImplTest.java    From ja-micro with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    ServiceProperties properties = new ServiceProperties();
    HttpClient httpClient = mock(HttpClient.class);
    HttpClientWrapper wrapper = new HttpClientWrapper(properties, httpClient, null, null);
    lb = new LoadBalancerImpl(properties, wrapper);
    dependencyHealthCheck = mock(ServiceDependencyHealthCheck.class);
}
 
Example #28
Source File: RegistrationManager.java    From ja-micro with Apache License 2.0 5 votes vote down vote up
@Inject
public RegistrationManager(ServiceProperties serviceProps,
                           HttpClient httpClient) {
    this.serviceProps = serviceProps;
    this.httpClient = httpClient;
    serviceId = serviceProps.getServiceInstanceId();
    serviceName = serviceProps.getServiceName();
}
 
Example #29
Source File: RegistrationMonitorWorker.java    From ja-micro with Apache License 2.0 5 votes vote down vote up
@Inject
public RegistrationMonitorWorker(HttpClient httpClient,
                                 ServiceProperties serviceProps,
                                 ServiceDependencyHealthCheck dependencyHealthCheck) {
    this.httpClient = httpClient;
    this.serviceProps = serviceProps;
    this.discoveredServices = new LinkedHashMap<>();
    this.dependencyHealthCheck = dependencyHealthCheck;
}
 
Example #30
Source File: AsyncRestServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void init(ServletConfig servletConfig) throws ServletException {
  super.init(servletConfig);
  SslContextFactory sslContextFactory = new SslContextFactory();
  client = new HttpClient(sslContextFactory);

  try {
    client.start();
  } catch (Exception e) {
    throw new ServletException(e);
  }
}