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

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1789360 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/helper/HtmlPostResponseProxy.java b/src/main/java/org/apache/sling/servlets/post/impl/helper/HtmlPostResponseProxy.java
index 6c967d0..3ae5da2 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/helper/HtmlPostResponseProxy.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/helper/HtmlPostResponseProxy.java
@@ -33,7 +33,9 @@
  * {@link org.apache.sling.servlets.post.AbstractSlingPostOperation} for
  * bridging into the new
  * {@link org.apache.sling.servlets.post.AbstractPostOperation}.
+ * @deprecated
  */
+@Deprecated
 public class HtmlPostResponseProxy implements PostResponse {
 
     private final HtmlResponse apiHtmlResponse;
@@ -46,18 +48,22 @@
         return apiHtmlResponse;
     }
 
+    @Override
     public Throwable getError() {
         return apiHtmlResponse.getError();
     }
 
+    @Override
     public String getLocation() {
         return apiHtmlResponse.getLocation();
     }
 
+    @Override
     public String getParentLocation() {
         return apiHtmlResponse.getParentLocation();
     }
 
+    @Override
     public String getPath() {
         return apiHtmlResponse.getPath();
     }
@@ -70,71 +76,88 @@
         return apiHtmlResponse.getProperty(name);
     }
 
+    @Override
     public String getReferer() {
         return apiHtmlResponse.getReferer();
     }
 
+    @Override
     public int getStatusCode() {
         return apiHtmlResponse.getStatusCode();
     }
 
+    @Override
     public String getStatusMessage() {
         return apiHtmlResponse.getStatusMessage();
     }
 
+    @Override
     public boolean isCreateRequest() {
         return apiHtmlResponse.isCreateRequest();
     }
 
+    @Override
     public boolean isSuccessful() {
         return apiHtmlResponse.isSuccessful();
     }
 
+    @Override
     public void onChange(String type, String... arguments) {
         apiHtmlResponse.onChange(type, arguments);
     }
 
+    @Override
     public void onCopied(String srcPath, String dstPath) {
         apiHtmlResponse.onCopied(srcPath, dstPath);
     }
 
+    @Override
     public void onCreated(String path) {
         apiHtmlResponse.onCreated(path);
     }
 
+    @Override
     public void onDeleted(String path) {
         apiHtmlResponse.onDeleted(path);
     }
 
+    @Override
     public void onModified(String path) {
         apiHtmlResponse.onModified(path);
     }
 
+    @Override
     public void onMoved(String srcPath, String dstPath) {
         apiHtmlResponse.onMoved(srcPath, dstPath);
     }
 
+    @Override
     public void send(HttpServletResponse response, boolean setStatus)
             throws IOException {
         apiHtmlResponse.send(response, setStatus);
     }
 
+    @Override
     public void setCreateRequest(boolean isCreateRequest) {
         apiHtmlResponse.setCreateRequest(isCreateRequest);
     }
 
+    @Override
     public void setError(Throwable error) {
         apiHtmlResponse.setError(error);
     }
 
+    @Override
     public void setLocation(String location) {
         apiHtmlResponse.setLocation(location);
     }
 
+    @Override
     public void setParentLocation(String parentLocation) {
         apiHtmlResponse.setParentLocation(parentLocation);
     }
 
+    @Override
     public void setPath(String path) {
         apiHtmlResponse.setPath(path);
     }
@@ -143,14 +166,17 @@
         apiHtmlResponse.setProperty(name, value);
     }
 
+    @Override
     public void setReferer(String referer) {
         apiHtmlResponse.setReferer(referer);
     }
 
+    @Override
     public void setStatus(int code, String message) {
         apiHtmlResponse.setStatus(code, message);
     }
 
+    @Override
     public void setTitle(String title) {
         apiHtmlResponse.setTitle(title);
     }
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/helper/ReferenceParser.java b/src/main/java/org/apache/sling/servlets/post/impl/helper/ReferenceParser.java
index 8b8d81e..ef07aa4 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/helper/ReferenceParser.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/helper/ReferenceParser.java
@@ -109,7 +109,7 @@
             // we ignore this
         }
         try {
-            return session.getNodeByUUID(value);
+            return session.getNodeByIdentifier(value);
         } catch (RepositoryException ignore) {
             // we ignore this
         }
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 757c363..287a3d1 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
@@ -17,16 +17,6 @@
 
 package org.apache.sling.servlets.post.impl.helper;
 
-import org.apache.sling.api.resource.ModifiableValueMap;
-import org.apache.sling.api.resource.PersistenceException;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ValueMap;
-import org.apache.sling.servlets.post.Modification;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.servlet.ServletContext;
-import javax.servlet.http.Part;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -40,6 +30,17 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import javax.servlet.ServletContext;
+import javax.servlet.http.Part;
+
+import org.apache.sling.api.resource.ModifiableValueMap;
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.servlets.post.Modification;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * Supports streamed uploads including where the stream is made up of partial body parts.
  * The behaviour is documented here https://cwiki.apache.org/confluence/display/SLING/Chunked+File+Upload+Support, adding the ability
@@ -82,7 +83,6 @@
     private final long chunkLength;
     private final long fileLength;
     private final Part part;
-    private final Map<String, List<String>> formFields;
     private ServletContext servletContext;
     private final boolean completed;
     private final boolean chunked;
@@ -97,7 +97,6 @@
      */
     public StreamedChunk(Part part, Map<String, List<String>> formFields, ServletContext servletContext) {
         this.part = part;
-        this.formFields = formFields;
         this.servletContext = servletContext;
 
         String contentRangeHeader = part.getHeader("Content-Range");
@@ -187,14 +186,14 @@
         vm.put(JCR_MIMETYPE, getContentType(part));
         if (chunked) {
             if ( vm.containsKey(SLING_FILE_LENGTH)) {
-                long previousFileLength = (Long) vm.get(SLING_FILE_LENGTH, Long.class);
+                long previousFileLength = vm.get(SLING_FILE_LENGTH, Long.class);
                 if (previousFileLength != fileLength) {
                     throw new IllegalStateException("Chunk file length has changed while cunks were being uploaded expected " + previousFileLength + " chunk contained  " + fileLength);
                 }
             }
             long previousChunksLength = 0;
             if ( vm.containsKey(SLING_CHUNKS_LENGTH)) {
-                previousChunksLength = (Long) vm.get(SLING_CHUNKS_LENGTH, Long.class);
+                previousChunksLength = vm.get(SLING_CHUNKS_LENGTH, Long.class);
                 if (previousChunksLength != offset) {
                     throw new IllegalStateException("Chunks recieved out of order, was expecting chunk starting at " + offset + " found last chunk ending at " + previousChunksLength);
                 }
@@ -218,7 +217,7 @@
      * @throws PersistenceException
      */
     private Resource initState(Resource fileResource, List<Modification> changes) throws PersistenceException {
-        Map<String, Object> resourceProps = new HashMap<String, Object>();
+        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));
@@ -252,7 +251,7 @@
      */
     private void storeChunk(Resource contentResource, List<Modification> changes) throws PersistenceException {
         if (chunked) {
-            Map<String, Object> chunkProperties = new HashMap<String, Object>();
+            Map<String, Object> chunkProperties = new HashMap<>();
             chunkProperties.put(JCR_PRIMARY_TYPE, SLING_CHUNK_NT);
             chunkProperties.put(SLING_OFFSET, offset);
             try {
@@ -314,7 +313,7 @@
      * @return
      */
     private InputStream getChunksInputStream(Resource contentResource) {
-        List<Resource> chunkResources = new ArrayList<Resource>();
+        List<Resource> chunkResources = new ArrayList<>();
         for ( Resource r : contentResource.getChildren()) {
             if (r.isResourceType(SLING_CHUNK_NT)) {
                 chunkResources.add(r);
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCopyMoveOperation.java b/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCopyMoveOperation.java
index 0c4a592..89274c3 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCopyMoveOperation.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCopyMoveOperation.java
@@ -82,7 +82,7 @@
                             + dest + ": destination exists");
                     return;
                 } else {
-                    checkoutIfNecessary(request.getResourceResolver().getResource(dstParent),
+                    this.jcrSsupport.checkoutIfNecessary(request.getResourceResolver().getResource(dstParent),
                             changes, versioningConfiguration);
                 }
 
@@ -93,7 +93,7 @@
                 if (!dstParent.equals("")) {
                     final Resource parentResource = request.getResourceResolver().getResource(dstParent);
                     if (parentResource != null ) {
-                        checkoutIfNecessary(parentResource, changes, versioningConfiguration);
+                        this.jcrSsupport.checkoutIfNecessary(parentResource, changes, versioningConfiguration);
                     } else {
                         response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED,
                             "Cannot " + getOperationName() + " " + resource + " to "
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java b/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
index e3add3b..8ba3597 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractCreateOperation.java
@@ -159,6 +159,10 @@
         }
     }
 
+    private boolean isVersionable(final Node node) throws RepositoryException {
+        return node.isNodeType("mix:versionable");
+    }
+
     protected void updateNodeType(final ResourceResolver resolver,
                     final String path,
                     final Map<String, RequestProperty> reqProperties,
@@ -174,7 +178,7 @@
                 final boolean wasVersionable = (node == null ? false : isVersionable(node));
 
                 if ( node != null ) {
-                    checkoutIfNecessary(rsrc, changes, versioningConfiguration);
+                    this.jcrSsupport.checkoutIfNecessary(rsrc, changes, versioningConfiguration);
                     node.setPrimaryType(nodeType);
                 } else {
                     mvm.put("jcr:primaryType", nodeType);
@@ -212,7 +216,7 @@
 
                 // clear existing mixins first
                 if ( node != null ) {
-                    checkoutIfNecessary(rsrc, changes, versioningConfiguration);
+                    this.jcrSsupport.checkoutIfNecessary(rsrc, changes, versioningConfiguration);
                     for (NodeType mixin : node.getMixinNodeTypes()) {
                         String mixinName = mixin.getName();
                         if (!newMixins.remove(mixinName)) {
@@ -610,7 +614,7 @@
                 // check for node type
                 final String nodeType = getPrimaryType(reqProperties, tmpPath);
 
-                checkoutIfNecessary(resource, changes, versioningConfiguration);
+                this.jcrSsupport.checkoutIfNecessary(resource, changes, versioningConfiguration);
 
                 try {
                     final Map<String, Object> props = new HashMap<>();
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java b/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java
index 0de6cf0..9947879 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java
@@ -26,9 +26,6 @@
 import java.util.NoSuchElementException;
 import java.util.Set;
 
-import javax.jcr.ItemNotFoundException;
-import javax.jcr.Node;
-import javax.jcr.RepositoryException;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.api.SlingHttpServletRequest;
@@ -59,6 +56,7 @@
      */
     protected final Logger log = LoggerFactory.getLogger(getClass());
 
+    /** The JCR support provides additional functionality if the resources a backed up by JCR. */
     protected final JCRSupport jcrSsupport = JCRSupport.INSTANCE;
 
     /**
@@ -309,45 +307,6 @@
         return request.getResource().getPath();
     }
 
-    private Node findVersionableAncestor(Node node) throws RepositoryException {
-        if (isVersionable(node)) {
-            return node;
-        }
-        try {
-            node = node.getParent();
-            return findVersionableAncestor(node);
-        } catch (ItemNotFoundException e) {
-            // top-level
-            return null;
-        }
-    }
-
-    protected boolean isVersionable(Node node) throws RepositoryException {
-        return node.isNodeType("mix:versionable");
-    }
-
-    protected void checkoutIfNecessary(final Resource resource,
-            final List<Modification> changes,
-            final VersioningConfiguration versioningConfiguration)
-    throws PersistenceException {
-        if (resource != null && versioningConfiguration.isAutoCheckout()) {
-            final Node node = resource.adaptTo(Node.class);
-            if ( node != null ) {
-                try {
-                    Node versionableNode = findVersionableAncestor(node);
-                    if (versionableNode != null) {
-                        if (!versionableNode.isCheckedOut()) {
-                            versionableNode.getSession().getWorkspace().getVersionManager().checkout(versionableNode.getPath());
-                            changes.add(Modification.onCheckout(versionableNode.getPath()));
-                        }
-                    }
-                } catch ( final RepositoryException re) {
-                    throw new PersistenceException(re.getMessage(), re);
-                }
-            }
-        }
-    }
-
     private static class ApplyToIterator implements Iterator<Resource> {
 
         private final ResourceResolver resolver;
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/operations/DeleteOperation.java b/src/main/java/org/apache/sling/servlets/post/impl/operations/DeleteOperation.java
index fd3468e..13c8008 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/operations/DeleteOperation.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/operations/DeleteOperation.java
@@ -103,7 +103,7 @@
                 uploadHandler.deleteChunks(node);
             }
         } else {
-            checkoutIfNecessary(resource.getParent(), changes,
+            this.jcrSsupport.checkoutIfNecessary(resource.getParent(), changes,
                 versioningConfiguration);
         }
 
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/operations/JCRSupport.java b/src/main/java/org/apache/sling/servlets/post/impl/operations/JCRSupport.java
index a482728..896d49d 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/operations/JCRSupport.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/operations/JCRSupport.java
@@ -24,6 +24,7 @@
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.servlets.post.Modification;
+import org.apache.sling.servlets.post.VersioningConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -62,4 +63,13 @@
         }
         return false;
     }
+
+    public void checkoutIfNecessary(final Resource rsrc,
+            final List<Modification> changes,
+            final VersioningConfiguration versioningConfiguration)
+    throws PersistenceException {
+        if ( rsrc != null && supportImpl != null ) {
+            ((JCRSupportImpl)supportImpl).checkoutIfNecessary(rsrc, changes, versioningConfiguration);
+        }
+    }
 }
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/operations/JCRSupportImpl.java b/src/main/java/org/apache/sling/servlets/post/impl/operations/JCRSupportImpl.java
index 5907896..730fa2b 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/operations/JCRSupportImpl.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/operations/JCRSupportImpl.java
@@ -20,6 +20,7 @@
 
 import java.util.List;
 
+import javax.jcr.ItemNotFoundException;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
@@ -29,6 +30,7 @@
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.servlets.post.Modification;
 import org.apache.sling.servlets.post.SlingPostConstants;
+import org.apache.sling.servlets.post.VersioningConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -160,4 +162,39 @@
         }
         return false;
     }
+
+    private Node findVersionableAncestor(Node node) throws RepositoryException {
+        if (isVersionable(node)) {
+            return node;
+        }
+        try {
+            node = node.getParent();
+            return findVersionableAncestor(node);
+        } catch (ItemNotFoundException e) {
+            // top-level
+            return null;
+        }
+    }
+
+    public void checkoutIfNecessary(final Resource resource,
+            final List<Modification> changes,
+            final VersioningConfiguration versioningConfiguration)
+    throws PersistenceException {
+        if (resource != null && versioningConfiguration.isAutoCheckout()) {
+            final Node node = resource.adaptTo(Node.class);
+            if ( node != null ) {
+                try {
+                    Node versionableNode = findVersionableAncestor(node);
+                    if (versionableNode != null) {
+                        if (!versionableNode.isCheckedOut()) {
+                            versionableNode.getSession().getWorkspace().getVersionManager().checkout(versionableNode.getPath());
+                            changes.add(Modification.onCheckout(versionableNode.getPath()));
+                        }
+                    }
+                } catch ( final RepositoryException re) {
+                    throw new PersistenceException(re.getMessage(), re);
+                }
+            }
+        }
+    }
 }
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 6237c0a..8757ced 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
@@ -249,14 +249,14 @@
             // first, otherwise ensure the parent location
             if (session.itemExists(propPath)) {
                 Node parent = session.getItem(propPath).getParent();
-                checkoutIfNecessary(resolver.getResource(parent.getPath()), changes, versioningConfiguration);
+                this.jcrSsupport.checkoutIfNecessary(resolver.getResource(parent.getPath()), changes, versioningConfiguration);
 
                 session.getItem(propPath).remove();
                 changes.add(Modification.onDeleted(propPath));
             } else {
                 Resource parent = deepGetOrCreateNode(resolver, property.getParentPath(),
                     reqProperties, changes, versioningConfiguration);
-                checkoutIfNecessary(parent, changes, versioningConfiguration);
+                this.jcrSsupport.checkoutIfNecessary(parent, changes, versioningConfiguration);
             }
 
             // move through the session and record operation
@@ -265,12 +265,12 @@
 
                 // node move/copy through session
                 if (isMove) {
-                    checkoutIfNecessary(resolver.getResource(sourceItem.getParent().getPath()), changes, versioningConfiguration);
+                    this.jcrSsupport.checkoutIfNecessary(resolver.getResource(sourceItem.getParent().getPath()), changes, versioningConfiguration);
                     session.move(source, propPath);
                 } else {
                     Node sourceNode = (Node) sourceItem;
                     Node destParent = (Node) session.getItem(property.getParentPath());
-                    checkoutIfNecessary(resolver.getResource(destParent.getPath()), changes, versioningConfiguration);
+                    this.jcrSsupport.checkoutIfNecessary(resolver.getResource(destParent.getPath()), changes, versioningConfiguration);
                     CopyOperation.copy(sourceNode, destParent,
                         property.getName());
                 }
@@ -282,12 +282,12 @@
 
                 // create destination property
                 Node destParent = (Node) session.getItem(property.getParentPath());
-                checkoutIfNecessary(resolver.getResource(destParent.getPath()), changes, versioningConfiguration);
+                this.jcrSsupport.checkoutIfNecessary(resolver.getResource(destParent.getPath()), changes, versioningConfiguration);
                 CopyOperation.copy(sourceProperty, destParent, null);
 
                 // remove source property (if not just copying)
                 if (isMove) {
-                    checkoutIfNecessary(resolver.getResource(sourceProperty.getParent().getPath()), changes, versioningConfiguration);
+                    this.jcrSsupport.checkoutIfNecessary(resolver.getResource(sourceProperty.getParent().getPath()), changes, versioningConfiguration);
                     sourceProperty.remove();
                 }
             }
@@ -334,7 +334,7 @@
                 final Node parentNode = parent.adaptTo(Node.class);
 
                 if ( parentNode != null ) {
-                    checkoutIfNecessary(parent, changes, versioningConfiguration);
+                    this.jcrSsupport.checkoutIfNecessary(parent, changes, versioningConfiguration);
 
                     if (property.getName().equals("jcr:mixinTypes")) {
 
@@ -385,7 +385,7 @@
                 final Resource parent = deepGetOrCreateNode(resolver,
                     prop.getParentPath(), reqProperties, changes, versioningConfiguration);
 
-                checkoutIfNecessary(parent, changes, versioningConfiguration);
+                this.jcrSsupport.checkoutIfNecessary(parent, changes, versioningConfiguration);
 
                 // skip jcr special properties
                 if (prop.getName().equals("jcr:primaryType")
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 e9c6179..05c1ffb 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
@@ -61,7 +61,7 @@
         String destPath = destParent + "/" + destName;
         Session session = item.getSession();
 
-        checkoutIfNecessary(source.getParent(), changes, versioningConfiguration);
+        this.jcrSsupport.checkoutIfNecessary(source.getParent(), changes, versioningConfiguration);
 
         if (session.itemExists(destPath)) {
             session.getItem(destPath).remove();