use of javax.websocket.WebSocketContainer in project wildfly by wildfly.
the class WebSocketTestCase method assertWebSocket.
private void assertWebSocket(URL webapp) throws InterruptedException, IOException, DeploymentException, URISyntaxException {
AnnotatedClient endpoint = new AnnotatedClient();
WebSocketContainer serverContainer = ContainerProvider.getWebSocketContainer();
try (Session session = serverContainer.connectToServer(endpoint, new URI("ws", "", TestSuiteEnvironment.getServerAddress(), TestSuiteEnvironment.getHttpPort(), webapp.getPath() + "websocket/Stuart", "", ""))) {
Assert.assertEquals("Hello Stuart", endpoint.getMessage());
}
}
use of javax.websocket.WebSocketContainer in project zeppelin by apache.
the class TerminalInterpreterTest method testInvalidCommand.
@Test
public void testInvalidCommand() {
Session session = null;
WebSocketContainer webSocketContainer = null;
try {
// mock connect terminal
boolean running = terminal.terminalThreadIsRunning();
assertTrue(running);
URI uri = URI.create("ws://localhost:" + terminal.getTerminalPort() + "/terminal/");
webSocketContainer = ContainerProvider.getWebSocketContainer();
// Attempt Connect
session = webSocketContainer.connectToServer(TerminalSocketTest.class, uri);
// Send Start terminal service message
String terminalReadyCmd = String.format("{\"type\":\"TERMINAL_READY\"," + "\"noteId\":\"noteId-1\",\"paragraphId\":\"paragraphId-1\"}");
LOGGER.info("send > " + terminalReadyCmd);
session.getBasicRemote().sendText(terminalReadyCmd);
Thread.sleep(10000);
LOGGER.info(TerminalSocketTest.ReceivedMsg.toString());
String msg = TerminalSocketTest.ReceivedMsg.get(0);
LOGGER.info(msg);
// {"text":"\u001b[?1034hbash-3.2$ \r\u001b[Kbash-3.2$
// \r\u001b[Kbash-3.2$ ","type":"TERMINAL_PRINT"}
String pattern = "\\{\"text\":\".*\"type\":\"TERMINAL_PRINT\"}";
boolean isMatch = Pattern.matches(pattern, msg);
assertTrue(isMatch);
// Send invalid_command message
String echoHelloWorldCmd = String.format("{\"type\":\"TERMINAL_COMMAND\"," + "\"command\":\"invalid_command\r\"}");
LOGGER.info("send > " + echoHelloWorldCmd);
session.getBasicRemote().sendText(echoHelloWorldCmd);
Thread.sleep(5000);
LOGGER.info(TerminalSocketTest.ReceivedMsg.toString());
// [{"text":"invalid_co \rmmand\r\n","type":"TERMINAL_PRINT"},
// {"text":"bash: invalid_command: command not found\r\n","type":"TERMINAL_PRINT"},
// {"text":"bash-3.2$ ","type":"TERMINAL_PRINT"}]
boolean return_invalid_command = false;
for (String msg2 : TerminalSocketTest.ReceivedMsg) {
boolean find = msg2.contains("invalid_command: command not found");
if (find) {
LOGGER.info("find return terminal print: " + msg2);
return_invalid_command = true;
break;
}
}
assertTrue(return_invalid_command);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
} catch (DeploymentException e) {
LOGGER.error(e.getMessage(), e);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
} finally {
try {
session.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
// the JSR-356 spec does not handle lifecycles (yet)
if (webSocketContainer instanceof LifeCycle) {
try {
((LifeCycle) webSocketContainer).stop();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
}
use of javax.websocket.WebSocketContainer in project zeppelin by apache.
the class TerminalInterpreterTest method testValidCommand.
@Test
public void testValidCommand() {
Session session = null;
WebSocketContainer webSocketContainer = null;
try {
// mock connect terminal
boolean running = terminal.terminalThreadIsRunning();
assertTrue(running);
URI uri = URI.create("ws://localhost:" + terminal.getTerminalPort() + "/terminal/");
webSocketContainer = ContainerProvider.getWebSocketContainer();
// Attempt Connect
session = webSocketContainer.connectToServer(TerminalSocketTest.class, uri);
// Send Start terminal service message
String terminalReadyCmd = String.format("{\"type\":\"TERMINAL_READY\"," + "\"noteId\":\"noteId-1\",\"paragraphId\":\"paragraphId-1\"}");
LOGGER.info("send > " + terminalReadyCmd);
session.getBasicRemote().sendText(terminalReadyCmd);
Thread.sleep(10000);
LOGGER.info(TerminalSocketTest.ReceivedMsg.toString());
String msg = TerminalSocketTest.ReceivedMsg.get(0);
LOGGER.info(msg);
// {"text":"\u001b[?1034hbash-3.2$ \r\u001b[Kbash-3.2$
// \r\u001b[Kbash-3.2$ ","type":"TERMINAL_PRINT"}
String pattern = "\\{\"text\":\".*\"type\":\"TERMINAL_PRINT\"}";
boolean isMatch = Pattern.matches(pattern, msg);
assertTrue(isMatch);
// Send echo 'hello world!' message
String echoHelloWorldCmd = String.format("{\"type\":\"TERMINAL_COMMAND\"," + "\"command\":\"echo 'hello world!'\r\"}");
LOGGER.info("send > " + echoHelloWorldCmd);
session.getBasicRemote().sendText(echoHelloWorldCmd);
Thread.sleep(5000);
// [{"text":"echo \u0027hell \ro world!\u0027\r\n","type":"TERMINAL_PRINT"},
// {"text":"hello world!\r\nbash-3.2$ ","type":"TERMINAL_PRINT"}]
LOGGER.info(TerminalSocketTest.ReceivedMsg.toString());
boolean return_hello_world = false;
for (String msg2 : TerminalSocketTest.ReceivedMsg) {
boolean find = msg2.contains("hello world!");
if (find) {
LOGGER.info("find return terminal print: " + msg2);
return_hello_world = true;
break;
}
}
assertTrue(return_hello_world);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
} catch (DeploymentException e) {
LOGGER.error(e.getMessage(), e);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
} finally {
try {
session.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
// the JSR-356 spec does not handle lifecycles (yet)
if (webSocketContainer instanceof LifeCycle) {
try {
((LifeCycle) webSocketContainer).stop();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
}
use of javax.websocket.WebSocketContainer in project tomee by apache.
the class ClasspathAsWebappTest method run.
@Test
public void run() throws MalformedURLException {
MyInitializer.found = null;
MyBean.VALUE = null;
try (final Container container = new Container(new Configuration().http(NetworkUtil.getNextAvailablePort()).property("openejb.container.additional.exclude", "org.apache.tomee.embedded.").property("openejb.additional.include", "tomee-").user("tomee", "tomeepwd").loginConfig(new LoginConfigBuilder().basic()).securityConstaint(new SecurityConstaintBuilder().addAuthRole("**").authConstraint(true).addCollection("api", "/api/resource2/"))).deployPathsAsWebapp(JarLocation.jarLocation(MyInitializer.class)).inject(this)) {
// Servlet (initializer, servlet)
assertNotNull(MyInitializer.found);
final Iterator<Class<?>> it = MyInitializer.found.iterator();
while (it.hasNext()) {
// ThreadStackRule defines one for instance
final Class<?> next = it.next();
if (next.getEnclosingClass() != null && !Modifier.isStatic(next.getModifiers())) {
it.remove();
}
}
assertEquals(1, MyInitializer.found.size());
assertEquals(Task1.class, MyInitializer.found.iterator().next());
try {
assertEquals("Servlet!", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/s")));
} catch (final IOException e) {
fail(e.getMessage());
}
try {
assertEquals("WebServlet", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/w")));
} catch (final IOException e) {
fail(e.getMessage());
}
// JSP
try {
assertEquals("JSP", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/?test=JSP")).trim());
} catch (final IOException e) {
fail(e.getMessage());
}
// CDI
assertNotNull(bean);
assertNull(bean.value());
MyBean.VALUE = "cdi";
assertEquals("cdi", bean.value());
// EJB
MyBean.VALUE = "ejb";
assertEquals("ejb", anEjb.run());
// JAXRS
try {
assertEquals("jaxrs", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/api/resource")));
} catch (final IOException e) {
fail(e.getMessage());
}
// JAXRS + servlet security
try {
final URL url = new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/api/resource2/");
final HttpURLConnection c = HttpURLConnection.class.cast(url.openConnection());
c.setRequestProperty("Authorization", "Basic " + printBase64Binary("tomee:tomeepwd".getBytes()));
assertEquals("tomee", IO.slurp(c.getInputStream()));
c.disconnect();
} catch (final IOException e) {
fail(e.getMessage());
}
try {
assertEquals("tomee", IO.slurp(new URL("http://tomee:tomeepwd@localhost:" + container.getConfiguration().getHttpPort() + "/api/resource2/")));
fail("should have been not authorized");
} catch (final IOException e) {
// ok
}
// WebSocket
final WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
try {
WebSocketClient.message = null;
final WebSocketClient webSocketClient = new WebSocketClient();
final Session session = webSocketContainer.connectToServer(webSocketClient, new URI("ws://localhost:" + container.getConfiguration().getHttpPort() + "/ws"));
webSocketClient.latch.await(20, TimeUnit.SECONDS);
session.close();
assertEquals("websocket", WebSocketClient.message);
} catch (final Exception e) {
fail(e.getMessage());
}
}
}
use of javax.websocket.WebSocketContainer in project meecrowave by apache.
the class WSTest method run.
@Test
public void run() throws InterruptedException, DeploymentException, IOException, URISyntaxException {
CountDownLatch cdl = new CountDownLatch(5);
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
String wsEndpoint = String.format("ws://localhost:%d/ws-chat", CONTAINER.getConfiguration().getHttpPort());
Session session = container.connectToServer(new ChatClient(cdl), new URI(wsEndpoint));
assertTrue(cdl.await(20, TimeUnit.SECONDS));
session.close();
}
Aggregations