incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r585568 - in /incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling: api/RequestParameter.java api/RequestParameterMap.java request/SlingRequestContext.java request/helpers/SlingRequestParameterMap.java
Date Wed, 17 Oct 2007 16:42:19 GMT
Author: fmeschbe
Date: Wed Oct 17 09:42:18 2007
New Revision: 585568

URL: http://svn.apache.org/viewvc?rev=585568&view=rev
Log:
SLING-65 Add RequestParameter abstraction

Added:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameter.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameterMap.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java
Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/SlingRequestContext.java

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameter.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameter.java?rev=585568&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameter.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameter.java
Wed Oct 17 09:42:18 2007
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.microsling.api;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * The <code>RequestParameter</code> class represents a single parameter sent
+ * with the client request. Instances of this class are returned by the
+ * {@link SlingRequest#getRequestParameter(String)},
+ * {@link SlingRequest#getRequestParameters(String)} and
+ * {@link SlingRequest#getRequestParameterMap()} method.
+ * 
+ * @see SlingRequest#getRequestParameter(String)
+ * @see SlingRequest#getRequestParameters(String)
+ * @see SlingRequest#getRequestParameterMap()
+ */
+public interface RequestParameter {
+
+    /**
+     * Determines whether or not this instance represents a simple form field or
+     * an uploaded file.
+     * 
+     * @return <code>true</code> if the instance represents a simple form
+     *         field; <code>false</code> if it represents an uploaded file.
+     */
+    boolean isFormField();
+
+    /**
+     * Returns the content type passed by the browser or <code>null</code> if
+     * not defined.
+     * 
+     * @return The content type passed by the browser or <code>null</code> if
+     *         not defined.
+     */
+    String getContentType();
+
+    /**
+     * Returns the size in bytes of the parameter.
+     * 
+     * @return The size in bytes of the parameter.
+     */
+    long getSize();
+
+    /**
+     * Returns the contents of the parameter as an array of bytes.
+     * 
+     * @return The contents of the parameter as an array of bytes.
+     */
+    byte[] get();
+
+    /**
+     * Returns an InputStream that can be used to retrieve the contents of the
+     * file.
+     * 
+     * @return An InputStream that can be used to retrieve the contents of the
+     *         file.
+     * @throws IOException if an error occurs.
+     */
+    InputStream getInputStream() throws IOException;
+
+    /**
+     * Returns the original filename in the client's filesystem, as provided by
+     * the browser (or other client software). In most cases, this will be the
+     * base file name, without path information. However, some clients, such as
+     * the Opera browser, do include path information.
+     * 
+     * @return The original filename in the client's filesystem.
+     */
+    String getFileName();
+
+    /**
+     * Returns the contents of the parameter as a String, using the default
+     * character encoding. This method uses {@link #get()} to retrieve the
+     * contents of the item.
+     * 
+     * @return The contents of the parameter, as a string.
+     */
+    String getString();
+
+    /**
+     * Returns the contents of the parameter as a String, using the specified
+     * encoding. This method uses link {@link #get()} to retrieve the contents
+     * of the item.
+     * 
+     * @param encoding The character encoding to use.
+     * @return The contents of the parameter, as a string.
+     * @throws UnsupportedEncodingException if the requested character encoding
+     *             is not available.
+     */
+    String getString(String encoding) throws UnsupportedEncodingException;
+
+}

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameterMap.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameterMap.java?rev=585568&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameterMap.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/RequestParameterMap.java
Wed Oct 17 09:42:18 2007
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.microsling.api;
+
+import java.util.Map;
+
+/**
+ * The <code>RequestParameterMap</code> encapsulates all request parameters of
+ * a request.
+ */
+public interface RequestParameterMap extends Map<String, RequestParameter[]> {
+
+    /** Returns all values for the named parameter or null if none
+     */
+    RequestParameter[] getValues(String name);
+
+    /** Returns the first value for the named parameter or null if none
+     */
+    RequestParameter getValue(String name);
+
+}

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/SlingRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/SlingRequestContext.java?rev=585568&r1=585567&r2=585568&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/SlingRequestContext.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/SlingRequestContext.java
Wed Oct 17 09:42:18 2007
@@ -25,10 +25,12 @@
 import javax.servlet.ServletRequest;
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.sling.microsling.api.RequestParameterMap;
 import org.apache.sling.microsling.api.Resource;
 import org.apache.sling.microsling.api.SlingRequestPathInfo;
 import org.apache.sling.microsling.api.exceptions.SlingException;
 import org.apache.sling.microsling.etc.MissingRequestAttributeException;
+import org.apache.sling.microsling.request.helpers.SlingRequestParameterMap;
 import org.apache.sling.microsling.request.helpers.SlingRequestPathInfoParser;
 
 /** Additional Request-based info used by for SlingServlets */
@@ -40,9 +42,10 @@
     private final Repository repository;
     private Resource resource;
     private final SlingRequestPathInfoParser requestPathInfo;
-    
+    private RequestParameterMap parameterMap;
+
     public static final String REQ_ATTR_NAME = SlingRequestContext.class.getName();
-    
+
     /** Get the SlingRequestContext from req */
     public static SlingRequestContext getFromRequest(HttpServletRequest req) throws MissingRequestAttributeException
{
         final SlingRequestContext ctx = (SlingRequestContext)req.getAttribute(REQ_ATTR_NAME);
@@ -55,27 +58,31 @@
     /** TODO parse the request to extract extension, selectors, etc. */
     public SlingRequestContext(ServletContext sctx,ServletRequest servletRequest) throws
ServletException {
         this.servletContext = sctx;
-        
+
         if( !(servletRequest instanceof HttpServletRequest) ) {
             throw new ServletException("SlingRequestContext requires an HttpServletRequest");
         }
         HttpServletRequest req = (HttpServletRequest)servletRequest;
-        
+
         // Store this into the request attributes
         req.setAttribute(REQ_ATTR_NAME,this);
-        
+
         // Access our Repository
         final String repoAttr = Repository.class.getName();
         repository = (Repository)sctx.getAttribute(repoAttr);
         if(repository==null) {
             throw new SlingException("Repository not available in ServletContext attribute
" + repoAttr);
         }
-        
+
         // Parse request URI
         requestPathInfo = new SlingRequestPathInfoParser(req.getPathInfo());
+
+        // load the request parameter map
+        // this can be done lazily when we keep the request here
+        parameterMap = new SlingRequestParameterMap(req);
     }
-    
-    /** Acquire a JCR Session if not done yet, and return it */  
+
+    /** Acquire a JCR Session if not done yet, and return it */
     public Session getSession() throws RepositoryException {
         if(session==null) {
             session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
@@ -90,7 +97,7 @@
     public void setResource(Resource resource) {
         this.resource = resource;
     }
-    
+
     public void setResponseContentType(String responseContentType) {
         this.responseContentType = responseContentType;
     }
@@ -103,8 +110,13 @@
     public String getMimeType(String filename) {
         return servletContext.getMimeType(filename);
     }
-    
+
     public SlingRequestPathInfo getRequestPathInfo() {
         return requestPathInfo;
+    }
+
+    /** Return the map of request parameters */
+    public RequestParameterMap getRequestParameterMap() {
+        return parameterMap;
     }
 }

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java?rev=585568&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/helpers/SlingRequestParameterMap.java
Wed Oct 17 09:42:18 2007
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.microsling.request.helpers;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.sling.microsling.api.RequestParameter;
+import org.apache.sling.microsling.api.RequestParameterMap;
+
+/**
+ * The <code>SlingRequestParameterMap</code> implements the
+ * <code>RequestParameterMap</code> map interface simply containing all
+ * request parameters as <code>RequestParameter</code> instances.
+ */
+public class SlingRequestParameterMap extends
+        HashMap<String, RequestParameter[]> implements RequestParameterMap {
+
+    /** Create from the HTTP request parameters */
+    public SlingRequestParameterMap(HttpServletRequest request) {
+        Map<?, ?> parameters = request.getParameterMap();
+        for (Map.Entry<?, ?> entry : parameters.entrySet()) {
+            String[] values = (String[]) entry.getValue();
+            RequestParameter[] rpValues = new RequestParameter[values.length];
+            for (int i = 0; i < values.length; i++) {
+                rpValues[i] = new SimpleRequestParameter(values[i]);
+            }
+            put((String) entry.getKey(), rpValues);
+        }
+    }
+
+    public RequestParameter[] getValues(String name) {
+        return get(name);
+    }
+
+    public RequestParameter getValue(String name) {
+        RequestParameter[] values = get(name);
+        return (values != null && values.length > 0) ? values[0] : null;
+    }
+
+    /** Simple implementation of the RequestParameter interface */
+    private static class SimpleRequestParameter implements RequestParameter {
+
+        private String value;
+
+        private byte[] cachedBytes;
+
+        SimpleRequestParameter(String value) {
+            this.value = value;
+        }
+
+        /**
+         * Convert the parameter string value to a byte[] using ISO-8859-1
+         * encoding, which is assumed to be the default for parameters
+         */
+        public byte[] get() {
+            if (cachedBytes == null) {
+                try {
+                    cachedBytes = getString().getBytes("ISO-88591-1");
+                } catch (UnsupportedEncodingException uee) {
+                    // don't care, fall back to platform default
+                    // actually, this is not expected as ISO-8859-1 is required
+                    cachedBytes = getString().getBytes();
+                }
+            }
+            return cachedBytes;
+        }
+
+        public String getContentType() {
+            return null;
+        }
+
+        public String getFileName() {
+            return null;
+        }
+
+        public InputStream getInputStream() {
+            return new ByteArrayInputStream(get());
+        }
+
+        public long getSize() {
+            return get().length;
+        }
+
+        public String getString() {
+            return value;
+        }
+
+        public String getString(String encoding) {
+            return value;
+        }
+
+        public boolean isFormField() {
+            return true;
+        }
+
+    }
+}



Mime
View raw message