use of javax.servlet.ServletRequestEvent in project jetty.project by eclipse.
the class RequestTest method testMultiPart.
@Test
public void testMultiPart() throws Exception {
final File testTmpDir = File.createTempFile("reqtest", null);
if (testTmpDir.exists())
testTmpDir.delete();
testTmpDir.mkdir();
testTmpDir.deleteOnExit();
assertTrue(testTmpDir.list().length == 0);
ContextHandler contextHandler = new ContextHandler();
contextHandler.setContextPath("/foo");
contextHandler.setResourceBase(".");
contextHandler.setHandler(new MultiPartRequestHandler(testTmpDir));
contextHandler.addEventListener(new MultiPartCleanerListener() {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
MultiPartInputStreamParser m = (MultiPartInputStreamParser) sre.getServletRequest().getAttribute(Request.__MULTIPART_INPUT_STREAM);
ContextHandler.Context c = (ContextHandler.Context) sre.getServletRequest().getAttribute(Request.__MULTIPART_CONTEXT);
assertNotNull(m);
assertNotNull(c);
assertTrue(c == sre.getServletContext());
assertTrue(!m.getParsedParts().isEmpty());
assertTrue(testTmpDir.list().length == 2);
super.requestDestroyed(sre);
String[] files = testTmpDir.list();
assertTrue(files.length == 0);
}
});
_server.stop();
_server.setHandler(contextHandler);
_server.start();
String multipart = "--AaB03x\r\n" + "content-disposition: form-data; name=\"field1\"\r\n" + "\r\n" + "Joe Blow\r\n" + "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"foo.upload\"\r\n" + "Content-Type: text/plain;charset=ISO-8859-1\r\n" + "\r\n" + "000000000000000000000000000000000000000000000000000\r\n" + "--AaB03x--\r\n";
String request = "GET /foo/x.html HTTP/1.1\r\n" + "Host: whatever\r\n" + "Content-Type: multipart/form-data; boundary=\"AaB03x\"\r\n" + "Content-Length: " + multipart.getBytes().length + "\r\n" + "Connection: close\r\n" + "\r\n" + multipart;
String responses = _connector.getResponse(request);
// System.err.println(responses);
assertTrue(responses.startsWith("HTTP/1.1 200"));
}
use of javax.servlet.ServletRequestEvent in project opennms by OpenNMS.
the class DispatcherServlet method service.
@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
final ServletRequestEvent sre = new ServletRequestEvent(getServletContext(), req);
this.controller.getRequestListener().requestInitialized(sre);
try {
req = new AttributeEventRequest(getServletContext(), this.controller.getRequestAttributeListener(), req);
this.controller.getDispatcher().dispatch(req, res, new NotFoundFilterChain());
} finally {
this.controller.getRequestListener().requestDestroyed(sre);
}
}
use of javax.servlet.ServletRequestEvent in project opennms by OpenNMS.
the class DispatcherFilter method doFilter.
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
final ServletRequestEvent sre = new ServletRequestEvent(m_filterConfig.getServletContext(), request);
m_controller.getRequestListener().requestInitialized(sre);
try {
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
final HttpServletRequest req = new AttributeEventRequest(m_filterConfig.getServletContext(), m_controller.getRequestAttributeListener(), (HttpServletRequest) request);
m_controller.getDispatcher().dispatch(req, (HttpServletResponse) response, chain);
} else {
chain.doFilter(request, response);
}
} finally {
m_controller.getRequestListener().requestDestroyed(sre);
}
}
use of javax.servlet.ServletRequestEvent in project felix by apache.
the class EventListenerTest method testServletRequestListener.
/**
* Tests {@link ServletRequestListener}s
*/
@Test
public void testServletRequestListener() throws Exception {
final List<String> list = new ArrayList<>();
final ServletRequestListener listener = new ServletRequestListener() {
@Override
public void requestDestroyed(ServletRequestEvent arg0) {
list.add("DESTROY");
}
@Override
public void requestInitialized(ServletRequestEvent arg0) {
list.add("INIT");
}
};
// register with default context
final ServiceRegistration<ServletRequestListener> reg1 = m_context.registerService(ServletRequestListener.class, listener, getListenerProps());
// register proprietary listener
final ServiceRegistration<ServletRequestListener> reg2 = m_context.registerService(ServletRequestListener.class, listener, null);
// register test servlet with default context
ServiceRegistration<Servlet> regS = m_context.registerService(Servlet.class, new TestServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setStatus(SC_OK);
resp.flushBuffer();
}
}, getServletProps("/test"));
try {
assertEquals(0, list.size());
assertContent(SC_OK, null, createURL("/test"));
assertEquals(2, list.size());
assertEquals("INIT", list.get(0));
assertEquals("DESTROY", list.get(1));
} finally {
reg1.unregister();
reg2.unregister();
regS.unregister();
}
}
use of javax.servlet.ServletRequestEvent in project polymap4-core by Polymap4.
the class GeoServerServlet method service.
protected void service(final HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final String servletPath = req.getServletPath();
String pathInfo = req.getPathInfo();
log.info("REQUEST: servletPath=" + servletPath + ", pathInfo=" + pathInfo + " ? " + req.getQueryString());
// schemas
if (pathInfo != null && pathInfo.startsWith("/schemas")) {
String resName = req.getPathInfo().substring(1);
URL res = GeoServerPlugin.instance().getBundle().getResource(resName);
if (res != null) {
try (InputStream in = res.openStream();
OutputStream out = resp.getOutputStream()) {
IOUtils.copy(in, out);
out.flush();
resp.flushBuffer();
}
} else {
log.warn("No such resource found: " + resName);
}
return;
}
String origin = req.getHeader("Origin");
resp.addHeader("Access-Control-Allow-Origin", StringUtils.isBlank(origin) ? "*" : origin);
resp.addHeader("Access-Control-Allow-Headers", req.getHeader("Access-Control-Request-Headers"));
resp.addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
// FIXME bad hack to work around an unresolved issue: after last commits
// it seems that in geoserver code the actual thread is working with a wrong
// request; it does not find serve und request param, even if they are there.
// This happens *only* behind mapzone dispatcher and with re-used HttpClient
// connection.
resp.setHeader("Connection", "close");
// service
session.execute(() -> {
try {
EventManager.instance().publish(new ServletRequestEvent(getServletContext(), req));
instance.set(this);
response.set(resp);
dispatcher.service(req, resp);
} catch (ServletException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
response.set(null);
instance.set(null);
}
});
}
Aggregations