You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/09/29 16:36:33 UTC

svn commit: r819966 - in /incubator/pivot/trunk/web/src/org/apache/pivot/web/server: ClientException.java QueryServlet.java

Author: tvolkert
Date: Tue Sep 29 14:36:32 2009
New Revision: 819966

URL: http://svn.apache.org/viewvc?rev=819966&view=rev
Log:
Added ClientException as a checked exception thrown by QueryServlet's protected doX() methods, to allow subclasses to send back responses more semantically correct that HTTP 500.

Added:
    incubator/pivot/trunk/web/src/org/apache/pivot/web/server/ClientException.java
Modified:
    incubator/pivot/trunk/web/src/org/apache/pivot/web/server/QueryServlet.java

Added: incubator/pivot/trunk/web/src/org/apache/pivot/web/server/ClientException.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/web/src/org/apache/pivot/web/server/ClientException.java?rev=819966&view=auto
==============================================================================
--- incubator/pivot/trunk/web/src/org/apache/pivot/web/server/ClientException.java (added)
+++ incubator/pivot/trunk/web/src/org/apache/pivot/web/server/ClientException.java Tue Sep 29 14:36:32 2009
@@ -0,0 +1,40 @@
+/*
+ * 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.pivot.web.server;
+
+/**
+ * Thrown when a client request is invalid.
+ */
+public class ClientException extends Exception {
+    private static final long serialVersionUID = 0;
+
+    public ClientException() {
+        super();
+    }
+
+    public ClientException(String message) {
+        super(message);
+    }
+
+    public ClientException(Throwable cause) {
+        super(cause);
+    }
+
+    public ClientException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}

Modified: incubator/pivot/trunk/web/src/org/apache/pivot/web/server/QueryServlet.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/web/src/org/apache/pivot/web/server/QueryServlet.java?rev=819966&r1=819965&r2=819966&view=diff
==============================================================================
--- incubator/pivot/trunk/web/src/org/apache/pivot/web/server/QueryServlet.java (original)
+++ incubator/pivot/trunk/web/src/org/apache/pivot/web/server/QueryServlet.java Tue Sep 29 14:36:32 2009
@@ -262,11 +262,21 @@
      * serialized by this servlet's serializer before being included in the
      * HTTP response
      *
+     * @throws ServletException
+     * If the server encounters an error while processing the request.
+     *
+     * @throws ClientException
+     * If the client request is invalid in any way. This will cause the client
+     * to receive an HTTP 400 (bad request) response.
+     *
+     * @throws UnsupportedOperationException
+     * If HTTP <tt>GET</tt> is not supported by the servlet.
+     *
      * @see #getParameters()
      * @see #getRequestHeaders()
      * @see #getResponseHeaders()
      */
-    protected Object doGet() throws ServletException {
+    protected Object doGet() throws ServletException, ClientException {
         throw new UnsupportedOperationException();
     }
 
@@ -287,11 +297,21 @@
      * The URL identifying the location of the object that was posted. The
      * semantics of this URL are up to the subclass to define
      *
+     * @throws ServletException
+     * If the server encounters an error while processing the request.
+     *
+     * @throws ClientException
+     * If the client request is invalid in any way. This will cause the client
+     * to receive an HTTP 400 (bad request) response.
+     *
+     * @throws UnsupportedOperationException
+     * If HTTP <tt>POST</tt> is not supported by the servlet.
+     *
      * @see #getParameters()
      * @see #getRequestHeaders()
      * @see #getResponseHeaders()
      */
-    protected URL doPost(Object value) throws ServletException {
+    protected URL doPost(Object value) throws ServletException, ClientException {
         throw new UnsupportedOperationException();
     }
 
@@ -308,11 +328,21 @@
      * The object that is being updated by the client. This object will have
      * been de-serialized from within the request by this servlet's serializer
      *
+     * @throws ServletException
+     * If the server encounters an error while processing the request.
+     *
+     * @throws ClientException
+     * If the client request is invalid in any way. This will cause the client
+     * to receive an HTTP 400 (bad request) response.
+     *
+     * @throws UnsupportedOperationException
+     * If HTTP <tt>PUT</tt> is not supported by the servlet.
+     *
      * @see #getParameters()
      * @see #getRequestHeaders()
      * @see #getResponseHeaders()
      */
-    protected void doPut(Object value) throws ServletException {
+    protected void doPut(Object value) throws ServletException, ClientException {
         throw new UnsupportedOperationException();
     }
 
@@ -325,11 +355,21 @@
      * Request parameters, and request/response headers are available to
      * subclasses via the corresponding query dictionary.
      *
+     * @throws ServletException
+     * If the server encounters an error while processing the request.
+     *
+     * @throws ClientException
+     * If the client request is invalid in any way. This will cause the client
+     * to receive an HTTP 400 (bad request) response.
+     *
+     * @throws UnsupportedOperationException
+     * If HTTP <tt>DELETE</tt> is not supported by the servlet.
+     *
      * @see #getParameters()
      * @see #getRequestHeaders()
      * @see #getResponseHeaders()
      */
-    protected void doDelete() throws ServletException {
+    protected void doDelete() throws ServletException, ClientException {
         throw new UnsupportedOperationException();
     }
 
@@ -506,6 +546,8 @@
             }
         } catch (UnsupportedOperationException exception) {
             doMethodNotAllowed(response);
+        } catch (ClientException exception) {
+            doBadRequest(response);
         } catch (SerializationException exception) {
             throw new ServletException(exception);
         }
@@ -528,6 +570,8 @@
             response.flushBuffer();
         } catch (UnsupportedOperationException exception) {
             doMethodNotAllowed(response);
+        } catch (ClientException exception) {
+            doBadRequest(response);
         } catch (SerializationException exception) {
             throw new ServletException(exception);
         }
@@ -549,6 +593,8 @@
             response.flushBuffer();
         } catch (UnsupportedOperationException exception) {
             doMethodNotAllowed(response);
+        } catch (ClientException exception) {
+            doBadRequest(response);
         } catch (SerializationException exception) {
             throw new ServletException(exception);
         }
@@ -566,6 +612,8 @@
             response.flushBuffer();
         } catch (UnsupportedOperationException exception) {
             doMethodNotAllowed(response);
+        } catch (ClientException exception) {
+            doBadRequest(response);
         }
     }
 
@@ -587,8 +635,6 @@
         doMethodNotAllowed(response);
     }
 
-    /**
-     */
     private void doUnauthorized(HttpServletRequest request, HttpServletResponse response)
         throws IOException {
         response.setHeader("WWW-Authenticate", "BASIC realm=\""
@@ -598,8 +644,6 @@
         response.flushBuffer();
     }
 
-    /**
-     */
     private void doForbidden(HttpServletRequest request, HttpServletResponse response)
         throws IOException {
         response.setStatus(403);
@@ -607,16 +651,18 @@
         response.flushBuffer();
     }
 
-    /**
-     */
     private void doMethodNotAllowed(HttpServletResponse response) throws IOException {
         response.setStatus(405);
         response.setContentLength(0);
         response.flushBuffer();
     }
 
-    /**
-     */
+    private void doBadRequest(HttpServletResponse response) throws IOException {
+        response.setStatus(400);
+        response.setContentLength(0);
+        response.flushBuffer();
+    }
+
     private void setResponseHeaders(HttpServletResponse response) {
         QueryDictionary responseHeaderDictionary = responseHeaders.get();