Search in sources :

Example 76 with Request

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

the class TraceHandlerDelegateTest method testMalformedPathInfoReturnsError.

@Test
public void testMalformedPathInfoReturnsError() throws IOException {
    Request baseRequest = createMock(Request.class);
    expect(baseRequest.getPathInfo()).andReturn("/..upADirectory");
    // Nothing on the helper should be invoked because the handler will error-out first.
    BuildTraces buildTraces = createMock(BuildTraces.class);
    replayAll();
    TraceHandlerDelegate traceHandler = new TraceHandlerDelegate(buildTraces);
    assertNull(traceHandler.getDataForRequest(baseRequest));
    verifyAll();
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) BuildTraces(com.facebook.buck.util.trace.BuildTraces) Test(org.junit.Test)

Example 77 with Request

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

the class TraceHandlerDelegateTest method testHandleGet.

@Test
public void testHandleGet() throws IOException, ServletException {
    Request baseRequest = createMock(Request.class);
    expect(baseRequest.getPathInfo()).andReturn("/abcdef");
    baseRequest.setHandled(true);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpServletResponse response = createMock(HttpServletResponse.class);
    response.setStatus(200);
    response.setContentType("text/html; charset=utf-8");
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    expect(response.getWriter()).andReturn(printWriter);
    response.flushBuffer();
    BuildTraces buildTraces = createMock(BuildTraces.class);
    expect(buildTraces.getTraceAttributesFor("abcdef")).andReturn(new TraceAttributes(Optional.of("buck build buck"), 2000L));
    Handler traceHandler = new TemplateHandler(new TraceHandlerDelegate(buildTraces));
    replayAll();
    traceHandler.handle("/trace/abcdef", baseRequest, request, response);
    verifyAll();
    String expectedScriptTag = "<script src=\"/tracedata/abcdef?callback=onTracesLoaded\">";
    String html = stringWriter.toString();
    assertThat(html, containsString(expectedScriptTag));
    assertThat(html, containsString("buck build buck"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) TraceAttributes(com.facebook.buck.util.trace.BuildTraces.TraceAttributes) Handler(org.eclipse.jetty.server.Handler) Matchers.containsString(org.hamcrest.Matchers.containsString) BuildTraces(com.facebook.buck.util.trace.BuildTraces) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 78 with Request

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

the class TracesHandlerTest method testHandleGet.

@Test
public void testHandleGet() throws IOException {
    BuildTraces buildTraces = createMock(BuildTraces.class);
    expect(buildTraces.getTraceAttributesFor(traceDir.resolve("build.a.trace"))).andReturn(new TraceAttributes(Optional.of("buck build buck"), 1000L));
    expect(buildTraces.getTraceAttributesFor(traceDir.resolve("build.b.trace"))).andReturn(new TraceAttributes(Optional.of("buck test --all --code-coverage"), 4000L));
    expect(buildTraces.getTraceAttributesFor(traceDir.resolve("build.c.trace"))).andReturn(new TraceAttributes(Optional.empty(), 2000L));
    expect(buildTraces.getTraceAttributesFor(traceDir.resolve("build.d.trace"))).andReturn(new TraceAttributes(Optional.of("buck test //test/com/facebook/buck/cli:cli"), 3000L));
    expect(buildTraces.listTraceFilesByLastModified()).andReturn(ImmutableList.of(traceDir.resolve("build.b.trace"), traceDir.resolve("build.d.trace"), traceDir.resolve("build.c.trace"), traceDir.resolve("build.a.trace")));
    Request baseRequest = createMock(Request.class);
    replayAll();
    TracesHandlerDelegate delegate = new TracesHandlerDelegate(buildTraces);
    TemplateHandler tracesHandler = new TemplateHandler(delegate);
    String html = tracesHandler.createHtmlForResponse(baseRequest);
    int indexB = html.indexOf("<a href=\"/trace/b\"><tt>build.b.trace</tt></a>");
    assertTrue(indexB > 0);
    int indexBCommand = html.indexOf("buck test --all --code-coverage");
    assertTrue(indexBCommand > 0);
    int indexD = html.indexOf("<a href=\"/trace/d\"><tt>build.d.trace</tt></a>");
    assertTrue(indexD > indexB);
    int indexDCommand = html.indexOf("buck test //test/com/facebook/buck/cli:cli");
    assertTrue(indexDCommand > indexBCommand);
    int indexC = html.indexOf("<a href=\"/trace/c\"><tt>build.c.trace</tt></a>");
    assertTrue(indexC > indexD);
    int indexA = html.indexOf("<a href=\"/trace/a\"><tt>build.a.trace</tt></a>");
    assertTrue(indexA > indexC);
    int indexACommand = html.indexOf("buck build buck");
    assertTrue(indexACommand > indexDCommand);
    verifyAll();
}
Also used : Request(org.eclipse.jetty.server.Request) TraceAttributes(com.facebook.buck.util.trace.BuildTraces.TraceAttributes) BuildTraces(com.facebook.buck.util.trace.BuildTraces) Test(org.junit.Test)

Example 79 with Request

use of org.eclipse.jetty.server.Request 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)

Example 80 with Request

use of org.eclipse.jetty.server.Request in project pinpoint by naver.

the class AbstractServerHandleInterceptor method createTrace.

private Trace createTrace(Object target, Object[] args) {
    final Request request = getRequest(args);
    final String requestURI = request.getRequestURI();
    if (excludeUrlFilter.filter(requestURI)) {
        if (isTrace) {
            logger.trace("filter requestURI:{}", requestURI);
        }
        return null;
    }
    // check sampling flag from client. If the flag is false, do not sample this request.
    final boolean sampling = samplingEnable(request);
    if (!sampling) {
        // Even if this transaction is not a sampling target, we have to create Trace object to mark 'not sampling'.
        // For example, if this transaction invokes rpc call, we can add parameter to tell remote node 'don't sample this transaction'
        final Trace trace = traceContext.disableSampling();
        if (isDebug) {
            logger.debug("remotecall sampling flag found. skip trace requestUrl:{}, remoteAddr:{}", request.getRequestURI(), request.getRemoteAddr());
        }
        return trace;
    }
    final TraceId traceId = populateTraceIdFromRequest(request);
    if (traceId != null) {
        final Trace trace = traceContext.continueTraceObject(traceId);
        if (trace.canSampled()) {
            SpanRecorder recorder = trace.getSpanRecorder();
            recordRootSpan(recorder, request);
            if (isDebug) {
                logger.debug("TraceID exist. continue trace. traceId:{}, requestUrl:{}, remoteAddr:{}", traceId, request.getRequestURI(), request.getRemoteAddr());
            }
        } else {
            if (isDebug) {
                logger.debug("TraceID exist. camSampled is false. skip trace. traceId:{}, requestUrl:{}, remoteAddr:{}", traceId, request.getRequestURI(), request.getRemoteAddr());
            }
        }
        return trace;
    } else {
        final Trace trace = traceContext.newTraceObject();
        if (trace.canSampled()) {
            SpanRecorder recorder = trace.getSpanRecorder();
            recordRootSpan(recorder, request);
            if (isDebug) {
                logger.debug("TraceID not exist. start new trace. requestUrl:{}, remoteAddr:{}", request.getRequestURI(), request.getRemoteAddr());
            }
        } else {
            if (isDebug) {
                logger.debug("TraceID not exist. camSampled is false. skip trace. requestUrl:{}, remoteAddr:{}", request.getRequestURI(), request.getRemoteAddr());
            }
        }
        return trace;
    }
}
Also used : Request(org.eclipse.jetty.server.Request)

Aggregations

Request (org.eclipse.jetty.server.Request)297 HttpServletRequest (javax.servlet.http.HttpServletRequest)250 HttpServletResponse (javax.servlet.http.HttpServletResponse)225 Test (org.junit.Test)177 IOException (java.io.IOException)169 ServletException (javax.servlet.ServletException)150 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)147 CountDownLatch (java.util.concurrent.CountDownLatch)66 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)64 InterruptedIOException (java.io.InterruptedIOException)45 Server (org.eclipse.jetty.server.Server)41 InputStream (java.io.InputStream)37 AtomicReference (java.util.concurrent.atomic.AtomicReference)35 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Response (org.eclipse.jetty.client.api.Response)27 Result (org.eclipse.jetty.client.api.Result)27 ServletInputStream (javax.servlet.ServletInputStream)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)23 ServletOutputStream (javax.servlet.ServletOutputStream)23 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)23