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;
}
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);
}
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());
}
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);
}
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("");
}
Aggregations