use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class StreamingUploadOperationTest method testPartsContentRange.
@Test
public void testPartsContentRange() 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")), mapOf("Content-Range", "bytes 0-3/8", "Content-Length", "4")));
partsList.add(new MockPart("test1.txt", "text/plain", "test1bad.txt", 4, new ByteArrayInputStream("part".getBytes("UTF-8")), mapOf("Content-Range", "bytes 4-7/8", "Content-Length", "4")));
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) {
List<Resource> children = new ArrayList<>();
for (Map.Entry<String, Resource> e : repository.entrySet()) {
if (isChild(resource.getPath(), e.getKey())) {
children.add(e.getValue());
}
}
return children;
}
private boolean isChild(String path, String key) {
if (key.length() > path.length() && key.startsWith(path)) {
return !key.substring(path.length() + 1).contains("/");
}
return false;
}
@Override
public Iterator<Resource> listChildren(Resource parent) {
return getChildren(parent).iterator();
}
@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 ");
}
String resourceType = (String) map.get("sling:resourceType");
if (resourceType == null) {
resourceType = (String) map.get("jcr:primaryType");
}
if (resourceType == null) {
LOG.warn("Resource type null for {} {} ", resource, resource.getPath() + "/" + s);
}
Resource newResource = new MockRealResource(this, resource.getPath() + "/" + s, resourceType, map);
repository.put(newResource.getPath(), newResource);
LOG.debug("Created Resource {} ", newResource.getPath());
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("Comittted {} ", 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("testpart", 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 StreamingUploadOperationTest method testParts.
@Test
public void testParts() 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("test1.txt@Length", null, null, 0, new ByteArrayInputStream("8".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("test1.txt@Offset", null, null, 0, new ByteArrayInputStream("0".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("test1.txt", "text/plain", "test1bad.txt", 4, new ByteArrayInputStream("test".getBytes("UTF-8")), mapOf("Content-Length", "4")));
partsList.add(new MockPart("test1.txt@Offset", null, null, 0, new ByteArrayInputStream("4".getBytes("UTF-8")), Collections.EMPTY_MAP));
partsList.add(new MockPart("test1.txt", "text/plain", "test1bad.txt", 4, new ByteArrayInputStream("part".getBytes("UTF-8")), mapOf("Content-Length", "4")));
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) {
List<Resource> children = new ArrayList<>();
for (Map.Entry<String, Resource> e : repository.entrySet()) {
if (isChild(resource.getPath(), e.getKey())) {
children.add(e.getValue());
}
}
return children;
}
private boolean isChild(String path, String key) {
if (key.length() > path.length() && key.startsWith(path)) {
return !key.substring(path.length() + 1).contains("/");
}
return false;
}
@Override
public Iterator<Resource> listChildren(Resource parent) {
return getChildren(parent).iterator();
}
@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 ");
}
String resourceType = (String) map.get("sling:resourceType");
if (resourceType == null) {
resourceType = (String) map.get("jcr:primaryType");
}
if (resourceType == null) {
LOG.warn("Resource type null for {} {} ", resource, resource.getPath() + "/" + s);
}
Resource newResource = new MockRealResource(this, resource.getPath() + "/" + s, resourceType, map);
repository.put(newResource.getPath(), newResource);
LOG.debug("Created Resource {} ", newResource.getPath());
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("Comittted {} ", 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("testpart", 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 AbstractCreateOperation method updateNodeType.
protected void updateNodeType(final ResourceResolver resolver, final String path, final Map<String, RequestProperty> reqProperties, final List<Modification> changes, final VersioningConfiguration versioningConfiguration) throws PersistenceException {
final String nodeType = getPrimaryType(reqProperties, path);
if (nodeType != null) {
final Resource rsrc = resolver.getResource(path);
final ModifiableValueMap mvm = rsrc.adaptTo(ModifiableValueMap.class);
if (mvm != null) {
final Object node = this.jcrSsupport.getNode(rsrc);
final boolean wasVersionable = (node == null ? false : this.jcrSsupport.isVersionable(rsrc));
if (node != null) {
this.jcrSsupport.checkoutIfNecessary(rsrc, changes, versioningConfiguration);
this.jcrSsupport.setPrimaryNodeType(node, nodeType);
} else {
mvm.put(JcrConstants.JCR_PRIMARYTYPE, nodeType);
}
if (node != null) {
// the mix:versionable mixin does an implicit checkout
if (!wasVersionable && versioningConfiguration.isCheckinOnNewVersionableNode() && this.jcrSsupport.isVersionable(rsrc)) {
changes.add(Modification.onCheckout(path));
}
}
}
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class Announcement method persistTo.
/**
* Persists this announcement using the given 'announcements' resource,
* under which a node with the primary key is created
**/
public void persistTo(Resource announcementsResource) throws PersistenceException {
Resource announcementChildResource = announcementsResource.getChild(getPrimaryKey());
// SLING-2967 used to introduce 'resetting the created time' here
// in order to become machine-clock independent.
// With introduction of SLING-3389, where we dont store any
// announcement-heartbeat-dates anymore at all, this resetting here
// became unnecessary.
final String announcementJson = asJSON();
if (announcementChildResource == null) {
final ResourceResolver resourceResolver = announcementsResource.getResourceResolver();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("topologyAnnouncement", announcementJson);
resourceResolver.create(announcementsResource, getPrimaryKey(), properties);
} else {
final ModifiableValueMap announcementChildMap = announcementChildResource.adaptTo(ModifiableValueMap.class);
announcementChildMap.put("topologyAnnouncement", announcementJson);
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class VotingView method vote.
/**
* add a vote from the given slingId to this voting
* @param slingId the slingId which is voting
* @param vote true for a yes-vote, false for a no-vote
*/
public void vote(final String slingId, final Boolean vote, final String leaderElectionId) {
if (logger.isDebugEnabled()) {
logger.debug("vote: slingId=" + slingId + ", vote=" + vote);
}
Resource r = getResource();
if (r == null) {
logger.error("vote: no resource set. slingId = " + slingId + ", vote=" + vote);
return;
}
Resource members = r.getChild("members");
if (members == null) {
logger.error("vote: no members resource available for " + r + ". slingId = " + slingId + ", vote=" + vote);
return;
}
final Resource memberResource = members.getChild(slingId);
if (memberResource == null) {
if (vote == null || !vote) {
// if I wanted to vote no or empty, then it's no big deal
// that I can't find my entry ..
logger.debug("vote: no memberResource found for slingId=" + slingId + ", vote=" + vote + ", resource=" + getResource());
} else {
// if I wanted to vote yes, then it is a big deal that I can't find myself
logger.error("vote: no memberResource found for slingId=" + slingId + ", vote=" + vote + ", resource=" + getResource());
}
return;
}
final ModifiableValueMap memberMap = memberResource.adaptTo(ModifiableValueMap.class);
if (vote == null) {
if (memberMap.containsKey("vote")) {
logger.info("vote: removing vote (vote==null) of slingId=" + slingId + " on: " + this);
} else {
logger.debug("vote: removing vote (vote==null) of slingId=" + slingId + " on: " + this);
}
memberMap.remove("vote");
} else {
boolean shouldVote = true;
try {
if (memberMap.containsKey("vote")) {
Object v = memberMap.get("vote");
if (v instanceof Property) {
Property p = (Property) v;
if (p.getBoolean() == vote) {
logger.debug("vote: already voted, with same vote (" + vote + "), not voting again");
shouldVote = false;
}
} else if (v instanceof Boolean) {
Boolean b = (Boolean) v;
if (b == vote) {
logger.debug("vote: already voted, with same vote (" + vote + "), not voting again");
shouldVote = false;
}
}
}
} catch (ValueFormatException e) {
logger.warn("vote: got a ValueFormatException: " + e, e);
} catch (RepositoryException e) {
logger.warn("vote: got a RepositoryException: " + e, e);
}
if (shouldVote) {
logger.info("vote: slingId=" + slingId + " is voting vote=" + vote + " on " + getResource());
memberMap.put("vote", vote);
memberMap.put("votedAt", Calendar.getInstance());
String currentLeaderElectionId = memberMap.get("leaderElectionId", String.class);
if (leaderElectionId != null && (currentLeaderElectionId == null || !currentLeaderElectionId.equals(leaderElectionId))) {
// SLING-5030 : to ensure leader-step-down after being
// isolated from the cluster, the leaderElectionId must
// be explicitly set upon voting.
// for 99% of the cases not be necessary,
// for the rejoin-after-isolation case however it is
logger.info("vote: changing leaderElectionId on vote to " + leaderElectionId);
memberMap.put("leaderElectionId", leaderElectionId);
memberMap.put("leaderElectionIdCreatedAt", new Date());
}
}
}
try {
getResource().getResourceResolver().commit();
} catch (PersistenceException e) {
logger.error("vote: PersistenceException while voting: " + e, e);
}
}
Aggregations