Java Code Examples for org.eclipse.jetty.util.ssl.SslContextFactory#Client

The following examples show how to use org.eclipse.jetty.util.ssl.SslContextFactory#Client . 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: SSLConfig.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public SslContextFactory.Client createClientContextFactory() {
  if (! isSSLMode()) {
    return null;
  }
  // else...

  SslContextFactory.Client factory = new SslContextFactory.Client();
  if (getKeyStore() != null) {
    factory.setKeyStorePath(getKeyStore());
  }
  if (getKeyStorePassword() != null) {
    factory.setKeyStorePassword(getKeyStorePassword());
  }

  if (isClientAuthMode()) {
    if (getTrustStore() != null)
      factory.setTrustStorePath(getTrustStore());
    if (getTrustStorePassword() != null)
      factory.setTrustStorePassword(getTrustStorePassword());
  }

  return factory;
}
 
Example 2
Source File: SSLTestConfig.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public SSLConfig buildClientSSLConfig() {
  if (!isSSLMode()) {
    return null;
  }

  return new SSLConfig(isSSLMode(), isClientAuthMode(), null, null, null, null) {
    @Override
    public SslContextFactory.Client createClientContextFactory() {
      SslContextFactory.Client factory = new SslContextFactory.Client(!checkPeerName);
      try {
        factory.setSslContext(buildClientSSLContext());
      } catch (KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException e) {
        throw new IllegalStateException("Unable to setup https scheme for HTTPClient to test SSL.", e);
      }
      return factory;
    }
  };
}
 
Example 3
Source File: WebClientLoggingIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenJettyHttpClient_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() {
    SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
    org.eclipse.jetty.client.HttpClient httpClient = new org.eclipse.jetty.client.HttpClient(sslContextFactory) {
        @Override
        public Request newRequest(URI uri) {
            Request request = super.newRequest(uri);
            return enhance(request);
        }
    };

    WebClient
      .builder()
      .clientConnector(new JettyClientHttpConnector(httpClient))
      .build()
      .post()
      .uri(sampleUrl)
      .body(BodyInserters.fromObject(post))
      .retrieve()
      .bodyToMono(String.class)
      .block();

    verify(jettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains(sampleResponseBody)));
}
 
Example 4
Source File: ClientConfig.java    From mutual-tls-ssl with Apache License 2.0 5 votes vote down vote up
@Bean
@Scope("prototype")
public org.eclipse.jetty.client.HttpClient jettyHttpClient(@Autowired(required = false) SSLFactory sslFactory) {
    if (nonNull(sslFactory)) {
        SslContextFactory.Client sslContextFactory = JettySslContextUtils.forClient(sslFactory);
        return new org.eclipse.jetty.client.HttpClient(sslContextFactory);
    } else {
        return new org.eclipse.jetty.client.HttpClient();
    }
}
 
Example 5
Source File: Http2SolrClient.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static SslContextFactory.Client getDefaultSslContextFactory() {
  String checkPeerNameStr = System.getProperty(HttpClientUtil.SYS_PROP_CHECK_PEER_NAME);
  boolean sslCheckPeerName = true;
  if (checkPeerNameStr == null || "false".equalsIgnoreCase(checkPeerNameStr)) {
    sslCheckPeerName = false;
  }

  SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(!sslCheckPeerName);

  if (null != System.getProperty("javax.net.ssl.keyStore")) {
    sslContextFactory.setKeyStorePath
        (System.getProperty("javax.net.ssl.keyStore"));
  }
  if (null != System.getProperty("javax.net.ssl.keyStorePassword")) {
    sslContextFactory.setKeyStorePassword
        (System.getProperty("javax.net.ssl.keyStorePassword"));
  }
  if (null != System.getProperty("javax.net.ssl.trustStore")) {
    sslContextFactory.setTrustStorePath
        (System.getProperty("javax.net.ssl.trustStore"));
  }
  if (null != System.getProperty("javax.net.ssl.trustStorePassword")) {
    sslContextFactory.setTrustStorePassword
        (System.getProperty("javax.net.ssl.trustStorePassword"));
  }

  sslContextFactory.setEndpointIdentificationAlgorithm(System.getProperty("solr.jetty.ssl.verifyClientHostName"));

  return sslContextFactory;
}
 
Example 6
Source File: Http2SolrClientTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetDefaultSslContextFactory() {
  assertNull(Http2SolrClient.getDefaultSslContextFactory().getEndpointIdentificationAlgorithm());

  System.setProperty("solr.jetty.ssl.verifyClientHostName", "HTTPS");
  SslContextFactory.Client sslContextFactory = Http2SolrClient.getDefaultSslContextFactory();
  assertEquals("HTTPS", sslContextFactory.getEndpointIdentificationAlgorithm());
  System.clearProperty("solr.jetty.ssl.verifyClientHostName");
}
 
Example 7
Source File: HttpClientModule.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
public HttpClient provideHttpClient(ExecutorService executorService,
                                    @Named("httpClient.maxConnectionsQueued") Integer maxConnectionsQueued,
                                    @Named("httpClient.maxConnectionPerRoute") Integer maxConnectionPerRoute,
                                    @Named("httpClient.requestBufferSize") Integer requestBufferSize,
                                    @Named("httpClient.responseBufferSize") Integer responseBufferSize,
                                    @Named("httpClient.maxRedirects") Integer maxRedirects,
                                    @Named("httpClient.trustAllCertificates") Boolean trustAllCertificates) {

    try {
        SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
        sslContextFactory.setTrustAll(trustAllCertificates);
        HttpClient httpClient = new HttpClient(sslContextFactory);
        httpClient.setExecutor(executorService);
        httpClient.setMaxConnectionsPerDestination(maxConnectionsQueued);
        httpClient.setMaxRequestsQueuedPerDestination(maxConnectionPerRoute);
        httpClient.setRequestBufferSize(requestBufferSize);
        httpClient.setResponseBufferSize(responseBufferSize);
        httpClient.setMaxRedirects(maxRedirects);
        httpClient.start();

        registerHttpClientShutdownHook(httpClient);

        return httpClient;
    } catch (Exception e) {
        System.out.println(Arrays.toString(e.getStackTrace()));
        throw new RuntimeException(e.getLocalizedMessage(), e);
    }
}
 
Example 8
Source File: HttpServerTest.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static HttpClient createJettyHttpClient(Path certificateFile) throws Exception {
    SslContextFactory.Client clientSslCtxFactory = new SslContextFactory.Client();
    clientSslCtxFactory.setHostnameVerifier(NoopHostnameVerifier.INSTANCE);
    clientSslCtxFactory.setSslContext(new SslContextBuilder().withTrustStore(certificateFile).build());

    HttpClient client = new HttpClient(clientSslCtxFactory);
    client.start();
    return client;
}
 
Example 9
Source File: ProxyServerFactory.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static Server of(String proxyTo, int port, File keystoreFile, String keystorePassword) {
  Server proxy = new Server();
  logger.info("Setting up HTTPS connector for web server");

  final SslContextFactory sslContextFactory = new SslContextFactory.Client();

  sslContextFactory.setKeyStorePath(keystoreFile.getAbsolutePath());
  sslContextFactory.setKeyStorePassword(keystorePassword);

  // SSL Connector
  final ServerConnector sslConnector = new ServerConnector(proxy,
      new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.toString()),
      new HttpConnectionFactory(new HttpConfiguration()));
  // regular http connector if one needs to inspect the wire. Requires tweaking the ElasticsearchPlugin to use http
  // final ServerConnector sslConnector = new ServerConnector(embeddedJetty,
  //   new HttpConnectionFactory(new HttpConfiguration()));
  sslConnector.setPort(port);
  proxy.addConnector(sslConnector);

  // root handler with request logging
  final RequestLogHandler rootHandler = new RequestLogHandler();
  proxy.setHandler(rootHandler);

  final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
  servletContextHandler.setContextPath("/");
  rootHandler.setHandler(servletContextHandler);

  // error handler
  ProxyServlet.Transparent proxyServlet = new ProxyServlet.Transparent() {
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
      try {
        HttpServletRequest hr = (HttpServletRequest) req;
        logger.debug("incoming {} {}://{}:{} {}",
            hr.getMethod(),
            req.getScheme(),
            req.getServerName(),
            req.getServerPort(),
            hr.getRequestURL());
        super.service(req, res);
      } catch (Exception e) {
        logger.error("can't proxy " + req, e);
        throw new RuntimeException(e);
      }
    }

    @Override
    protected String rewriteTarget(HttpServletRequest clientRequest) {
      final String serverName = clientRequest.getServerName();
      final int serverPort = clientRequest.getServerPort();
      final String query = clientRequest.getQueryString();

      String result = super.rewriteTarget(clientRequest);

      logger.debug("Proxying {}://{}:{}{} to {}\n",
          clientRequest.getScheme(),
          serverName,
          serverPort,
          query != null ? '?' + query : "",
          result);

      return result;
    }
  };
  // Rest API
  final ServletHolder proxyHolder = new ServletHolder(proxyServlet);
  proxyHolder.setInitParameter("proxyTo", proxyTo);
  proxyHolder.setInitOrder(1);

  servletContextHandler.addServlet(proxyHolder, "/*");

  return proxy;
}