001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/enterprise/control/AbstractListener.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.enterprise.control;
045:
046: import java.util.Enumeration;
047: import java.util.HashMap;
048: import java.util.List;
049:
050: import javax.servlet.ServletRequest;
051: import javax.servlet.http.HttpServletRequest;
052:
053: import org.deegree.datatypes.parameter.ParameterValueIm;
054: import org.deegree.framework.util.StringTools;
055:
056: /**
057: * The abstract listener allows the reuse of basic functionality.
058: *
059: * @author <a href="mailto:tfriebe@gmx.net">Torsten Friebe</a>
060: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
061: *
062: * @version $Revision: 9338 $
063: */
064:
065: public abstract class AbstractListener implements WebListener {
066:
067: private FormEvent event;
068:
069: private Object returnValue;
070:
071: private String alternativeDefaultTarget;
072:
073: private String alternativeNext;
074:
075: private String defaultTarget;
076:
077: private String next;
078:
079: private List<ParameterValueIm> params;
080:
081: /**
082: *
083: *
084: * @param e
085: */
086: public abstract void actionPerformed(FormEvent e);
087:
088: /**
089: *
090: *
091: * @param e
092: */
093: public final void handle(FormEvent e) {
094: this .event = e;
095: this .getNextPageFormRequest();
096: this .actionPerformed(e);
097: getRequest().setAttribute("returnValue", getReturnValue());
098: getRequest().setAttribute("next", getNextPage());
099: }
100:
101: /**
102: *
103: *
104: * @return
105: */
106: public ServletRequest getRequest() {
107: Object source = this .event.getSource();
108: return (ServletRequest) source;
109: }
110:
111: public String getHomePath() {
112: String path2Dir = ((HttpServletRequest) this .getRequest())
113: .getSession(true).getServletContext().getRealPath("/");
114: if (!path2Dir.startsWith("/")) {
115: path2Dir = '/' + path2Dir;
116: }
117: return path2Dir;
118: }
119:
120: /**
121: *
122: *
123: * @param target
124: */
125: protected final void setDefaultNextPage(String target) {
126: this .defaultTarget = target;
127: }
128:
129: /**
130: *
131: *
132: * @param target
133: */
134: protected final void setDefaultAlternativeNextPage(String target) {
135: this .alternativeDefaultTarget = target;
136: }
137:
138: /**
139: * Sets the next page for this request.
140: */
141: public void setNextPage(String target) {
142: this .next = target;
143: }
144:
145: /**
146: *
147: *
148: * @return
149: */
150: public String getNextPage() {
151: return ((this .next == null) ? this .defaultTarget : this .next);
152: }
153:
154: /**
155: *
156: *
157: * @param target
158: */
159: public void setAlternativeNextPage(String target) {
160: this .alternativeNext = target;
161: }
162:
163: /**
164: *
165: *
166: * @return
167: */
168: public String getAlternativeNextPage() {
169: return ((this .alternativeNext == null) ? this .alternativeDefaultTarget
170: : this .alternativeNext);
171: }
172:
173: /**
174: * @return
175: *
176: */
177: public Object getReturnValue() {
178: return this .returnValue;
179: }
180:
181: /**
182: * @param model
183: *
184: */
185: public void setReturnValue(Object model) {
186: this .returnValue = model;
187: }
188:
189: /**
190: *
191: */
192: private void getNextPageFormRequest() {
193: String target = null;
194: if ((target = this .getRequest().getParameter("nextPage")) != null) {
195: this .setNextPage(target);
196: }
197: }
198:
199: /**
200: *
201: */
202: protected void gotoErrorPage(String message) {
203: getRequest().setAttribute("SOURCE",
204: "" + this .getClass().getName());
205: getRequest().setAttribute("MESSAGE", message);
206: setNextPage("error.jsp");
207: }
208:
209: /**
210: * sets the list of assigned initialization parameters
211: *
212: * @param params
213: */
214: void setInitParameterList(List<ParameterValueIm> params) {
215: this .params = params;
216: }
217:
218: /**
219: * @see #setInitParameterList(List)
220: * @return the list of assigned initialization parameters
221: */
222: public List<ParameterValueIm> getInitParameterList() {
223: return params;
224: }
225:
226: /**
227: * returns a named initi parameter or <code>null</code> if the parameter is not known
228: *
229: * @param name
230: * @return a named initi parameter or <code>null</code> if the parameter is not known
231: */
232: public String getInitParameter(String name) {
233: for (int i = 0; i < params.size(); i++) {
234: ParameterValueIm param = params.get(i);
235: if (param.getDescriptor().getName().equals(name)) {
236: return (String) param.getValue();
237: }
238: }
239: return null;
240: }
241:
242: /**
243: * transforms the request to a set of name value pairs stored in a HashMap
244: * @return map of request parameters
245: */
246: protected HashMap<String, String> toModel() {
247: HashMap<String, String> model = new HashMap<String, String>();
248: ServletRequest req = getRequest();
249: Enumeration iterator = req.getParameterNames();
250:
251: while (iterator.hasMoreElements()) {
252: String name = (String) iterator.nextElement();
253: String[] value = req.getParameterValues(name);
254:
255: int pos = name.indexOf('@') + 1;
256:
257: if (pos < 0) {
258: pos = 0;
259: }
260:
261: name = name.substring(pos, name.length());
262: model.put(name.toUpperCase(), StringTools.arrayToString(
263: value, ','));
264: }
265:
266: return model;
267: }
268: }
|