01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.navigate.handler;
16:
17: import java.util.Map;
18: import java.util.Set;
19:
20: import javax.servlet.http.HttpServletRequest;
21:
22: import org.apache.struts.action.ActionForward;
23: import org.strecks.constants.InfrastructureKeys;
24: import org.strecks.context.ActionContext;
25: import org.strecks.navigate.NavigationHandler;
26: import org.strecks.util.Assert;
27: import org.strecks.util.StringUtils;
28: import org.strecks.view.ActionForwardViewAdapter;
29: import org.strecks.view.ViewAdapter;
30: import org.strecks.web.RedirectHelper;
31:
32: /**
33: * Builds a URL using the action name as well as any request redirect parameters added using the
34: * <code>RedirectHelper</code> instance. Uses this URL to create an <code>ActionForward</code>
35: * and wrap in an <code>ActionForwardViewAdapter</code>
36: * @author Phil Zoio
37: */
38: public class ActionRedirectNavigationHandler implements
39: NavigationHandler<String> {
40:
41: public ViewAdapter getActionForward(ActionContext actionContext,
42: String actionName) {
43:
44: Assert.notNull(actionContext);
45: Assert.notNull(actionName);
46:
47: HttpServletRequest request = actionContext.getRequest();
48: RedirectHelper helper = (RedirectHelper) request
49: .getAttribute(InfrastructureKeys.REDIRECT_HELPER);
50:
51: Map<String, String> requestParameters = null;
52:
53: if (helper != null) {
54: requestParameters = helper.getRequestParameters();
55: request.getSession().setAttribute(
56: InfrastructureKeys.REDIRECT_PARAMETERS,
57: helper.getSessionParameters());
58: }
59:
60: String url = getRedirectURL(actionName, requestParameters);
61:
62: ActionForward actionForward = new ActionForward(url);
63: actionForward.setRedirect(true);
64: return new ActionForwardViewAdapter(actionForward);
65:
66: }
67:
68: String getRedirectURL(String redirectAction,
69: Map<String, String> requestParameters) {
70:
71: StringBuffer buffer = new StringBuffer();
72: buffer.append(redirectAction).append(".do");
73:
74: if (requestParameters != null) {
75: Set<String> keys = requestParameters.keySet();
76: buffer.append("?");
77: for (String key : keys) {
78: String string = requestParameters.get(key);
79: buffer.append(key).append("=");
80: buffer.append(string);
81: buffer.append("&");
82: }
83: StringUtils.shorten(buffer, 1);
84: }
85: String toString = buffer.toString();
86:
87: return toString;
88:
89: }
90:
91: }
|