use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class ProxyTest method startProxy.
private void startProxy(HttpServlet proxyServlet, Map<String, String> initParams) throws Exception {
QueuedThreadPool proxyPool = new QueuedThreadPool();
proxyPool.setName("proxy");
proxy = new Server(proxyPool);
HttpConfiguration configuration = new HttpConfiguration();
configuration.setSendDateHeader(false);
configuration.setSendServerVersion(false);
String value = initParams.get("outputBufferSize");
if (value != null)
configuration.setOutputBufferSize(Integer.valueOf(value));
proxyConnector = new ServerConnector(proxy, new HTTP2ServerConnectionFactory(configuration));
proxy.addConnector(proxyConnector);
ServletContextHandler proxyContext = new ServletContextHandler(proxy, "/", true, false);
ServletHolder proxyServletHolder = new ServletHolder(proxyServlet);
proxyServletHolder.setInitParameters(initParams);
proxyContext.addServlet(proxyServletHolder, "/*");
proxy.start();
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class AsyncServletTest method testStartAsyncThenServerIdleTimeout.
private void testStartAsyncThenServerIdleTimeout(long sessionTimeout, long streamTimeout) throws Exception {
prepareServer(new HTTP2ServerConnectionFactory(new HttpConfiguration()) {
@Override
protected ServerSessionListener newSessionListener(Connector connector, EndPoint endPoint) {
return new HTTPServerSessionListener(connector, endPoint) {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
stream.setIdleTimeout(streamTimeout);
return super.onNewStream(stream, frame);
}
};
}
});
connector.setIdleTimeout(sessionTimeout);
ServletContextHandler context = new ServletContextHandler(server, "/");
long timeout = Math.min(sessionTimeout, streamTimeout);
CountDownLatch errorLatch = new CountDownLatch(1);
context.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = (AsyncContext) request.getAttribute(AsyncContext.class.getName());
if (asyncContext == null) {
AsyncContext context = request.startAsync();
context.setTimeout(2 * timeout);
request.setAttribute(AsyncContext.class.getName(), context);
context.addListener(new AsyncListener() {
@Override
public void onComplete(AsyncEvent event) throws IOException {
}
@Override
public void onTimeout(AsyncEvent event) throws IOException {
event.getAsyncContext().complete();
}
@Override
public void onError(AsyncEvent event) throws IOException {
errorLatch.countDown();
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}
});
} else {
throw new ServletException();
}
}
}), servletPath + "/*");
server.start();
prepareClient();
client.start();
Session session = newClient(new Session.Listener.Adapter());
HttpFields fields = new HttpFields();
MetaData.Request metaData = newRequest("GET", fields);
HeadersFrame frame = new HeadersFrame(metaData, null, true);
CountDownLatch clientLatch = new CountDownLatch(1);
session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
MetaData.Response response = (MetaData.Response) frame.getMetaData();
if (response.getStatus() == HttpStatus.OK_200 && frame.isEndStream())
clientLatch.countDown();
}
});
// When the server idle times out, but the request has been dispatched
// then the server must ignore the idle timeout as per Servlet semantic.
Assert.assertFalse(errorLatch.await(2 * timeout, TimeUnit.MILLISECONDS));
Assert.assertTrue(clientLatch.await(2 * timeout, TimeUnit.MILLISECONDS));
}
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();
}
Aggregations