Search in sources :

Example 1 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project buck by facebook.

the class TraceHandlerDelegate method getDataForRequest.

@Override
@Nullable
public SoyMapData getDataForRequest(Request baseRequest) throws IOException {
    String path = baseRequest.getPathInfo();
    Matcher matcher = TraceDataHandler.ID_PATTERN.matcher(path);
    if (!matcher.matches()) {
        return null;
    }
    SoyMapData templateData = new SoyMapData();
    String id = matcher.group(1);
    templateData.put("traceId", id);
    // Read the args to `buck` out of the Chrome Trace.
    TraceAttributes traceAttributes = buildTraces.getTraceAttributesFor(id);
    templateData.put("dateTime", traceAttributes.getFormattedDateTime());
    if (traceAttributes.getCommand().isPresent()) {
        templateData.put("command", traceAttributes.getCommand().get());
    }
    return templateData;
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) Matcher(java.util.regex.Matcher) TraceAttributes(com.facebook.buck.util.trace.BuildTraces.TraceAttributes) Nullable(javax.annotation.Nullable)

Example 2 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project buck by facebook.

the class IndexHandlerDelegateTest method testIndexHandlerReturnsCorrectTemplateAndData.

@Test
public void testIndexHandlerReturnsCorrectTemplateAndData() throws IOException {
    Request baseRequest = EasyMock.createMock(Request.class);
    EasyMock.replay(baseRequest);
    IndexHandlerDelegate indexHandlerDelegate = new IndexHandlerDelegate();
    assertEquals("buck.index", indexHandlerDelegate.getTemplateForRequest(baseRequest));
    SoyMapData templateData = indexHandlerDelegate.getDataForRequest(baseRequest);
    assertTrue(templateData.getKeys().isEmpty());
    EasyMock.verify(baseRequest);
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) Request(org.eclipse.jetty.server.Request) Test(org.junit.Test)

Example 3 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project buck by facebook.

the class TemplateHandlerTest method testHandleSimpleRequest.

@Test
public void testHandleSimpleRequest() throws IOException, ServletException {
    TemplateHandlerDelegate delegate = new TemplateHandlerDelegate() {

        @Override
        public ImmutableSet<String> getTemplates() {
            return ImmutableSet.of("example.soy");
        }

        @Override
        public String getTemplateForRequest(Request baseRequest) {
            return "example.hello";
        }

        @Override
        public SoyMapData getDataForRequest(Request baseRequest) throws IOException {
            return new SoyMapData("name", "Michael");
        }
    };
    String target = "target";
    Request baseRequest = createMock(Request.class);
    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();
    replayAll();
    TemplateHandler handler = new TemplateHandler(delegate);
    handler.handle(target, baseRequest, request, response);
    verifyAll();
    assertEquals("Hello, Michael!", stringWriter.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SoyMapData(com.google.template.soy.data.SoyMapData) StringWriter(java.io.StringWriter) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 4 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project gerrit by GerritCodeReview.

the class IndexServlet method getTemplateData.

static SoyMapData getTemplateData(String canonicalURL, String cdnPath) throws URISyntaxException {
    String canonicalPath = computeCanonicalPath(canonicalURL);
    String staticPath = "";
    if (cdnPath != null) {
        staticPath = cdnPath;
    } else if (canonicalPath != null) {
        staticPath = canonicalPath;
    }
    // The resource path must be typed as safe for use in a script src.
    // TODO(wyatta): Upgrade this to use an appropriate safe URL type.
    SanitizedContent sanitizedStaticPath = UnsafeSanitizedContentOrdainer.ordainAsSafe(staticPath, SanitizedContent.ContentKind.TRUSTED_RESOURCE_URI);
    return new SoyMapData("canonicalPath", canonicalPath, "staticResourcePath", sanitizedStaticPath);
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) SanitizedContent(com.google.template.soy.data.SanitizedContent)

Example 5 with SoyMapData

use of com.google.template.soy.data.SoyMapData in project gerrit by GerritCodeReview.

the class IndexServletTest method noPathAndNoCDN.

@Test
public void noPathAndNoCDN() throws URISyntaxException {
    SoyMapData data = IndexServlet.getTemplateData("http://example.com/", null);
    assertThat(data.getSingle("canonicalPath").stringValue()).isEqualTo("");
    assertThat(data.getSingle("staticResourcePath").stringValue()).isEqualTo("");
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) Test(org.junit.Test)

Aggregations

SoyMapData (com.google.template.soy.data.SoyMapData)17 Test (org.junit.Test)9 SoyListData (com.google.template.soy.data.SoyListData)8 SoyTofu (com.google.template.soy.tofu.SoyTofu)3 TraceAttributes (com.facebook.buck.util.trace.BuildTraces.TraceAttributes)2 SoyFileSet (com.google.template.soy.SoyFileSet)2 Matcher (java.util.regex.Matcher)2 Nullable (javax.annotation.Nullable)2 Request (org.eclipse.jetty.server.Request)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Splitter (com.google.common.base.Splitter)1 Injector (com.google.inject.Injector)1 SoyModule (com.google.template.soy.SoyModule)1 SanitizedContent (com.google.template.soy.data.SanitizedContent)1 SoyData (com.google.template.soy.data.SoyData)1 SoyDataException (com.google.template.soy.data.SoyDataException)1 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)1 SoyMsgBundleHandler (com.google.template.soy.msgs.SoyMsgBundleHandler)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1