org.apache.sling.servlets.post.Modification Java Examples

The following examples show how to use org.apache.sling.servlets.post.Modification. 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: MultiFieldDropTargetPostProcessorTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
private void testAppendToExistingMultiValue(Map<String, Object> parameterMap, String... expectedValues) throws Exception {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
    request.setResource(context.resourceResolver().resolve("/content/postprocessor/jcr:content/existingMultiValue"));
    request.setParameterMap(parameterMap);

    MultiFieldDropTargetPostProcessor dropTargetPostProcessor = new MultiFieldDropTargetPostProcessor();
    List<Modification> modifications = new ArrayList<>();
    dropTargetPostProcessor.process(request, modifications);
    Resource resource = request.getResource();

    assertThat(modifications)
        .isNotEmpty()
        .hasSize(1);

    assertThat(resource).isNotNull();

    assertThat(resource.getValueMap())
        .isNotEmpty()
        .containsKeys("./items")
        .doesNotContainKeys("./multiDropTarget->@items");

    assertThat(resource.getValueMap().get("./items", String[].class))
        .isNotEmpty()
        .containsExactly(expectedValues);
}
 
Example #2
Source File: MultiFieldDropTargetPostProcessorTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateNewMultiValue() throws Exception {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
    request.setResource(context.resourceResolver().resolve("/content/postprocessor/jcr:content/noExistingMultiValue"));
    request.setParameterMap(Collections.singletonMap("./multiDropTarget->@items", "b"));

    MultiFieldDropTargetPostProcessor dropTargetPostProcessor = new MultiFieldDropTargetPostProcessor();
    List<Modification> modifications = new ArrayList<>();
    dropTargetPostProcessor.process(request, modifications);
    Resource resource = request.getResource();

    assertThat(modifications).isNotEmpty().hasSize(1);
    assertThat(resource).isNotNull();

    assertThat(resource.getValueMap())
        .isNotEmpty()
        .containsKeys("./items")
        .doesNotContainKeys("./multiDropTarget->@items");

    assertThat(resource.getValueMap().get("./items", String[].class))
        .isNotEmpty()
        .containsExactly("b");
}
 
Example #3
Source File: MultiFieldDropTargetPostProcessorTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testConvertExistingSingleValueToMultifield() throws Exception {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
    request.setResource(context.resourceResolver().resolve("/content/postprocessor/jcr:content/existingSingleValue"));
    request.setParameterMap(Collections.singletonMap("./multiDropTarget->@items", "b"));

    MultiFieldDropTargetPostProcessor dropTargetPostProcessor = new MultiFieldDropTargetPostProcessor();
    List<Modification> modifications = new ArrayList<>();
    dropTargetPostProcessor.process(request, modifications);
    Resource resource = request.getResource();

    assertThat(modifications)
        .isNotEmpty()
        .hasSize(1);

    assertThat(resource).isNotNull();

    assertThat(resource.getValueMap())
        .isNotEmpty()
        .containsKeys("./items")
        .doesNotContainKeys("./multiDropTarget->@items");

    assertThat(resource.getValueMap().get("./items", String[].class))
        .isNotEmpty()
        .containsExactly("a", "b");
}
 
Example #4
Source File: LinkOperation.java    From sling-samples with Apache License 2.0 6 votes vote down vote up
@Override
protected void doRun(SlingHttpServletRequest request,
        HtmlResponse response, List<Modification> changes)
        throws RepositoryException {

    Session session = request.getResourceResolver().adaptTo(Session.class);
    String resourcePath = request.getResource().getPath();
    if (session.itemExists(resourcePath)) {
        Node source = (Node) session.getItem(resourcePath);

        // create a symetric link
        RequestParameter linkParam = request.getRequestParameter("target");
        if (linkParam != null) {
            String linkPath = linkParam.getString();
            if (session.itemExists(linkPath)) {
                Item targetItem = session.getItem(linkPath);
                if (targetItem.isNode()) {
                    linkHelper.createSymetricLink(source,
                        (Node) targetItem, "link");
                }
            }
        }
    }

}
 
Example #5
Source File: MultiFieldDropTargetPostProcessorTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testDropTargetWithSkuSelection() throws Exception {
    Map<String, Object> map = new HashMap<>();
    map.put("./multiDropTarget->@items", "/var/commerce/products/a");
    map.put("./selectionId", "sku");
    map.put("./multiple", Boolean.FALSE);
    map.put("./@CopyFrom", "/does/not/matter");

    // During an AEM drag and drop, the Sling POST will contain a COPY modification with the target component
    // In this test, we mock that something is copied to /content/postprocessor/jcr:content/noExistingMultiValue
    List<Modification> modifications = new ArrayList<>();
    modifications.add(Modification.onCopied("/does/not/matter", "/content/postprocessor/jcr:content/noExistingMultiValue"));

    // During an AEM drag and drop, the Sling POST is typically sent to the responsivegrid
    // In this test, we just set to any location, we'll just have to check that the dropped properties are not added there
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
    request.setResource(context.resourceResolver().resolve("/content/postprocessor/jcr:content/noExistingComposite"));
    request.setParameterMap(map);

    MultiFieldDropTargetPostProcessor dropTargetPostProcessor = new MultiFieldDropTargetPostProcessor();
    dropTargetPostProcessor.process(request, modifications);

    assertThat(modifications).hasSize(2); // there is an extra modification to /content/postprocessor/jcr:content/noExistingMultiValue

    // The target resource of the POST should NOT have the new property
    Resource resource = request.getResource();
    assertThat(resource.getValueMap()).hasSize(1); // just {"jcr:primaryType"="nt:unstructured"}

    // The "copy" resource of the POST MUST contain the new property
    Resource dropTargetResource = resource.getResourceResolver().getResource("/content/postprocessor/jcr:content/noExistingMultiValue");
    ValueMap targetValueMap = dropTargetResource.getValueMap();
    assertThat(targetValueMap.get("items", String.class)).isEqualTo("a");
}
 
Example #6
Source File: MultiFieldDropTargetPostProcessorTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateSubnodeAndAppendValue() throws Exception {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
    request.setResource(context.resourceResolver().resolve("/content/postprocessor/jcr:content/noExistingMultiValue"));
    request.setParameterMap(Collections.singletonMap("./multiDropTarget->/subNode/@items", "b"));

    MultiFieldDropTargetPostProcessor dropTargetPostProcessor = new MultiFieldDropTargetPostProcessor();
    List<Modification> modifications = new ArrayList<>();
    dropTargetPostProcessor.process(request, modifications);
    Resource resource = request.getResource();

    assertThat(modifications)
        .isNotEmpty()
        .hasSize(1);

    assertThat(resource).isNotNull();
    assertThat(resource.getChild("multiDropTarget->")).isNull();
    assertThat(resource.getChild("subNode")).isNotNull();

    assertThat(resource.getValueMap())
        .isNotEmpty()
        .doesNotContainKeys("./multiDropTarget->/subNode/@items");

    assertThat(resource.getChild("subNode").getValueMap().get("./items", String[].class))
        .isNotEmpty()
        .containsExactly("b");
}
 
Example #7
Source File: LinkProcessor.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
public void process(SlingHttpServletRequest request,
		List<Modification> changes) throws Exception {

	Session session = request.getResourceResolver().adaptTo(Session.class);

	RequestParameter linkParam = request.getRequestParameter(":link");
	if (linkParam != null){
		String linkPath = linkParam.getString();
		// check if a new node have been created
		if (changes.size() > 0 && changes.get(0).getType() == ModificationType.CREATE) {
			// hack to get the resource path
			// is it possible to add the response to the method header ?
			String resourcePath = changes.get(0).getSource();
			Node source = (Node) session.getItem(resourcePath);

			// create a symetric link
			if (session.itemExists(linkPath)) {
				Item targetItem = session.getItem(linkPath);
				if (targetItem.isNode()) {
					linkHelper.createSymetricLink(source, (Node) targetItem, "link");
				}
			}
		}
	}


}
 
Example #8
Source File: EncryptPostProcessor.java    From sling-whiteboard with Apache License 2.0 5 votes vote down vote up
@Override
public void process(SlingHttpServletRequest slingRequest, List<Modification> modifications) throws Exception {

    Set<Modification> mods = modifications.stream()
            .filter(modification -> modification.getSource().endsWith(config.suffix())).collect(Collectors.toSet());

    if (mods.isEmpty()) {
        return;
    }

    ResourceResolver resolver = slingRequest.getResourceResolver();
    Session session = resolver.adaptTo(Session.class);

    for (Modification mod : mods) {
        String encryptPropertyPath = mod.getSource();

        String propertyPath = encryptPropertyPath.substring(0, encryptPropertyPath.lastIndexOf(config.suffix()));
        String resourcePath = propertyPath.substring(0, propertyPath.lastIndexOf('/'));

        if (config.inline()) {
            session.move(encryptPropertyPath, propertyPath);
        }

        EncryptableValueMap map = resolver.resolve(resourcePath).adaptTo(EncryptableValueMap.class);
        map.encrypt(propertyPath.substring(resourcePath.length() + 1, propertyPath.length()));
        session.removeItem(encryptPropertyPath);
    }

    modifications.removeAll(mods);

    mods.forEach(mod -> {
        logger.debug("removed {} for source {}", mod.getType() , mod.getSource());
    });
}
 
Example #9
Source File: MultiFieldDropTargetPostProcessor.java    From commerce-cif-connector with Apache License 2.0 4 votes vote down vote up
@Override
public void process(SlingHttpServletRequest request, List<Modification> modifications) throws Exception {
    RequestParameterMap requestParameterMap = request.getRequestParameterMap();

    for (String key : requestParameterMap.keySet()) {
        if (key.startsWith(DROP_TARGET_PREFIX)) {

            RequestParameter requestParameter = requestParameterMap.getValue(key);
            if (requestParameter != null) {
                String target = key.replace(DROP_TARGET_PREFIX, StringUtils.EMPTY);
                String propertyValue = requestParameter.getString();
                Resource resource = request.getResource();

                // If it's actually a drag and drop, the POST contains a Sling './@CopyFrom' parameter
                // and the modifications map will already contain the COPY operation done by the D'n'D
                if (requestParameterMap.containsKey(SLING_COPY_FROM)) {
                    Modification copyFrom = modifications
                        .stream()
                        .filter(m -> ModificationType.COPY.equals(m.getType()))
                        .findFirst().orElseGet(null);
                    if (copyFrom != null) {
                        Resource dropComponent = resource.getResourceResolver().getResource(copyFrom.getDestination());
                        ModifiableValueMap properties = dropComponent.adaptTo(ModifiableValueMap.class);
                        properties.remove(target);
                        resource = dropComponent; // The property changes will be done on the D'n'D target resource
                    }
                }

                RequestParameter selectionTypeParameter = requestParameterMap.getValue(SELECTION_ID);
                String selectionType = selectionTypeParameter != null ? selectionTypeParameter.getString() : null;

                RequestParameter multipleParameter = requestParameterMap.getValue(MULTIPLE);
                boolean multiple = true;
                if (multipleParameter != null) {
                    multiple = Boolean.valueOf(multipleParameter.getString());
                }

                processProperty(resource, target, propertyValue, key, selectionType, multiple);
                modifications.add(Modification.onModified(resource.getPath()));
            }
        }
    }
}
 
Example #10
Source File: MultiFieldDropTargetPostProcessorTest.java    From commerce-cif-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateSubnodeWithUniqueNameAndAppendValue() throws Exception {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
    request.setResource(context.resourceResolver().resolve("/content/postprocessor/jcr:content/existingComposite"));
    request.setParameterMap(Collections.singletonMap("./multiDropTarget->/subNode/{{COMPOSITE}}/@link", "b"));

    MultiFieldDropTargetPostProcessor dropTargetPostProcessor = new MultiFieldDropTargetPostProcessor();
    List<Modification> modifications = new ArrayList<>();
    dropTargetPostProcessor.process(request, modifications);
    Resource resource = request.getResource();

    assertThat(modifications)
        .isNotEmpty()
        .hasSize(1);

    assertThat(resource).isNotNull();

    assertThat(resource.getValueMap())
        .isNotEmpty()
        .doesNotContainKeys("./items")
        .doesNotContainKeys("./multiDropTarget->/subNode/{{COMPOSITE}}/@link");

    assertThat(resource.getChild("multiDropTarget->")).isNull();
    assertThat(resource.getChild("subNode")).isNotNull();
    assertThat(resource.getChild("subNode").getChildren()).hasSize(3);
    assertThat(resource.getChild("subNode/item0")).isNotNull();
    assertThat(resource.getChild("subNode/item1")).isNotNull();
    assertThat(resource.getChild("subNode/item2")).isNotNull();

    assertThat(resource.getChild("subNode/item0").getValueMap())
        .contains(
            entry("jcr:primaryType", "nt:unstructured"),
            entry("link", "a"));

    assertThat(resource.getChild("subNode/item1").getValueMap())
        .contains(
            entry("jcr:primaryType", "nt:unstructured"),
            entry("link", "c"));

    assertThat(resource.getChild("subNode/item2").getValueMap())
        .contains(
            entry("link", "b"));
}