Search in sources :

Example 21 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class RedirectServletTest method assertStatus.

private static void assertStatus(final int expectedStatus, final int testStatus) {
    final ValueMap valueMap;
    if (testStatus == -2) {
        valueMap = null;
    } else if (testStatus == -1) {
        valueMap = new ValueMapDecorator(new HashMap<String, Object>());
    } else {
        valueMap = new ValueMapDecorator(Collections.singletonMap(RedirectServlet.STATUS_PROP, (Object) testStatus));
    }
    final int actualStatus = RedirectServlet.getStatus(valueMap);
    assertEquals(expectedStatus, actualStatus);
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator)

Example 22 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class SuperimposingManagerImplTest method prepareSuperimposingResource.

private Resource prepareSuperimposingResource(String superimposedPath, String sourcePath, boolean registerParent, boolean overlayable) {
    Resource resource = mock(Resource.class);
    when(resource.getPath()).thenReturn(superimposedPath);
    ValueMap props = new ValueMapDecorator(new HashMap<String, Object>());
    props.put(PROP_SUPERIMPOSE_SOURCE_PATH, sourcePath);
    props.put(PROP_SUPERIMPOSE_REGISTER_PARENT, registerParent);
    props.put(PROP_SUPERIMPOSE_OVERLAYABLE, overlayable);
    when(resource.adaptTo(ValueMap.class)).thenReturn(props);
    when(resourceResolver.getResource(superimposedPath)).thenReturn(resource);
    return resource;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator)

Example 23 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class FormServlet method doPost.

@Override
protected void doPost(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
    final Map<String, Object> base = new LinkedHashMap<>();
    final ValueMapDecorator parameters = new ValueMapDecorator(base);
    final Enumeration<String> names = request.getParameterNames();
    while (names.hasMoreElements()) {
        final String name = names.nextElement();
        parameters.put(name, request.getRequestParameter(name));
    }
    logger.debug("parameters: {}", parameters);
    final String formType = request.getParameter("formType");
    logger.debug("form type is '{}'", formType);
    final Form form = FormFactory.build(formType, parameters);
    if (form == null) {
        fail(null, 400, request, response);
        return;
    }
    final String resourcePath = request.getRequestPathInfo().getResourcePath();
    final ValidationModel validationModel = validationService.getValidationModel(form.getResourceType(), resourcePath, false);
    if (validationModel == null) {
        logger.error("no validation model found");
        fail(form, 500, request, response);
        return;
    }
    final ValidationResult validationResult = validationService.validate(parameters, validationModel);
    form.setValidationResult(validationResult);
    if (!validationResult.isValid()) {
        logger.debug("validation result not valid");
        fail(form, 400, request, response);
        return;
    }
    // render form with message template
    // TODO
    final String template = "/apps/fling/messaging/form/comment.txt";
    final Map<String, Object> variables = Collections.singletonMap("form", form);
    final String message;
    try (final ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {
        final IContext context = new DefaultSlingContext(resourceResolver, Locale.ENGLISH, variables);
        logger.debug("rendering message template '{}' with variables: {}", template, variables);
        message = templateEngine.process(template, context);
    } catch (Exception e) {
        // TODO
        // TODO
        logger.error("sending message failed: {}", e.getMessage(), e);
        fail(form, 500, request, response);
        return;
    }
    logger.debug("message: '{}'", message);
    try {
        final CompletableFuture<Result> future = messageService.send(message, recipient);
        future.get(1, TimeUnit.SECONDS);
        logger.debug("comment [{}] form sent to {}", message, recipient);
    } catch (Exception e) {
        logger.error("sending message failed: {}", e.getMessage(), e);
        fail(form, 500, request, response);
        return;
    }
    succeed(form, request, response);
}
Also used : IContext(org.thymeleaf.context.IContext) Form(org.apache.sling.samples.fling.form.Form) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ValidationResult(org.apache.sling.validation.ValidationResult) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Result(org.apache.sling.commons.messaging.Result) ValidationResult(org.apache.sling.validation.ValidationResult) ValidationModel(org.apache.sling.validation.model.ValidationModel) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DefaultSlingContext(org.apache.sling.scripting.thymeleaf.DefaultSlingContext)

Example 24 with ValueMapDecorator

use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.

the class MergedResourceTest method testResourceTypeByValueMap.

@Test
public void testResourceTypeByValueMap() throws Exception {
    final ValueMap vm1 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vma"));
    final MockResource r1 = new MockResource("/a", vm1, null);
    final ValueMap vm2 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vmb"));
    final MockResource r2 = new MockResource("/b", vm2, null);
    final ValueMap vm3 = new ValueMapDecorator(Collections.singletonMap(ResourceResolver.PROPERTY_RESOURCE_TYPE, (Object) "vmc"));
    final MockResource r3 = new MockResource("/c", vm3, null);
    final List<Resource> resources = new ArrayList<Resource>();
    resources.add(r1);
    resources.add(r2);
    resources.add(r3);
    final List<ValueMap> valueMaps = new ArrayList<ValueMap>();
    valueMaps.add(vm1);
    valueMaps.add(vm2);
    valueMaps.add(vm3);
    final MergedResource mr = new MergedResource(null, "/a", "a", resources, valueMaps);
    assertEquals("vmc", mr.getResourceType());
    assertNull(mr.getResourceSuperType());
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) MockResource(org.apache.sling.testing.resourceresolver.MockResource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.testing.resourceresolver.MockResource) Test(org.junit.Test)

Example 25 with ValueMapDecorator

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

the class TextValueDataResourceSource method initValueMap.

private void initValueMap() {
    valueMap = new ValueMapDecorator(new HashMap<String, Object>());
    valueMap.put(PN_VALUE, getValue());
    valueMap.put(PN_TEXT, getText());
}
Also used : HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator)

Aggregations

ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)63 Test (org.junit.Test)45 ValueMap (org.apache.sling.api.resource.ValueMap)44 HashMap (java.util.HashMap)36 Resource (org.apache.sling.api.resource.Resource)36 ValidationModel (org.apache.sling.validation.model.ValidationModel)9 ValidationResult (org.apache.sling.validation.ValidationResult)8 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)6 ArrayList (java.util.ArrayList)3 RepositoryException (javax.jcr.RepositoryException)3 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)3 ResourceModelWithRequiredField (org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredField)3 MockResource (org.apache.sling.testing.resourceresolver.MockResource)3 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)3 Calendar (java.util.Calendar)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2