incubator-ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r829564 - in /incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet: AceRestException.java DeploymentServlet.java
Date Sun, 25 Oct 2009 12:50:37 GMT
Author: marrs
Date: Sun Oct 25 12:50:37 2009
New Revision: 829564

URL: http://svn.apache.org/viewvc?rev=829564&view=rev
Log:
Applied both patches Toni Menzel supplied for ACE-61.
Reformatted the code to comply with the coding standard.
Made a small modification to the method that tries to close the output, it now checks for
null itself so there's no need to embed it in a null check before invoking it.

Added:
    incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/AceRestException.java
  (with props)
Modified:
    incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/DeploymentServlet.java

Added: incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/AceRestException.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/AceRestException.java?rev=829564&view=auto
==============================================================================
--- incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/AceRestException.java
(added)
+++ incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/AceRestException.java
Sun Oct 25 12:50:37 2009
@@ -0,0 +1,47 @@
+/*
+ * 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.ace.deployment.servlet;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Handle common rest problems here.
+ * This can be thrown by services (unaware of how to handle it in the end) and handeled inside
the catching servlet.
+ */
+public class AceRestException extends Exception {
+    private final int m_statusCode;
+    private final String m_description;
+
+    public AceRestException(int statusCode, String description) {
+        super(statusCode + ":" + description);
+        m_statusCode = statusCode;
+        m_description = description;
+    }
+
+    /**
+     * handling code where we turn <code>this</code> into http error.
+     *
+     * @param response
+     */
+    public void handleAsHttpError(HttpServletResponse response) throws IOException {
+        response.sendError(m_statusCode, m_description);
+    }
+}

Propchange: incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/AceRestException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/DeploymentServlet.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/DeploymentServlet.java?rev=829564&r1=829563&r2=829564&view=diff
==============================================================================
--- incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/DeploymentServlet.java
(original)
+++ incubator/ace/trunk/server/src/org/apache/ace/deployment/servlet/DeploymentServlet.java
Sun Oct 25 12:50:37 2009
@@ -20,6 +20,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Dictionary;
 import java.util.List;
 
@@ -63,106 +64,140 @@
      * <li><code>HttpServletResponse.SC_OK</code> - If all went fine
      */
     @Override
-    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
-        String path = request.getPathInfo();
-        if (path == null) {
-            sendError(response, HttpServletResponse.SC_BAD_REQUEST, "Request URI is invalid");
-            return;
-        }
+    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException {
+        try {
+            String[] pathElements = verifyAndGetPathElements(request.getPathInfo());
+            String gatewayID = pathElements[1];
+            List<String> versions = getVersions(gatewayID);
+            int numberOfElements = pathElements.length;
 
-        String[] pathElements = path.split("/");
-        int numberOfElements = pathElements.length;
+            if (numberOfElements == 3) {
+                handleVersionsRequest(versions, response);
+            }
+            else {
+                String version = pathElements[3];
+                handlePackageDelivery(gatewayID, version, versions, request, response);
+            }
 
-        if ((numberOfElements < 3) || (numberOfElements > 4) || !VERSIONS.equals(pathElements[2]))
{
-            sendError(response, HttpServletResponse.SC_BAD_REQUEST, "Request URI is invalid");
-            return;
         }
+        catch (AceRestException e) {
+            m_log.log(LogService.LOG_WARNING, e.getMessage(), e);
+            e.handleAsHttpError(response);
+        }
+    }
 
-        String gatewayID = pathElements[1];
-        List<String> versions;
+    /**
+     * Serve the case where requested path is like:
+     * http://host/endpoint/gatewayid/versions/ returns a list of versions available for
the specified gateway
+     *
+     * @param versions versions to be put into response
+     * @param response response object.
+     */
+    private void handleVersionsRequest(List<String> versions, HttpServletResponse response)
throws AceRestException {
+        ServletOutputStream output = null;
+
+        response.setContentType(TEXT_MIMETYPE);
         try {
-            versions = m_provider.getVersions(gatewayID);
+            output = response.getOutputStream();
+            for (String version : versions) {
+                output.print(version);
+                output.print("\n");
+            }
         }
-        catch (IllegalArgumentException iae) {
-            String description = "Unknown gateway (" + gatewayID + ")";
-            m_log.log(LogService.LOG_WARNING, description, iae);
-            sendError(response, HttpServletResponse.SC_NOT_FOUND, description);
-            return;
+        catch (IOException e) {
+            throw new AceRestException(HttpServletResponse.SC_BAD_REQUEST, "Request URI is
invalid");
         }
-        catch (IOException ioe) {
-            String description = "Error getting available versions.";
-            m_log.log(LogService.LOG_WARNING, description, ioe);
-            sendError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, description);
-            return;
+        finally {
+            tryClose(output);
         }
+    }
 
+    private void handlePackageDelivery(final String gatewayID, final String version, final
List<String> versions, final HttpServletRequest request, final HttpServletResponse response)
throws AceRestException {
         ServletOutputStream output = null;
+
+        response.setContentType(TEXT_MIMETYPE);
         try {
             output = response.getOutputStream();
-            if (numberOfElements == 3) {
-                response.setContentType(TEXT_MIMETYPE);
-                for (String version : versions) {
-                    output.print(version);
-                    output.print("\n");
-                }
+            if (!versions.contains(version)) {
+                throw new AceRestException(HttpServletResponse.SC_NOT_FOUND, "Unknown version
(" + version + ")");
+            }
+            String current = request.getParameter(CURRENT);
+
+            InputStream inputStream;
+            if (current != null) {
+                inputStream = m_streamGenerator.getDeploymentPackage(gatewayID, current,
version);
             }
             else {
-                String version = pathElements[3];
-                if (!versions.contains(version)) {
-                    sendError(response, HttpServletResponse.SC_NOT_FOUND, "Unknown version
(" + version + ")");
-                    return;
-                }
-                String current = request.getParameter(CURRENT);
-
-                InputStream inputStream;
-                if (current != null) {
-                    inputStream = m_streamGenerator.getDeploymentPackage(gatewayID, current,
version);
-                }
-                else {
-                    inputStream = m_streamGenerator.getDeploymentPackage(gatewayID, version);
-                }
-
-                response.setContentType(DP_MIMETYPE);
-                byte[] buffer = new byte[1024 * 32];
-                for (int bytesRead = inputStream.read(buffer); bytesRead != -1; bytesRead
= inputStream.read(buffer)) {
-                    output.write(buffer, 0, bytesRead);
-                }
-            }
-        }
-        catch (IOException ex) {
-            String description = "Problem reading request or response data";
-            m_log.log(LogService.LOG_WARNING, description, ex);
-            sendError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, description);
-        }
-        finally {
-            try {
-                if (output != null) {
-                    output.close();
-                }
+                inputStream = m_streamGenerator.getDeploymentPackage(gatewayID, version);
             }
-            catch (Exception ex) {
-                m_log.log(LogService.LOG_WARNING, "Exception trying to close stream after
request: " + request.getRequestURL(), ex);
+
+            response.setContentType(DP_MIMETYPE);
+            byte[] buffer = new byte[1024 * 32];
+            for (int bytesRead = inputStream.read(buffer); bytesRead != -1; bytesRead = inputStream.read(buffer))
{
+                output.write(buffer, 0, bytesRead);
             }
         }
+        catch (IOException e) {
+            throw new AceRestException(HttpServletResponse.SC_BAD_REQUEST, "Request URI is
invalid");
+        }
+        finally {
+            tryClose(output);
+        }
     }
 
-    // send a response with the specified status code and description
-    private void sendError(HttpServletResponse response, int statusCode, String description)
{
-        m_log.log(LogService.LOG_WARNING, "Deployment request failed: " + description);
+    private List<String> getVersions(String gatewayID) throws AceRestException {
         try {
-            response.sendError(statusCode, description);
+            return m_provider.getVersions(gatewayID);
+        }
+        catch (IllegalArgumentException iae) {
+            throw new AceRestException(HttpServletResponse.SC_NOT_FOUND, "Unknown gateway
(" + gatewayID + ")");
+        }
+        catch (IOException ioe) {
+            throw new AceRestException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error
getting available versions.");
+        }
+    }
+
+    private void tryClose(OutputStream output) {
+        try {
+            if (output != null) {
+                output.close();
+            }
         }
         catch (IOException e) {
-            m_log.log(LogService.LOG_WARNING, "Unable to send error response with status
code '" + statusCode + "'", e);
+            m_log.log(LogService.LOG_WARNING, "Exception trying to close stream after request.
", e);
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Make sure the path is valid.
+     * Also returns the splited version of #path.
+     *
+     * @param path http request path
+     *
+     * @return splitted version of #path. Split delim is "/"
+     *
+     * @throws AceRestException if path is not valid or cannot be processed.
+     */
+    private String[] verifyAndGetPathElements(String path) throws AceRestException {
+        if (path == null) {
+            throw new AceRestException(HttpServletResponse.SC_BAD_REQUEST, "Request URI is
invalid");
+        }
+        String[] elements = path.split("/");
+        int numberOfElements = elements.length;
+
+        if ((numberOfElements < 3) || (numberOfElements > 4) || !VERSIONS.equals(elements[2]))
{
+            throw new AceRestException(HttpServletResponse.SC_BAD_REQUEST, "Request URI is
invalid");
         }
+        return elements;
     }
 
     @Override
     public String getServletInfo() {
-        return "LiQ Deployment Servlet Endpoint";
+        return "Ace Deployment Servlet Endpoint";
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     public void updated(Dictionary settings) throws ConfigurationException {
         // Nothing needs to be done - handled by DependencyManager
     }



Mime
View raw message