You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/01/23 15:10:03 UTC

svn commit: r614535 - in /incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl: SlingMainServlet.java request/RequestData.java services/ServiceLocatorImpl.java

Author: fmeschbe
Date: Wed Jan 23 06:09:59 2008
New Revision: 614535

URL: http://svn.apache.org/viewvc?rev=614535&view=rev
Log:
SLING-186:
   RequestData: Replace HttpStatusCodeException by ResourceNotFoundException
       throw IllegalArgumentException (instead of SlingException) in the
       various unwrap methods if the request/response object is not as
       expected.
   SlingMainServlet: Handle exceptions correctly
   ServiceLocatorImpl: Drop method implementation not defined by API anymore

Modified:
    incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java
    incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/RequestData.java
    incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/services/ServiceLocatorImpl.java

Modified: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java?rev=614535&r1=614534&r2=614535&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java (original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingMainServlet.java Wed Jan 23 06:09:59 2008
@@ -45,12 +45,12 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.sling.api.HttpStatusCodeException;
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceNotFoundException;
 import org.apache.sling.api.services.ServiceLocator;
 import org.apache.sling.api.servlets.ServletResolver;
 import org.apache.sling.commons.mime.MimeTypeService;
@@ -156,7 +156,8 @@
         if (req instanceof HttpServletRequest
             && res instanceof HttpServletResponse) {
 
-            this.service((HttpServletRequest) req, (HttpServletResponse) res);
+            service((HttpServletRequest) req, (HttpServletResponse) res);
+            
         } else {
             throw new ServletException(
                 "Apache Sling must be run in an HTTP servlet environment.");
@@ -198,11 +199,27 @@
                 getSlingAuthenticator().requestAuthentication(clientRequest,
                     clientResponse);
 
-            } catch (HttpStatusCodeException hsce) {
-                // convert the status code exception to sendError
-                getErrorHandler().handleError(hsce.getStatusCode(),
-                    hsce.getMessage(), clientRequest, clientResponse);
+            } catch (ResourceNotFoundException rnfe) {
+                
+                // send this exception as a 404 status
+                getErrorHandler().handleError(HttpServletResponse.SC_NOT_FOUND,
+                    rnfe.getMessage(), clientRequest, clientResponse);
 
+            } catch (SlingException se) {
+                
+                // if we have request data and a non-null active servlet name
+                // we assume, that this is the name of the causing servlet
+                if (requestData != null
+                    && requestData.getActiveServletName() != null) {
+                    clientRequest.setAttribute(ERROR_SERVLET_NAME,
+                        requestData.getActiveServletName());
+                }
+
+                // send this exception as is (albeit unwrapping and wrapped
+                // exception.
+                Throwable t = (se.getCause() != null) ? se.getCause() : se;
+                getErrorHandler().handleError(t, clientRequest, clientResponse);
+                
             } catch (Throwable t) {
 
                 // if we have request data and a non-null active servlet name
@@ -243,7 +260,7 @@
         if (rd != null) {
             rd.include(request, response);
         } else {
-            throw new SlingException("Got no request dispatcher for " + path);
+            log.error("includeServlet: Got no request dispatcher for {}", path);
         }
     }
 

Modified: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/RequestData.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/RequestData.java?rev=614535&r1=614534&r2=614535&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/RequestData.java (original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/request/RequestData.java Wed Jan 23 06:09:59 2008
@@ -41,20 +41,18 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.sling.api.HttpStatusCodeException;
 import org.apache.sling.api.SlingConstants;
-import org.apache.sling.api.SlingException;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.request.RequestProgressTracker;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceNotFoundException;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.services.ServiceLocator;
 import org.apache.sling.api.servlets.ServletResolver;
 import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
 import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
-import org.apache.sling.core.CoreConstants;
 import org.apache.sling.core.RequestUtil;
 import org.apache.sling.core.impl.SlingHttpServletRequestImpl;
 import org.apache.sling.core.impl.SlingHttpServletResponseImpl;
@@ -71,7 +69,7 @@
  * The <code>RequestData</code> class provides access to objects which are set
  * on a Servlet Request wide basis such as the repository session, the
  * persistence manager, etc.
- *
+ * 
  * @see ContentData
  */
 public class RequestData implements BufferProvider {
@@ -119,7 +117,7 @@
 
     /**
      * The name of the currently active serlvet.
-     *
+     * 
      * @see #setActiveServletName(String)
      * @see #getActiveServletName()
      */
@@ -145,8 +143,7 @@
         JcrResourceResolverFactory rmf = slingMainServlet.getResourceResolverFactory();
         if (rmf == null) {
             log.error("RequestData: Missing JcrResourceResolverFactory");
-            throw new HttpStatusCodeException(HttpServletResponse.SC_NOT_FOUND,
-                "No resource can be found");
+            throw new ResourceNotFoundException("No resource can be found");
         }
 
         // officially, getting the manager may fail, but not i this
@@ -217,9 +214,13 @@
 
     /**
      * Unwraps the ServletRequest to a SlingHttpServletRequest.
+     * 
+     * @throws IllegalArgumentException If the <code>request</code> is not a
+     *             <code>SlingHttpServletRequest</code> and not a
+     *             <code>ServletRequestWrapper</code> wrapping a
+     *             <code>SlingHttpServletRequest</code>.
      */
-    public static SlingHttpServletRequest unwrap(ServletRequest request)
-            throws SlingException {
+    public static SlingHttpServletRequest unwrap(ServletRequest request) {
 
         // early check for most cases
         if (request instanceof SlingHttpServletRequest) {
@@ -238,18 +239,21 @@
 
         // if we unwrapped everything and did not find a
         // SlingHttpServletRequest, we lost
-        throw new SlingException(
+        throw new IllegalArgumentException(
             "ServletRequest not wrapping SlingHttpServletRequest");
     }
 
     /**
      * Unwraps the SlingHttpServletRequest to a SlingHttpServletRequestImpl
-     *
+     * 
      * @param request
-     * @throws SlingException
+     * @throws IllegalArgumentException If <code>request</code> is not a
+     *             <code>SlingHttpServletRequestImpl</code> and not
+     *             <code>SlingHttpServletRequestWrapper</code> wrapping a
+     *             <code>SlingHttpServletRequestImpl</code>.
      */
     public static SlingHttpServletRequestImpl unwrap(
-            SlingHttpServletRequest request) throws SlingException {
+            SlingHttpServletRequest request) {
         while (request instanceof SlingHttpServletRequestWrapper) {
             request = ((SlingHttpServletRequestWrapper) request).getSlingRequest();
         }
@@ -258,14 +262,19 @@
             return (SlingHttpServletRequestImpl) request;
         }
 
-        throw new SlingException("SlingHttpServletRequest not of correct type");
+        throw new IllegalArgumentException(
+            "SlingHttpServletRequest not of correct type");
     }
 
     /**
      * Unwraps the ServletRequest to a SlingHttpServletRequest.
+     * 
+     * @throws IllegalArgumentException If the <code>response</code> is not a
+     *             <code>SlingHttpServletResponse</code> and not a
+     *             <code>ServletResponseWrapper</code> wrapping a
+     *             <code>SlingHttpServletResponse</code>.
      */
-    public static SlingHttpServletResponse unwrap(ServletResponse response)
-            throws SlingException {
+    public static SlingHttpServletResponse unwrap(ServletResponse response) {
 
         // early check for most cases
         if (response instanceof SlingHttpServletResponse) {
@@ -284,18 +293,21 @@
 
         // if we unwrapped everything and did not find a
         // SlingHttpServletResponse, we lost
-        throw new SlingException(
+        throw new IllegalArgumentException(
             "ServletResponse not wrapping SlingHttpServletResponse");
     }
 
     /**
      * Unwraps a SlingHttpServletResponse to a SlingHttpServletResponseImpl
-     *
+     * 
      * @param response
-     * @throws SlingException
+     * @throws IllegalArgumentException If <code>response</code> is not a
+     *             <code>SlingHttpServletResponseImpl</code> and not
+     *             <code>SlingHttpServletResponseWrapper</code> wrapping a
+     *             <code>SlingHttpServletResponseImpl</code>.
      */
     public static SlingHttpServletResponseImpl unwrap(
-            SlingHttpServletResponse response) throws SlingException {
+            SlingHttpServletResponse response) {
         while (response instanceof SlingHttpServletResponseWrapper) {
             response = ((SlingHttpServletResponseWrapper) response).getSlingResponse();
         }
@@ -304,36 +316,60 @@
             return (SlingHttpServletResponseImpl) response;
         }
 
-        throw new SlingException("SlingHttpServletResponse not of correct type");
+        throw new IllegalArgumentException(
+            "SlingHttpServletResponse not of correct type");
     }
 
-    public static RequestData getRequestData(SlingHttpServletRequest request)
-            throws SlingException {
-        return unwrap(request).getRequestData();
+    /**
+     * @param request
+     * @return
+     * @throws IllegalArgumentException If the <code>request</code> is not a
+     *             <code>SlingHttpServletRequest</code> and not a
+     *             <code>ServletRequestWrapper</code> wrapping a
+     *             <code>SlingHttpServletRequest</code>.
+     */
+    public static RequestData getRequestData(ServletRequest request) {
+        return unwrap(unwrap(request)).getRequestData();
     }
 
-    public static RequestData getRequestData(ServletRequest request)
-            throws SlingException {
-        return unwrap(unwrap(request)).getRequestData();
+    /**
+     * @param request
+     * @return
+     * @throws IllegalArgumentException If <code>request</code> is not a
+     *             <code>SlingHttpServletRequestImpl</code> and not
+     *             <code>SlingHttpServletRequestWrapper</code> wrapping a
+     *             <code>SlingHttpServletRequestImpl</code>.
+     */
+    public static RequestData getRequestData(SlingHttpServletRequest request) {
+        return unwrap(request).getRequestData();
     }
 
+    /**
+     * @param request
+     * @return
+     * @throws IllegalArgumentException if <code>request</code> is not a
+     *             <code>HttpServletRequest</code> of if <code>request</code>
+     *             is not backed by <code>SlingHttpServletRequestImpl</code>.
+     */
     public static SlingHttpServletRequest toSlingHttpServletRequest(
-            ServletRequest request) throws SlingException {
-        // unwrap to SlingHttpServletRequest
+            ServletRequest request) {
+
+        // unwrap to SlingHttpServletRequest, may throw if no
+        // SlingHttpServletRequest is wrapped in request
         SlingHttpServletRequest cRequest = unwrap(request);
 
-        // check type of response, don't care actually for the response itself
+        // ensure the SlingHttpServletRequest is backed by
+        // SlingHttpServletRequestImpl
         RequestData.unwrap(cRequest);
 
-        // if the servlet response is actually the SlingHttpServletResponse, we
-        // are done
+        // if the request is not wrapper at all, we are done
         if (cRequest == request) {
             return cRequest;
         }
 
         // ensure the request is a HTTP request
         if (!(request instanceof HttpServletRequest)) {
-            throw new SlingException("Request is not an HTTP request");
+            throw new IllegalArgumentException("Request is not an HTTP request");
         }
 
         // otherwise, we create a new response wrapping the servlet response
@@ -342,8 +378,17 @@
             (HttpServletRequest) request);
     }
 
+    /**
+     * @param response
+     * @return
+     * @throws IllegalArgumentException if <code>response</code> is not a
+     *             <code>HttpServletResponse</code> of if
+     *             <code>response</code> is not backed by
+     *             <code>SlingHttpServletResponseImpl</code>.
+     */
     public static SlingHttpServletResponse toSlingHttpServletResponse(
-            ServletResponse response) throws SlingException {
+            ServletResponse response) {
+        
         // unwrap to SlingHttpServletResponse
         SlingHttpServletResponse cResponse = unwrap(response);
 
@@ -358,7 +403,8 @@
 
         // ensure the response is a HTTP response
         if (!(response instanceof HttpServletResponse)) {
-            throw new SlingException("Response is not an HTTP response");
+            throw new IllegalArgumentException(
+                "Response is not an HTTP response");
         }
 
         // otherwise, we create a new response wrapping the servlet response
@@ -381,7 +427,7 @@
      * servlet terminates normally. In case of a Throwable, the active servlet
      * name is not reset and indicates which servlet caused the potential abort
      * of the request.
-     *
+     * 
      * @param request The request object for the service method
      * @param response The response object for the service method
      * @throws IOException May be thrown by the servlet's service method

Modified: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/services/ServiceLocatorImpl.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/services/ServiceLocatorImpl.java?rev=614535&r1=614534&r2=614535&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/services/ServiceLocatorImpl.java (original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/services/ServiceLocatorImpl.java Wed Jan 23 06:09:59 2008
@@ -26,7 +26,6 @@
 
 import org.apache.sling.api.services.InvalidServiceFilterSyntaxException;
 import org.apache.sling.api.services.ServiceLocator;
-import org.apache.sling.api.services.ServiceNotAvailableException;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
@@ -53,19 +52,6 @@
     }
 
     /**
-     * @see org.apache.sling.api.services.ServiceLocator#getRequiredService(java.lang.Class)
-     */
-    public <ServiceType> ServiceType getRequiredService(Class<ServiceType> type)
-            throws ServiceNotAvailableException {
-        final ServiceType service = this.getService(type);
-        if (service == null) {
-            throw new ServiceNotAvailableException("Service " + type.getName()
-                + " is not available.");
-        }
-        return service;
-    }
-
-    /**
      * @see org.apache.sling.api.services.ServiceLocator#getService(java.lang.Class)
      */
     @SuppressWarnings("unchecked")
@@ -109,8 +95,8 @@
             }
             return result;
         } catch (InvalidSyntaxException ise) {
-            throw new InvalidServiceFilterSyntaxException(
-                "Invalid filter syntax: " + filter, ise);
+            throw new InvalidServiceFilterSyntaxException(filter,
+                "Invalid filter syntax", ise);
         }
     }