Author: fmeschbe
Date: Tue Jan 6 06:40:08 2009
New Revision: 731959
URL: http://svn.apache.org/viewvc?rev=731959&view=rev
Log:
SLING-784 Applied modified patch by Alex Saar (thanks)
Modified:
incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletUpdateTest.java
incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
Modified:
incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletUpdateTest.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletUpdateTest.java?rev=731959&r1=731958&r2=731959&view=diff
==============================================================================
---
incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletUpdateTest.java
(original)
+++
incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletUpdateTest.java
Tue Jan 6 06:40:08 2009
@@ -22,6 +22,9 @@
import javax.servlet.http.HttpServletResponse;
+import org.apache.sling.commons.json.JSONArray;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.testing.integration.HttpTestBase;
import org.apache.sling.servlets.post.SlingPostConstants;
@@ -110,4 +113,42 @@
assertTrue(content.indexOf("\"f\":[\"123\"]") > 0);
assertTrue(content.indexOf("\"g\":\"456\"") > 0);
}
+
+ public void testMixinTypes() throws IOException, JSONException {
+
+ // create a node without mixin node types
+ final Map <String, String> props = new HashMap <String, String> ();
+ props.put("jcr:primaryType","nt:unstructured");
+ final String location = testClient.createNode(postUrl +
SlingPostConstants.DEFAULT_CREATE_SUFFIX, props);
+
+ // assert no mixins
+ String content = getContent(location + ".json", CONTENT_TYPE_JSON);
+ JSONObject json = new JSONObject(content);
+ assertFalse("jcr:mixinTypes not expected to be set",
json.has("jcr:mixinTypes"));
+
+ // add mixin
+ props.clear();
+ props.put("jcr:mixinTypes", "mix:versionable");
+ testClient.createNode(location, props);
+
+ content = getContent(location + ".json", CONTENT_TYPE_JSON);
+ json = new JSONObject(content);
+ assertTrue("jcr:mixinTypes expected after setting them",
json.has("jcr:mixinTypes"));
+
+ Object mixObject = json.get("jcr:mixinTypes");
+ assertTrue("jcr:mixinTypes must be an array", mixObject instanceof
JSONArray);
+
+ JSONArray mix = (JSONArray) mixObject;
+ assertTrue("jcr:mixinTypes must have a single entry", mix.length() ==
1);
+ assertEquals("jcr:mixinTypes must have correct value",
"mix:versionable", mix.get(0));
+
+ // remove mixin
+ props.clear();
+ props.put("jcr:mixinty...@delete", "-");
+ testClient.createNode(location, props);
+
+ content = getContent(location + ".json", CONTENT_TYPE_JSON);
+ json = new JSONObject(content);
+ assertTrue("no jcr:mixinTypes expected after clearing it",
!json.has("jcr:mixinTypes"));
+ }
}
\ No newline at end of file
Modified:
incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java?rev=731959&r1=731958&r2=731959&view=diff
==============================================================================
---
incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
(original)
+++
incubator/sling/trunk/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
Tue Jan 6 06:40:08 2009
@@ -18,15 +18,19 @@
*/
package org.apache.sling.servlets.post.impl.operations;
+import java.util.Arrays;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
import javax.servlet.ServletContext;
import org.apache.sling.api.SlingException;
@@ -208,10 +212,37 @@
String path = response.getPath();
if (!session.itemExists(path)) {
+
deepGetOrCreateNode(session, path, reqProperties, changes);
response.setCreateRequest(true);
+
+ } else {
+
+ String[] mixins = getMixinTypes(reqProperties, path);
+ if (mixins != null) {
+
+ Item item = session.getItem(path);
+ if (item.isNode()) {
+
+ Set<String> newMixins = new HashSet<String>();
+ newMixins.addAll(Arrays.asList(mixins));
+
+ // clear existing mixins first
+ Node node = (Node) item;
+ for (NodeType mixin : node.getMixinNodeTypes()) {
+ String mixinName = mixin.getName();
+ if (!newMixins.remove(mixinName)) {
+ node.removeMixin(mixinName);
+ }
+ }
+
+ // add new mixins
+ for (String mixin : newMixins) {
+ node.addMixin(mixin);
+ }
+ }
+ }
}
-
}
/**
@@ -352,7 +383,17 @@
if (property.isDelete()) {
String propPath = property.getPath();
if (session.itemExists(propPath)) {
- session.getItem(propPath).remove();
+ Item item = session.getItem(property.getParentPath());
+
+ if (property.getName().equals("jcr:mixinTypes") &&
item.isNode()) {
+ // clear all mixins
+ Node parent = (Node) item;
+ for (NodeType mixin : parent.getMixinNodeTypes()) {
+ parent.removeMixin(mixin.getName());
+ }
+ } else {
+ item.remove();
+ }
changes.add(Modification.onDeleted(propPath));
}
}
@@ -599,7 +640,7 @@
private String[] getMixinTypes(Map<String, RequestProperty> reqProperties,
String path) {
RequestProperty prop = reqProperties.get(path + "/jcr:mixinTypes");
- return prop == null ? null : prop.getStringValues();
+ return (prop == null) || !prop.hasValues() ? null :
prop.getStringValues();
}
/**