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

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1791262 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupport.java b/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupport.java
index f844087..bba35da 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupport.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupport.java
@@ -187,4 +187,13 @@
         }
         return null;
     }
+
+    public void setPrimaryNodeType(final Object node, final String type)
+    throws PersistenceException {
+        if ( node != null && supportImpl != null ) {
+            ((JCRSupportImpl)supportImpl).setPrimaryNodeType(node, type);
+        } else {
+            throw new PersistenceException("Node type should be set but JCR support is not available");
+        }
+    }
 }
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 9e09928..9aa9cd0 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
@@ -394,4 +394,13 @@
     public Object getNode(final Resource rsrc) {
         return rsrc.adaptTo(Node.class);
     }
+
+    public void setPrimaryNodeType(final Object node, final String type)
+    throws PersistenceException {
+        try {
+            ((Node)node).setPrimaryType(type);
+        } catch ( final RepositoryException re) {
+            throw new PersistenceException(re.getMessage(), re);
+        }
+    }
 }
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 95f503f..036de5b 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
@@ -25,10 +25,6 @@
 import java.util.Random;
 import java.util.regex.Pattern;
 
-import javax.jcr.Node;
-import javax.jcr.RepositoryException;
-import javax.servlet.ServletException;
-
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.request.RequestParameter;
@@ -141,7 +137,7 @@
             final PostResponse response,
             final List<Modification> changes,
             final VersioningConfiguration versioningConfiguration)
-    throws PersistenceException, RepositoryException {
+    throws PersistenceException {
 
         final String path = response.getPath();
         final Resource resource = resolver.getResource(path);
@@ -161,18 +157,18 @@
                     final Map<String, RequestProperty> reqProperties,
                     final List<Modification> changes,
                     final VersioningConfiguration versioningConfiguration)
-    throws PersistenceException, RepositoryException {
+    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 Node node = rsrc.adaptTo(Node.class);
+                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);
-                    node.setPrimaryType(nodeType);
+                    this.jcrSsupport.setPrimaryNodeType(node, nodeType);
                 } else {
                     mvm.put(JcrConstants.JCR_PRIMARYTYPE, nodeType);
                 }
@@ -220,9 +216,6 @@
     /**
      * Collects the properties that form the content to be written back to the
      * resource tree.
-     *
-     * @throws RepositoryException if a repository error occurs
-     * @throws ServletException if an internal error occurs
      */
     protected Map<String, RequestProperty> collectContent(
             final SlingHttpServletRequest request,
@@ -530,7 +523,7 @@
                     final Map<String, RequestProperty> reqProperties,
                     final List<Modification> changes,
                     final VersioningConfiguration versioningConfiguration)
-    throws PersistenceException, RepositoryException {
+    throws PersistenceException {
         if (log.isDebugEnabled()) {
             log.debug("Deep-creating resource '{}'", path);
         }
@@ -642,7 +635,7 @@
 
 
     protected String generateName(SlingHttpServletRequest request, String basePath)
-    	throws RepositoryException {
+    	throws PersistenceException {
 
 		// SLING-1091: If a :name parameter is supplied, the (first) value of this parameter is used unmodified as the name
 		//    for the new node. If the name is illegally formed with respect to JCR name requirements, an exception will be
@@ -658,7 +651,7 @@
 
 		        // if the resulting path already exists then report an error
 	            if (request.getResourceResolver().getResource(basePath) != null) {
-	    		    throw new RepositoryException(
+	    		    throw new PersistenceException(
 	    			        "Collision in node names for path=" + basePath);
 	            }
 
@@ -692,7 +685,7 @@
     }
 
     /** Generate a unique path in case the node name generator didn't */
-    private String ensureUniquePath(SlingHttpServletRequest request, String basePath) throws RepositoryException {
+    private String ensureUniquePath(SlingHttpServletRequest request, String basePath) throws PersistenceException {
 		// if resulting path exists, add a suffix until it's not the case
 		// anymore
         final ResourceResolver resolver = request.getResourceResolver();
@@ -713,7 +706,7 @@
 
 	        // Give up after MAX_TRIES
 	        if (resolver.getResource(basePath) != null ) {
-	            throw new RepositoryException(
+	            throw new PersistenceException(
 	                "Collision in generated node names under " + basePath + ", generated path " + basePath + " already exists");
 	        }
 		}
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 1dd29e7..1ded8c7 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
@@ -164,7 +164,7 @@
         if (doGenerateName) {
             try {
                 path = generateName(request, path);
-            } catch (RepositoryException re) {
+            } catch (PersistenceException re) {
                 throw new SlingException("Failed to generate name", re);
             }
         }