use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.
the class ServletSessionPersistenceTestCase method testSimpleSessionUsage.
@Test
public void testSimpleSessionUsage() throws IOException, ServletException {
final PathHandler pathHandler = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentInfo builder = new DeploymentInfo().setClassLoader(SimpleServletTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").setSessionPersistenceManager(new InMemorySessionPersistence()).setServletSessionConfig(new ServletSessionConfig().setPath("/servletContext/aa")).addServlets(new ServletInfo("servlet", SessionServlet.class).addMapping("/aa/b"));
DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
try {
pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
} catch (ServletException e) {
throw new RuntimeException(e);
}
DefaultServer.setRootHandler(pathHandler);
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/aa/b");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("1", response);
String cookieValue = result.getHeaders("Set-Cookie")[0].getValue();
Assert.assertTrue(cookieValue, cookieValue.contains("JSESSIONID"));
Assert.assertTrue(cookieValue, cookieValue.contains("/servletContext/aa"));
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("2", response);
manager.stop();
manager.undeploy();
manager.deploy();
pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("3", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.
the class WebSocketServletTest method testText.
@Test
public void testText() throws Exception {
final AtomicBoolean connected = new AtomicBoolean(false);
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentUtils.setupServlet(new ServletInfo("websocket", WebSocketServlet.class, new ImmediateInstanceFactory<Servlet>(new WebSocketServlet(new WebSocketConnectionCallback() {
@Override
public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
connected.set(true);
channel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
final String string = message.getData();
if (string.equals("hello")) {
WebSockets.sendText("world", channel, null);
} else {
WebSockets.sendText(string, channel, null);
}
}
});
channel.resumeReceives();
}
}))).addMapping("/*"));
final FutureResult latch = new FutureResult();
WebSocketTestClient client = new WebSocketTestClient(io.netty.handler.codec.http.websocketx.WebSocketVersion.V13, new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/servletContext/"));
client.connect();
client.send(new TextWebSocketFrame(Unpooled.copiedBuffer("hello", US_ASCII)), new FrameChecker(TextWebSocketFrame.class, "world".getBytes(US_ASCII), latch));
latch.getIoFuture().get();
client.destroy();
}
use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.
the class GetCookiesTestCase method setup.
@BeforeClass
public static void setup() throws ServletException {
final PathHandler root = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
ServletInfo s = new ServletInfo("servlet", ValidCookieEchoServlet.class).addMapping("/aaa");
DeploymentInfo builder = new DeploymentInfo().setClassLoader(GetCookiesTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServlet(s);
DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
root.addPrefixPath(builder.getContextPath(), manager.start());
DefaultServer.setRootHandler(root);
}
use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.
the class DeploymentUtils method setupServlet.
/**
* Sets up a simple servlet deployment with the provided servlets.
*
* This is just a convenience method for simple deployments
*
* @param servlets The servlets to add
*/
public static Deployment setupServlet(final ServletExtension servletExtension, final ServletInfo... servlets) {
final PathHandler pathHandler = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentInfo builder = new DeploymentInfo().setClassLoader(SimpleServletTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServlets(servlets);
if (servletExtension != null) {
builder.addServletExtension(servletExtension);
}
DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
try {
pathHandler.addPrefixPath(builder.getContextPath(), manager.start());
} catch (ServletException e) {
throw new RuntimeException(e);
}
DefaultServer.setRootHandler(pathHandler);
return manager.getDeployment();
}
use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.
the class AbstractResponseWrapperTestCase method setup.
@Before
public void setup() throws ServletException {
DeploymentInfo builder = new DeploymentInfo();
builder.setExceptionHandler(LoggingExceptionHandler.builder().add(IllegalArgumentException.class, "io.undertow", Logger.Level.DEBUG).build());
final PathHandler root = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
builder.addServlet(new ServletInfo("wrapperServlet", WrapperServlet.class).addMapping("/*"));
builder.addFilter(new FilterInfo("standard", StandardRequestWrappingFilter.class));
builder.addFilterUrlMapping("standard", "/standard", DispatcherType.REQUEST);
builder.addFilter(new FilterInfo("nonstandard", NonStandardRequestWrappingFilter.class));
builder.addFilterUrlMapping("nonstandard", "/nonstandard", DispatcherType.REQUEST);
builder.setClassIntrospecter(TestClassIntrospector.INSTANCE).setClassLoader(AbstractResponseWrapperTestCase.class.getClassLoader()).setContextPath("/servletContext").setDeploymentName("servletContext.war").setAllowNonStandardWrappers(isNonStandardAllowed());
final DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
root.addPrefixPath(builder.getContextPath(), manager.start());
DefaultServer.setRootHandler(root);
}
Aggregations