Author: cziegeler
Date: Fri Mar 24 11:06:49 2017
New Revision: 1788407

URL: http://svn.apache.org/viewvc?rev=1788407&view=rev
Log:
SLING-6705 : Make use of java.jcr api optional

Modified:
    sling/trunk/bundles/servlets/get/pom.xml
    
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
    
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
    
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java

Modified: sling/trunk/bundles/servlets/get/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/pom.xml?rev=1788407&r1=1788406&r2=1788407&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/get/pom.xml (original)
+++ sling/trunk/bundles/servlets/get/pom.xml Fri Mar 24 11:06:49 2017
@@ -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>

Modified: 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java?rev=1788407&r1=1788406&r2=1788407&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
 (original)
+++ 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
 Fri Mar 24 11:06:49 2017
@@ -31,9 +31,6 @@ import java.util.Date;
 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.Res
 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 @@ public class StreamRendererServlet exten
     // Accept-Ranges header value
     private static final String ACCEPT_RANGES_BYTES = "bytes";
 
-
-
     /**
      * Full range marker.
      */
@@ -165,14 +161,9 @@ public class StreamRendererServlet exten
 
         // 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) {

Modified: 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java?rev=1788407&r1=1788406&r2=1788407&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
 (original)
+++ 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
 Fri Mar 24 11:06:49 2017
@@ -41,6 +41,8 @@ import org.xml.sax.SAXException;
  */
 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 @@ public class XMLRendererServlet extends
         // 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);
-                    } 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);
+        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().exportSystemView(node.getPath(), ch, 
false, false);
+                        resp.sendError(HttpServletResponse.SC_NO_CONTENT); // 
NO Content
                     }
-                } 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
             }

Modified: 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java?rev=1788407&r1=1788406&r2=1788407&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java
 (original)
+++ 
sling/trunk/bundles/servlets/get/src/main/java/org/apache/sling/servlets/get/impl/impl/info/SlingInfoServlet.java
 Fri Mar 24 11:06:49 2017
@@ -55,7 +55,7 @@ public class SlingInfoServlet extends Sl
     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 @@ public class SlingInfoServlet extends Sl
 
     @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


Reply via email to