Author: julien(a)jboss.com
Date: 2007-03-20 11:23:56 -0400 (Tue, 20 Mar 2007)
New Revision: 6776
Added:
trunk/core/src/main/org/jboss/portal/core/controller/classic/HTTPResponse.java
trunk/core/src/main/org/jboss/portal/core/controller/classic/HandlerResponse.java
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/classic/AbstractResponseHandler.java
trunk/core/src/main/org/jboss/portal/core/controller/classic/ClassicController.java
trunk/core/src/main/org/jboss/portal/core/controller/classic/ClassicResponseHandler.java
trunk/core/src/main/org/jboss/portal/core/controller/classic/CommandForward.java
trunk/core/src/main/org/jboss/portal/core/controller/classic/ResponseHandler.java
trunk/core/src/main/org/jboss/portal/core/controller/portlet/PortletResponseHandler.java
trunk/core/src/main/org/jboss/portal/core/model/portal/PortalObjectResponseHandler.java
Log:
improves the response handling mechanism in the core controller
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/classic/AbstractResponseHandler.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/classic/AbstractResponseHandler.java 2007-03-20
15:10:11 UTC (rev 6775)
+++
trunk/core/src/main/org/jboss/portal/core/controller/classic/AbstractResponseHandler.java 2007-03-20
15:23:56 UTC (rev 6776)
@@ -22,43 +22,12 @@
******************************************************************************/
package org.jboss.portal.core.controller.classic;
-import org.jboss.portal.server.ServerInvocation;
-import org.jboss.portal.server.ServerException;
import org.jboss.portal.jems.as.system.AbstractJBossService;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
*/
public abstract class AbstractResponseHandler extends AbstractJBossService implements
ResponseHandler
{
-
- public static void sendStatusCode(ServerInvocation invocation, int sc) throws
ServerException
- {
- try
- {
- HttpServletResponse resp = invocation.getServerContext().getClientResponse();
- resp.sendError(sc);
- }
- catch (IOException e)
- {
- throw new ServerException(e);
- }
- }
-
- public static void sendRedirect(ServerInvocation invocation, String redirect) throws
ServerException
- {
- try
- {
- HttpServletResponse resp = invocation.getServerContext().getClientResponse();
- resp.sendRedirect(redirect);
- }
- catch (IOException e)
- {
- throw new ServerException(e);
- }
- }
}
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/classic/ClassicController.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/classic/ClassicController.java 2007-03-20
15:10:11 UTC (rev 6775)
+++
trunk/core/src/main/org/jboss/portal/core/controller/classic/ClassicController.java 2007-03-20
15:23:56 UTC (rev 6776)
@@ -52,6 +52,7 @@
public class ClassicController extends Controller
{
+ // Unhardcode this
private ResponseHandler[] handlers = new ResponseHandler[]
{
new ClassicResponseHandler(),
@@ -60,11 +61,9 @@
new PortletInstanceResponseHandler()
};
+
public void handle(ServerInvocation invocation) throws ServerException
{
- URLContext urlContext = invocation.getServerContext().getURLContext();
- ControllerContext ctx = new ControllerContext(invocation, this);
-
// Invoke the chain that creates the initial command
ControllerCommand cmd = commandFactory.doMapping(invocation,
invocation.getServerContext().getPortalHost(),
invocation.getServerContext().getPortalContextPath(),
invocation.getServerContext().getPortalRequestPath());
@@ -72,78 +71,123 @@
if (cmd == null)
{
// Handle that case
- throw new ServerException("No valid command");
+ throw new ServerException("No command was produced by the command
factory");
}
- try
+ // Create controller context
+ ControllerContext ctx = new ControllerContext(invocation, this);
+
+ // Handle the command created
+ handleCommand(ctx, cmd);
+ }
+
+ /**
+ * Handle a command which means it executes the command and reacts upon the response
created by the command.
+ *
+ * @param ctx the controller context
+ * @param cmd the command
+ * @throws ServerException
+ */
+ protected void handleCommand(final ControllerContext ctx, final ControllerCommand cmd)
throws ServerException
+ {
+ HandlerResponse handlerResp = executeCommand(ctx, cmd);
+
+ //
+ if (handlerResp == null)
{
- while (true)
+ return;
+ }
+
+ // Find out if we can execute in the same server invocation
+ if (handlerResp instanceof CommandForward)
+ {
+ CommandForward forward = (CommandForward)handlerResp;
+ URLContext urlContext =
ctx.getServerInvocation().getServerContext().getURLContext();
+ if (requiresRedirect(cmd, urlContext, forward))
{
- CommandForward forward;
+ String url = ctx.renderURL(forward.getCommand(), forward.getURLContext(),
null);
+ sendResponse(ctx, new HTTPResponse.SendRedirect(url));
+ }
+ else
+ {
+ executeCommand(ctx, forward.getCommand());
+ }
+ }
+ else
+ {
+ HTTPResponse hr = (HTTPResponse)handlerResp;
+ sendResponse(ctx, hr);
+ }
+ }
- //
- try
- {
- // Execute command
- Object response = ctx.execute(cmd);
+ /**
+ * All http responses in the stack should be handled here.
+ */
+ protected void sendResponse(ControllerContext ctx, HTTPResponse resp)
+ {
+ try
+ {
+ resp.sendResponse(ctx.getServerInvocation().getServerContext());
+ }
+ catch (IOException e)
+ {
+ log.error("Cound not send http response", e);
+ }
+ }
- // Handle the result
- forward = handleResponse(ctx, cmd, response);
- }
- catch (CommandRedirectionException e)
- {
- // Handle the redirection as forward
- forward = new CommandForward(e.getRedirection(), null);
- }
+ protected HandlerResponse executeCommand(ControllerContext ctx, ControllerCommand cmd)
throws ServerException
+ {
+ URLContext urlContext =
ctx.getServerInvocation().getServerContext().getURLContext();
- //
- if (forward == null)
- {
- break;
- }
+ try
+ {
+ // Execute command
+ Object commandResponse = ctx.execute(cmd);
- // Find out if we can execute in the same server invocation
- if (requiresRedirect(cmd, urlContext, forward))
+ // Handle the result
+ for (int i = 0;i < handlers.length;i++)
+ {
+ ResponseHandler handler = handlers[i];
+ HandlerResponse handlerResponse = handler.handleResponse(ctx, cmd,
commandResponse);
+ if (handlerResponse != null)
{
- String url = ctx.renderURL(forward.getCommand(), forward.getURLContext(),
null);
- if (url == null)
- {
- throw new ControllerException();
- }
- AbstractResponseHandler.sendRedirect(invocation, url);
- break;
+ return handlerResponse;
}
- else
- {
- cmd = forward.getCommand();
- }
}
+
+ // We were not able to determine a suitable response
+ return null;
}
+ catch (CommandRedirectionException e)
+ {
+ // Handle the redirection as forward
+ return new CommandForward(e.getRedirection(), null);
+ }
catch (InsufficientTransportGuaranteeException e)
{
urlContext = URLContext.newInstance(true, urlContext.isAuthenticated());
- ServerURL serverURL = getURLFactory().doMapping(invocation, cmd);
- String url = invocation.getResponse().renderURL(serverURL, urlContext, null);
- AbstractResponseHandler.sendRedirect(invocation, url);
+ ServerURL serverURL = getURLFactory().doMapping(ctx.getServerInvocation(),
cmd);
+ String url = ctx.getServerInvocation().getResponse().renderURL(serverURL,
urlContext, null);
+ return new HTTPResponse.SendRedirect(url);
}
catch (ControllerSecurityException e)
{
if (urlContext.isAuthenticated())
{
- AbstractResponseHandler.sendStatusCode(invocation,
HttpServletResponse.SC_UNAUTHORIZED);
+ return new HTTPResponse.SetStatusCode(HttpServletResponse.SC_UNAUTHORIZED);
}
else
{
urlContext = URLContext.newInstance(urlContext.isSecure(), true);
- ServerURL serverURL = getURLFactory().doMapping(invocation, cmd);
- String url = invocation.getResponse().renderURL(serverURL, urlContext,
null);
- AbstractResponseHandler.sendRedirect(invocation, url);
+ ServerURL serverURL = getURLFactory().doMapping(ctx.getServerInvocation(),
cmd);
+ String url = ctx.getServerInvocation().getResponse().renderURL(serverURL,
urlContext, null);
+ return new HTTPResponse.SendRedirect(url);
}
}
catch (ResourceNotFoundException e)
{
log.error("Resource not found " + e.getRef(), e);
- AbstractResponseHandler.sendStatusCode(invocation,
HttpServletResponse.SC_NOT_FOUND);
+ return new HTTPResponse.SetStatusCode(HttpServletResponse.SC_NOT_FOUND);
}
catch (ControllerException e)
{
@@ -159,20 +203,6 @@
}
}
- private CommandForward handleResponse(ControllerContext ctx, ControllerCommand cmd,
Object response) throws IOException, ServletException, ServerException
- {
- for (int i = 0;i < handlers.length;i++)
- {
- ResponseHandler handler = handlers[i];
- CommandForward forward = handler.handleResponse(ctx, cmd, response);
- if (forward != null)
- {
- return forward;
- }
- }
- return null;
- }
-
/**
* Return true if the execution of the next command requires a redirect.
*
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/classic/ClassicResponseHandler.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/classic/ClassicResponseHandler.java 2007-03-20
15:10:11 UTC (rev 6775)
+++
trunk/core/src/main/org/jboss/portal/core/controller/classic/ClassicResponseHandler.java 2007-03-20
15:23:56 UTC (rev 6776)
@@ -36,10 +36,7 @@
import org.jboss.portal.core.model.portal.command.RenderPageCommand;
import javax.servlet.ServletException;
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
-import java.io.InputStream;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -50,7 +47,7 @@
private PortalObjectId defaultPortalPath = PortalObjectId.parse("/default",
PortalObjectId.CANONICAL_FORMAT);
- public CommandForward handleResponse(ControllerContext ctx, ControllerCommand cmd,
Object response) throws IOException, ServletException, ServerException
+ public HandlerResponse handleResponse(ControllerContext ctx, ControllerCommand cmd,
Object response) throws IOException, ServletException, ServerException
{
ServerInvocation invocation = ctx.getServerInvocation();
@@ -79,27 +76,12 @@
}
//
- sendRedirect(invocation, location);
-
- //
- return null;
+ return new HTTPResponse.SendRedirect(location);
}
else if (response instanceof StreamContentResponse)
{
StreamContentResponse scr = (StreamContentResponse)response;
- HttpServletResponse resp = invocation.getServerContext().getClientResponse();
- resp.setContentType(scr.getContentType());
- ServletOutputStream sout = resp.getOutputStream();
- InputStream is = scr.getInputStream();
- byte[] buf = new byte[2048];
- int len;
- while ((len = is.read(buf)) > 0)
- {
- sout.write(buf, 0, len);
- }
- sout.flush();
- sout.close();
- return null;
+ return new HTTPResponse.SendBinary(scr.getContentType(), scr.getInputStream());
}
else
{
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/classic/CommandForward.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/classic/CommandForward.java 2007-03-20
15:10:11 UTC (rev 6775)
+++
trunk/core/src/main/org/jboss/portal/core/controller/classic/CommandForward.java 2007-03-20
15:23:56 UTC (rev 6776)
@@ -26,10 +26,12 @@
import org.jboss.portal.core.controller.ControllerCommand;
/**
+ * Forward to a new command.
+ *
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
*/
-public class CommandForward
+public class CommandForward extends HandlerResponse
{
/** . */
private final ControllerCommand cmd;
Added: trunk/core/src/main/org/jboss/portal/core/controller/classic/HTTPResponse.java
===================================================================
--- trunk/core/src/main/org/jboss/portal/core/controller/classic/HTTPResponse.java
(rev 0)
+++
trunk/core/src/main/org/jboss/portal/core/controller/classic/HTTPResponse.java 2007-03-20
15:23:56 UTC (rev 6776)
@@ -0,0 +1,118 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.core.controller.classic;
+
+import org.jboss.portal.server.ServerInvocationContext;
+import org.jboss.portal.common.util.Tools;
+
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.ServletOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Response that sends a response to the http stream.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class HTTPResponse extends HandlerResponse
+{
+
+ public abstract void sendResponse(ServerInvocationContext ctx) throws IOException;
+
+ public static class SetStatusCode extends HTTPResponse
+ {
+
+ /** . */
+ private final int statusCode;
+
+ public SetStatusCode(int statusCode)
+ {
+ this.statusCode = statusCode;
+ }
+
+ public void sendResponse(ServerInvocationContext ctx) throws IOException
+ {
+ HttpServletResponse resp = ctx.getClientResponse();
+ resp.sendError(statusCode);
+ }
+ }
+
+ public static class SendRedirect extends HTTPResponse
+ {
+
+ /** . */
+ private final String redirect;
+
+ public SendRedirect(String redirect)
+ {
+ this.redirect = redirect;
+ }
+
+ public void sendResponse(ServerInvocationContext ctx) throws IOException
+ {
+ HttpServletResponse resp = ctx.getClientResponse();
+ resp.sendRedirect(redirect);
+ }
+ }
+
+ public static class SendBinary extends HTTPResponse
+ {
+
+ /** . */
+ private final String contentType;
+
+ /** . */
+ private final InputStream in;
+
+ public SendBinary(String contentType, InputStream inputStream)
+ {
+ this.contentType = contentType;
+ this.in = inputStream;
+ }
+
+ public void sendResponse(ServerInvocationContext ctx) throws IOException
+ {
+ HttpServletResponse resp = ctx.getClientResponse();
+ resp.setContentType(contentType);
+ ServletOutputStream sout = null;
+ try
+ {
+ sout = resp.getOutputStream();
+ byte[] buf = new byte[2048];
+ int len;
+ while ((len = in.read(buf)) > 0)
+ {
+ sout.write(buf, 0, len);
+ }
+ sout.flush();
+ }
+ finally
+ {
+ Tools.safeClose(in);
+ Tools.safeClose(sout);
+ }
+ }
+ }
+}
Added: trunk/core/src/main/org/jboss/portal/core/controller/classic/HandlerResponse.java
===================================================================
--- trunk/core/src/main/org/jboss/portal/core/controller/classic/HandlerResponse.java
(rev 0)
+++
trunk/core/src/main/org/jboss/portal/core/controller/classic/HandlerResponse.java 2007-03-20
15:23:56 UTC (rev 6776)
@@ -0,0 +1,33 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.core.controller.classic;
+
+/**
+ * The response from a a response handler.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class HandlerResponse
+{
+}
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/classic/ResponseHandler.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/classic/ResponseHandler.java 2007-03-20
15:10:11 UTC (rev 6775)
+++
trunk/core/src/main/org/jboss/portal/core/controller/classic/ResponseHandler.java 2007-03-20
15:23:56 UTC (rev 6776)
@@ -35,5 +35,5 @@
*/
public interface ResponseHandler
{
- public CommandForward handleResponse(ControllerContext ctx, ControllerCommand cmd,
Object response) throws IOException, ServletException, ServerException;
+ public HandlerResponse handleResponse(ControllerContext ctx, ControllerCommand cmd,
Object response) throws IOException, ServletException, ServerException;
}
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/portlet/PortletResponseHandler.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/portlet/PortletResponseHandler.java 2007-03-20
15:10:11 UTC (rev 6775)
+++
trunk/core/src/main/org/jboss/portal/core/controller/portlet/PortletResponseHandler.java 2007-03-20
15:23:56 UTC (rev 6776)
@@ -26,6 +26,8 @@
import org.jboss.portal.core.controller.ControllerContext;
import org.jboss.portal.core.controller.classic.AbstractResponseHandler;
import org.jboss.portal.core.controller.classic.CommandForward;
+import org.jboss.portal.core.controller.classic.HandlerResponse;
+import org.jboss.portal.core.controller.classic.HTTPResponse;
import org.jboss.portal.core.model.portal.command.InvokePortletWindowActionCommand;
import org.jboss.portal.core.model.portal.command.RenderPageCommand;
import org.jboss.portal.core.model.portal.Page;
@@ -59,7 +61,7 @@
StateString navState,
PortletResponse response);
- public CommandForward handleResponse(ControllerContext ctx, ControllerCommand cmd,
Object response) throws IOException, ServletException, ServerException
+ public HandlerResponse handleResponse(ControllerContext ctx, ControllerCommand cmd,
Object response) throws IOException, ServletException, ServerException
{
if (response instanceof PortletResponse)
{
@@ -88,8 +90,7 @@
{
HTTPRedirectionResponse redirectionResult = (HTTPRedirectionResponse)pir;
String url = redirectionResult.getLocation();
- sendRedirect(invocation, url);
- return null;
+ return new HTTPResponse.SendRedirect(url);
}
else if (pir instanceof InsufficientTransportGuaranteeResponse)
{
@@ -124,17 +125,13 @@
}
//
- sendRedirect(invocation, location);
-
- //
- return null;
+ return new HTTPResponse.SendRedirect(location);
}
else if (pir instanceof ErrorResponse)
{
ErrorResponse error = (ErrorResponse)pir;
error.logErrorTo(log, "An portlet exception occured in portlet");
- sendStatusCode(invocation, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return null;
+ return new
HTTPResponse.SetStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
else
{
Modified:
trunk/core/src/main/org/jboss/portal/core/model/portal/PortalObjectResponseHandler.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/model/portal/PortalObjectResponseHandler.java 2007-03-20
15:10:11 UTC (rev 6775)
+++
trunk/core/src/main/org/jboss/portal/core/model/portal/PortalObjectResponseHandler.java 2007-03-20
15:23:56 UTC (rev 6776)
@@ -24,6 +24,7 @@
import org.jboss.portal.core.controller.classic.AbstractResponseHandler;
import org.jboss.portal.core.controller.classic.CommandForward;
+import org.jboss.portal.core.controller.classic.HandlerResponse;
import org.jboss.portal.core.controller.ControllerCommand;
import org.jboss.portal.core.controller.ControllerContext;
import org.jboss.portal.core.model.portal.command.response.UpdateViewResponse;
@@ -41,7 +42,7 @@
public class PortalObjectResponseHandler extends AbstractResponseHandler
{
- public CommandForward handleResponse(ControllerContext ctx, ControllerCommand cmd,
Object response) throws IOException, ServletException, ServerException
+ public HandlerResponse handleResponse(ControllerContext ctx, ControllerCommand cmd,
Object response) throws IOException, ServletException, ServerException
{
if (response instanceof UpdateViewResponse)
{