SLING-6723 : Make dependency to javax.jcr, jcr.contentloader and jcr.api optional

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1793056 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupportImpl.java b/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupportImpl.java
index ed641b0..724928f 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupportImpl.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupportImpl.java
@@ -414,7 +414,10 @@
     throws PersistenceException {
         try {
             final Session session = ((Item)src).getSession();
-            session.move(((Item)src).getPath(), ((Node)dstParent).getPath() + '/' + name);
+            final Item source = ((Item)src);
+            final String targetParentPath = ((Node)dstParent).getPath();
+            final String targetPath = (targetParentPath.equals("/") ? "" : targetParentPath) + '/' + name;
+            session.move(source.getPath(), targetPath);
         } catch ( final RepositoryException re) {
             throw new PersistenceException(re.getMessage(), re);
         }
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/helper/StreamedChunk.java b/src/main/java/org/apache/sling/servlets/post/impl/helper/StreamedChunk.java
index 287a3d1..1283acb 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/helper/StreamedChunk.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/helper/StreamedChunk.java
@@ -33,6 +33,7 @@
 import javax.servlet.ServletContext;
 import javax.servlet.http.Part;
 
+import org.apache.jackrabbit.JcrConstants;
 import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
@@ -64,20 +65,13 @@
  *
  */
 public class StreamedChunk {
-    private static final String NT_RESOURCE = "nt:resource";
-    private static final String JCR_LASTMODIFIED = "jcr:lastModified";
-    private static final String JCR_MIMETYPE = "jcr:mimeType";
-    private static final String JCR_DATA = "jcr:data";
-    private static final String JCR_CONTENT = "jcr:content";
-    private static final String JCR_PRIMARY_TYPE = "jcr:primaryType";
     private static final String SLING_CHUNKS_LENGTH = "sling:length";
     private static final String SLING_FILE_LENGTH = "sling:fileLength";
-    private static final String JCR_MIXIN_TYPES = "jcr:mixinTypes";
     private static final String SLING_CHUNK_MIXIN = "sling:chunks";
     private static final String SLING_CHUNK_NT = "sling:chunk";
     private static final String SLING_OFFSET = "sling:offset";
     private static final String MT_APP_OCTET = "application/octet-stream";
-    private static final Logger LOGGER = LoggerFactory.getLogger(StreamedChunk.class);
+    private final Logger LOGGER = LoggerFactory.getLogger(StreamedChunk.class);
 
     private final long offset;
     private final long chunkLength;
@@ -150,7 +144,7 @@
      * @throws PersistenceException
      */
     public Resource store(Resource fileResource, List<Modification> changes) throws PersistenceException {
-        Resource result = fileResource.getChild(JCR_CONTENT);
+        Resource result = fileResource.getChild(JcrConstants.JCR_CONTENT);
         if (result != null) {
             updateState(result, changes);
         } else {
@@ -182,8 +176,8 @@
         if ( vm == null ) {
             throw new PersistenceException("Resource at " + contentResource.getPath() + " is not modifiable.");
         }
-        vm.put(JCR_LASTMODIFIED, Calendar.getInstance());
-        vm.put(JCR_MIMETYPE, getContentType(part));
+        vm.put(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
+        vm.put(JcrConstants.JCR_MIMETYPE, getContentType(part));
         if (chunked) {
             if ( vm.containsKey(SLING_FILE_LENGTH)) {
                 long previousFileLength = vm.get(SLING_FILE_LENGTH, Long.class);
@@ -199,10 +193,10 @@
                 }
             }
             vm.put(SLING_CHUNKS_LENGTH, previousChunksLength + chunkLength);
-            vm.put(JCR_MIXIN_TYPES, SLING_CHUNK_MIXIN);
+            vm.put(JcrConstants.JCR_MIXINTYPES, SLING_CHUNK_MIXIN);
         } else {
             try {
-                vm.put(JCR_DATA, part.getInputStream());
+                vm.put(JcrConstants.JCR_DATA, part.getInputStream());
             } catch (IOException e) {
                 throw new PersistenceException("Error while retrieving inputstream from request part.", e);
             }
@@ -218,25 +212,25 @@
      */
     private Resource initState(Resource fileResource, List<Modification> changes) throws PersistenceException {
         Map<String, Object> resourceProps = new HashMap<>();
-        resourceProps.put(JCR_PRIMARY_TYPE, NT_RESOURCE);
-        resourceProps.put(JCR_LASTMODIFIED, Calendar.getInstance());
-        resourceProps.put(JCR_MIMETYPE, getContentType(part));
+        resourceProps.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_RESOURCE);
+        resourceProps.put(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
+        resourceProps.put(JcrConstants.JCR_MIMETYPE, getContentType(part));
 
         if (chunked) {
             resourceProps.put(SLING_CHUNKS_LENGTH, chunkLength);
             resourceProps.put(SLING_FILE_LENGTH, fileLength);
-            resourceProps.put(JCR_MIXIN_TYPES, SLING_CHUNK_MIXIN);
+            resourceProps.put(JcrConstants.JCR_MIXINTYPES, SLING_CHUNK_MIXIN);
             // add a zero size file to satisfy JCR constraints.
-            resourceProps.put(JCR_DATA, new ByteArrayInputStream(new byte[0]));
+            resourceProps.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(new byte[0]));
         } else {
             try {
-                resourceProps.put(JCR_DATA, part.getInputStream());
+                resourceProps.put(JcrConstants.JCR_DATA, part.getInputStream());
             } catch (IOException e) {
                 throw new PersistenceException("Error while retrieving inputstream from request part.", e);
             }
         }
 
-        Resource result = fileResource.getResourceResolver().create(fileResource, JCR_CONTENT, resourceProps);
+        Resource result = fileResource.getResourceResolver().create(fileResource, JcrConstants.JCR_CONTENT, resourceProps);
         for( String key : resourceProps.keySet()) {
             changes.add(Modification.onModified(result.getPath() + '/' + key));
         }
@@ -252,10 +246,10 @@
     private void storeChunk(Resource contentResource, List<Modification> changes) throws PersistenceException {
         if (chunked) {
             Map<String, Object> chunkProperties = new HashMap<>();
-            chunkProperties.put(JCR_PRIMARY_TYPE, SLING_CHUNK_NT);
+            chunkProperties.put(JcrConstants.JCR_PRIMARYTYPE, SLING_CHUNK_NT);
             chunkProperties.put(SLING_OFFSET, offset);
             try {
-                chunkProperties.put(JCR_DATA, part.getInputStream());
+                chunkProperties.put(JcrConstants.JCR_DATA, part.getInputStream());
             } catch (IOException e) {
                 throw new PersistenceException("Error while retrieving inputstream from request part.", e);
             }
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java b/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
index 158f8ef..d22d10c 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/operations/ModifyOperation.java
@@ -321,7 +321,11 @@
                     throw new PersistenceException("Resource '" + parent.getPath() + "' is not modifiable.");
                 }
                 if ( vm.containsKey(property.getName()) ) {
-                    vm.remove(property.getName());
+                    if ( JcrConstants.JCR_MIXINTYPES.equals(property.getName()) ) {
+                        vm.put(JcrConstants.JCR_MIXINTYPES, new String[0]);
+                    } else {
+                        vm.remove(property.getName());
+                    }
                 } else {
                     final Resource childRsrc = resolver.getResource(parent.getPath() + '/' + property.getName());
                     if ( childRsrc != null ) {
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/operations/MoveOperation.java b/src/main/java/org/apache/sling/servlets/post/impl/operations/MoveOperation.java
index a7a804b..ddde17d 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/operations/MoveOperation.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/operations/MoveOperation.java
@@ -39,7 +39,7 @@
     @Override
     protected Resource execute(final List<Modification> changes,
             final Resource source,
-            String destParent,
+            final String destParent,
             String destName,
             final VersioningConfiguration versioningConfiguration)
     throws PersistenceException {
@@ -47,11 +47,6 @@
             destName = source.getName();
         }
 
-        if (destParent.equals("/")) {
-            destParent = "";
-        }
-        final String destPath = destParent + "/" + destName;
-
         final Resource destParentRsrc = source.getResourceResolver().getResource(destParent);
         final Resource dest = destParentRsrc.getChild(destName);
         if (dest != null ) {
@@ -61,14 +56,18 @@
         // ensure we have an item underlying the request's resource
         final Object item = this.jcrSsupport.getItem(source);
         final Object target = this.jcrSsupport.getNode(destParentRsrc);
+
         if (item == null || target == null ) {
             move(source, destParentRsrc);
         } else {
             this.jcrSsupport.checkoutIfNecessary(source.getParent(), changes, versioningConfiguration);
             this.jcrSsupport.move(item, target, destName);
         }
-        changes.add(Modification.onMoved(source.getPath(), destPath));
-        return source.getResourceResolver().getResource(destPath);
+        final Resource result = destParentRsrc.getChild(destName);
+        if ( result != null ) {
+            changes.add(Modification.onMoved(source.getPath(), result.getPath()));
+        }
+        return result;
     }
 
     /**