001 // $HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/framework/util/KVP2Map.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.framework.util;
045
046 import java.util.Enumeration;
047 import java.util.HashMap;
048 import java.util.Map;
049 import java.util.StringTokenizer;
050
051 import javax.servlet.http.HttpServletRequest;
052
053 /**
054 * offeres utility method for transformating a key-value-pair
055 * encoded request to a <tt>Map</tt>
056 *
057 * @version $Revision: 9339 $
058 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
059 * @author last edited by: $Author: apoth $
060 *
061 * @version 1.0. $Revision: 9339 $, $Date: 2007-12-27 13:31:52 +0100 (Do, 27 Dez 2007) $
062 *
063 * @since 2.0
064 */
065 public class KVP2Map {
066
067 /**
068 * Transforms a String/KVPs like it is used for HTTP-GET request
069 * to a Map.
070 *
071 * TODO: Check if the trim () call may cause side effects. It is currently used to
072 * eliminate possible new line characters at the end of the string, that occured
073 * using the <code>GenericClient</code>.
074 *
075 * @param kvp key-value-pair encoded request
076 * @return created Map
077 */
078 public static Map<String,String> toMap(String kvp) {
079
080 StringTokenizer st = new StringTokenizer( kvp.trim (), "&?" );
081 HashMap<String,String> map = new HashMap<String,String>();
082
083 while ( st.hasMoreTokens() ) {
084 String s = st.nextToken();
085 if ( s != null ) {
086 int pos = s.indexOf( '=' );
087
088 if ( pos > -1 ) {
089 String s1 = s.substring( 0, pos );
090 String s2 = s.substring( pos + 1, s.length() );
091 map.put( s1.toUpperCase(), s2 );
092 }
093 }
094 }
095
096 return map;
097
098 }
099
100 /**
101 * @param iterator Enumeration containing KVP encoded parameters
102 * @return created Map
103 */
104 public static Map<String,String> toMap(Enumeration iterator) {
105 HashMap<String,String> map = new HashMap<String,String>();
106
107 while ( iterator.hasMoreElements() ) {
108 String s = (String)iterator.nextElement();
109 if ( s != null ) {
110 int pos = s.indexOf( '=' );
111
112 if ( pos > -1 ) {
113 String s1 = s.substring( 0, pos );
114 String s2 = s.substring( pos + 1, s.length() );
115 map.put( s1.toUpperCase(), s2 );
116 }
117 }
118 }
119
120 return map;
121 }
122
123 /**
124 * returns the parameters of a <tt>HttpServletRequest</tt> as <tt>Map</tt>.
125 * (HINT: all the keys get changed to upper case)
126 *
127 * @param request
128 * @return a Map which contains kvp's from the given request
129 */
130 public static Map<String, String> toMap(HttpServletRequest request) {
131 HashMap<String, String> map = new HashMap<String, String>();
132
133 Enumeration iterator = request.getParameterNames();
134 while ( iterator.hasMoreElements() ) {
135 String s = (String)iterator.nextElement();
136 String val = request.getParameter(s);
137 map.put(s.toUpperCase(), val);
138 }
139
140 return map;
141 }
142
143 }