use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class TestMemcachedSessions method testMemcached.
@Test
public void testMemcached() throws Exception {
String contextPath = "/";
Server server = new Server(0);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setResourceBase(System.getProperty("java.io.tmpdir"));
server.setHandler(context);
NullSessionCache dsc = new NullSessionCache(context.getSessionHandler());
dsc.setSessionDataStore(new CachingSessionDataStore(new MemcachedSessionDataMap("localhost", "11211"), new NullSessionDataStore()));
context.getSessionHandler().setSessionCache(dsc);
// Add a test servlet
ServletHolder h = new ServletHolder();
h.setServlet(new TestServlet());
context.addServlet(h, "/");
try {
server.start();
int port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
HttpClient client = new HttpClient();
client.start();
try {
int value = 42;
ContentResponse response = client.GET("http://localhost:" + port + contextPath + "?action=set&value=" + value);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
String resp = response.getContentAsString();
assertEquals(resp.trim(), String.valueOf(value));
// Be sure the session value is still there
Request request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
resp = response.getContentAsString();
assertEquals(String.valueOf(value), resp.trim());
//Delete the session
request = client.newRequest("http://localhost:" + port + contextPath + "?action=del");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//Check that the session is gone
request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
resp = response.getContentAsString();
assertEquals("No session", resp.trim());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class EventSourceServletTest method testBasicFunctionality.
@Test
public void testBasicFunctionality() throws Exception {
final AtomicReference<EventSource.Emitter> emitterRef = new AtomicReference<EventSource.Emitter>();
final CountDownLatch emitterLatch = new CountDownLatch(1);
final CountDownLatch closeLatch = new CountDownLatch(1);
class S extends EventSourceServlet {
@Override
protected EventSource newEventSource(HttpServletRequest request) {
return new EventSource() {
public void onOpen(Emitter emitter) throws IOException {
emitterRef.set(emitter);
emitterLatch.countDown();
}
public void onClose() {
closeLatch.countDown();
}
};
}
}
String servletPath = "/eventsource";
ServletHolder servletHolder = new ServletHolder(new S());
int heartBeatPeriod = 2;
servletHolder.setInitParameter("heartBeatPeriod", String.valueOf(heartBeatPeriod));
context.addServlet(servletHolder, servletPath);
Socket socket = new Socket("localhost", connector.getLocalPort());
writeHTTPRequest(socket, servletPath);
BufferedReader reader = readAndDiscardHTTPResponse(socket);
Assert.assertTrue(emitterLatch.await(1, TimeUnit.SECONDS));
EventSource.Emitter emitter = emitterRef.get();
Assert.assertNotNull(emitter);
String data = "foo";
emitter.data(data);
String line = reader.readLine();
String received = "";
while (line != null) {
received += line;
if (line.length() == 0)
break;
line = reader.readLine();
}
Assert.assertEquals("data: " + data, received);
socket.close();
Assert.assertTrue(closeLatch.await(heartBeatPeriod * 3, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class EventSourceServletTest method testEncoding.
@Test
public void testEncoding() throws Exception {
// The EURO symbol
final String data = "€";
class S extends EventSourceServlet {
@Override
protected EventSource newEventSource(HttpServletRequest request) {
return new EventSource() {
public void onOpen(Emitter emitter) throws IOException {
emitter.data(data);
}
public void onClose() {
}
};
}
}
String servletPath = "/eventsource";
context.addServlet(new ServletHolder(new S()), servletPath);
Socket socket = new Socket("localhost", connector.getLocalPort());
writeHTTPRequest(socket, servletPath);
BufferedReader reader = readAndDiscardHTTPResponse(socket);
String line = reader.readLine();
String received = "";
while (line != null) {
received += line;
if (line.length() == 0)
break;
line = reader.readLine();
}
Assert.assertEquals("data: " + data, received);
socket.close();
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class EventSourceServletTest method testServerSideClose.
@Test
public void testServerSideClose() throws Exception {
final AtomicReference<EventSource.Emitter> emitterRef = new AtomicReference<EventSource.Emitter>();
final CountDownLatch emitterLatch = new CountDownLatch(1);
class S extends EventSourceServlet {
@Override
protected EventSource newEventSource(HttpServletRequest request) {
return new EventSource() {
public void onOpen(Emitter emitter) throws IOException {
emitterRef.set(emitter);
emitterLatch.countDown();
}
public void onClose() {
}
};
}
}
String servletPath = "/eventsource";
context.addServlet(new ServletHolder(new S()), servletPath);
Socket socket = new Socket("localhost", connector.getLocalPort());
writeHTTPRequest(socket, servletPath);
BufferedReader reader = readAndDiscardHTTPResponse(socket);
Assert.assertTrue(emitterLatch.await(1, TimeUnit.SECONDS));
EventSource.Emitter emitter = emitterRef.get();
Assert.assertNotNull(emitter);
String comment = "foo";
emitter.comment(comment);
String line = reader.readLine();
String received = "";
while (line != null) {
received += line;
if (line.length() == 0)
break;
line = reader.readLine();
}
Assert.assertEquals(": " + comment, received);
emitter.close();
line = reader.readLine();
Assert.assertNull(line);
socket.close();
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class EventSourceServletTest method testMultiLineData.
@Test
public void testMultiLineData() throws Exception {
String data1 = "data1";
String data2 = "data2";
String data3 = "data3";
String data4 = "data4";
final String data = data1 + "\r\n" + data2 + "\r" + data3 + "\n" + data4;
class S extends EventSourceServlet {
@Override
protected EventSource newEventSource(HttpServletRequest request) {
return new EventSource() {
public void onOpen(Emitter emitter) throws IOException {
emitter.data(data);
}
public void onClose() {
}
};
}
}
String servletPath = "/eventsource";
context.addServlet(new ServletHolder(new S()), servletPath);
Socket socket = new Socket("localhost", connector.getLocalPort());
writeHTTPRequest(socket, servletPath);
BufferedReader reader = readAndDiscardHTTPResponse(socket);
String line1 = reader.readLine();
Assert.assertEquals("data: " + data1, line1);
String line2 = reader.readLine();
Assert.assertEquals("data: " + data2, line2);
String line3 = reader.readLine();
Assert.assertEquals("data: " + data3, line3);
String line4 = reader.readLine();
Assert.assertEquals("data: " + data4, line4);
String line5 = reader.readLine();
Assert.assertEquals(0, line5.length());
socket.close();
}
Aggregations