use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class EditorTest method getItemsEditor.
private Editor getItemsEditor(String suffix) {
// get the carousel component node resource
Resource resource = context.resourceResolver().getResource(CAROUSEL_PATH);
// prepare the request object
final MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver(), context.bundleContext());
// set the suffix
MockRequestPathInfo requestPathInfo = (MockRequestPathInfo) request.getRequestPathInfo();
requestPathInfo.setSuffix(suffix);
// define the bindings
SlingBindings slingBindings = new SlingBindings();
slingBindings.put(SlingBindings.RESOURCE, resource);
if (resource != null) {
slingBindings.put(WCMBindings.PROPERTIES, resource.adaptTo(ValueMap.class));
}
slingBindings.put(WCMBindings.PAGE_MANAGER, context.pageManager());
request.setAttribute(SlingBindings.class.getName(), slingBindings);
// adapt to the class to test
return request.adaptTo(Editor.class);
}
use of org.apache.sling.api.scripting.SlingBindings in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class SearchResultServlet method doGet.
@Override
protected void doGet(@NotNull final SlingHttpServletRequest request, @NotNull final SlingHttpServletResponse response) throws IOException {
Page currentPage = Optional.ofNullable(request.getResourceResolver().adaptTo(PageManager.class)).map(pm -> pm.getContainingPage(request.getResource())).orElse(null);
if (currentPage != null) {
SlingBindings bindings = new SlingBindings();
bindings.setSling(new ScriptHelper(bundleContext, null, request, response));
request.setAttribute(SlingBindings.class.getName(), bindings);
Search searchComponent = getSearchComponent(request, currentPage);
try {
List<ListItem> results = getResults(request, searchComponent, currentPage.getPageManager());
response.setContentType("application/json");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
new ObjectMapper().writeValue(response.getWriter(), results);
} catch (NumberFormatException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
Aggregations