001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.struts.forms;
017:
018: import javax.servlet.ServletContext;
019: import javax.servlet.http.HttpServletRequest;
020:
021: import org.apache.struts.Globals;
022: import org.apache.struts.action.ActionForm;
023: import org.apache.struts.action.ActionMapping;
024: import org.apache.struts.action.ActionServlet;
025: import org.apache.struts.config.FormBeanConfig;
026: import org.apache.struts.config.ModuleConfig;
027: import org.apache.struts.util.RequestUtils;
028:
029: import de.odysseus.calyxo.base.ModuleContext;
030: import de.odysseus.calyxo.base.conf.ConfigException;
031: import de.odysseus.calyxo.forms.FormData;
032: import de.odysseus.calyxo.forms.FormFactory;
033: import de.odysseus.calyxo.forms.conf.FormsRootConfig;
034:
035: /**
036: * Struts Forms support.
037: *
038: * @author Christoph Beck
039: */
040: public class StrutsFormsSupport extends
041: de.odysseus.calyxo.forms.FormsSupport {
042: private ModuleConfig moduleConfig;
043: private ActionServlet servlet;
044:
045: StrutsFormsSupport(ModuleConfig config) {
046: this .moduleConfig = config;
047: }
048:
049: /* (non-Javadoc)
050: * @see de.odysseus.calyxo.forms.FormsSupport#init(de.odysseus.calyxo.base.ModuleContext, de.odysseus.calyxo.forms.conf.FormsRootConfig)
051: */
052: protected void init(ModuleContext context, FormsRootConfig config,
053: FormFactory factory) throws ConfigException {
054: super .init(context, config, factory);
055: ServletContext servletContext = context.getServletContext();
056: servlet = (ActionServlet) servletContext
057: .getAttribute(Globals.ACTION_SERVLET_KEY);
058: }
059:
060: /**
061: * Look up an action mapping.
062: * The <code>action</code> parameter is manipulated as follows in
063: * computing the name of the requested mapping:
064: * <ul>
065: * <li>Any query string (starting with a querstion mark) is removed.</li>
066: * <li>If the resulting value does not start with a slash, then a slash is prepended.</li>
067: * </ul>
068: *
069: * @param action (module-relative) action path
070: * @return action mapping
071: */
072: protected ActionMapping getActionMapping(String action) {
073: int question = action.indexOf("?");
074: if (question > 0) {
075: action = action.substring(0, question);
076: }
077: if (!action.startsWith("/")) {
078: action = "/" + action;
079: }
080: return (ActionMapping) moduleConfig.findActionConfig(action);
081: }
082:
083: /*
084: * (non-Javadoc)
085: * @see de.odysseus.calyxo.forms.FormsSupport#getFormData(javax.servlet.http.HttpServletRequest, java.lang.String, boolean)
086: */
087: public FormData getFormData(HttpServletRequest request,
088: String action, boolean create) {
089: ActionForm form = null;
090:
091: // Look up the action mapping
092: ActionMapping mapping = getActionMapping(action);
093: if (mapping == null)
094: return null;
095:
096: // Is there a form bean associated with this mapping?
097: String attribute = mapping.getAttribute();
098: if (attribute == null) {
099: return null;
100: }
101:
102: // Look up the form bean configuration information to use
103: FormBeanConfig config = moduleConfig.findFormBeanConfig(mapping
104: .getName());
105: if (config == null) {
106: return null;
107: }
108:
109: // Look up any existing form bean instance
110: if ("request".equals(mapping.getScope())) {
111: form = (ActionForm) request.getAttribute(attribute);
112: } else {
113: form = (ActionForm) request.getSession().getAttribute(
114: attribute);
115: }
116:
117: if (form == null && create) {
118: // get the action form instance
119: form = RequestUtils.createActionForm(config, servlet);
120: if (form == null) {
121: return null;
122: }
123: if ("request".equals(mapping.getScope())) {
124: request.setAttribute(attribute, form);
125: } else {
126: request.getSession().setAttribute(attribute, form);
127: }
128: }
129:
130: if (form instanceof FormData) {
131: return (FormData) form;
132: }
133: return null;
134: }
135:
136: /*
137: * (non-Javadoc)
138: * @see de.odysseus.calyxo.forms.FormsSupport#removeFormData(javax.servlet.http.HttpServletRequest, java.lang.String)
139: */
140: public void removeFormData(HttpServletRequest request, String action) {
141: // Look up the action mapping
142: ActionMapping mapping = getActionMapping(action);
143: if (mapping == null)
144: return;
145:
146: // Is there a form bean associated with this mapping?
147: String attribute = mapping.getAttribute();
148: if (attribute == null) {
149: return;
150: }
151:
152: // Look up the form bean configuration information to use
153: FormBeanConfig config = moduleConfig.findFormBeanConfig(mapping
154: .getName());
155: if (config == null) {
156: return;
157: }
158:
159: // Remove existing form bean instance
160: if ("request".equals(mapping.getScope())) {
161: request.removeAttribute(attribute);
162: } else {
163: request.getSession().removeAttribute(attribute);
164: }
165: }
166:
167: /*
168: * (non-Javadoc)
169: * @see de.odysseus.calyxo.forms.FormsSupport#lookupFormName(java.lang.String)
170: */
171: protected String lookupFormName(String action) {
172: // Look up the action mapping
173: ActionMapping mapping = getActionMapping(action);
174: // answer the form name
175: return mapping == null ? null : mapping.getName();
176: }
177: }
|