Java Code Examples for org.apache.sling.api.scripting.SlingScriptHelper#getService()

The following examples show how to use org.apache.sling.api.scripting.SlingScriptHelper#getService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IsProductDetailPageServlet.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    final Config cfg = new Config(request.getResource());
    final SlingScriptHelper sling = ((SlingBindings) request.getAttribute(SlingBindings.class.getName())).getSling();
    final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request);
    final String path = ex.getString(cfg.get("path", String.class));
    boolean decision = isProductDetailPage(path, request.getResourceResolver());
    request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(decision));
    if (decision) {
        prepareCatalogPathProperty(path, request);
    }
}
 
Example 2
Source File: IsProductListPageServlet.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    final Config cfg = new Config(request.getResource());
    final SlingScriptHelper sling = ((SlingBindings) request.getAttribute(SlingBindings.class.getName())).getSling();
    final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request);
    final String path = ex.getString(cfg.get("path", String.class));
    boolean decision = isProductListPage(path, request.getResourceResolver());
    request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(decision));
    if (decision) {
        prepareCatalogPathProperty(path, request);
    }
}
 
Example 3
Source File: ConfigurationColumnPreview.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@PostConstruct
protected void initModel() {

    Config cfg = new Config(request.getResource());

    final SlingScriptHelper sling = ((SlingBindings) request.getAttribute(SlingBindings.class.getName())).getSling();
    ExpressionResolver expressionResolver = sling.getService(ExpressionResolver.class);
    final ExpressionHelper ex = new ExpressionHelper(expressionResolver, request);

    String itemResourcePath = ex.getString(cfg.get("path", String.class));
    LOG.debug("Item in preview is at path {}", itemResourcePath);

    itemResource = request.getResourceResolver().getResource(itemResourcePath);
    if (itemResource == null) {
        return;
    }
    isFolder = itemResource.isResourceType(JcrConstants.NT_FOLDER) || itemResource.isResourceType(JcrResourceConstants.NT_SLING_FOLDER)
        || itemResource
            .isResourceType(JcrResourceConstants.NT_SLING_ORDERED_FOLDER);

    if (isFolder) {
        properties = itemResource.getValueMap();
    } else {
        Resource jcrContent = itemResource.getChild(JcrConstants.JCR_CONTENT);
        properties = jcrContent != null ? jcrContent.getValueMap() : itemResource.getValueMap();
    }
}
 
Example 4
Source File: SearchDataSourceServlet.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    final String PARAMETER_OFFSET = "_commerce_offset";
    final String PARAMETER_LIMIT = "_commerce_limit";
    final String PARAMETER_COMMERCE_TYPE = "_commerce_commerce_type";
    final Config cfg = new Config(request.getResource().getChild(Config.DATASOURCE));
    final SlingScriptHelper sling = ((SlingBindings) request.getAttribute(SlingBindings.class.getName())).getSling();
    final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request);
    final String itemRT = cfg.get("itemResourceType", String.class);
    final long offset = ex.get(cfg.get("offset", "0"), long.class);
    final long limit = ex.get(cfg.get("limit", "20"), long.class);
    final String commerceType = ex.get(cfg.get("commerceType", "product"), String.class);

    Map<String, Object> queryParameters = new HashMap<>(request.getParameterMap());
    queryParameters.put(PARAMETER_OFFSET, String.valueOf(offset));
    queryParameters.put(PARAMETER_LIMIT, String.valueOf(limit));
    queryParameters.put(PARAMETER_COMMERCE_TYPE, commerceType);

    final String rootPath = request.getParameter("root");

    String rootCategoryId = new CatalogSearchSupport(request.getResourceResolver()).findCategoryId(rootPath);
    if (rootCategoryId != null) {
        queryParameters.put(CATEGORY_ID_PARAMETER, rootCategoryId);
        queryParameters.put(CATEGORY_PATH_PARAMETER, rootPath);
    }
    String queryString = new ObjectMapper().writeValueAsString(queryParameters);
    try {
        Iterator<Resource> virtualResults = request.getResourceResolver().findResources(queryString, VIRTUAL_PRODUCT_QUERY_LANGUAGE);

        final DataSource ds = new SimpleDataSource(new TransformIterator<>(virtualResults, r -> new ResourceWrapper(r) {
            public String getResourceType() {
                return itemRT;
            }
        }));

        request.setAttribute(DataSource.class.getName(), ds);
    } catch (Exception x) {
        response.sendError(500, x.getMessage());
        LOGGER.error("Error finding resources", x);
    }
}
 
Example 5
Source File: Recaptcha.java    From publick-sling-blog with Apache License 2.0 5 votes vote down vote up
/**
 * Initialization of the component.
 *
 * Reads the resource, gets the properties and site key.
 *
 * Options for "theme" are:
 * <ul>
 * <li>dark
 * <li>light (default)
 * </ul>
 *
 * Options for "type" are:
 * <ul>
 * <li>audio
 * <li>image (default)
 * </ul>
 *
 * Options for "size" are:
 * <ul>
 * <li>compact
 * <li>normal (default)
 * </ul>
 */
@Override
public void activate() {
    SlingScriptHelper scriptHelper = getSlingScriptHelper();
    RecaptchaService recaptchaService = scriptHelper.getService(RecaptchaService.class);

    if (recaptchaService == null) {
        show = false;
    } else {
        resource = getResource();

        ValueMap properties = resource.adaptTo(ValueMap.class);
        String sizeProperty = properties.get(SIZE_PROPERTY, String.class);
        String themeProperty = properties.get(THEME_PROPERTY, String.class);
        String typeProperty = properties.get(TYPE_PROPERTY, String.class);
        boolean enableProperty = properties.get(ENABLE_PROPERTY, true);

        boolean enableService = recaptchaService.getEnabled();
        siteKey = recaptchaService.getSiteKey();

        if (enableService && enableProperty && StringUtils.isNotBlank(siteKey)) {
            show = true;

            if (THEME_DARK.equals(themeProperty)) {
                theme = themeProperty;
            }

            if (TYPE_AUDIO.equals(typeProperty)) {
                type = typeProperty;
            }

            if (SIZE_COMPACT.equals(sizeProperty)) {
                size = sizeProperty;
            }
        } else {
            show = false;
        }
    }
}
 
Example 6
Source File: BlogView.java    From publick-sling-blog with Apache License 2.0 5 votes vote down vote up
/**
 * Sightly component initialization.
 */
@Override
public void activate() {
    resource = getResource();
    request = getRequest();
    resolver = getResourceResolver();
    listView = Arrays.asList(request.getRequestPathInfo().getSelectors()).contains(LIST_VIEW_SELECTOR);

    SlingScriptHelper scriptHelper = getSlingScriptHelper();
    linkRewriter = scriptHelper.getService(LinkRewriterService.class);

    getBlog(resource);
}
 
Example 7
Source File: children.java    From commerce-cif-connector with Apache License 2.0 4 votes vote down vote up
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response){

        final SlingScriptHelper sling = getScriptHelper(request);

        final ExpressionHelper ex = new ExpressionHelper(sling.getService(ExpressionResolver.class), request);
        final Config dsCfg = new Config(request.getResource().getChild(Config.DATASOURCE));
        final CommerceBasePathsService cbps = sling.getService(CommerceBasePathsService.class);

        final String query = ex.getString(dsCfg.get("query", String.class));

        final String parentPath;
        final String searchName;
        final String rootPath = ex.getString(dsCfg.get("rootPath", cbps.getProductsBasePath()));

        if (query != null) {
            final int slashIndex = query.lastIndexOf('/');
            if (slashIndex < 0) {
                parentPath = rootPath;
                searchName = query.toLowerCase();
            } else if (!query.startsWith(rootPath)) {
                parentPath = rootPath;
                searchName = null;
            } else if (slashIndex == query.length() - 1) {
                parentPath = query;
                searchName = null;
            } else {
                parentPath = query.substring(0, slashIndex + 1);
                searchName = query.substring(slashIndex + 1).toLowerCase();
            }
        } else {
            parentPath = ex.getString(dsCfg.get("path", String.class));
            searchName = null;
        }

        final Resource parent = request.getResourceResolver().getResource(parentPath);

        final DataSource ds;
        if (parent == null) {
            ds = EmptyDataSource.instance();
        } else {
            final Integer offset = ex.get(dsCfg.get("offset", String.class), Integer.class);
            final Integer limit = ex.get(dsCfg.get("limit", String.class), Integer.class);
            final String itemRT = dsCfg.get("itemResourceType", String.class);
            final String filter = ex.getString(dsCfg.get("filter", String.class));

            final Collection<Predicate<Resource>> predicates = new ArrayList<>(2);
            predicates.add(createPredicate(filter));

            if (searchName != null) {
                final Pattern searchNamePattern = Pattern.compile(Pattern.quote(searchName), Pattern.CASE_INSENSITIVE);
                predicates.add(resource -> searchNamePattern.matcher(resource.getName()).lookingAt());
            }

            final Predicate predicate = PredicateUtils.allPredicate(predicates);
            final Transformer transformer = createTransformer(itemRT, predicate);


            final List<Resource> list;
            if (FILTER_CATEGORY.equals(filter)) {
                class CategoryFinder extends AbstractResourceVisitor {
                    private CategoryPredicate categoryPredicate = new CategoryPredicate();
                    private List<Resource> categories = new ArrayList<Resource>();
                    @Override
                    protected void visit(Resource res) {
                        if (categoryPredicate.evaluate(res)) {
                            categories.add(res);
                        }
                    }
                };
                CategoryFinder categoryFinder = new CategoryFinder();
                categoryFinder.accept(parent);
                list = IteratorUtils.toList(new FilterIterator(categoryFinder.categories.iterator(), predicate));
            } else {
                list =IteratorUtils.toList(new FilterIterator(parent.listChildren(), predicate));
            }

            //force reloading the children of the root node to hit the virtual resource provider
            if (rootPath.equals(parentPath)) {
                for (int i = 0; i < list.size(); i++) {
                    list.set(i, request.getResourceResolver().getResource(list.get(i).getPath()));
                }
            }

            @SuppressWarnings("unchecked")
            DataSource datasource = new AbstractDataSource() {

                public Iterator<Resource> iterator() {
                    Collections.sort(list, Comparator.comparing(Resource::getName));
                    return new TransformIterator(new PagingIterator<>(list.iterator(), offset, limit), transformer);
                }
            };

            ds = datasource;
        }

        request.setAttribute(DataSource.class.getName(), ds);
    }