001: package org.apache.struts.action;
002:
003: import java.io.IOException;
004:
005: import javax.servlet.ServletException;
006: import javax.servlet.http.HttpServletRequest;
007: import javax.servlet.http.HttpServletResponse;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.struts.Globals;
011: import org.apache.struts.config.ForwardConfig;
012: import org.apache.struts.config.ModuleConfig;
013:
014: public class PortletStrutsLifecycle implements IPortletStrutsLifecycle {
015:
016: private RequestProcessor _processor;
017: private Log _log;
018: private ModuleConfig _moduleConfig;
019:
020: public PortletStrutsLifecycle(RequestProcessor processor, Log log,
021: ModuleConfig moduleConfig) {
022: _processor = processor;
023: _log = log;
024: _moduleConfig = moduleConfig;
025: }
026:
027: public void processAction(HttpServletRequest request,
028: HttpServletResponse response) throws IOException,
029: ServletException {
030:
031: System.out.println("*** PortletStrutsLifecycle.processAction");
032:
033: // Wrap multipart requests with a special wrapper
034: request = _processor.processMultipart(request);
035:
036: // Identify the path component we will use to select a mapping
037: String path = _processor.processPath(request, response);
038: if (path == null) {
039: return;
040: }
041:
042: if (_log.isDebugEnabled()) {
043: _log.debug("Processing a '" + request.getMethod()
044: + "' for path '" + path + "'");
045: }
046:
047: // Select a Locale for the current user if requested
048: _processor.processLocale(request, response);
049:
050: // Set the content type and no-caching headers if requested
051: _processor.processContent(request, response);
052: _processor.processNoCache(request, response);
053:
054: // General purpose preprocessing hook
055: if (!_processor.processPreprocess(request, response)) {
056: return;
057: }
058:
059: // Identify the mapping for this request
060: ActionMapping mapping = _processor.processMapping(request,
061: response, path);
062: if (mapping == null) {
063: return;
064: }
065:
066: // Check for any role required to perform this action
067: if (!_processor.processRoles(request, response, mapping)) {
068: return;
069: }
070:
071: // Process any ActionForm bean related to this request
072: ActionForm form = _processor.processActionForm(request,
073: response, mapping);
074: _processor.processPopulate(request, response, form, mapping);
075:
076: // Calling the local version instead of delegating to the processor,
077: // because portlet needs special support of separating processing
078: // from rendering
079: if (!processValidate(request, response, form, mapping)) {
080: return;
081: }
082:
083: StrutsCommand command = null;
084:
085: // Process a forward or include specified by this mapping
086: String forwardStr = mapping.getForward();
087: if (forwardStr != null) {
088: command = new ForwardCommand(request, forwardStr);
089: CommandUtils.saveStrutsCommand(request, command);
090: return;
091: }
092:
093: String includeStr = mapping.getInclude();
094: if (includeStr != null) {
095: command = new IncludeCommand(request, includeStr);
096: CommandUtils.saveStrutsCommand(request, command);
097: return;
098: }
099:
100: // Create or acquire the Action instance to process this request
101: Action action = _processor.processActionCreate(request,
102: response, mapping);
103: if (action != null) {
104: ActionForward forward = _processor.processActionPerform(
105: request, response, action, form, mapping);
106: command = new ForwardConfigCommand(request, forward);
107: CommandUtils.saveStrutsCommand(request, command);
108: return;
109: }
110: }
111:
112: public void render(HttpServletRequest request,
113: HttpServletResponse response) throws IOException,
114: ServletException {
115:
116: System.out.println("*** PortletStrutsLifecycle.render");
117:
118: StrutsCommand savedAction = CommandUtils
119: .getStrutsCommand(request);
120: savedAction.execute(_processor, request, response);
121: }
122:
123: protected boolean processValidate(HttpServletRequest request,
124: HttpServletResponse response, ActionForm form,
125: ActionMapping mapping) throws IOException, ServletException {
126:
127: if (form == null) {
128: return (true);
129: }
130:
131: // Was this request cancelled?
132: if (request.getAttribute(Globals.CANCEL_KEY) != null) {
133: if (_log.isDebugEnabled()) {
134: _log
135: .debug(" Cancelled transaction, skipping validation");
136: }
137: return (true);
138: }
139:
140: // Has validation been turned off for this mapping?
141: if (!mapping.getValidate()) {
142: return (true);
143: }
144:
145: // Call the form bean's validation method
146: if (_log.isDebugEnabled()) {
147: _log.debug(" Validating input form properties");
148: }
149: ActionErrors errors = form.validate(mapping, request);
150: if ((errors == null) || errors.isEmpty()) {
151: if (_log.isTraceEnabled()) {
152: _log.trace(" No errors detected, accepting input");
153: }
154: return (true);
155: }
156:
157: // Special handling for multipart request
158: if (form.getMultipartRequestHandler() != null) {
159: if (_log.isTraceEnabled()) {
160: _log.trace(" Rolling back multipart request");
161: }
162: form.getMultipartRequestHandler().rollback();
163: }
164:
165: // Has an input form been specified for this mapping?
166: String input = mapping.getInput();
167: if (input == null) {
168: if (_log.isTraceEnabled()) {
169: _log
170: .trace(" Validation failed but no input form available");
171: }
172: response.sendError(
173: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
174: _processor.getInternal().getMessage("noInput",
175: mapping.getPath()));
176: return (false);
177: }
178:
179: // Save our error messages and return to the input form if possible
180: if (_log.isDebugEnabled()) {
181: _log.debug(" Validation failed, returning to '" + input
182: + "'");
183: }
184: request.setAttribute(Globals.ERROR_KEY, errors);
185:
186: // save the actions for render phase
187: if (_moduleConfig.getControllerConfig().getInputForward()) {
188: ForwardConfig forward = mapping.findForward(input);
189: StrutsCommand command = new ForwardConfigCommand(request,
190: forward);
191: CommandUtils.saveStrutsCommand(request, command);
192: } else {
193: StrutsCommand command = new ForwardCommand(request, input);
194: CommandUtils.saveStrutsCommand(request, command);
195: }
196:
197: return (false);
198: }
199: }
|