Search in sources :

Example 21 with RequestParameter

use of org.apache.sling.api.request.RequestParameter in project acs-aem-commons by Adobe-Consulting-Services.

the class PropertyMergePostProcessor method getPropertyMerges.

/**
 * Gets the corresponding list of PropertyMerge directives from the
 * RequestParams.
 *
 * @param requestParameters the Request Param Map
 * @return a list of the PropertyMerge directives by Destination
 */
@SuppressWarnings("squid:S3776")
private List<PropertyMerge> getPropertyMerges(final SlingHttpServletRequest request) {
    final RequestParameterMap requestParameters = request.getRequestParameterMap();
    final HashMap<String, Set<String>> mapping = new HashMap<>();
    boolean isBulkUpdate = Boolean.valueOf(getParamValue(requestParameters, "dam:bulkUpdate"));
    // Collect the Destination / Source mappings
    requestParameters.forEach((key, values) -> {
        if (!StringUtils.endsWith(key, AT_SUFFIX)) {
            // Not a @PropertyMerge request param
            return;
        }
        Function<String, String> stripPrefix = (s -> StringUtils.removeStart(StringUtils.stripToNull(s), IGNORE_PREFIX));
        final String source = stripPrefix.apply(StringUtils.substringBefore(key, AT_SUFFIX));
        Stream.of(values).map(RequestParameter::getString).map(stripPrefix).filter(Objects::nonNull).forEach(destination -> {
            if (source.equalsIgnoreCase(OPERATION_ALL_TAGS)) {
                // if this is a request for merging all tags, look at everyting that might be a tag
                trackAllTagsMergeParameters(request, destination, mapping);
            } else if (isBulkUpdate) {
                // if this is a DAM bulk update, search all request params ending with this value
                trackAssetMergeParameters(requestParameters, source, destination, mapping);
            } else {
                trackMergeParameters(mapping, source, destination);
            }
        });
    });
    // Convert the Mappings into PropertyMerge objects
    return mapping.entrySet().stream().map(entry -> new PropertyMerge(entry.getKey(), entry.getValue(), areDuplicatesAllowed(requestParameters, entry.getKey()), getFieldTypeHint(requestParameters, entry.getKey()))).collect(Collectors.toList());
}
Also used : RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Function(java.util.function.Function) TagManager(com.day.cq.tagging.TagManager) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Calendar(java.util.Calendar) PersistenceException(org.apache.sling.api.resource.PersistenceException) LinkedHashSet(java.util.LinkedHashSet) JcrConstants(org.apache.jackrabbit.JcrConstants) Logger(org.slf4j.Logger) Collection(java.util.Collection) Resource(org.apache.sling.api.resource.Resource) Set(java.util.Set) Component(org.apache.felix.scr.annotations.Component) RequestParameter(org.apache.sling.api.request.RequestParameter) Collectors(java.util.stream.Collectors) Service(org.apache.felix.scr.annotations.Service) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) SlingPostProcessor(org.apache.sling.servlets.post.SlingPostProcessor) Modification(org.apache.sling.servlets.post.Modification) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashMap(java.util.HashMap)

Example 22 with RequestParameter

use of org.apache.sling.api.request.RequestParameter in project acs-aem-commons by Adobe-Consulting-Services.

the class RTEConfigurationServlet method writeConfigResource.

private void writeConfigResource(Resource resource, String rteName, SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, JSONException, ServletException {
    JSONObject widget = createEmptyWidget(rteName);
    // these two size properties seem to be necessary to get the size correct
    // in a component dialog
    widget.put("width", RTE_WIDTH);
    widget.put("height", RTE_HEIGHT);
    RequestParameterMap map = request.getRequestParameterMap();
    for (Map.Entry<String, RequestParameter[]> entry : map.entrySet()) {
        String key = entry.getKey();
        RequestParameter[] params = entry.getValue();
        if (params != null) {
            if (params.length > 1 || EXTERNAL_STYLESHEETS_PROPERTY.equals(key)) {
                JSONArray arr = new JSONArray();
                for (int i = 0; i < params.length; i++) {
                    arr.put(params[i].getString());
                }
                widget.put(key, arr);
            } else if (params.length == 1) {
                widget.put(key, params[0].getString());
            }
        }
    }
    if (widget.has("fieldLabel")) {
        widget.remove("hideLabel");
    }
    JSONObject config = toJSONObject(resource);
    if (config == null) {
        config = new JSONObject();
    }
    if (config.optBoolean("includeDefault")) {
        config = underlay(config, resource.getResourceResolver().getResource(DEFAULT_CONFIG));
    }
    widget.put("rtePlugins", config);
    JSONObject parent = new JSONObject();
    parent.put("xtype", "dialogfieldset");
    parent.put("border", false);
    parent.put("padding", 0);
    parent.accumulate("items", widget);
    parent.write(response.getWriter());
}
Also used : RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) JSONObject(org.apache.sling.commons.json.JSONObject) RequestParameter(org.apache.sling.api.request.RequestParameter) JSONArray(org.apache.sling.commons.json.JSONArray) RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) Map(java.util.Map)

Example 23 with RequestParameter

use of org.apache.sling.api.request.RequestParameter in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ModelElementsDataSourceServlet method doGet.

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) {
    // First try to get the model path from request parameters
    // otherwise determine model path from component resource.
    RequestParameter modelPathRequestParameter = request.getRequestParameter(PARAMETER_AND_PN_MODEL_PATH);
    boolean isOrderBy = request.getResource().isResourceType(RESOURCE_TYPE_ORDER_BY_V1);
    String modelPath;
    if (modelPathRequestParameter != null) {
        modelPath = modelPathRequestParameter.getString();
    } else {
        Config config = getConfig(request);
        ValueMap componentValueMap = getComponentValueMap(config, request);
        // get model path from component resource
        modelPath = componentValueMap != null ? componentValueMap.get(ContentFragmentList.PN_MODEL_PATH, String.class) : null;
    }
    DataSource dataSource = EmptyDataSource.instance();
    if (modelPath != null) {
        ResourceResolver resourceResolver = request.getResourceResolver();
        String pathToCFModelElements = String.format("%s/%s/model/cq:dialog/content/items", modelPath, JcrConstants.JCR_CONTENT);
        Resource cfModelElementRoot = resourceResolver.getResource(pathToCFModelElements);
        if (cfModelElementRoot != null) {
            Iterator<Resource> resourceIterator = cfModelElementRoot.listChildren();
            List<Resource> resourceList = new LinkedList<>();
            if (isOrderBy) {
                resourceList.add(createResource(resourceResolver, "Created", JcrConstants.JCR_CREATED));
                resourceList.add(createResource(resourceResolver, "Last Modified", JcrConstants.JCR_CONTENT + "/" + JcrConstants.JCR_LASTMODIFIED));
            }
            while (resourceIterator.hasNext()) {
                Resource elementResource = resourceIterator.next();
                ValueMap valueMap = elementResource.getValueMap();
                String valueValue = valueMap.get("name", "");
                String textValue = valueMap.get("fieldLabel", valueValue);
                if (isOrderBy && StringUtils.isNotEmpty(valueValue)) {
                    valueValue = "jcr:content/data/master/" + valueValue;
                }
                String metaType = valueMap.get("metaType", StringUtils.EMPTY);
                if (!isOrderBy || StringUtils.startsWith(metaType, "text-")) {
                    Resource syntheticResource = createResource(resourceResolver, textValue, valueValue);
                    resourceList.add(syntheticResource);
                }
            }
            dataSource = new SimpleDataSource(resourceList.iterator());
        }
    }
    request.setAttribute(DataSource.class.getName(), dataSource);
}
Also used : RequestParameter(org.apache.sling.api.request.RequestParameter) Config(com.adobe.granite.ui.components.Config) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) LinkedList(java.util.LinkedList) EmptyDataSource(com.adobe.granite.ui.components.ds.EmptyDataSource) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) DataSource(com.adobe.granite.ui.components.ds.DataSource)

Example 24 with RequestParameter

use of org.apache.sling.api.request.RequestParameter in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class PageImageThumbnail method initModel.

@PostConstruct
protected void initModel() {
    configPath = request.getRequestPathInfo().getResourcePath();
    componentPath = request.getRequestPathInfo().getSuffix();
    if (StringUtils.isBlank(componentPath)) {
        RequestParameter itemParam = request.getRequestParameter("item");
        if (itemParam == null) {
            log.error("Suffix and 'item' param are blank");
            return;
        }
        componentPath = itemParam.getString();
    }
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    if (pageManager == null) {
        log.error("pagemanager is null");
        return;
    }
    Resource component = resourceResolver.getResource(componentPath);
    if (component == null) {
        log.error("the component at {} does not exist", componentPath);
        return;
    }
    Page currentPage = pageManager.getContainingPage(component);
    if (currentPage != null) {
        currentPagePath = currentPage.getPath();
    }
    Page targetPage = null;
    RequestParameter pageLinkParam = request.getRequestParameter("pageLink");
    if (pageLinkParam != null) {
        // retrieve the page link from the request parameter
        String pageLink = pageLinkParam.getString();
        targetPage = pageManager.getPage(pageLink);
    } else {
        // retrieve the page link from the component model
        Teaser teaserModel = modelFactory.getModelFromWrappedRequest(request, component, Teaser.class);
        Link link = null;
        if (teaserModel != null) {
            link = teaserModel.getLink();
        } else {
            Image imageModel = modelFactory.getModelFromWrappedRequest(request, component, Image.class);
            if (imageModel != null) {
                link = imageModel.getImageLink();
            }
        }
        if (link != null) {
            targetPage = (Page) link.getReference();
        } else {
            targetPage = currentPage;
        }
    }
    if (targetPage == null) {
        log.info("A target page cannot be found for the link defined in the request parameter or on the server at {}.", component.getPath());
        return;
    }
    Resource featuredImage = ComponentUtils.getFeaturedImage(targetPage);
    if (featuredImage == null) {
        log.info("No featured image defined for the page at {}", targetPage.getPath());
        return;
    }
    Image imageModel = modelFactory.getModelFromWrappedRequest(request, featuredImage, Image.class);
    if (imageModel == null) {
        log.info("the image model of {} is null", featuredImage.getPath());
        return;
    }
    this.alt = imageModel.getAlt();
    this.src = imageModel.getSrc();
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) Teaser(com.adobe.cq.wcm.core.components.models.Teaser) RequestParameter(org.apache.sling.api.request.RequestParameter) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) Image(com.adobe.cq.wcm.core.components.models.Image) Link(com.adobe.cq.wcm.core.components.commons.link.Link) PostConstruct(javax.annotation.PostConstruct)

Example 25 with RequestParameter

use of org.apache.sling.api.request.RequestParameter in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class InheritedField method initModel.

@PostConstruct
private void initModel() {
    path = slingRequest.getRequestPathInfo().getSuffix();
    if (StringUtils.isBlank(path)) {
        RequestParameter itemParam = slingRequest.getRequestParameter("item");
        if (itemParam == null) {
            log.error("Suffix and 'item' param are blank");
            return;
        }
        path = itemParam.getString();
    }
    if (StringUtils.isBlank(prop)) {
        log.error("'prop' value is null");
        return;
    }
    ResourceResolver rr = slingRequest.getResourceResolver();
    PageManager pm = rr.adaptTo(PageManager.class);
    if (pm == null) {
        log.error("pagemanager is null");
        return;
    }
    containingPage = pm.getPage(path);
    if (containingPage == null) {
        log.error("page is null");
        return;
    }
    inheritedValue = Utils.getInheritedValue(containingPage.getParent(), prop);
    Resource contentResource = containingPage.getContentResource();
    if (contentResource == null) {
        return;
    }
    ValueMap props = contentResource.adaptTo(ValueMap.class);
    if (props == null) {
        return;
    }
    override = props.get(getProp() + OVERRIDE_SUFFIX, OVERRIDE_DEFAULT);
    specifiedValue = props.get(getProp(), String.class);
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) RequestParameter(org.apache.sling.api.request.RequestParameter) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PostConstruct(javax.annotation.PostConstruct)

Aggregations

RequestParameter (org.apache.sling.api.request.RequestParameter)25 RequestParameterMap (org.apache.sling.api.request.RequestParameterMap)8 Map (java.util.Map)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 Resource (org.apache.sling.api.resource.Resource)4 RequestProperty (org.apache.sling.servlets.post.impl.helper.RequestProperty)4 ArrayList (java.util.ArrayList)3 Node (javax.jcr.Node)3 Session (javax.jcr.Session)3 PersistenceException (org.apache.sling.api.resource.PersistenceException)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 PageManager (com.day.cq.wcm.api.PageManager)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 PostConstruct (javax.annotation.PostConstruct)2 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 ValueMap (org.apache.sling.api.resource.ValueMap)2