incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r587499 - in /incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling: api/ contenttype/ request/ resource/ scripting/ servlet/ slingservlets/
Date Tue, 23 Oct 2007 13:23:18 GMT
Author: bdelacretaz
Date: Tue Oct 23 06:23:17 2007
New Revision: 587499

URL: http://svn.apache.org/viewvc?rev=587499&view=rev
Log:
SLING-79 - make SlingRequestContext an abstract class instead of interface

Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java?rev=587499&r1=587498&r2=587499&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java
Tue Oct 23 06:23:17 2007
@@ -16,51 +16,65 @@
  */
 package org.apache.sling.microsling.api;
 
-import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.microsling.api.exceptions.SlingException;
 import org.apache.sling.microsling.api.requestparams.RequestParameterMap;
 import org.apache.sling.microsling.helpers.exceptions.MissingRequestAttributeException;
 
 /** Provides Sling-specific information about the current Request.
- *  An object implementing this interface must be stored as a 
- *  request attribute using this interface's name as the attribute key,
- *  and can be retrieved using the SlingRequestContext.Accessor class. 
+ *  Can be acquired using getFromRequest(...).
  */
-public interface SlingRequestContext {
+public abstract class SlingRequestContext {
 
-    public static class Accessor {
-        public static SlingRequestContext getSlingRequestContext(HttpServletRequest req)

-        throws MissingRequestAttributeException {
-            final String REQ_ATTR_NAME = SlingRequestContext.class.getName();
-            final SlingRequestContext ctx = (SlingRequestContext)req.getAttribute(REQ_ATTR_NAME);
-            if(ctx == null) {
-                throw new MissingRequestAttributeException(REQ_ATTR_NAME);
-            }
-            return ctx;
+    protected final HttpServletRequest request;
+    protected final HttpServletResponse response;
+    protected final ServletContext servletContext;
+    
+    /** Attribute name used to store this in the request */
+    protected final static String REQ_ATTR_NAME = SlingRequestContext.class.getName();
+    
+    /** Get the SlingRequestContext that's stored in req as an attribute */
+    public static SlingRequestContext getFromRequest(HttpServletRequest req) 
+    throws SlingException {
+        final SlingRequestContext ctx = (SlingRequestContext)req.getAttribute(REQ_ATTR_NAME);
+        if(ctx == null) {
+            throw new MissingRequestAttributeException(REQ_ATTR_NAME);
         }
+        return ctx;
+    }
+    
+    /** Create a SlingRequestContext and store it as a request attribute */
+    public SlingRequestContext(ServletRequest req, ServletResponse resp, ServletContext sctx)
throws SlingException {
+        this.request = (HttpServletRequest)req;
+        this.response = (HttpServletResponse)resp;
+        this.servletContext = sctx;
+        request.setAttribute(REQ_ATTR_NAME,this);
     }
     
     /** The ServiceLocator provides access to other services */
-    ServiceLocator getServiceLocator();
+    public abstract ServiceLocator getServiceLocator() throws SlingException;
 
     /** Acquire a JCR Session if not done yet, and return it */
-    Session getSession() throws SlingException;
+    public abstract Session getSession() throws SlingException;
 
     /** Get the Resource that the current Request processes, null if not found */
-    Resource getResource() throws SlingException;
+    public abstract Resource getResource() throws SlingException;
 
-    /** TODO does this really belong here? */
-    void setResponseContentType(String responseContentType);
+    /**  The preferred Content-Type to use for the response */
+    public abstract String getPreferredResponseContentType() throws SlingException;
     
-    /** TODO see the real Sling, this could have several possible values */ 
-    String getResponseContentType();
+    /** The possible Content-Type values for the response, in order of preference */
+    public abstract String [] getResponseContentTypes() throws SlingException;
 
     /** Return the SlingRequestPathInfo for this request: selectors, extension, suffix, etc.
*/
-    SlingRequestPathInfo getRequestPathInfo() throws SlingException;
+    public abstract SlingRequestPathInfo getRequestPathInfo() throws SlingException;
 
     /** Return the map of request parameters */
-    RequestParameterMap getRequestParameterMap();
+    public abstract RequestParameterMap getRequestParameterMap() throws SlingException;
 }

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java?rev=587499&r1=587498&r2=587499&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/contenttype/ResponseContentTypeResolverFilter.java
Tue Oct 23 06:23:17 2007
@@ -9,6 +9,7 @@
 
 import org.apache.sling.microsling.api.SlingRequestContext;
 import org.apache.sling.microsling.api.helpers.AbstractFilter;
+import org.apache.sling.microsling.request.MicroslingRequestContext;
 
 public class ResponseContentTypeResolverFilter extends AbstractFilter {
 
@@ -21,7 +22,7 @@
             HttpServletResponse response, FilterChain filterChain)
             throws IOException, ServletException {
 
-        final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(request);
+        final MicroslingRequestContext ctx = (MicroslingRequestContext)SlingRequestContext.getFromRequest(request);
 
         String file = "dummy." + ctx.getRequestPathInfo().getExtension();
         final String contentType = getFilterConfig().getServletContext().getMimeType(file);

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java?rev=587499&r1=587498&r2=587499&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java
Tue Oct 23 06:23:17 2007
@@ -21,9 +21,8 @@
 import javax.jcr.Session;
 import javax.jcr.SimpleCredentials;
 import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
-import javax.servlet.http.HttpServletRequest;
+import javax.servlet.ServletResponse;
 
 import org.apache.sling.microsling.api.Resource;
 import org.apache.sling.microsling.api.ResourceResolver;
@@ -36,32 +35,22 @@
 import org.apache.sling.microsling.request.helpers.SlingRequestParameterMap;
 
 /** Additional Request-based info used by for SlingServlets */
-public class MicroslingRequestContext implements SlingRequestContext {
+public class MicroslingRequestContext extends SlingRequestContext {
 
     private Session session;
     private String responseContentType;
-    private final ServletContext servletContext;
     private final Repository repository;
     private Resource resource;
     private MicroslingRequestPathInfo requestPathInfo;
-    private final HttpServletRequest request;
     private final ServiceLocator serviceLocator;
     private RequestParameterMap parameterMap;
 
     /** Build a MicroslingRequestContext and add it to the request attributes */
-    public MicroslingRequestContext(ServletContext sctx,ServletRequest servletRequest,ServiceLocator
serviceLocator) 
-    throws ServletException {
-        this.servletContext = sctx;
+    public MicroslingRequestContext(ServletContext sctx,ServletRequest req,ServletResponse
resp,ServiceLocator serviceLocator) 
+    throws SlingException {
+        super(req,resp,sctx);
         this.serviceLocator = serviceLocator;
 
-        if( !(servletRequest instanceof HttpServletRequest) ) {
-            throw new ServletException("SlingRequestContext requires an HttpServletRequest");
-        }
-        request = (HttpServletRequest)servletRequest;
-
-        // Store this into the request attributes
-        request.setAttribute(SlingRequestContext.class.getName(),this);
-
         // Access our Repository
         final String repoAttr = Repository.class.getName();
         repository = (Repository)sctx.getAttribute(repoAttr);
@@ -98,10 +87,6 @@
         this.responseContentType = responseContentType;
     }
 
-    public String getResponseContentType() {
-        return responseContentType;
-    }
-
     /** Use the ServletContext to compute the mime-type for given filename */
     public String getMimeType(String filename) {
         return servletContext.getMimeType(filename);
@@ -123,6 +108,17 @@
     
     public ServiceLocator getServiceLocator() {
         return serviceLocator;
+    }
+
+    @Override
+    public String getPreferredResponseContentType() throws SlingException {
+        return responseContentType;
+    }
+
+    @Override
+    /** microsling only initializes the first value in the returned array */
+    public String[] getResponseContentTypes() throws SlingException {
+        return new String [] { responseContentType };
     }
 
 }

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java?rev=587499&r1=587498&r2=587499&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/resource/MicroslingResourceResolver.java
Tue Oct 23 06:23:17 2007
@@ -36,7 +36,7 @@
     private static final Logger log = LoggerFactory.getLogger(MicroslingResourceResolver.class);
   
             
     public Resource getResource(HttpServletRequest request) throws SlingException {
-        final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(request);
+        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(request);
         Resource result = null;
         String path = null;
 

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java?rev=587499&r1=587498&r2=587499&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/scripting/SlingScriptResolver.java
Tue Oct 23 06:23:17 2007
@@ -91,7 +91,7 @@
      */
     public boolean evaluateScript(final HttpServletRequest req,final HttpServletResponse
resp) throws ServletException, IOException {
         try {
-            final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(req);
+            final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
 
             Script script = resolveScript(ctx, req.getMethod());
             if (script == null) {
@@ -148,7 +148,7 @@
         String scriptFilename = scriptFilenameBuilder.buildScriptFilename(
             method,
             ctx.getRequestPathInfo().getSelectorString(),
-            ctx.getResponseContentType(),
+            ctx.getPreferredResponseContentType(),
             "*"
         );
         String scriptPath =

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java?rev=587499&r1=587498&r2=587499&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroslingMainServlet.java
Tue Oct 23 06:23:17 2007
@@ -110,7 +110,7 @@
             throws ServletException, IOException {
 
         // our filters might need the SlingRequestContext to store info in it
-        new MicroslingRequestContext(getServletContext(), req, serviceLocator);
+        new MicroslingRequestContext(getServletContext(), req, resp, serviceLocator);
         filterChain.service(req, resp);
     }
 
@@ -158,7 +158,8 @@
                 boolean scriptExists = scriptResolver.evaluateScript(req, resp);
                 if (!scriptExists) {
                     if(log.isDebugEnabled()) {
-                        final Resource r = SlingRequestContext.Accessor.getSlingRequestContext(req).getResource();
+                        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
+                        final Resource r = ctx.getResource();
                         log.debug("No specific Servlet or script found for Resource " + r
+ ", using default Servlet");
                     }
                     delegateToSlingServlet(defaultSlingServlet, req, resp);
@@ -180,7 +181,8 @@
             throws SlingException {
 
         // use the resource type to select a servlet
-        final Resource r = SlingRequestContext.Accessor.getSlingRequestContext(req).getResource();

+        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
+        final Resource r = ctx.getResource(); 
         String type = (r == null ? null : r.getResourceType());
         final Servlet result = (type != null) ? servlets.get(type) : null;
         

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java?rev=587499&r1=587498&r2=587499&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
Tue Oct 23 06:23:17 2007
@@ -49,7 +49,7 @@
         resp.setContentType("text/plain");
 
         try {
-            final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(req);
+            final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
             final Resource  r = ctx.getResource();
             if (r == null) {
                 throw new HttpStatusCodeException(404, "Resource not found: "
@@ -86,7 +86,7 @@
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
             throws ServletException, IOException {
-        final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(req);
+        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(req);
         String redirectPath = req.getPathInfo();
         
         Session s = null;

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java?rev=587499&r1=587498&r2=587499&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
Tue Oct 23 06:23:17 2007
@@ -62,7 +62,7 @@
     protected void doGet(HttpServletRequest request,
             HttpServletResponse response) throws ServletException, IOException {
 
-        SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(request);
+        final SlingRequestContext ctx = SlingRequestContext.getFromRequest(request);
 
 
         try {
@@ -81,7 +81,7 @@
 
                         String contentType = getMimeType(content);
                         if (contentType == null) {
-                            contentType = ctx.getResponseContentType();
+                            contentType = ctx.getPreferredResponseContentType();
                         }
 
                         spool(response,



Mime
View raw message