App { public JsonObject renderFragment(HttpRequest request, HttpResponse response) { String uriWithoutContextPath = request.getUriWithoutContextPath(); String uriPart = uriWithoutContextPath.substring(UriUtils.FRAGMENTS_URI_PREFIX.length()); String fragmentName = NameUtils.getFullyQualifiedName(rootComponent.getName(), uriPart); Fragment fragment = lookup.getAllFragments().get(fragmentName); if (fragment == null) { throw new FragmentNotFoundException("Requested fragment '" + fragmentName + "' does not exists."); } Model model = new MapModel(request.getFormParams()); RequestLookup requestLookup = createRequestLookup(request, response); API api = new API(sessionManager, authorizer, requestLookup); JsonObject output = new JsonObject(); output.addProperty("html", fragment.render(model, lookup, requestLookup, api)); output.addProperty(Placeholder.headJs.name(), requestLookup.getPlaceholderContent(Placeholder.headJs).orElse(null)); output.addProperty(Placeholder.js.name(), requestLookup.getPlaceholderContent(Placeholder.js).orElse(null)); output.addProperty(Placeholder.css.name(), requestLookup.getPlaceholderContent(Placeholder.css).orElse(null)); return output; } App(String name, String contextPath, Set<Component> components, Set<Theme> themes,
               Configuration configuration, Bindings bindings, I18nResources i18nResources,
               SessionManager sessionManager, Authorizer authorizer); String getName(); String getContextPath(); Map<String, Component> getComponents(); Map<String, Fragment> getFragments(); Map<String, Layout> getLayouts(); Map<String, Theme> getThemes(); Configuration getConfiguration(); String renderPage(HttpRequest request, HttpResponse response); JsonObject renderFragment(HttpRequest request, HttpResponse response); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString();  }
@Test public void testRenderFragment() throws Exception { final String fragment1Content = "Fragment 1 content."; Fragment f1 = createFragment("cmp.f1", fragment1Content); final String fragment2Content = "Fragment 2 content."; Fragment f2 = createFragment("cmp.f2", fragment2Content); Component cmp = new Component("cmp", null, "/cmp", emptySortedSet(), ImmutableSet.of(f1, f2), emptySet(), emptySet(), null); Component rootComponent = new Component("root", null, Component.ROOT_COMPONENT_CONTEXT_PATH, emptySortedSet(), emptySet(), emptySet(), singleton(cmp), null); Configuration configuration = createConfiguration(); SessionManager sessionManager = createSessionManager(); App app = new App(null, "/test", ImmutableSet.of(cmp, rootComponent), emptySet(), configuration, null, null, sessionManager, null); HttpRequest request = createRequest(app.getContextPath(), "/fragments/cmp.f1"); when(request.getFormParams()).thenReturn(emptyMap()); JsonObject output = app.renderFragment(request, null); Assert.assertEquals(output.get("html").getAsString(), fragment1Content); request = createRequest(app.getContextPath(), "/fragments/cmp.f2"); when(request.getFormParams()).thenReturn(emptyMap()); output = app.renderFragment(request, null); Assert.assertEquals(output.get("html").getAsString(), fragment2Content); }