Search in sources :

Example 1 with Interceptor

use of org.eclipse.jetty.server.HttpOutput.Interceptor in project jetty.project by eclipse.

the class BufferedResponseHandler method handle.

/* ------------------------------------------------------------ */
/**
     * @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    ServletContext context = baseRequest.getServletContext();
    String path = context == null ? baseRequest.getRequestURI() : URIUtil.addPaths(baseRequest.getServletPath(), baseRequest.getPathInfo());
    LOG.debug("{} handle {} in {}", this, baseRequest, context);
    HttpOutput out = baseRequest.getResponse().getHttpOutput();
    // Are we already being gzipped?
    HttpOutput.Interceptor interceptor = out.getInterceptor();
    while (interceptor != null) {
        if (interceptor instanceof BufferedInterceptor) {
            LOG.debug("{} already intercepting {}", this, request);
            _handler.handle(target, baseRequest, request, response);
            return;
        }
        interceptor = interceptor.getNextInterceptor();
    }
    // If not a supported method - no Vary because no matter what client, this URI is always excluded
    if (!_methods.test(baseRequest.getMethod())) {
        LOG.debug("{} excluded by method {}", this, request);
        _handler.handle(target, baseRequest, request, response);
        return;
    }
    // Use pathInfo because this is be
    if (!isPathBufferable(path)) {
        LOG.debug("{} excluded by path {}", this, request);
        _handler.handle(target, baseRequest, request, response);
        return;
    }
    // If the mime type is known from the path, then apply mime type filtering 
    String mimeType = context == null ? MimeTypes.getDefaultMimeByExtension(path) : context.getMimeType(path);
    if (mimeType != null) {
        mimeType = MimeTypes.getContentTypeWithoutCharset(mimeType);
        if (!isMimeTypeBufferable(mimeType)) {
            LOG.debug("{} excluded by path suffix mime type {}", this, request);
            // handle normally without setting vary header
            _handler.handle(target, baseRequest, request, response);
            return;
        }
    }
    // install interceptor and handle
    out.setInterceptor(new BufferedInterceptor(baseRequest.getHttpChannel(), out.getInterceptor()));
    if (_handler != null)
        _handler.handle(target, baseRequest, request, response);
}
Also used : ServletContext(javax.servlet.ServletContext) Interceptor(org.eclipse.jetty.server.HttpOutput.Interceptor) HttpOutput(org.eclipse.jetty.server.HttpOutput)

Example 2 with Interceptor

use of org.eclipse.jetty.server.HttpOutput.Interceptor in project jetty.project by eclipse.

the class HttpOutputTest method testWriteInterception.

@Test
public void testWriteInterception() throws Exception {
    final Resource big = Resource.newClassPathResource("simple/big.txt");
    _handler._writeLengthIfKnown = false;
    _handler._content = BufferUtil.toBuffer(big, false);
    _handler._arrayBuffer = new byte[1024];
    _handler._interceptor = new ChainedInterceptor() {

        Interceptor _next;

        @Override
        public void write(ByteBuffer content, boolean complete, Callback callback) {
            String s = BufferUtil.toString(content).toUpperCase().replaceAll("BIG", "BIGGER");
            _next.write(BufferUtil.toBuffer(s), complete, callback);
        }

        @Override
        public boolean isOptimizedForDirectBuffers() {
            return _next.isOptimizedForDirectBuffers();
        }

        @Override
        public Interceptor getNextInterceptor() {
            return _next;
        }

        @Override
        public void setNext(Interceptor interceptor) {
            _next = interceptor;
        }
    };
    String response = _connector.getResponse("GET / HTTP/1.0\nHost: localhost:80\n\n");
    assertThat(response, containsString("HTTP/1.1 200 OK"));
    assertThat(response, Matchers.not(containsString("Content-Length")));
    assertThat(response, containsString("400\tTHIS IS A BIGGER FILE"));
}
Also used : Callback(org.eclipse.jetty.util.Callback) Resource(org.eclipse.jetty.util.resource.Resource) Matchers.containsString(org.hamcrest.Matchers.containsString) Interceptor(org.eclipse.jetty.server.HttpOutput.Interceptor) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

Interceptor (org.eclipse.jetty.server.HttpOutput.Interceptor)2 ByteBuffer (java.nio.ByteBuffer)1 ServletContext (javax.servlet.ServletContext)1 HttpOutput (org.eclipse.jetty.server.HttpOutput)1 Callback (org.eclipse.jetty.util.Callback)1 Resource (org.eclipse.jetty.util.resource.Resource)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1