Search in sources :

Example 1 with ServletRequestEvent

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"));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletRequestEvent(javax.servlet.ServletRequestEvent) MultiPartInputStreamParser(org.eclipse.jetty.util.MultiPartInputStreamParser) File(java.io.File) Test(org.junit.Test)

Example 2 with ServletRequestEvent

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);
    }
}
Also used : ServletRequestEvent(javax.servlet.ServletRequestEvent) NotFoundFilterChain(org.opennms.container.web.felix.base.internal.dispatch.NotFoundFilterChain)

Example 3 with ServletRequestEvent

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestEvent(javax.servlet.ServletRequestEvent) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 4 with ServletRequestEvent

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();
    }
}
Also used : ServletRequestListener(javax.servlet.ServletRequestListener) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletRequestEvent(javax.servlet.ServletRequestEvent) Servlet(javax.servlet.Servlet) Test(org.junit.Test)

Example 5 with ServletRequestEvent

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);
        }
    });
}
Also used : ServletException(javax.servlet.ServletException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServletRequestEvent(javax.servlet.ServletRequestEvent) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

ServletRequestEvent (javax.servlet.ServletRequestEvent)22 ServletRequestListener (javax.servlet.ServletRequestListener)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 Test (org.junit.Test)7 ServletException (javax.servlet.ServletException)5 IOException (java.io.IOException)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 HttpSession (javax.servlet.http.HttpSession)3 EventListener (java.util.EventListener)2 ServletRequest (javax.servlet.ServletRequest)2 HttpSessionBindingEvent (javax.servlet.http.HttpSessionBindingEvent)2 ServletsMockitoUtil.createHttpSessionBindingEvent (jodd.petite.ServletsMockitoUtil.createHttpSessionBindingEvent)2 ServletsMockitoUtil.createServletRequestEvent (jodd.petite.ServletsMockitoUtil.createServletRequestEvent)2 Ses (jodd.petite.tst.Ses)2 RequestContextListener (jodd.servlet.RequestContextListener)2 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)2 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)2 MultiPartInputStreamParser (org.eclipse.jetty.util.MultiPartInputStreamParser)2