SLING-6705 : Make use of java.jcr api optional

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1788407 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index a185055..551a29f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -53,6 +53,15 @@
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Import-Package>
+                            javax.jcr;resolution:=optional,
+                            javax.jcr.version;resolution:=optional,
+                            *
+                        </Import-Package>
+                    </instructions>
+                </configuration>
             </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
index 4908257..b5e7bb9 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
@@ -31,9 +31,6 @@
 import java.util.Iterator;
 import java.util.StringTokenizer;
 
-import javax.jcr.Node;
-import javax.jcr.PathNotFoundException;
-import javax.jcr.RepositoryException;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.ServletOutputStream;
@@ -50,6 +47,7 @@
 import org.apache.sling.api.resource.ResourceNotFoundException;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.servlets.HttpConstants;
 import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
 import org.slf4j.Logger;
@@ -82,8 +80,6 @@
     // Accept-Ranges header value
     private static final String ACCEPT_RANGES_BYTES = "bytes";
 
-
-
     /**
      * Full range marker.
      */
@@ -165,14 +161,9 @@
 
         // fall back to plain text rendering if the resource has no stream
         if (resource.getResourceType().equals(JcrConstants.NT_LINKEDFILE)) {
-            try {
-                String actualResourcePath = resource.adaptTo(Node.class).getProperty(JcrConstants.JCR_CONTENT).getNode().getPath();
-                resource = request.getResourceResolver().getResource(actualResourcePath);
-            } catch (PathNotFoundException e) {
-                throw new ResourceNotFoundException("No data to render");
-            } catch (RepositoryException e) {
-                throw new IOException(e);
-            }
+            final ValueMap vm = resource.adaptTo(ValueMap.class);
+            final String actualResourcePath = vm.get(JcrConstants.JCR_CONTENT, String.class);
+            resource = request.getResourceResolver().getResource(actualResourcePath);
         }
         InputStream stream = resource.adaptTo(InputStream.class);
         if (stream != null) {
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
index 1139437..bd4e808 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
@@ -41,6 +41,8 @@
  */
 public class XMLRendererServlet extends SlingSafeMethodsServlet {
 
+    private static final long serialVersionUID = -3520278283206430415L;
+
     public static final String EXT_XML = "xml";
 
     private static final String SYSVIEW = "sysview";
@@ -62,35 +64,42 @@
         // are we included?
         final boolean isIncluded = req.getAttribute(SlingConstants.ATTR_REQUEST_SERVLET) != null;
 
-        final Node node = r.adaptTo(Node.class);
-        if ( node != null ) {
-            try {
-                if ( req.getRequestPathInfo().getSelectorString() == null
-                     || req.getRequestPathInfo().getSelectorString().equals(DOCVIEW) ) {
-                    // check if response is adaptable to a content handler
-                    final ContentHandler ch = resp.adaptTo(ContentHandler.class);
-                    if ( ch == null ) {
-                        node.getSession().exportDocumentView(node.getPath(), resp.getOutputStream(), false, false);
+        try {
+            final Node node = r.adaptTo(Node.class);
+            if ( node != null ) {
+                try {
+                    if ( req.getRequestPathInfo().getSelectorString() == null
+                         || req.getRequestPathInfo().getSelectorString().equals(DOCVIEW) ) {
+                        // check if response is adaptable to a content handler
+                        final ContentHandler ch = resp.adaptTo(ContentHandler.class);
+                        if ( ch == null ) {
+                            node.getSession().exportDocumentView(node.getPath(), resp.getOutputStream(), false, false);
+                        } else {
+                            node.getSession().exportDocumentView(node.getPath(), ch, false, false);
+                        }
+                    } else if ( req.getRequestPathInfo().getSelectorString().equals(SYSVIEW) ) {
+                        // check if response is adaptable to a content handler
+                        final ContentHandler ch = resp.adaptTo(ContentHandler.class);
+                        if ( ch == null ) {
+                            node.getSession().exportSystemView(node.getPath(), resp.getOutputStream(), false, false);
+                        } else {
+                            node.getSession().exportSystemView(node.getPath(), ch, false, false);
+                        }
                     } else {
-                        node.getSession().exportDocumentView(node.getPath(), ch, false, false);
+                        resp.sendError(HttpServletResponse.SC_NO_CONTENT); // NO Content
                     }
-                } else if ( req.getRequestPathInfo().getSelectorString().equals(SYSVIEW) ) {
-                    // check if response is adaptable to a content handler
-                    final ContentHandler ch = resp.adaptTo(ContentHandler.class);
-                    if ( ch == null ) {
-                        node.getSession().exportSystemView(node.getPath(), resp.getOutputStream(), false, false);
-                    } else {
-                        node.getSession().exportSystemView(node.getPath(), ch, false, false);
-                    }
-                } else {
+                } catch (RepositoryException e) {
+                    throw new ServletException("Unable to export resource as xml: " + r, e);
+                } catch (SAXException e) {
+                    throw new ServletException("Unable to export resource as xml: " + r, e);
+                }
+            } else {
+                if ( !isIncluded ) {
                     resp.sendError(HttpServletResponse.SC_NO_CONTENT); // NO Content
                 }
-            } catch (RepositoryException e) {
-                throw new ServletException("Unable to export resource as xml: " + r, e);
-            } catch (SAXException e) {
-                throw new ServletException("Unable to export resource as xml: " + r, e);
             }
-        } else {
+        } catch ( final Throwable t) {
+            // if the JCR api is not available, we get here
             if ( !isIncluded ) {
                 resp.sendError(HttpServletResponse.SC_NO_CONTENT); // NO Content
             }
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java
index a2872e0..e76b23d 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java
@@ -55,7 +55,7 @@
     private static final String CACHE_CONTROL_HEADER_VALUE =
         "private, no-store, no-cache, max-age=0, must-revalidate";
 
-    private Map<String, SlingInfoProvider> infoProviders = new HashMap<String, SlingInfoProvider>();
+    private Map<String, SlingInfoProvider> infoProviders = new HashMap<>();
 
     @Override
     protected void doGet(SlingHttpServletRequest request,
@@ -195,7 +195,11 @@
 
     @Activate
     protected void activate() {
-        infoProviders.put(SessionInfoProvider.PROVIDER_LABEL,
-            new SessionInfoProvider());
+        try {
+            infoProviders.put(SessionInfoProvider.PROVIDER_LABEL,
+                new SessionInfoProvider());
+        } catch ( final Throwable t) {
+            // if no JCR API is available the above might throw an exception
+        }
     }
 }
\ No newline at end of file