Search in sources :

Example 41 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class MergedResourceProviderTestForMergingPicker method testModifiableValueMap.

@Test
public void testModifiableValueMap() throws PersistenceException {
    final String path = "/merged/mvmTest";
    try {
        assertNotNull(this.resolver.getResource("/libs/mvmTest"));
        assertNull(this.resolver.getResource("/apps/mvmTest"));
        final Resource rsrc = this.provider.getResource(ctx, path, ResourceContext.EMPTY_CONTEXT, null);
        assertNotNull(rsrc);
        final ValueMap beforeVM = rsrc.getValueMap();
        assertEquals("1", beforeVM.get("a"));
        assertEquals("2", beforeVM.get("b"));
        final ModifiableValueMap mvm = rsrc.adaptTo(ModifiableValueMap.class);
        assertNotNull(mvm);
        assertEquals("1", mvm.get("a"));
        assertEquals("2", mvm.get("b"));
        mvm.put("c", "3");
        mvm.remove("a");
        assertNotNull(this.resolver.getResource("/libs/mvmTest"));
        assertNotNull(this.resolver.getResource("/apps/mvmTest"));
        final Resource rsrc2 = this.provider.getResource(ctx, path, ResourceContext.EMPTY_CONTEXT, null);
        assertNotNull(rsrc2);
        final ValueMap afterVM = rsrc2.getValueMap();
        assertNull(afterVM.get("a"));
        assertEquals("2", afterVM.get("b"));
        assertEquals("3", afterVM.get("c"));
        final Resource rsrcL = this.resolver.getResource("/libs/mvmTest");
        assertEquals("1", rsrcL.getValueMap().get("a"));
        assertEquals("2", rsrcL.getValueMap().get("b"));
        assertNull(rsrcL.getValueMap().get("c"));
        final Resource rsrcA = this.resolver.getResource("/apps/mvmTest");
        assertNull(rsrcA.getValueMap().get("a"));
        assertNull(rsrcA.getValueMap().get("b"));
        assertEquals("3", rsrcA.getValueMap().get("c"));
        final String[] hidden = rsrcA.getValueMap().get(MergedResourceConstants.PN_HIDE_PROPERTIES, String[].class);
        assertNotNull(hidden);
        assertEquals(1, hidden.length);
        assertEquals("a", hidden[0]);
    } finally {
        this.resolver.revert();
    }
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 42 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class StreamingUploadOperationTest method test.

@Test
public void test() throws PersistenceException, RepositoryException, UnsupportedEncodingException {
    List<Modification> changes = new ArrayList<>();
    PostResponse response = new AbstractPostResponse() {

        @Override
        protected void doSend(HttpServletResponse response) throws IOException {
        }

        @Override
        public void onChange(String type, String... arguments) {
        }

        @Override
        public String getPath() {
            return "/test/upload/location";
        }
    };
    List<Part> partsList = new ArrayList<>();
    partsList.add(new MockPart("formfield1", null, null, 0, new ByteArrayInputStream("testformfield1".getBytes("UTF-8")), Collections.EMPTY_MAP));
    partsList.add(new MockPart("formfield2", null, null, 0, new ByteArrayInputStream("testformfield2".getBytes("UTF-8")), Collections.EMPTY_MAP));
    partsList.add(new MockPart("test1.txt", "text/plain", "test1bad.txt", 4, new ByteArrayInputStream("test".getBytes("UTF-8")), Collections.EMPTY_MAP));
    partsList.add(new MockPart("*", "text/plain2", "test2.txt", 8, new ByteArrayInputStream("test1234".getBytes("UTF-8")), Collections.EMPTY_MAP));
    partsList.add(new MockPart("badformfield2", null, null, 0, new ByteArrayInputStream("testbadformfield2".getBytes("UTF-8")), Collections.EMPTY_MAP));
    final Iterator<Part> partsIterator = partsList.iterator();
    final Map<String, Resource> repository = new HashMap<>();
    final ResourceResolver resourceResolver = new MockResourceResolver() {

        @Override
        public Resource getResource(String path) {
            Resource resource = repository.get(path);
            if (resource == null) {
                if ("/test/upload/location".equals(path)) {
                    resource = new MockRealResource(this, path, "sling:Folder");
                    repository.put(path, resource);
                    LOG.debug("Created {} ", path);
                }
            }
            LOG.debug("Resource {} is {} {}", path, resource, ResourceUtil.isSyntheticResource(resource));
            return resource;
        }

        @Override
        public Iterable<Resource> getChildren(Resource resource) {
            return null;
        }

        @Override
        public void delete(Resource resource) throws PersistenceException {
        }

        @Override
        public Resource create(Resource resource, String s, Map<String, Object> map) throws PersistenceException {
            Resource childResource = resource.getChild(s);
            if (childResource != null) {
                throw new IllegalArgumentException("Child " + s + " already exists ");
            }
            Resource newResource = new MockRealResource(this, resource.getPath() + "/" + s, (String) map.get("sling:resourceType"), map);
            repository.put(newResource.getPath(), newResource);
            return newResource;
        }

        @Override
        public void revert() {
        }

        @Override
        public void commit() throws PersistenceException {
            LOG.debug("Committing");
            for (Map.Entry<String, Resource> e : repository.entrySet()) {
                LOG.debug("Committing {} ", e.getKey());
                Resource r = e.getValue();
                ModifiableValueMap vm = r.adaptTo(ModifiableValueMap.class);
                for (Map.Entry<String, Object> me : vm.entrySet()) {
                    if (me.getValue() instanceof InputStream) {
                        try {
                            String value = IOUtils.toString((InputStream) me.getValue());
                            LOG.debug("Converted {} {}  ", me.getKey(), value);
                            vm.put(me.getKey(), value);
                        } catch (IOException e1) {
                            throw new PersistenceException("Failed to commit input stream", e1);
                        }
                    }
                }
                LOG.debug("Converted {} ", vm);
            }
            LOG.debug("Committted {} ", repository);
        }

        @Override
        public boolean hasChanges() {
            return false;
        }
    };
    SlingHttpServletRequest request = new MockSlingHttpServlet3Request(null, null, null, null, null) {

        @Override
        public Object getAttribute(String name) {
            if ("request-parts-iterator".equals(name)) {
                return partsIterator;
            }
            return super.getAttribute(name);
        }

        @Override
        public ResourceResolver getResourceResolver() {
            return resourceResolver;
        }
    };
    streamedUplodOperation.doRun(request, response, changes);
    {
        Resource r = repository.get("/test/upload/location/test1.txt");
        Assert.assertNotNull(r);
        ValueMap m = r.adaptTo(ValueMap.class);
        Assert.assertNotNull(m);
        Assert.assertEquals("nt:file", m.get("jcr:primaryType"));
    }
    {
        Resource r = repository.get("/test/upload/location/test1.txt/jcr:content");
        Assert.assertNotNull(r);
        ValueMap m = r.adaptTo(ValueMap.class);
        Assert.assertNotNull(m);
        Assert.assertEquals("nt:resource", m.get("jcr:primaryType"));
        Assert.assertTrue(m.get("jcr:lastModified") instanceof Calendar);
        Assert.assertEquals("text/plain", m.get("jcr:mimeType"));
        Assert.assertEquals("test", m.get("jcr:data"));
    }
    {
        Resource r = repository.get("/test/upload/location/test2.txt");
        Assert.assertNotNull(r);
        ValueMap m = r.adaptTo(ValueMap.class);
        Assert.assertNotNull(m);
        Assert.assertEquals("nt:file", m.get("jcr:primaryType"));
    }
    {
        Resource r = repository.get("/test/upload/location/test2.txt/jcr:content");
        Assert.assertNotNull(r);
        ValueMap m = r.adaptTo(ValueMap.class);
        Assert.assertNotNull(m);
        Assert.assertEquals("nt:resource", m.get("jcr:primaryType"));
        Assert.assertTrue(m.get("jcr:lastModified") instanceof Calendar);
        Assert.assertEquals("text/plain2", m.get("jcr:mimeType"));
        Assert.assertEquals("test1234", m.get("jcr:data"));
    }
}
Also used : Modification(org.apache.sling.servlets.post.Modification) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ArrayList(java.util.ArrayList) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) AbstractPostResponse(org.apache.sling.servlets.post.AbstractPostResponse) PostResponse(org.apache.sling.servlets.post.PostResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) Resource(org.apache.sling.api.resource.Resource) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) AbstractPostResponse(org.apache.sling.servlets.post.AbstractPostResponse) MockSlingHttpServlet3Request(org.apache.sling.servlets.post.impl.helper.MockSlingHttpServlet3Request) ByteArrayInputStream(java.io.ByteArrayInputStream) Part(javax.servlet.http.Part) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 43 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class ResourceCollectionImpl method setType.

/**
     * Sets the sling resource type on content node of collection
     * 
     * @param type <code>sling:resourceType</code> to be set on the content node
     * @return
     */
public void setType(String type) throws PersistenceException {
    ModifiableValueMap mvp = resource.adaptTo(ModifiableValueMap.class);
    mvp.put(RESOURCE_TYPE, type);
}
Also used : ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 44 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class ResourceCollectionImpl method add.

/**
     * {@inheritDoc}
     */
public boolean add(Resource res, Map<String, Object> properties) throws PersistenceException {
    if (res != null && !contains(res)) {
        ModifiableValueMap vm = membersResource.adaptTo(ModifiableValueMap.class);
        String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[] {});
        order = (String[]) ArrayUtils.add(order, res.getPath());
        vm.put(ResourceCollectionConstants.REFERENCES_PROP, order);
        if (properties == null) {
            properties = new HashMap<String, Object>();
        }
        properties.put(ResourceCollectionConstants.REF_PROPERTY, res.getPath());
        resolver.create(membersResource, ResourceUtil.createUniqueChildName(membersResource, res.getName()), properties);
        log.debug("added member to resource {} to collection {}", new String[] { res.getPath(), resource.getPath() });
        return true;
    }
    return false;
}
Also used : ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 45 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class ResourceCollectionImpl method add.

/**
     * {@inheritDoc}
     */
public boolean add(Resource res) throws PersistenceException {
    if (res != null && !contains(res)) {
        ModifiableValueMap vm = membersResource.adaptTo(ModifiableValueMap.class);
        String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[] {});
        order = (String[]) ArrayUtils.add(order, res.getPath());
        vm.put(ResourceCollectionConstants.REFERENCES_PROP, order);
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(ResourceCollectionConstants.REF_PROPERTY, res.getPath());
        resolver.create(membersResource, ResourceUtil.createUniqueChildName(membersResource, res.getName()), properties);
        log.debug("added member to resource {} to collection {}", new String[] { res.getPath(), resource.getPath() });
        return true;
    }
    return false;
}
Also used : HashMap(java.util.HashMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Aggregations

ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)111 Resource (org.apache.sling.api.resource.Resource)74 PersistenceException (org.apache.sling.api.resource.PersistenceException)32 Test (org.junit.Test)28 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)26 HashMap (java.util.HashMap)22 ValueMap (org.apache.sling.api.resource.ValueMap)22 Calendar (java.util.Calendar)13 LoginException (org.apache.sling.api.resource.LoginException)9 ChildResource (org.apache.sling.validation.model.ChildResource)9 Date (java.util.Date)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 ValidationModel (org.apache.sling.validation.model.ValidationModel)7 InputStream (java.io.InputStream)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6