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