use of org.apache.sling.scripting.core.ScriptHelper in project sling by apache.
the class ExportServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Map<String, String> options = createOptionMap(request);
ScriptHelper scriptHelper = new ScriptHelper(bundleContext, null, request, response);
try {
addScriptBindings(scriptHelper, request, response);
String exported;
try {
exported = accessor.getExportedString(request, options, modelFactory, exporterName);
} catch (ExportException e) {
logger.error("Could not perform export with " + exporterName + " requested by model.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
} catch (MissingExporterException e) {
logger.error("Could not get exporter " + exporterName + " requested by model.", e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
if (exported == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
response.setContentType(request.getResponseContentType());
response.getWriter().write(exported);
} finally {
scriptHelper.cleanup();
}
}
use of org.apache.sling.scripting.core.ScriptHelper 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