use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.
the class MainTest method _testWithoutSSLAuthentication.
/**
* Test to see that SSLHandshakeException is thrown when client don't have
* trusted key.
*/
private void _testWithoutSSLAuthentication(ClientConfig clientConfig) {
SslConfigurator sslConfig = SslConfigurator.newInstance().trustStoreFile(TRUSTORE_CLIENT_FILE).trustStorePassword(TRUSTSTORE_CLIENT_PWD);
Client client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(sslConfig.createSSLContext()).build();
System.out.println("Client: GET " + Server.BASE_URI);
WebTarget target = client.target(Server.BASE_URI);
target.register(LoggingFeature.class);
boolean caught = false;
try {
target.path("/").request().get(String.class);
} catch (Exception e) {
caught = true;
}
assertTrue(caught);
// solaris throws java.net.SocketException instead of SSLHandshakeException
// assertTrue(msg.contains("SSLHandshakeException"));
}
use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.
the class JdkHttpsServerTest method getClientSslContext.
private SSLContext getClientSslContext() throws IOException {
final InputStream trustStore = JdkHttpsServerTest.class.getResourceAsStream(TRUSTSTORE_CLIENT_FILE);
final InputStream keyStore = JdkHttpsServerTest.class.getResourceAsStream(KEYSTORE_CLIENT_FILE);
final SslConfigurator sslConfigClient = SslConfigurator.newInstance().trustStoreBytes(ByteStreams.toByteArray(trustStore)).trustStorePassword(TRUSTSTORE_CLIENT_PWD).keyStoreBytes(ByteStreams.toByteArray(keyStore)).keyPassword(KEYSTORE_CLIENT_PWD);
return sslConfigClient.createSSLContext();
}
use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.
the class JdkHttpsServerTest method getServerSslContext.
private SSLContext getServerSslContext() throws IOException {
final InputStream trustStore = JdkHttpsServerTest.class.getResourceAsStream(TRUSTSTORE_SERVER_FILE);
final InputStream keyStore = JdkHttpsServerTest.class.getResourceAsStream(KEYSTORE_SERVER_FILE);
final SslConfigurator sslConfigServer = SslConfigurator.newInstance().keyStoreBytes(ByteStreams.toByteArray(keyStore)).keyPassword(KEYSTORE_SERVER_PWD).trustStoreBytes(ByteStreams.toByteArray(trustStore)).trustStorePassword(TRUSTSTORE_SERVER_PWD);
return sslConfigServer.createSSLContext();
}
use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.
the class SslFilterTest method openClientSocket.
/**
* Creates an SSL client. Returns when SSL handshake has been completed.
*
* @param completionLatch latch that will be triggered when the expected number of bytes has been received.
* @param readBuffer buffer where received message will be written. Must be the size of the expected message,
* because when it is filled {@code completionLatch} will be triggered.
* @throws Throwable any exception that occurs until SSL handshake has completed.
*/
private Filter<ByteBuffer, ByteBuffer, ByteBuffer, ByteBuffer> openClientSocket(String host, final ByteBuffer readBuffer, final CountDownLatch completionLatch, HostnameVerifier customHostnameVerifier) throws Throwable {
SslConfigurator sslConfig = SslConfigurator.newInstance().trustStoreFile(this.getClass().getResource("/truststore_client").getPath()).trustStorePassword("asdfgh").keyStoreFile(this.getClass().getResource("/keystore_client").getPath()).keyStorePassword("asdfgh");
TransportFilter transportFilter = new TransportFilter(17_000, ThreadPoolConfig.defaultConfig(), 100_000);
final SslFilter sslFilter = new SslFilter(transportFilter, sslConfig.createSSLContext(), host, customHostnameVerifier);
// exceptions errors that occur before SSL handshake has finished are thrown from this method
final AtomicReference<Throwable> exception = new AtomicReference<>();
final CountDownLatch connectLatch = new CountDownLatch(1);
final CountDownLatch startSslLatch = new CountDownLatch(1);
Filter<ByteBuffer, ByteBuffer, ByteBuffer, ByteBuffer> clientSocket = new Filter<ByteBuffer, ByteBuffer, ByteBuffer, ByteBuffer>(sslFilter) {
@Override
void processConnect() {
connectLatch.countDown();
}
@Override
boolean processRead(ByteBuffer data) {
readBuffer.put(data);
if (!readBuffer.hasRemaining()) {
completionLatch.countDown();
}
return false;
}
@Override
void startSsl() {
if (startSslLatch.getCount() == 1) {
downstreamFilter.startSsl();
try {
startSslLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
sslFilter.rehandshake();
}
}
@Override
void processSslHandshakeCompleted() {
startSslLatch.countDown();
}
@Override
void processError(Throwable t) {
if (connectLatch.getCount() == 1 || startSslLatch.getCount() == 1) {
exception.set(t);
connectLatch.countDown();
startSslLatch.countDown();
}
}
@Override
void write(ByteBuffer data, CompletionHandler<ByteBuffer> completionHandler) {
downstreamFilter.write(data, completionHandler);
}
@Override
void processConnectionClosed() {
downstreamFilter.close();
}
@Override
void close() {
downstreamFilter.close();
}
};
clientSocket.connect(new InetSocketAddress(host, PORT), null);
try {
connectLatch.await();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
clientSocket.startSsl();
if (exception.get() != null) {
clientSocket.close();
throw exception.get();
}
return clientSocket;
}
use of org.glassfish.jersey.SslConfigurator in project jersey by jersey.
the class MainTest method _testWithoutBasicAuth.
/**
* Test to see that HTTP 401 is returned when client tries to GET without
* proper credentials.
*/
private void _testWithoutBasicAuth(ClientConfig clientConfig) {
SslConfigurator sslConfig = SslConfigurator.newInstance().trustStoreFile(TRUSTORE_CLIENT_FILE).trustStorePassword(TRUSTSTORE_CLIENT_PWD).keyStoreFile(KEYSTORE_CLIENT_FILE).keyPassword(KEYSTORE_CLIENT_PWD);
Client client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(sslConfig.createSSLContext()).build();
System.out.println("Client: GET " + Server.BASE_URI);
WebTarget target = client.target(Server.BASE_URI);
target.register(LoggingFeature.class);
Response response;
try {
response = target.path("/").request().get(Response.class);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
assertEquals(401, response.getStatus());
}
Aggregations