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/08 23:49:16 UTC

svn commit: r920555 - in /pivot/trunk: web-server/src/org/apache/pivot/web/server/QueryServlet.java web/src/org/apache/pivot/web/PostQuery.java web/src/org/apache/pivot/web/PutQuery.java web/src/org/apache/pivot/web/Query.java

Author: gbrown
Date: Mon Mar  8 22:49:16 2010
New Revision: 920555

URL: http://svn.apache.org/viewvc?rev=920555&view=rev
Log:
Change return type of PutQuery to Boolean (HTTP specification allows PUT to create a resource on the server; this flag indicates whether or not a resource was created).

Modified:
    pivot/trunk/web-server/src/org/apache/pivot/web/server/QueryServlet.java
    pivot/trunk/web/src/org/apache/pivot/web/PostQuery.java
    pivot/trunk/web/src/org/apache/pivot/web/PutQuery.java
    pivot/trunk/web/src/org/apache/pivot/web/Query.java

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=920555&r1=920554&r2=920555&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 Mon Mar  8 22:49:16 2010
@@ -138,7 +138,6 @@
     public static final String HTTPS_PROTOCOL = "https";
     public static final String URL_ENCODING = "UTF-8";
 
-    public static final String ACTION_HEADER = "Action";
     public static final String CONTENT_TYPE_HEADER = "Content-Type";
     public static final String CONTENT_LENGTH_HEADER = "Content-Length";
     public static final String LOCATION_HEADER = "Location";
@@ -261,7 +260,8 @@
      * @param value
      *
      * @return
-     * A URL containing the location of the created resource.
+     * A URL containing the location of the created resource, or <tt>null</tt> if
+     * operation did not result in the creation of a resource.
      *
      * @throws QueryException
      */
@@ -270,28 +270,19 @@
     }
 
     /**
-     * Handles an HTTP POST/Action request. The default implementation throws an HTTP
-     * 405 query exception.
-     *
-     * @param path
-     * @param action
-     *
-     * @throws QueryException
-     */
-    protected void doPostAction(Path path, String action) throws QueryException {
-        throw new QueryException(Query.Status.METHOD_NOT_ALLOWED);
-    }
-
-    /**
      * Handles an HTTP GET request. The default implementation throws an HTTP
      * 405 query exception.
      *
      * @param path
      * @param value
      *
+     * @return
+     * <tt>true</tt> if the operation resulted in the creation of a resource;
+     * <tt>false</tt>, otherwise.
+     *
      * @throws QueryException
      */
-    protected void doPut(Path path, Object value) throws QueryException {
+    protected boolean doPut(Path path, Object value) throws QueryException {
         throw new QueryException(Query.Status.METHOD_NOT_ALLOWED);
     }
 
@@ -403,7 +394,7 @@
         }
 
         if (!response.isCommitted()) {
-            response.setStatus(200);
+            response.setStatus(Query.Status.OK);
             setResponseHeaders(response);
 
             Serializer<Object> serializer = (Serializer<Object>)createSerializer(path);
@@ -456,63 +447,55 @@
     @Override
     protected final void doPost(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException {
-        String action = request.getHeader(ACTION_HEADER);
+        Path path = getPath(request);
 
-        if (action == null) {
-            Path path = getPath(request);
+        URL location = null;
+        try {
+            validate(path);
 
-            URL location = null;
-            try {
-                validate(path);
+            Object value = null;
+            if (request.getContentLength() > 0) {
                 Serializer<?> serializer = createSerializer(path);
-                Object value = serializer.readObject(request.getInputStream());
-                location = doPost(path, value);
-            } catch (SerializationException exception) {
-                throw new ServletException(exception);
-            } catch (QueryException exception) {
-                response.setStatus(exception.getStatus());
-                response.flushBuffer();
+                value = serializer.readObject(request.getInputStream());
             }
 
-            if (!response.isCommitted()) {
-                response.setStatus(201);
-                setResponseHeaders(response);
-                response.setHeader(LOCATION_HEADER, location.toString());
-                response.setContentLength(0);
-            }
-        } else {
-            Path path = getPath(request);
-
-            try {
-                validate(path);
-                doPostAction(path, action);
-            } catch (QueryException exception) {
-                response.setStatus(exception.getStatus());
-                response.flushBuffer();
-            }
+            location = doPost(path, value);
+        } catch (SerializationException exception) {
+            throw new ServletException(exception);
+        } catch (QueryException exception) {
+            response.setStatus(exception.getStatus());
+            response.flushBuffer();
+        }
 
-            if (!response.isCommitted()) {
-                response.setStatus(204);
-                setResponseHeaders(response);
-                response.setContentLength(0);
+        if (!response.isCommitted()) {
+            if (location == null) {
+                response.setStatus(Query.Status.NO_CONTENT);
+            } else {
+                response.setStatus(Query.Status.CREATED);
+                response.setHeader(LOCATION_HEADER, location.toString());
             }
 
-            response.flushBuffer();
+            setResponseHeaders(response);
+            response.setContentLength(0);
         }
     }
 
     @Override
     protected final void doPut(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException {
-        Object value = null;
-
         Path path = getPath(request);
 
+        boolean created = false;
         try {
             validate(path);
-            Serializer<?> serializer = createSerializer(path);
-            value = serializer.readObject(request.getInputStream());
-            doPut(path, value);
+
+            Object value = null;
+            if (request.getContentLength() > 0) {
+                Serializer<?> serializer = createSerializer(path);
+                value = serializer.readObject(request.getInputStream());
+            }
+
+            created = doPut(path, value);
         } catch (SerializationException exception) {
             throw new ServletException(exception);
         } catch (QueryException exception) {
@@ -521,7 +504,7 @@
         }
 
         if (!response.isCommitted()) {
-            response.setStatus(204);
+            response.setStatus(created ? Query.Status.CREATED : Query.Status.NO_CONTENT);
             setResponseHeaders(response);
             response.setContentLength(0);
             response.flushBuffer();

Modified: pivot/trunk/web/src/org/apache/pivot/web/PostQuery.java
URL: http://svn.apache.org/viewvc/pivot/trunk/web/src/org/apache/pivot/web/PostQuery.java?rev=920555&r1=920554&r2=920555&view=diff
==============================================================================
--- pivot/trunk/web/src/org/apache/pivot/web/PostQuery.java (original)
+++ pivot/trunk/web/src/org/apache/pivot/web/PostQuery.java Mon Mar  8 22:49:16 2010
@@ -73,12 +73,14 @@
 
         execute(METHOD, value);
 
-        String location = getResponseHeaders().get("Location");
-        if (location != null) {
-            try {
-                valueLocation = new URL(getLocation(), location);
-            } catch(MalformedURLException exception) {
-                throw new RuntimeException(exception);
+        if (getStatus() == Status.CREATED) {
+            String location = getResponseHeaders().get("Location");
+            if (location != null) {
+                try {
+                    valueLocation = new URL(getLocation(), location);
+                } catch(MalformedURLException exception) {
+                    throw new RuntimeException(exception);
+                }
             }
         }
 

Modified: pivot/trunk/web/src/org/apache/pivot/web/PutQuery.java
URL: http://svn.apache.org/viewvc/pivot/trunk/web/src/org/apache/pivot/web/PutQuery.java?rev=920555&r1=920554&r2=920555&view=diff
==============================================================================
--- pivot/trunk/web/src/org/apache/pivot/web/PutQuery.java (original)
+++ pivot/trunk/web/src/org/apache/pivot/web/PutQuery.java Mon Mar  8 22:49:16 2010
@@ -19,7 +19,7 @@
 /**
  * Executes an HTTP PUT operation.
  */
-public class PutQuery extends Query<Void> {
+public class PutQuery extends Query<Boolean> {
     private Object value = null;
 
     public static final Method METHOD = Method.PUT;
@@ -58,10 +58,15 @@
 
     /**
      * Synchronously executes the PUT operation.
+     *
+     * @return
+     * <tt>true</tt> if the operation resulted in the creation of a server
+     * resource; <tt>false</tt>, otherwise.
      */
     @Override
-    public Void execute() throws QueryException {
+    public Boolean execute() throws QueryException {
         execute(METHOD, value);
-        return null;
+
+        return (getStatus() == Status.CREATED);
     }
 }

Modified: pivot/trunk/web/src/org/apache/pivot/web/Query.java
URL: http://svn.apache.org/viewvc/pivot/trunk/web/src/org/apache/pivot/web/Query.java?rev=920555&r1=920554&r2=920555&view=diff
==============================================================================
--- pivot/trunk/web/src/org/apache/pivot/web/Query.java (original)
+++ pivot/trunk/web/src/org/apache/pivot/web/Query.java Mon Mar  8 22:49:16 2010
@@ -377,7 +377,7 @@
         URL location = getLocation();
         HttpURLConnection connection = null;
 
-        Serializer<Object> serializer = (Serializer<Object>) this.serializer;
+        Serializer<Object> serializer = (Serializer<Object>)this.serializer;
 
         bytesSent = 0;
         bytesReceived = 0;
@@ -396,6 +396,7 @@
             } else {
                 connection = (HttpURLConnection) location.openConnection(proxy);
             }
+
             connection.setRequestMethod(method.toString());
             connection.setAllowUserInteraction(false);
             connection.setInstanceFollowRedirects(false);
@@ -408,7 +409,7 @@
             }
 
             // Set the request headers
-            if (method == Method.POST || method == Method.PUT) {
+            if (value != null) {
                 connection.setRequestProperty("Content-Type", serializer.getMIMEType(value));
             }
 
@@ -424,14 +425,14 @@
 
             // Set the input/output state
             connection.setDoInput(true);
-            connection.setDoOutput(method == Method.POST || method == Method.PUT);
+            connection.setDoOutput(value != null);
 
             // Connect to the server
             connection.connect();
             queryListeners.connected(this);
 
             // Write the request body
-            if (method == Method.POST || method == Method.PUT) {
+            if (value != null) {
                 OutputStream outputStream = null;
                 try {
                     outputStream = connection.getOutputStream();