Search in sources :

Example 51 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientURITest method testAsteriskFormTarget.

@Test
public void testAsteriskFormTarget() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Assert.assertEquals("*", target);
            Assert.assertEquals("*", request.getPathInfo());
        }
    });
    Request request = client.newRequest("localhost", connector.getLocalPort()).method(HttpMethod.OPTIONS).scheme(scheme).path("*").timeout(5, TimeUnit.SECONDS);
    Assert.assertEquals("*", request.getPath());
    Assert.assertNull(request.getQuery());
    Fields params = request.getParams();
    Assert.assertEquals(0, params.getSize());
    Assert.assertNull(request.getURI());
    ContentResponse response = request.send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Fields(org.eclipse.jetty.util.Fields) Test(org.junit.Test)

Example 52 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class TerminatingRegexRuleTest method init.

@Before
public void init() throws Exception {
    rewriteHandler = new RewriteHandler();
    rewriteHandler.setServer(_server);
    rewriteHandler.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.setStatus(HttpStatus.CREATED_201);
            request.setAttribute("target", target);
            request.setAttribute("URI", request.getRequestURI());
            request.setAttribute("info", request.getPathInfo());
        }
    });
    rewriteHandler.start();
    TerminatingRegexRule rule1 = new TerminatingRegexRule();
    rule1.setRegex("^/login.jsp$");
    rewriteHandler.addRule(rule1);
    RedirectRegexRule rule2 = new RedirectRegexRule();
    rule2.setRegex("^/login.*$");
    rule2.setReplacement("http://login.company.com/");
    rewriteHandler.addRule(rule2);
    start(false);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Before(org.junit.Before)

Example 53 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class AbstractRuleTestCase method start.

protected void start(final boolean isSecure) throws Exception {
    _connector = new LocalConnector(_server);
    _connector.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration().addCustomizer(new HttpConfiguration.Customizer() {

        @Override
        public void customize(Connector connector, HttpConfiguration channelConfig, Request request) {
            request.setSecure(isSecure);
        }
    });
    _server.setConnectors(new Connector[] { _connector });
    _server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            _request = baseRequest;
            _response = _request.getResponse();
            try {
                _latch.await();
            } catch (InterruptedException e) {
                throw new ServletException(e);
            }
        }
    });
    _server.start();
    _latch = new CountDownLatch(1);
    _connector.executeRequest("GET / HTTP/1.0\nCookie: set=already\n\n");
    while (_response == null) Thread.sleep(1);
}
Also used : LocalConnector(org.eclipse.jetty.server.LocalConnector) Connector(org.eclipse.jetty.server.Connector) LocalConnector(org.eclipse.jetty.server.LocalConnector) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException)

Example 54 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class RewriteHandlerTest method init.

@Before
public void init() throws Exception {
    _handler = new RewriteHandler();
    _handler.setServer(_server);
    _handler.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.setStatus(201);
            request.setAttribute("target", target);
            request.setAttribute("URI", request.getRequestURI());
            request.setAttribute("info", request.getPathInfo());
        }
    });
    _handler.start();
    _rule1 = new RewritePatternRule();
    _rule1.setPattern("/aaa/*");
    _rule1.setReplacement("/bbb");
    _rule2 = new RewritePatternRule();
    _rule2.setPattern("/bbb/*");
    _rule2.setReplacement("/ccc");
    _rule3 = new RewritePatternRule();
    _rule3.setPattern("/ccc/*");
    _rule3.setReplacement("/ddd");
    _rule4 = new RewriteRegexRule();
    _rule4.setRegex("/xxx/(.*)");
    _rule4.setReplacement("/$1/zzz");
    _handler.setRules(new Rule[] { _rule1, _rule2, _rule3, _rule4 });
    start(false);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Before(org.junit.Before)

Example 55 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class ServerConnectorTimeoutTest method testHttpWriteIdleTimeout.

@Test
public void testHttpWriteIdleTimeout() throws Exception {
    _httpConfiguration.setBlockingTimeout(500);
    configureServer(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            IO.copy(request.getInputStream(), response.getOutputStream());
        }
    });
    Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
    client.setSoTimeout(10000);
    Assert.assertFalse(client.isClosed());
    final OutputStream os = client.getOutputStream();
    final InputStream is = client.getInputStream();
    final StringBuilder response = new StringBuilder();
    CompletableFuture<Void> responseFuture = CompletableFuture.runAsync(() -> {
        try (InputStreamReader reader = new InputStreamReader(is, UTF_8)) {
            int c;
            while ((c = reader.read()) != -1) {
                response.append((char) c);
            }
        } catch (IOException e) {
        // Valid path (as connection is forcibly closed)
        // t.printStackTrace(System.err);
        }
    });
    CompletableFuture<Void> requestFuture = CompletableFuture.runAsync(() -> {
        try {
            os.write(("POST /echo HTTP/1.0\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 20\r\n" + "\r\n").getBytes("utf-8"));
            os.flush();
            os.write("123456789\n".getBytes("utf-8"));
            os.flush();
            TimeUnit.SECONDS.sleep(1);
            os.write("=========\n".getBytes("utf-8"));
            os.flush();
        } catch (InterruptedException | IOException e) {
        // Valid path, as write of second half of content can fail
        // e.printStackTrace(System.err);
        }
    });
    try (StacklessLogging scope = new StacklessLogging(HttpChannel.class)) {
        requestFuture.get(2, TimeUnit.SECONDS);
        responseFuture.get(3, TimeUnit.SECONDS);
        Assert.assertThat(response.toString(), containsString(" 500 "));
        Assert.assertThat(response.toString(), Matchers.not(containsString("=========")));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)219 HttpServletRequest (javax.servlet.http.HttpServletRequest)216 HttpServletResponse (javax.servlet.http.HttpServletResponse)216 IOException (java.io.IOException)203 ServletException (javax.servlet.ServletException)203 Test (org.junit.Test)188 Request (org.eclipse.jetty.server.Request)121 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 CountDownLatch (java.util.concurrent.CountDownLatch)80 InterruptedIOException (java.io.InterruptedIOException)40 Result (org.eclipse.jetty.client.api.Result)38 InputStream (java.io.InputStream)35 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)32 Response (org.eclipse.jetty.client.api.Response)32 Request (org.eclipse.jetty.client.api.Request)29 OutputStream (java.io.OutputStream)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24