Search in sources :

Example 51 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project tomcat by apache.

the class ApplicationFilterChain method internalDoFilter.

private void internalDoFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    // Call the next filter if there is one
    if (pos < n) {
        ApplicationFilterConfig filterConfig = filters[pos++];
        try {
            Filter filter = filterConfig.getFilter();
            if (request.isAsyncSupported() && "false".equalsIgnoreCase(filterConfig.getFilterDef().getAsyncSupported())) {
                request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE);
            }
            if (Globals.IS_SECURITY_ENABLED) {
                final ServletRequest req = request;
                final ServletResponse res = response;
                Principal principal = ((HttpServletRequest) req).getUserPrincipal();
                Object[] args = new Object[] { req, res, this };
                SecurityUtil.doAsPrivilege("doFilter", filter, classType, args, principal);
            } else {
                filter.doFilter(request, response, this);
            }
        } catch (IOException | ServletException | RuntimeException e) {
            throw e;
        } catch (Throwable e) {
            e = ExceptionUtils.unwrapInvocationTargetException(e);
            ExceptionUtils.handleThrowable(e);
            throw new ServletException(sm.getString("filterChain.filter"), e);
        }
        return;
    }
    // We fell off the end of the chain -- call the servlet instance
    try {
        if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
            lastServicedRequest.set(request);
            lastServicedResponse.set(response);
        }
        if (request.isAsyncSupported() && !servletSupportsAsync) {
            request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE);
        }
        // Use potentially wrapped request from this point
        if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse) && Globals.IS_SECURITY_ENABLED) {
            final ServletRequest req = request;
            final ServletResponse res = response;
            Principal principal = ((HttpServletRequest) req).getUserPrincipal();
            Object[] args = new Object[] { req, res };
            SecurityUtil.doAsPrivilege("service", servlet, classTypeUsedInService, args, principal);
        } else {
            servlet.service(request, response);
        }
    } catch (IOException | ServletException | RuntimeException e) {
        throw e;
    } catch (Throwable e) {
        e = ExceptionUtils.unwrapInvocationTargetException(e);
        ExceptionUtils.handleThrowable(e);
        throw new ServletException(sm.getString("filterChain.servlet"), e);
    } finally {
        if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
            lastServicedRequest.set(null);
            lastServicedResponse.set(null);
        }
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Filter(javax.servlet.Filter) Principal(java.security.Principal)

Example 52 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project tomcat by apache.

the class SSIFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    // cast once
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    // indicate that we're in SSI processing
    req.setAttribute(Globals.SSI_FLAG_ATTR, "true");
    // setup to capture output
    ByteArrayServletOutputStream basos = new ByteArrayServletOutputStream();
    ResponseIncludeWrapper responseIncludeWrapper = new ResponseIncludeWrapper(getServletContext(), req, res, basos);
    // process remainder of filter chain
    chain.doFilter(req, responseIncludeWrapper);
    // we can't assume the chain flushed its output
    responseIncludeWrapper.flushOutputStreamOrWriter();
    byte[] bytes = basos.toByteArray();
    // get content type
    String contentType = responseIncludeWrapper.getContentType();
    // is this an allowed type for SSI processing?
    if (contentTypeRegEx.matcher(contentType).matches()) {
        String encoding = res.getCharacterEncoding();
        // set up SSI processing
        SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(getServletContext(), req, res, isVirtualWebappRelative, debug, encoding);
        SSIProcessor ssiProcessor = new SSIProcessor(ssiExternalResolver, debug, allowExec);
        // prepare readers/writers
        Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes), encoding);
        ByteArrayOutputStream ssiout = new ByteArrayOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(ssiout, encoding));
        // do SSI processing
        long lastModified = ssiProcessor.process(reader, responseIncludeWrapper.getLastModified(), writer);
        // set output bytes
        writer.flush();
        bytes = ssiout.toByteArray();
        // override headers
        if (expires != null) {
            res.setDateHeader("expires", (new java.util.Date()).getTime() + expires.longValue() * 1000);
        }
        if (lastModified > 0) {
            res.setDateHeader("last-modified", lastModified);
        }
        res.setContentLength(bytes.length);
        Matcher shtmlMatcher = shtmlRegEx.matcher(responseIncludeWrapper.getContentType());
        if (shtmlMatcher.matches()) {
            // Convert shtml mime type to ordinary html mime type but preserve
            // encoding, if any.
            String enc = shtmlMatcher.group(1);
            res.setContentType("text/html" + ((enc != null) ? enc : ""));
        }
    }
    // write output
    OutputStream out = null;
    try {
        out = res.getOutputStream();
    } catch (IllegalStateException e) {
    // Ignore, will try to use a writer
    }
    if (out == null) {
        res.getWriter().write(new String(bytes));
    } else {
        out.write(bytes);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Example 53 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project tomcat by apache.

the class TestExpiresFilter method testUseDefaultConfiguration1.

@Test
public void testUseDefaultConfiguration1() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("image/jpeg");
            response.getWriter().print("Hello world");
        }
    };
    validate(servlet, Integer.valueOf(1 * 60));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 54 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project tomcat by apache.

the class TestExpiresFilter method testUseContentTypeWithoutCharsetExpiresConfiguration.

@Test
public void testUseContentTypeWithoutCharsetExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/xml; charset=iso-8859-1");
            response.getWriter().print("Hello world");
        }
    };
    validate(servlet, Integer.valueOf(5 * 60));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 55 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project tomcat by apache.

the class TestExpiresFilter method testUseContentTypeExpiresConfiguration.

@Test
public void testUseContentTypeExpiresConfiguration() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/xml; charset=utf-8");
            response.getWriter().print("Hello world");
        }
    };
    validate(servlet, Integer.valueOf(3 * 60));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)1635 HttpServletRequest (javax.servlet.http.HttpServletRequest)1312 Test (org.junit.Test)705 IOException (java.io.IOException)576 ServletException (javax.servlet.ServletException)491 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)223 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)195 Request (org.eclipse.jetty.server.Request)186 HttpServlet (javax.servlet.http.HttpServlet)157 CountDownLatch (java.util.concurrent.CountDownLatch)156 FilterChain (javax.servlet.FilterChain)148 PrintWriter (java.io.PrintWriter)138 Test (org.testng.annotations.Test)127 HashMap (java.util.HashMap)106 ServletOutputStream (javax.servlet.ServletOutputStream)105 InterruptedIOException (java.io.InterruptedIOException)97 InputStream (java.io.InputStream)85 OutputStream (java.io.OutputStream)81 HttpSession (javax.servlet.http.HttpSession)75 ServletResponse (javax.servlet.ServletResponse)74