Search in sources :

Example 6 with BuildTraces

use of com.facebook.buck.util.trace.BuildTraces in project buck by facebook.

the class WebServer method createHandlers.

@VisibleForTesting
ImmutableList<ContextHandler> createHandlers() {
    Map<String, Handler> contextPathToHandler = Maps.newHashMap();
    contextPathToHandler.put(INDEX_CONTEXT_PATH, new TemplateHandler(new IndexHandlerDelegate()));
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase(staticContentDirectory);
    contextPathToHandler.put(STATIC_CONTEXT_PATH, resourceHandler);
    // Handlers for traces.
    BuildTraces buildTraces = new BuildTraces(projectFilesystem);
    contextPathToHandler.put(TRACE_CONTEXT_PATH, new TemplateHandler(new TraceHandlerDelegate(buildTraces)));
    contextPathToHandler.put(TRACES_CONTEXT_PATH, new TemplateHandler(new TracesHandlerDelegate(buildTraces)));
    contextPathToHandler.put(TRACE_DATA_CONTEXT_PATH, new TraceDataHandler(buildTraces));
    contextPathToHandler.put(ARTIFACTS_CONTEXT_PATH, artifactCacheHandler);
    ImmutableList.Builder<ContextHandler> handlers = ImmutableList.builder();
    for (Map.Entry<String, Handler> entry : contextPathToHandler.entrySet()) {
        String contextPath = entry.getKey();
        Handler handler = entry.getValue();
        ContextHandler contextHandler = new ContextHandler(contextPath);
        contextHandler.setHandler(handler);
        handlers.add(contextHandler);
    }
    // Create a handler that acts as a WebSocket server.
    ServletContextHandler servletContextHandler = new ServletContextHandler(/* parent */
    server, /* contextPath */
    "/ws", /* sessions */
    true, /* security */
    false);
    servletContextHandler.addServlet(new ServletHolder(streamingWebSocketServlet), "/build");
    handlers.add(servletContextHandler);
    return handlers.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) BuildTraces(com.facebook.buck.util.trace.BuildTraces) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with BuildTraces

use of com.facebook.buck.util.trace.BuildTraces in project buck by facebook.

the class TraceDataHandlerTest method testHandleGetWithCallback.

@Test
public void testHandleGetWithCallback() throws IOException, ServletException {
    Request baseRequest = createMock(Request.class);
    expect(baseRequest.getMethod()).andReturn("GET");
    expect(baseRequest.getPathInfo()).andReturn("/abcdef");
    expect(baseRequest.getParameter("callback")).andReturn("my.callback");
    baseRequest.setHandled(true);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpServletResponse response = createMock(HttpServletResponse.class);
    response.setStatus(200);
    response.setContentType("application/javascript; charset=utf-8");
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    expect(response.getWriter()).andReturn(printWriter);
    response.flushBuffer();
    BuildTraces buildTraces = createMock(BuildTraces.class);
    Iterable<InputStream> traces = Arrays.asList(new ByteArrayInputStream("{\"foo\":\"bar\"}".getBytes(Charsets.UTF_8)));
    expect(buildTraces.getInputsForTraces("abcdef")).andReturn(traces);
    TraceDataHandler traceDataHandler = new TraceDataHandler(buildTraces);
    replayAll();
    traceDataHandler.handle("/trace/abcdef?callback=my.callback", baseRequest, request, response);
    verifyAll();
    assertEquals("my.callback([{\"foo\":\"bar\"}]);\n", stringWriter.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) BuildTraces(com.facebook.buck.util.trace.BuildTraces) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 8 with BuildTraces

use of com.facebook.buck.util.trace.BuildTraces in project buck by facebook.

the class TraceDataHandlerTest method testHandleGetWithMultipleTrace.

@Test
public void testHandleGetWithMultipleTrace() throws IOException, ServletException {
    Request baseRequest = createMock(Request.class);
    expect(baseRequest.getMethod()).andReturn("GET");
    expect(baseRequest.getPathInfo()).andReturn("/abcdef");
    expect(baseRequest.getParameter("callback")).andReturn(null);
    baseRequest.setHandled(true);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpServletResponse response = createMock(HttpServletResponse.class);
    response.setStatus(200);
    response.setContentType("application/javascript; charset=utf-8");
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    expect(response.getWriter()).andReturn(printWriter);
    response.flushBuffer();
    BuildTraces buildTraces = createMock(BuildTraces.class);
    Iterable<InputStream> traces = Arrays.asList(new ByteArrayInputStream("{\"foo\":\"bar\"}".getBytes(Charsets.UTF_8)), new ByteArrayInputStream("{\"baz\":\"blech\"}".getBytes(Charsets.UTF_8)));
    expect(buildTraces.getInputsForTraces("abcdef")).andReturn(traces);
    TraceDataHandler traceDataHandler = new TraceDataHandler(buildTraces);
    replayAll();
    traceDataHandler.handle("/trace/abcdef", baseRequest, request, response);
    verifyAll();
    assertEquals("[{\"foo\":\"bar\"},{\"baz\":\"blech\"}]", stringWriter.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) BuildTraces(com.facebook.buck.util.trace.BuildTraces) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

BuildTraces (com.facebook.buck.util.trace.BuildTraces)8 Request (org.eclipse.jetty.server.Request)7 Test (org.junit.Test)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 TraceAttributes (com.facebook.buck.util.trace.BuildTraces.TraceAttributes)2 Handler (org.eclipse.jetty.server.Handler)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 Map (java.util.Map)1 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)1 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1