Search in sources :

Example 1 with ScriptHelper

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();
    }
}
Also used : MissingExporterException(org.apache.sling.models.factory.MissingExporterException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) ExportException(org.apache.sling.models.factory.ExportException)

Example 2 with ScriptHelper

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);
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) TypePredicateEvaluator(com.day.cq.search.eval.TypePredicateEvaluator) StringUtils(org.apache.commons.lang3.StringUtils) Page(com.day.cq.wcm.api.Page) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SearchResult(com.day.cq.search.result.SearchResult) PredicateConverter(com.day.cq.search.PredicateConverter) Map(java.util.Map) PageListItemImpl(com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl) Search(com.adobe.cq.wcm.core.components.models.Search) PN_FRAGMENT_VARIATION_PATH(com.adobe.cq.wcm.core.components.models.ExperienceFragment.PN_FRAGMENT_VARIATION_PATH) PathPredicateEvaluator(com.day.cq.search.eval.PathPredicateEvaluator) Session(javax.jcr.Session) Servlet(javax.servlet.Servlet) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) BundleContext(org.osgi.framework.BundleContext) Objects(java.util.Objects) PageManager(com.day.cq.wcm.api.PageManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Query(com.day.cq.search.Query) Optional(java.util.Optional) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) NotNull(org.jetbrains.annotations.NotNull) FulltextPredicateEvaluator(com.day.cq.search.eval.FulltextPredicateEvaluator) ModelFactory(org.apache.sling.models.factory.ModelFactory) LinkHandler(com.adobe.cq.wcm.core.components.internal.link.LinkHandler) HashMap(java.util.HashMap) LanguageManager(com.day.cq.wcm.api.LanguageManager) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) StreamSupport(java.util.stream.StreamSupport) Activate(org.osgi.service.component.annotations.Activate) SearchImpl(com.adobe.cq.wcm.core.components.internal.models.v1.SearchImpl) QueryBuilder(com.day.cq.search.QueryBuilder) Iterator(java.util.Iterator) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) LiveRelationshipManager(com.day.cq.wcm.msm.api.LiveRelationshipManager) LocalizationUtils(com.adobe.cq.wcm.core.components.internal.LocalizationUtils) PredicateGroup(com.day.cq.search.PredicateGroup) Template(com.day.cq.wcm.api.Template) NameConstants(com.day.cq.wcm.api.NameConstants) Reference(org.osgi.service.component.annotations.Reference) PageManager(com.day.cq.wcm.api.PageManager) SlingBindings(org.apache.sling.api.scripting.SlingBindings) Search(com.adobe.cq.wcm.core.components.models.Search) Page(com.day.cq.wcm.api.Page) ListItem(com.adobe.cq.wcm.core.components.models.ListItem) ScriptHelper(org.apache.sling.scripting.core.ScriptHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

LocalizationUtils (com.adobe.cq.wcm.core.components.internal.LocalizationUtils)1 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)1 PageListItemImpl (com.adobe.cq.wcm.core.components.internal.models.v1.PageListItemImpl)1 SearchImpl (com.adobe.cq.wcm.core.components.internal.models.v1.SearchImpl)1 PN_FRAGMENT_VARIATION_PATH (com.adobe.cq.wcm.core.components.models.ExperienceFragment.PN_FRAGMENT_VARIATION_PATH)1 ListItem (com.adobe.cq.wcm.core.components.models.ListItem)1 Search (com.adobe.cq.wcm.core.components.models.Search)1 PredicateConverter (com.day.cq.search.PredicateConverter)1 PredicateGroup (com.day.cq.search.PredicateGroup)1 Query (com.day.cq.search.Query)1 QueryBuilder (com.day.cq.search.QueryBuilder)1 FulltextPredicateEvaluator (com.day.cq.search.eval.FulltextPredicateEvaluator)1 PathPredicateEvaluator (com.day.cq.search.eval.PathPredicateEvaluator)1 TypePredicateEvaluator (com.day.cq.search.eval.TypePredicateEvaluator)1 SearchResult (com.day.cq.search.result.SearchResult)1 LanguageManager (com.day.cq.wcm.api.LanguageManager)1 NameConstants (com.day.cq.wcm.api.NameConstants)1 Page (com.day.cq.wcm.api.Page)1 PageManager (com.day.cq.wcm.api.PageManager)1 Template (com.day.cq.wcm.api.Template)1