You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/03/06 13:46:56 UTC

svn commit: r919754 - in /pivot/trunk: core/src/org/apache/pivot/collections/Sequence.java web-server/src/org/apache/pivot/web/server/QueryServlet.java

Author: gbrown
Date: Sat Mar  6 12:46:56 2010
New Revision: 919754

URL: http://svn.apache.org/viewvc?rev=919754&view=rev
Log:
Updated QueryServlet to pass an instance of Path (an immutable sequence of Strings) to the handler methods instead of a string. This saves the handler methods the effort of parsing the string and also avoids unnecessarily parsing it multiple times (e.g. in validate(), createSerializer(), and in the actual method handler). Also made the handler methods protected since calling them without first preparing the thread local member variables would result in an invalid state, and made a trivial argument name change to Sequence.Tree.Path.

Modified:
    pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java
    pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java

Modified: pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java?rev=919754&r1=919753&r2=919754&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/collections/Sequence.java Sat Mar  6 12:46:56 2010
@@ -58,22 +58,22 @@
             }
 
             @Override
-            public int add(Integer item) {
-                return elements.add(item);
+            public int add(Integer element) {
+                return elements.add(element);
             }
 
             @Override
-            public void insert(Integer item, int index) {
-                elements.insert(item, index);
+            public void insert(Integer element, int index) {
+                elements.insert(element, index);
             }
 
             @Override
-            public Integer update(int index, Integer item) {
-                return elements.update(index, item);
+            public Integer update(int index, Integer element) {
+                return elements.update(index, element);
             }
 
             @Override
-            public int remove(Integer item) {
+            public int remove(Integer element) {
                 throw new UnsupportedOperationException();
             }
 
@@ -88,8 +88,8 @@
             }
 
             @Override
-            public int indexOf(Integer item) {
-                return elements.indexOf(item);
+            public int indexOf(Integer element) {
+                return elements.indexOf(element);
             }
 
             @Override
@@ -109,12 +109,12 @@
                 sb.append("[");
 
                 int i = 0;
-                for (Integer item : elements) {
+                for (Integer element : elements) {
                     if (i > 0) {
                         sb.append(", ");
                     }
 
-                    sb.append(item);
+                    sb.append(element);
                     i++;
                 }
 
@@ -145,17 +145,17 @@
             }
 
             @Override
-            public int add(Integer item) {
+            public int add(Integer element) {
                 throw new UnsupportedOperationException();
             }
 
             @Override
-            public void insert(Integer item, int index) {
+            public void insert(Integer element, int index) {
                 throw new UnsupportedOperationException();
             }
 
             @Override
-            public Integer update(int index, Integer item) {
+            public Integer update(int index, Integer element) {
                 throw new UnsupportedOperationException();
             }
 

Modified: pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java
URL: http://svn.apache.org/viewvc/pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java?rev=919754&r1=919753&r2=919754&view=diff
==============================================================================
--- pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java (original)
+++ pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java Sat Mar  6 12:46:56 2010
@@ -25,14 +25,18 @@
 import java.net.URL;
 import java.net.URLDecoder;
 import java.util.Enumeration;
+import java.util.Iterator;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.pivot.collections.ArrayList;
+import org.apache.pivot.collections.Sequence;
 import org.apache.pivot.serialization.SerializationException;
 import org.apache.pivot.serialization.Serializer;
+import org.apache.pivot.util.ImmutableIterator;
 import org.apache.pivot.web.Query;
 import org.apache.pivot.web.QueryDictionary;
 import org.apache.pivot.web.QueryException;
@@ -41,6 +45,82 @@
  * Abstract base class for query servlets.
  */
 public abstract class QueryServlet extends HttpServlet {
+    /**
+     * Immutable string sequence representing a query path. The path is constructed
+     * by splitting the path info provided by the base servlet on the path separator
+     * character ("/").
+     */
+    public static class Path implements Sequence<String>, Iterable<String> {
+        private ArrayList<String> elements;
+
+        public Path() {
+            this(new String[] {});
+        }
+
+        public Path(String[] elements) {
+            this.elements = new ArrayList<String>(elements);
+        }
+
+        @Override
+        public int add(String element) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public void insert(String element, int index) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public String update(int index, String element) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public int remove(String element) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public Sequence<String> remove(int index, int count) {
+            return elements.remove(index, count);
+        }
+
+        @Override
+        public String get(int index) {
+            return elements.get(index);
+        }
+
+        @Override
+        public int indexOf(String element) {
+            return elements.indexOf(element);
+        }
+
+        @Override
+        public int getLength() {
+            return elements.getLength();
+        }
+
+        @Override
+        public Iterator<String> iterator() {
+            return new ImmutableIterator<String>(elements.iterator());
+        }
+
+        @Override
+        public String toString() {
+            StringBuilder sb = new StringBuilder();
+
+            int i = 0;
+            for (String element : elements) {
+                sb.append("/");
+                sb.append(element);
+                i++;
+            }
+
+            return sb.toString();
+        }
+    }
+
     private static final long serialVersionUID = 4881638232902478092L;
 
     private boolean determineContentLength = false;
@@ -131,7 +211,7 @@
      *
      * @throws ServletException
      */
-    public void prepare() throws ServletException {
+    protected void prepare() throws ServletException {
     }
 
     /**
@@ -142,7 +222,7 @@
      *
      * @throws ServletException
      */
-    public void dispose() throws ServletException {
+    protected void dispose() throws ServletException {
     }
 
     /**
@@ -151,9 +231,11 @@
      * <p>
      * The default implementation is a no-op.
      *
+     * @param path
+     *
      * @throws QueryException
      */
-    public void validate(String path) throws QueryException {
+    protected void validate(Path path) throws QueryException {
     }
 
     /**
@@ -167,7 +249,7 @@
      *
      * @throws QueryException
      */
-    public Object doGet(String path) throws QueryException {
+    protected Object doGet(Path path) throws QueryException {
         throw new QueryException(Query.Status.METHOD_NOT_ALLOWED);
     }
 
@@ -183,7 +265,7 @@
      *
      * @throws QueryException
      */
-    public URL doPost(String path, Object value) throws QueryException {
+    protected URL doPost(Path path, Object value) throws QueryException {
         throw new QueryException(Query.Status.METHOD_NOT_ALLOWED);
     }
 
@@ -196,7 +278,7 @@
      *
      * @throws QueryException
      */
-    public void doPostAction(String path, String action) throws QueryException {
+    protected void doPostAction(Path path, String action) throws QueryException {
         throw new QueryException(Query.Status.METHOD_NOT_ALLOWED);
     }
 
@@ -209,7 +291,7 @@
      *
      * @throws QueryException
      */
-    public void doPut(String path, Object value) throws QueryException {
+    protected void doPut(Path path, Object value) throws QueryException {
         throw new QueryException(Query.Status.METHOD_NOT_ALLOWED);
     }
 
@@ -221,7 +303,7 @@
      *
      * @throws QueryException
      */
-    public void doDelete(String path) throws QueryException {
+    protected void doDelete(Path path) throws QueryException {
         throw new QueryException(Query.Status.METHOD_NOT_ALLOWED);
     }
 
@@ -229,8 +311,10 @@
      * Creates a serializer that will be used to serialize the current request data.
      *
      * @param path
+     *
+     * @throws ServletException
      */
-    public abstract Serializer<?> createSerializer(String path) throws ServletException;
+    protected abstract Serializer<?> createSerializer(Path path) throws ServletException;
 
     @Override
     @SuppressWarnings("unchecked")
@@ -307,7 +391,7 @@
     @SuppressWarnings("unchecked")
     protected final void doGet(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException {
-        String path = request.getPathInfo();
+        Path path = getPath(request);
 
         Object result = null;
         try {
@@ -375,15 +459,13 @@
         String action = request.getHeader(ACTION_HEADER);
 
         if (action == null) {
-            Object value = null;
-
-            String path = request.getPathInfo();
+            Path path = getPath(request);
 
             URL location = null;
             try {
                 validate(path);
                 Serializer<?> serializer = createSerializer(path);
-                value = serializer.readObject(request.getInputStream());
+                Object value = serializer.readObject(request.getInputStream());
                 location = doPost(path, value);
             } catch (SerializationException exception) {
                 throw new ServletException(exception);
@@ -399,7 +481,7 @@
                 response.setContentLength(0);
             }
         } else {
-            String path = request.getPathInfo();
+            Path path = getPath(request);
 
             try {
                 validate(path);
@@ -424,7 +506,7 @@
         throws IOException, ServletException {
         Object value = null;
 
-        String path = request.getPathInfo();
+        Path path = getPath(request);
 
         try {
             validate(path);
@@ -450,7 +532,7 @@
     protected final void doDelete(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException {
         try {
-            String path = request.getPathInfo();
+            Path path = getPath(request);
             validate(path);
             doDelete(path);
         } catch (QueryException exception) {
@@ -487,6 +569,12 @@
         response.flushBuffer();
     }
 
+    private Path getPath(HttpServletRequest request) {
+        String pathInfo = request.getPathInfo();
+        Path path = (pathInfo.length() == 0) ? new Path() : new Path(pathInfo.substring(1).split("/"));
+        return path;
+    }
+
     private void setResponseHeaders(HttpServletResponse response) {
         QueryDictionary responseHeaderDictionary = responseHeaders.get();