[jboss-svn-commits] JBL Code SVN: r19741 - labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/server/files.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Apr 28 03:19:24 EDT 2008


Author: michael.neale at jboss.com
Date: 2008-04-28 03:19:24 -0400 (Mon, 28 Apr 2008)
New Revision: 19741

Modified:
   labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/server/files/RestAPIServlet.java
Log:
JBRULES-1562 remote REST api

Modified: labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/server/files/RestAPIServlet.java
===================================================================
--- labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/server/files/RestAPIServlet.java	2008-04-28 07:16:12 UTC (rev 19740)
+++ labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/server/files/RestAPIServlet.java	2008-04-28 07:19:24 UTC (rev 19741)
@@ -18,6 +18,7 @@
 
 
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 
 import javax.security.auth.login.LoginException;
 import javax.servlet.ServletException;
@@ -55,14 +56,31 @@
 
 
 
-    protected void doGet(HttpServletRequest req,
-                         HttpServletResponse res) throws ServletException,
+    protected void doGet(final HttpServletRequest req,
+                         final HttpServletResponse res) throws ServletException,
                                                  IOException {
-        authAndInit(req, res);
-        RestAPI api = null;
-        Response apiRes = api.get(req.getRequestURI());
-        apiRes.writeData(res.getOutputStream());
+        doAuthorizedAction(req, res, new A() {
+			public void a() {
+				try {
 
+					RestAPI api = getAPI();
+					Response apiRes = api.get(req.getRequestURI());
+			        res.setContentType( "application/x-download" );
+			        res.setHeader( "Content-Disposition",
+			                       "attachment; filename=data;");
+					apiRes.writeData(res.getOutputStream());
+					res.getOutputStream().flush();
+
+				} catch (UnsupportedEncodingException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				} catch (IOException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+			}
+        });
+
     }
 
 
@@ -71,7 +89,7 @@
      * Here we perform the action in the appropriate security context.
      * TODO: add in a closure for the action.
      */
-	private void authAndInit(HttpServletRequest req, HttpServletResponse res) throws IOException {
+	private void doAuthorizedAction(HttpServletRequest req, HttpServletResponse res, A action) throws IOException {
 
         String auth = req.getHeader("Authorization");
 
@@ -80,19 +98,24 @@
           res.sendError(res.SC_UNAUTHORIZED);
         }
         else {
-          // Allowed, so do it already
+          action.a();
         }
 	}
 
 
 
-	RestAPI getAPI() throws Exception {
+	RestAPI getAPI()  {
 		if (Contexts.isApplicationContextActive()) {
 			RulesRepository repo = (RulesRepository) Component.getInstance( "repository" );
 			return new RestAPI(repo);
 		} else {
-			RulesRepository repo = new RulesRepository( TestEnvironmentSessionHelper.getSession( false ) );
-			return new RestAPI(repo);
+			try {
+				RulesRepository repo = new RulesRepository( TestEnvironmentSessionHelper.getSession( false ) );
+				return new RestAPI(repo);
+			} catch (Exception e) {
+				throw new IllegalStateException("Unable to run tests", e);
+			}
+
 		}
 	}
 
@@ -150,5 +173,9 @@
     }
 
 
+    /**
+     * For closures. Damn you java when will you catch up with the 70s.
+     */
+    static interface A { public void a(); }
 
 }
\ No newline at end of file




More information about the jboss-svn-commits mailing list