Search in sources :

Example 81 with Callback

use of org.eclipse.jetty.util.Callback 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

Callback (org.eclipse.jetty.util.Callback)81 Test (org.junit.Test)71 CountDownLatch (java.util.concurrent.CountDownLatch)68 HttpFields (org.eclipse.jetty.http.HttpFields)52 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)52 Stream (org.eclipse.jetty.http2.api.Stream)51 MetaData (org.eclipse.jetty.http.MetaData)50 Session (org.eclipse.jetty.http2.api.Session)50 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)46 HttpServletResponse (javax.servlet.http.HttpServletResponse)44 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)41 IOException (java.io.IOException)39 FuturePromise (org.eclipse.jetty.util.FuturePromise)39 ByteBuffer (java.nio.ByteBuffer)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)37 ServletException (javax.servlet.ServletException)34 Promise (org.eclipse.jetty.util.Promise)30 ServletOutputStream (javax.servlet.ServletOutputStream)26 HttpServlet (javax.servlet.http.HttpServlet)21 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)20