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();
}
}
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"));
}
}
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);
}
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;
}
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;
}
Aggregations