Java Code Examples for org.eclipse.jetty.client.HttpClient#stop()

The following examples show how to use org.eclipse.jetty.client.HttpClient#stop() . 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: RestfulTestsUtil.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * Send request.
 * @param url the url
 * @param method the method name
 * @param content the content
 * @return the http status code
 * @throws Exception exception when error
 */
public static int sentRequest(final String url, final String method, final String content) throws Exception {
    HttpClient httpClient = new HttpClient();
    try {
        httpClient.start();
        ContentExchange contentExchange = new ContentExchange();
        contentExchange.setMethod(method);
        contentExchange.setRequestContentType(MediaType.APPLICATION_JSON);
        contentExchange.setRequestContent(new ByteArrayBuffer(content.getBytes("UTF-8")));
        httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
        contentExchange.setURL(url);
        httpClient.send(contentExchange);
        contentExchange.waitForDone();
        return contentExchange.getResponseStatus();
    } finally {
        httpClient.stop();
    }
}
 
Example 2
Source File: RestfulTestsUtil.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * Send get request.
 * @param url the url
 * @return the http response
 * @throws Exception exception when error
 */
public static String sentGetRequest(final String url) throws Exception {
    HttpClient httpClient = new HttpClient();
    try {
        httpClient.start();
        ContentExchange contentExchange = new ContentExchange();
        contentExchange.setMethod("GET");
        contentExchange.setRequestContentType(MediaType.APPLICATION_JSON);
        httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
        contentExchange.setURL(url);
        httpClient.send(contentExchange);
        contentExchange.waitForDone();
        return contentExchange.getResponseContent();
    } finally {
        httpClient.stop();
    }
}
 
Example 3
Source File: RestfulServerTest.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 6 votes vote down vote up
private static ContentExchange sentRequest(final String content) throws Exception {
    HttpClient httpClient = new HttpClient();
    try {
        httpClient.start();
        ContentExchange result = new ContentExchange();
        result.setMethod("POST");
        result.setRequestContentType(MediaType.APPLICATION_JSON);
        result.setRequestContent(new ByteArrayBuffer(content.getBytes("UTF-8")));
        httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
        result.setURL(URL);
        httpClient.send(result);
        result.waitForDone();
        return result;
    } finally {
        httpClient.stop();
    }
}
 
Example 4
Source File: HttpServerTest.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Test
public void requireThatProxyProtocolIsAcceptedAndActualRemoteAddressStoredInAccessLog() 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;
    sendJettyClientRequest(driver, client, new V1.Tag(proxiedRemoteAddress, proxiedRemotePort));
    sendJettyClientRequest(driver, client, new V2.Tag(proxiedRemoteAddress, proxiedRemotePort));
    client.stop();
    assertThat(driver.close(), is(true));

    assertThat(accessLogMock.logEntries, hasSize(2));
    assertLogEntryHasRemote(accessLogMock.logEntries.get(0), proxiedRemoteAddress, proxiedRemotePort);
    assertLogEntryHasRemote(accessLogMock.logEntries.get(1), proxiedRemoteAddress, proxiedRemotePort);
}
 
Example 5
Source File: HttpServerTest.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Test
public void requireThatConnectorWithProxyProtocolMixedEnabledAcceptsBothProxyProtocolAndHttps() 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*/true);
    HttpClient client = createJettyHttpClient(certificateFile);

    String proxiedRemoteAddress = "192.168.0.100";
    sendJettyClientRequest(driver, client, null);
    sendJettyClientRequest(driver, client, new V2.Tag(proxiedRemoteAddress, 12345));
    client.stop();
    assertThat(driver.close(), is(true));

    assertThat(accessLogMock.logEntries, hasSize(2));
    assertLogEntryHasRemote(accessLogMock.logEntries.get(0), "127.0.0.1", 0);
    assertLogEntryHasRemote(accessLogMock.logEntries.get(1), proxiedRemoteAddress, 0);
}
 
Example 6
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 7
Source File: CommonStarter.java    From nem.deploy with MIT License 6 votes vote down vote up
private void stopOtherInstance(final URL stopURL) throws Exception {
	final HttpClient httpClient = new HttpClient();
	try {
		httpClient.start();
		LOGGER.info("Send shutdown to other instance...");
		final ContentResponse response = httpClient.GET(stopURL.toURI());
		if (response.getStatus() != HttpStatus.OK.value()) {
			LOGGER.info(String.format("Other instance returned %d: %s", response.getStatus(), response.getContentAsString()));
		} else {
			LOGGER.info("Pause 2 seconds");
			Thread.sleep(2000);
		}
	} finally {
		httpClient.stop();
	}
}
 
Example 8
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 9
Source File: RestfulTestsUtil.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * Send request.
 * @param url the url
 * @param method the method name
 * @return http status code
 * @throws Exception exception when error
 */
public static int sentRequest(final String url, final String method) throws Exception {
    HttpClient httpClient = new HttpClient();
    try {
        httpClient.start();
        ContentExchange contentExchange = new ContentExchange();
        contentExchange.setMethod(method);
        contentExchange.setURL(url);
        httpClient.send(contentExchange);
        contentExchange.waitForDone();
        return contentExchange.getResponseStatus();
    } finally {
        httpClient.stop();
    }
}
 
Example 10
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 11
Source File: JettyConnectionMetricsTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void contributesClientConnectorMetrics() throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.setFollowRedirects(false);
    httpClient.addBean(new JettyConnectionMetrics(registry));

    CountDownLatch latch = new CountDownLatch(1);
    httpClient.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {
        @Override
        public void lifeCycleStopped(LifeCycle event) {
            latch.countDown();
        }
    });

    httpClient.start();

    Request post = httpClient.POST("http://localhost:" + connector.getLocalPort());
    post.content(new StringContentProvider("123456"));
    post.send();
    httpClient.stop();

    assertTrue(latch.await(10, SECONDS));
    assertThat(registry.get("jetty.connections.max").gauge().value()).isEqualTo(1.0);
    assertThat(registry.get("jetty.connections.request").tag("type", "client").timer().count())
            .isEqualTo(1);
    assertThat(registry.get("jetty.connections.bytes.out").summary().totalAmount()).isGreaterThan(1);
}
 
Example 12
Source File: ProxyIsAHttpProxyTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    LinkedBlockingQueue<String> dataQueue = new LinkedBlockingQueue<>();
    Server streamingServer = new Server(0);
    streamingServer.setHandler(newStreamingHandler(dataQueue));
    streamingServer.start();

    Properties props = new Properties();
    props.setProperty("httpOutputBufferSize", "1");
    props.setProperty("httpReverseProxy.foobar.path", "/stream");
    props.setProperty("httpReverseProxy.foobar.proxyTo", streamingServer.getURI().toString());
    props.setProperty("servicePort", "0");
    props.setProperty("webServicePort", "0");

    ProxyConfiguration proxyConfig = PulsarConfigurationLoader.create(props, ProxyConfiguration.class);
    AuthenticationService authService = new AuthenticationService(
            PulsarConfigurationLoader.convertFrom(proxyConfig));

    WebServer webServer = new WebServer(proxyConfig, authService);
    ProxyServiceStarter.addWebServerHandlers(webServer, proxyConfig, null,
                                             new BrokerDiscoveryProvider(proxyConfig, mockZooKeeperClientFactory));
    webServer.start();

    HttpClient httpClient = new HttpClient();
    httpClient.start();
    try {
        LinkedBlockingQueue<Byte> responses = new LinkedBlockingQueue<>();
        CompletableFuture<Result> promise = new CompletableFuture<>();
        httpClient.newRequest(webServer.getServiceUri()).path("/stream")
            .onResponseContent((response, content) -> {
                    while (content.hasRemaining()) {
                        try {
                            responses.put(content.get());
                        } catch (Exception e) {
                            log.error("Error reading response", e);
                            promise.completeExceptionally(e);
                        }
                    }
                })
            .send((result) -> {
                    log.info("Response complete");
                    promise.complete(result);
                });

        dataQueue.put("Some data");
        assertEventuallyTrue(() -> responses.size() == "Some data".length());
        Assert.assertEquals("Some data", drainToString(responses));
        Assert.assertFalse(promise.isDone());

        dataQueue.put("More data");
        assertEventuallyTrue(() -> responses.size() == "More data".length());
        Assert.assertEquals("More data", drainToString(responses));
        Assert.assertFalse(promise.isDone());

        dataQueue.put("DONE");
        assertEventuallyTrue(() -> promise.isDone());
        Assert.assertTrue(promise.get().isSucceeded());
    } finally {
        webServer.stop();
        httpClient.stop();
        streamingServer.stop();
    }
}
 
Example 13
Source File: LoginHelper.java    From EMP-Connector with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static BayeuxParameters login(URL loginEndpoint, String username, String password,
        BayeuxParameters parameters) throws Exception {
    HttpClient client = new HttpClient(parameters.sslContextFactory());
    try {
        client.getProxyConfiguration().getProxies().addAll(parameters.proxies());
        client.start();
        URL endpoint = new URL(loginEndpoint, getSoapUri());
        Request post = client.POST(endpoint.toURI());
        post.content(new ByteBufferContentProvider("text/xml", ByteBuffer.wrap(soapXmlForLogin(username, password))));
        post.header("SOAPAction", "''");
        post.header("PrettyPrint", "Yes");
        ContentResponse response = post.send();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
        spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();

        LoginResponseParser parser = new LoginResponseParser();
        saxParser.parse(new ByteArrayInputStream(response.getContent()), parser);

        String sessionId = parser.sessionId;
        if (sessionId == null || parser.serverUrl == null) { throw new ConnectException(
                String.format("Unable to login: %s", parser.faultstring)); }

        URL soapEndpoint = new URL(parser.serverUrl);
        String cometdEndpoint = Float.parseFloat(parameters.version()) < 37 ? COMETD_REPLAY_OLD : COMETD_REPLAY;
        URL replayEndpoint = new URL(soapEndpoint.getProtocol(), soapEndpoint.getHost(), soapEndpoint.getPort(),
                new StringBuilder().append(cometdEndpoint).append(parameters.version()).toString());
        return new DelegatingBayeuxParameters(parameters) {
            @Override
            public String bearerToken() {
                return sessionId;
            }

            @Override
            public URL endpoint() {
                return replayEndpoint;
            }
        };
    } finally {
        client.stop();
        client.destroy();
    }
}
 
Example 14
Source File: QueryEngine.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
  * Do not accept new queries and halt any running binding set chunk tasks.
  */
 public void shutdownNow() {
     
     shutdown = true;
     
     /*
      * Stop the QueryEngineTask: this is the task that accepts chunks that
      * are available for evaluation and assigns them to the
      * AbstractRunningQuery.
      */
     final Future<?> f = engineFuture.get();
     if (f != null) {
         if (log.isInfoEnabled())
             log.info("Cancelling engineFuture: " + this);
         f.cancel(true/* mayInterruptIfRunning */);
     }
     
     // stop the service on which we ran the QueryEngineTask.
     final ExecutorService s = engineService.get();
     if (s != null) {
         if (log.isInfoEnabled())
             log.info("Terminating engineService: "+this);
         s.shutdownNow();
     }
     
     final HttpClient cm = clientConnectionManagerRef.get();
     if (cm != null) {
         if (log.isInfoEnabled())
             log.info("Terminating HttpClient: " + this);
         try {
	cm.stop();
} catch (Exception e) {
	log.error("Problem stopping HttpClient", e);
}
     }

     // halt any running queries.
     for(AbstractRunningQuery q : runningQueries.values()) {
         
         q.cancel(true/*mayInterruptIfRunning*/);
         
     }
     
     // clear the queues
     priorityQueue.clear();
     deadlineQueue.clear();

     // clear references.
     engineFuture.set(null);
     engineService.set(null);
     clientConnectionManagerRef.set(null);
     
 }
 
Example 15
Source File: Example1.java    From database with GNU General Public License v2.0 2 votes vote down vote up
static public void main(final String[] args) throws Exception {

        /**
         * The top-level SPARQL end point for a NanoSparqlServer instance.
         */
        final String serviceURL = "http://localhost:" + Config.BLAZEGRAPH_HTTP_PORT + "/"
                + BigdataStatics.getContextPath();

        /**
         * The namespace of the KB instance that you want to connect to on that
         * server. The default namespace is "kb".
         */
        final String namespace = "kb";
        
        ExecutorService executor = null;
        
        RemoteRepositoryManager repo = null;
        
        HttpClient client = null;

        try {

            executor = Executors.newCachedThreadPool();
            
           	client = HttpClientConfigurator.getInstance().newInstance();

            repo = new RemoteRepositoryManager(
            		serviceURL, client, executor);

            final IObjectManager om = new NanoSparqlObjectManager(repo.getRepositoryForDefaultNamespace(),
                    namespace);

            new Example1(om).call();

        } finally {

            if (repo != null) {
            	repo.close();
            }
            
            if (client != null) {
            	client.stop();
            }

            if (executor != null) {
                executor.shutdownNow();
            }

        }

    }
 
Example 16
Source File: Example1.java    From database with GNU General Public License v2.0 2 votes vote down vote up
static public void main(final String[] args) throws Exception {

        /**
         * The top-level SPARQL end point for a NanoSparqlServer instance.
         */
        final String serviceURL = "http://localhost:" + Config.BLAZEGRAPH_HTTP_PORT + "/"
                + BigdataStatics.getContextPath();

        /**
         * The namespace of the KB instance that you want to connect to on that
         * server. The default namespace is "kb".
         */
        final String namespace = "kb";
        
        ExecutorService executor = null;

        RemoteRepositoryManager repo = null;

        HttpClient client = null;

        try {

            executor = Executors.newCachedThreadPool();
            
           	client = HttpClientConfigurator.getInstance().newInstance();

            repo = new RemoteRepositoryManager(
            		serviceURL, client, executor);

            final IObjectManager om = new NanoSparqlObjectManager(repo.getRepositoryForDefaultNamespace(),
                    namespace);

            new Example1(om).call();

        } finally {

            if (repo != null) {

            	repo.close();

            }

            if (client != null) {

            	client.stop();

            }

            if (executor != null) {

                executor.shutdownNow();

            }

        }

    }
 
Example 17
Source File: Example2.java    From database with GNU General Public License v2.0 2 votes vote down vote up
static public void main(final String[] args) throws Exception {

        /**
         * The top-level SPARQL end point for a NanoSparqlServer instance.
         */
        final String serviceURL = "http://localhost:" + Config.BLAZEGRAPH_HTTP_PORT + "/"
                + BigdataStatics.getContextPath();

        /**
         * The namespace of the KB instance that you want to connect to on that
         * server. The default namespace is "kb".
         */
        final String namespace = "kb";
        
        ExecutorService executor = null;

        RemoteRepositoryManager repo = null;

        HttpClient client = null;

        try {

            executor = Executors.newCachedThreadPool();
            
           	client = HttpClientConfigurator.getInstance().newInstance();

            repo = new RemoteRepositoryManager(
            		serviceURL, client, executor);

            final IObjectManager om = new NanoSparqlObjectManager(repo.getRepositoryForDefaultNamespace(),
                    namespace);

            new Example2(om).call();

        } finally {

            if (repo != null) {

            	repo.close();

            }

            if (client != null) {

            	client.stop();

            }

            if (executor != null) {

                executor.shutdownNow();

            }

        }

    }