use of org.eclipse.jetty.websocket.client.WebSocketClient in project chassis by Kixeye.
the class WebSocketTransportTest method testWebSocketServiceWithJsonWithPskEncryption.
@Test
public void testWebSocketServiceWithJsonWithPskEncryption() throws Exception {
// create AES shared key cipher
Security.addProvider(new BouncyCastleProvider());
KeyGenerator kgen = KeyGenerator.getInstance("AES", "BC");
kgen.init(128);
SecretKey key = kgen.generateKey();
byte[] aesKey = key.getEncoded();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("websocket.enabled", "true");
properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
properties.put("websocket.hostname", "localhost");
properties.put("http.enabled", "false");
properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
properties.put("http.hostname", "localhost");
properties.put("websocket.crypto.enabled", "true");
properties.put("websocket.crypto.cipherProvider", "BC");
properties.put("websocket.crypto.cipherTransformation", "AES/ECB/PKCS7Padding");
properties.put("websocket.crypto.secretKeyAlgorithm", "AES");
properties.put("websocket.crypto.secretKeyData", BaseEncoding.base16().encode(aesKey));
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
context.setEnvironment(environment);
context.register(PropertySourcesPlaceholderConfigurer.class);
context.register(TransportConfiguration.class);
context.register(TestWebSocketService.class);
WebSocketClient wsClient = new WebSocketClient();
try {
context.refresh();
final MessageSerDe serDe = context.getBean(JsonJacksonMessageSerDe.class);
final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
messageRegistry.registerType("stuff", TestObject.class);
wsClient.start();
QueuingWebSocketListener webSocket = new QueuingWebSocketListener(serDe, messageRegistry, context.getBean(WebSocketPskFrameProcessor.class));
Session session = wsClient.connect(webSocket, new URI("ws://localhost:" + properties.get("websocket.port") + "/" + serDe.getMessageFormatName())).get(5000, TimeUnit.MILLISECONDS);
Envelope envelope = new Envelope("getStuff", null, null, Lists.newArrayList(new Header("testheadername", Lists.newArrayList("testheaderval"))), null);
byte[] rawEnvelope = serDe.serialize(envelope);
rawEnvelope = SymmetricKeyCryptoUtils.encrypt(rawEnvelope, 0, rawEnvelope.length, key, "AES/ECB/PKCS7Padding", "BC");
session.getRemote().sendBytes(ByteBuffer.wrap(rawEnvelope));
TestObject response = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals("stuff", response.value);
byte[] rawStuff = serDe.serialize(new TestObject("more stuff"));
envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
rawEnvelope = serDe.serialize(envelope);
rawEnvelope = SymmetricKeyCryptoUtils.encrypt(rawEnvelope, 0, rawEnvelope.length, key, "AES/ECB/PKCS7Padding", "BC");
session.getRemote().sendBytes(ByteBuffer.wrap(rawEnvelope));
response = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals("stuff", response.value);
envelope = new Envelope("getStuff", null, null, null);
rawEnvelope = serDe.serialize(envelope);
rawEnvelope = SymmetricKeyCryptoUtils.encrypt(rawEnvelope, 0, rawEnvelope.length, key, "AES/ECB/PKCS7Padding", "BC");
session.getRemote().sendBytes(ByteBuffer.wrap(rawEnvelope));
response = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals("more stuff", response.value);
rawStuff = serDe.serialize(new TestObject(RandomStringUtils.randomAlphanumeric(100)));
envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
rawEnvelope = serDe.serialize(envelope);
rawEnvelope = SymmetricKeyCryptoUtils.encrypt(rawEnvelope, 0, rawEnvelope.length, key, "AES/ECB/PKCS7Padding", "BC");
session.getRemote().sendBytes(ByteBuffer.wrap(rawEnvelope));
ServiceError error = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(error);
Assert.assertEquals(ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE, error.code);
envelope = new Envelope("expectedError", null, null, null);
rawEnvelope = serDe.serialize(envelope);
rawEnvelope = SymmetricKeyCryptoUtils.encrypt(rawEnvelope, 0, rawEnvelope.length, key, "AES/ECB/PKCS7Padding", "BC");
session.getRemote().sendBytes(ByteBuffer.wrap(rawEnvelope));
error = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(error);
Assert.assertEquals(TestWebSocketService.EXPECTED_EXCEPTION.code, error.code);
Assert.assertEquals(TestWebSocketService.EXPECTED_EXCEPTION.description, error.description);
envelope = new Envelope("unexpectedError", null, null, null);
rawEnvelope = serDe.serialize(envelope);
rawEnvelope = SymmetricKeyCryptoUtils.encrypt(rawEnvelope, 0, rawEnvelope.length, key, "AES/ECB/PKCS7Padding", "BC");
session.getRemote().sendBytes(ByteBuffer.wrap(rawEnvelope));
error = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(error);
Assert.assertEquals(ExceptionServiceErrorMapper.UNKNOWN_ERROR_CODE, error.code);
} finally {
try {
wsClient.stop();
} finally {
context.close();
}
}
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project chassis by Kixeye.
the class WebSocketTransportTest method testEmptyWebSocketFrameUsingBinary.
@Test
public void testEmptyWebSocketFrameUsingBinary() throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("websocket.enabled", "true");
properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
properties.put("websocket.hostname", "localhost");
properties.put("http.enabled", "false");
properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
properties.put("http.hostname", "localhost");
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
context.setEnvironment(environment);
context.register(PropertySourcesPlaceholderConfigurer.class);
context.register(TransportConfiguration.class);
context.register(TestWebSocketService.class);
WebSocketClient wsClient = new WebSocketClient();
try {
//start server
context.refresh();
// start client
wsClient.start();
final MessageSerDe serDe = context.getBean(JsonJacksonMessageSerDe.class);
final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
QueuingWebSocketListener listener = new QueuingWebSocketListener(serDe, messageRegistry, null);
WebSocketSession session = (WebSocketSession) wsClient.connect(listener, new URI("ws://localhost:" + properties.get("websocket.port") + "/" + serDe.getMessageFormatName())).get(5000, TimeUnit.MILLISECONDS);
session.getRemote().sendBytes(ByteBuffer.wrap(new byte[0]));
ServiceError error = listener.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(error);
Assert.assertEquals("EMPTY_ENVELOPE", error.code);
Assert.assertEquals("STOPPED", session.getState());
} finally {
try {
wsClient.stop();
} finally {
context.close();
}
}
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project chassis by Kixeye.
the class WebSocketTransportTest method testWebSocketServiceWithJsonWithWss.
@Test
public void testWebSocketServiceWithJsonWithWss() throws Exception {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("secureWebsocket.enabled", "true");
properties.put("secureWebsocket.port", "" + SocketUtils.findAvailableTcpPort());
properties.put("secureWebsocket.hostname", "localhost");
properties.put("secureWebsocket.selfSigned", "true");
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
context.setEnvironment(environment);
context.register(PropertySourcesPlaceholderConfigurer.class);
context.register(TransportConfiguration.class);
context.register(TestWebSocketService.class);
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setTrustAll(true);
WebSocketClient wsClient = new WebSocketClient(sslContextFactory);
try {
context.refresh();
final MessageSerDe serDe = context.getBean(JsonJacksonMessageSerDe.class);
final WebSocketMessageRegistry messageRegistry = context.getBean(WebSocketMessageRegistry.class);
messageRegistry.registerType("stuff", TestObject.class);
wsClient.start();
QueuingWebSocketListener webSocket = new QueuingWebSocketListener(serDe, messageRegistry, null);
Session session = wsClient.connect(webSocket, new URI("wss://localhost:" + properties.get("secureWebsocket.port") + "/" + serDe.getMessageFormatName())).get(5000, TimeUnit.MILLISECONDS);
Envelope envelope = new Envelope("getStuff", null, null, Lists.newArrayList(new Header("testheadername", Lists.newArrayList("testheaderval"))), null);
session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
TestObject response = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals("stuff", response.value);
byte[] rawStuff = serDe.serialize(new TestObject("more stuff"));
envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
response = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals("stuff", response.value);
envelope = new Envelope("getStuff", null, null, null);
session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
response = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals("more stuff", response.value);
rawStuff = serDe.serialize(new TestObject(RandomStringUtils.randomAlphanumeric(100)));
envelope = new Envelope("setStuff", "stuff", null, ByteBuffer.wrap(rawStuff));
session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
ServiceError error = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(error);
Assert.assertEquals(ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE, error.code);
envelope = new Envelope("expectedError", null, null, null);
session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
error = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(error);
Assert.assertEquals(TestWebSocketService.EXPECTED_EXCEPTION.code, error.code);
Assert.assertEquals(TestWebSocketService.EXPECTED_EXCEPTION.description, error.description);
envelope = new Envelope("unexpectedError", null, null, null);
session.getRemote().sendBytes(ByteBuffer.wrap(serDe.serialize(envelope)));
error = webSocket.getResponse(5, TimeUnit.SECONDS);
Assert.assertNotNull(error);
Assert.assertEquals(ExceptionServiceErrorMapper.UNKNOWN_ERROR_CODE, error.code);
} finally {
try {
wsClient.stop();
} finally {
context.close();
}
}
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project zeppelin by apache.
the class ZeppelinhubClient method createNewWebsocketClient.
private WebSocketClient createNewWebsocketClient() {
SslContextFactory sslContextFactory = new SslContextFactory();
WebSocketClient client = new WebSocketClient(sslContextFactory);
client.setMaxTextMessageBufferSize(Client.getMaxNoteSize());
client.getPolicy().setMaxTextMessageSize(Client.getMaxNoteSize());
client.setMaxIdleTimeout(CONNECTION_IDLE_TIME);
return client;
}
use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.
the class LargeAnnotatedTest method testEcho.
@Test
public void testEcho() throws Exception {
WSServer wsb = new WSServer(testdir, "app");
wsb.createWebInf();
wsb.copyEndpoint(LargeEchoConfiguredSocket.class);
try {
wsb.start();
URI uri = wsb.getServerBaseURI();
WebAppContext webapp = wsb.createWebAppContext();
wsb.deployWebapp(webapp);
// wsb.dump();
WebSocketClient client = new WebSocketClient(bufferPool);
try {
client.getPolicy().setMaxTextMessageSize(128 * 1024);
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<Session> foo = client.connect(clientEcho, uri.resolve("echo/large"));
// wait for connect
foo.get(1, TimeUnit.SECONDS);
// The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies
byte[] txt = new byte[100 * 1024];
Arrays.fill(txt, (byte) 'o');
String msg = new String(txt, StandardCharsets.UTF_8);
clientEcho.sendMessage(msg);
Queue<String> msgs = clientEcho.awaitMessages(1);
Assert.assertEquals("Expected message", msg, msgs.poll());
} finally {
client.stop();
}
} finally {
wsb.stop();
}
}
Aggregations