Search in sources :

Example 86 with AbstractHandler

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

the class HttpClientTest method testRequestAfterFailedRequest.

@Test
public void testRequestAfterFailedRequest() throws Exception {
    int length = FlowControlStrategy.DEFAULT_WINDOW_SIZE;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                response.getOutputStream().write(new byte[length]);
            } catch (IOException e) {
            }
        }
    });
    // Make a request with a large enough response buffer.
    org.eclipse.jetty.client.api.Request request = client.newRequest(newURI());
    FutureResponseListener listener = new FutureResponseListener(request, length);
    request.send(listener);
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), 200);
    // Make a request with a small response buffer, should fail.
    try {
        request = client.newRequest(newURI());
        listener = new FutureResponseListener(request, length / 10);
        request.send(listener);
        listener.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertThat(x.getMessage(), Matchers.containsString("Buffering capacity exceeded"));
    }
    // Verify that we can make another request.
    request = client.newRequest(newURI());
    listener = new FutureResponseListener(request, length);
    request.send(listener);
    response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), 200);
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ExecutionException(java.util.concurrent.ExecutionException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 87 with AbstractHandler

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

the class HttpClientTest method testOPTIONS.

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

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

Example 88 with AbstractHandler

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

the class HttpClientTest method testOPTIONSWithRelativeRedirect.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if ("*".equals(target)) {
                // Be nasty and send a relative redirect.
                // Code 303 will change the method to GET.
                response.setStatus(HttpStatus.SEE_OTHER_303);
                response.setHeader("Location", "/");
            }
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).method(HttpMethod.OPTIONS).path("*").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 89 with AbstractHandler

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

the class PublicAnnouncementManagerIntegrationTest method testAnnouncementsWork.

@Test
public void testAnnouncementsWork() throws Exception {
    final AtomicReference<byte[]> requestBody = new AtomicReference<>();
    try (HttpdForTests httpd = new HttpdForTests()) {
        httpd.addHandler(new AbstractHandler() {

            @Override
            public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
                httpServletResponse.setStatus(200);
                request.setHandled(true);
                if (request.getUri().getPath().equals("/status.php")) {
                    return;
                }
                requestBody.set(ByteStreams.toByteArray(httpServletRequest.getInputStream()));
                FrontendRequest thriftRequest = new FrontendRequest();
                ThriftUtil.deserialize(ThriftProtocol.BINARY, requestBody.get(), thriftRequest);
                assertTrue("Request should contain the repository.", thriftRequest.getAnnouncementRequest().getRepository().equals(REPOSITORY));
                try (DataOutputStream out = new DataOutputStream(httpServletResponse.getOutputStream())) {
                    Announcement announcement = new Announcement();
                    announcement.setErrorMessage(ERROR_MSG);
                    announcement.setSolutionMessage(SOLUTION_MSG);
                    AnnouncementResponse announcementResponse = new AnnouncementResponse();
                    announcementResponse.setAnnouncements(ImmutableList.of(announcement));
                    FrontendResponse frontendResponse = new FrontendResponse();
                    frontendResponse.setType(FrontendRequestType.ANNOUNCEMENT);
                    frontendResponse.setAnnouncementResponse(announcementResponse);
                    out.write(ThriftUtil.serialize(ThriftProtocol.BINARY, frontendResponse));
                }
            }
        });
        httpd.start();
        Clock clock = new DefaultClock();
        BuckEventBus eventBus = BuckEventBusFactory.newInstance(clock);
        ExecutionEnvironment executionEnvironment = new DefaultExecutionEnvironment(ImmutableMap.copyOf(System.getenv()), System.getProperties());
        BuckConfig buckConfig = new FakeBuckConfig.Builder().setSections(ImmutableMap.of("log", ImmutableMap.of("slb_server_pool", "http://localhost:" + httpd.getRootUri().getPort()))).build();
        TestConsole console = new TestConsole();
        SuperConsoleEventBusListener listener = new SuperConsoleEventBusListener(new SuperConsoleConfig(FakeBuckConfig.builder().build()), console, clock, /* verbosity */
        TestResultSummaryVerbosity.of(false, false), executionEnvironment, Optional.empty(), Locale.US, logPath, TimeZone.getTimeZone("UTC"));
        eventBus.register(listener);
        PublicAnnouncementManager manager = new PublicAnnouncementManager(clock, eventBus, listener, REPOSITORY, new RemoteLogBuckConfig(buckConfig), MoreExecutors.newDirectExecutorService());
        manager.getAndPostAnnouncements();
        Optional<String> announcements = listener.getPublicAnnouncements();
        assertEquals("The header and the message", announcements.get(), "**-------------------------------**\n" + "**- Sticky Public Announcements -**\n" + "**-------------------------------**\n" + "** This is the error message. This is the solution message.");
    }
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) RemoteLogBuckConfig(com.facebook.buck.util.network.RemoteLogBuckConfig) DefaultExecutionEnvironment(com.facebook.buck.util.environment.DefaultExecutionEnvironment) ExecutionEnvironment(com.facebook.buck.util.environment.ExecutionEnvironment) Announcement(com.facebook.buck.distributed.thrift.Announcement) AnnouncementResponse(com.facebook.buck.distributed.thrift.AnnouncementResponse) DataOutputStream(java.io.DataOutputStream) DefaultClock(com.facebook.buck.timing.DefaultClock) Clock(com.facebook.buck.timing.Clock) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) RemoteLogBuckConfig(com.facebook.buck.util.network.RemoteLogBuckConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) FakeBuckConfig(com.facebook.buck.cli.FakeBuckConfig) DefaultClock(com.facebook.buck.timing.DefaultClock) FrontendRequest(com.facebook.buck.distributed.thrift.FrontendRequest) HttpdForTests(com.facebook.buck.testutil.integration.HttpdForTests) DefaultExecutionEnvironment(com.facebook.buck.util.environment.DefaultExecutionEnvironment) Request(org.eclipse.jetty.server.Request) FrontendRequest(com.facebook.buck.distributed.thrift.FrontendRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) FrontendResponse(com.facebook.buck.distributed.thrift.FrontendResponse) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 90 with AbstractHandler

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

the class HttpProcess method start.

@Override
public void start() {
    writeTimeToFile("startingAt");
    ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    context.setClassLoader(Thread.currentThread().getContextClassLoader());
    server.setHandler(context);
    context.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
            if ("/ping".equals(target)) {
                request.setHandled(true);
                httpServletResponse.getWriter().print("ping");
            } else if ("/restart".equals(target)) {
                writeTimeToFile("restartAskedAt");
                request.setHandled(true);
                processCommands.askForRestart();
                httpServletResponse.getWriter().print("ok");
            } else if ("/kill".equals(target)) {
                writeTimeToFile("killedAt");
                System.exit(0);
            }
        }
    });
    try {
        server.start();
    } catch (Exception e) {
        throw new IllegalStateException("Fail to start Jetty", e);
    }
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) 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) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

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