KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > portal > dispatcher > DeliveryServletDispatcher


1 /*
2  * WebWork, Web Application Framework
3  *
4  * Distributable under Apache license.
5  * See terms of license at opensource.org
6  */

7 package org.infoglue.deliver.portal.dispatcher;
8
9 import java.io.IOException JavaDoc;
10
11 import javax.servlet.RequestDispatcher JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15
16 import org.infoglue.cms.security.InfoGluePrincipal;
17 import org.infoglue.cms.util.CmsPropertyHandler;
18
19 import webwork.action.Action;
20 import webwork.action.ActionContext;
21 import webwork.action.ServletActionContext;
22 import webwork.dispatcher.ActionResult;
23 import webwork.dispatcher.GenericDispatcher;
24 import webwork.dispatcher.ServletDispatcher;
25 import webwork.util.ServletValueStack;
26
27 /**
28  * Main dispatcher servlet. It works in three phases: first propagate all
29  * parameters to the command JavaBean. Second, call execute() to let the
30  * JavaBean create the result data. Third, delegate to the JSP that corresponds to
31  * the result state that was chosen by the JavaBean.
32  *
33  * The command JavaBeans can be found in a package prefixed with either
34  * of the package names in the comma-separated "packages" servlet init parameter.
35  *
36  * Modified by Raymond Lai (alpha2_valen@yahoo.com) on 1 Nov 2003:
37  * modified wrapRequest() to set the character encoding of HttpServletRequest
38  * using the parameter "webwork.i18n.encoding" in webwork.properties.
39  *
40  * @author Rickard Öberg (rickard@middleware-company.com)
41  * @author Matt Baldree (matt@smallleap.com)
42  * @version $Revision: 1.8 $
43  */

44 public class DeliveryServletDispatcher extends ServletDispatcher
45 {
46     private String JavaDoc actionExtension = ".action";
47     
48    /**
49     * Service a request.
50     * The request is first checked to see if it is a multi-part. If it is, then the request
51     * is wrapped so WW will be able to work with the multi-part as if it was a normal request.
52     * Next, we will process all actions until an action returns a non-action which is usually
53     * a view. For each action in a chain, the action's context will be first set and then the
54     * action will be instantiated. Next, the previous action if this action isn't the first in
55     * the chain will have its attributes copied to the current action.
56     *
57     * @param aRequest
58     * @param aResponse
59     * @exception ServletException
60     */

61    public void service(HttpServletRequest JavaDoc aRequest, HttpServletResponse JavaDoc aResponse) throws ServletException JavaDoc
62    {
63        //wrap request if needed
64
if(CmsPropertyHandler.getApplicationName().equalsIgnoreCase("cms"))
65        {
66            super.service(aRequest, aResponse);
67            return;
68        }
69        
70       // Get action
71
String JavaDoc servletPath = (String JavaDoc) aRequest.getAttribute("javax.servlet.include.servlet_path");
72       if (servletPath == null)
73          servletPath = aRequest.getServletPath();
74       
75       
76       String JavaDoc actionName = getActionName(servletPath);
77       GenericDispatcher gd = new GenericDispatcher(actionName, false);
78       ActionContext context = gd.prepareContext();
79
80       InfoGluePrincipal principal = (InfoGluePrincipal)aRequest.getSession().getAttribute("infogluePrincipal");
81       if(principal != null)
82           aRequest.setAttribute("infoglueRemoteUser", principal.getName());
83
84       aRequest.setAttribute("webwork.request_url", aRequest.getRequestURL());
85
86       ServletActionContext.setContext(aRequest, aResponse, getServletContext(), actionName);
87
88       gd.prepareValueStack();
89       ActionResult ar = null;
90       try
91       {
92            gd.executeAction();
93            ar = gd.finish();
94       }
95       catch (Throwable JavaDoc e)
96       {
97           log.error("Could not execute action", e);
98           try
99           {
100               aResponse.sendError(404, "Could not execute action [" + actionName + "]:" + e.getMessage() + getHTMLErrorMessage(e));
101           }
102           catch (IOException JavaDoc e1)
103           {
104           }
105       }
106
107       if (ar != null && ar.getActionException() != null)
108       {
109           log.error("Could not execute action", ar.getActionException());
110           try
111           {
112               aResponse.sendError(500, ar.getActionException().getMessage() + getHTMLErrorMessage(ar.getActionException()));
113           }
114           catch (IOException JavaDoc e1)
115           {
116           }
117       }
118
119       // check if no view exists
120
if (ar != null && ar.getResult() != null && ar.getView() == null && !ar.getResult().equals(Action.NONE)) {
121           try
122           {
123               aResponse.sendError(404, "No view for result [" + ar.getResult() + "] exists for action [" + actionName + "]");
124           }
125           catch (IOException JavaDoc e)
126           {
127           }
128       }
129
130       if (ar != null && ar.getView() != null && ar.getActionException() == null)
131       {
132           String JavaDoc view = ar.getView().toString();
133           log.debug("Result:" + view);
134
135           RequestDispatcher JavaDoc dispatcher = null;
136           try
137           {
138                dispatcher = aRequest.getRequestDispatcher(view);
139           }
140           catch (Throwable JavaDoc e)
141           {
142               // Ignore
143
}
144
145           if (dispatcher == null)
146               throw new ServletException JavaDoc("No presentation file with name '" + view + "' found!");
147
148           try
149           {
150               // If we're included, then include the view
151
// Otherwise do forward
152
// This allow the page to, for example, set content type
153
if (aRequest.getAttribute("javax.servlet.include.servlet_path") == null)
154               {
155                    aRequest.setAttribute("webwork.view_uri", view);
156                    aRequest.setAttribute("webwork.request_uri", aRequest.getRequestURI());
157                    aRequest.setAttribute("webwork.request_url", aRequest.getRequestURL());
158                    //aRequest.setAttribute("webwork.contextPath",aRequest.getContextPath());
159

160                    dispatcher.forward(aRequest, aResponse);
161               }
162               else
163               {
164                    //aRequest.setAttribute("webwork.request_uri",aRequest.getAttribute("javax.servlet.include.request_uri"));
165
//aRequest.setAttribute("webwork.contextPath",aRequest.getAttribute("javax.servlet.include.context_path"));
166
dispatcher.include(aRequest, aResponse);
167               }
168           }
169           catch (IOException JavaDoc e)
170           {
171               throw new ServletException JavaDoc(e);
172           }
173           finally
174           {
175               // Get last action from stack and and store it in request attribute STACK_HEAD
176
// It is then popped from the stack.
177
aRequest.setAttribute(STACK_HEAD, ServletValueStack.getStack(aRequest).popValue());
178           }
179       }
180
181       gd.finalizeContext();
182    }
183
184    /**
185     * Determine action name by extracting last string and removing
186     * extension. (/.../.../Foo.action -> Foo)
187     */

188    private String JavaDoc getActionName(String JavaDoc name)
189    {
190       // Get action name ("Foo.action" -> "Foo" action)
191
int beginIdx = name.lastIndexOf("/");
192       int endIdx = name.lastIndexOf(actionExtension);
193       return name.substring((beginIdx == -1 ? 0 : beginIdx + 1),
194             endIdx == -1 ? name.length() : endIdx);
195    }
196
197 }
198
Popular Tags