src/core/net/sf/basedb/util/QueryParameters.java

Code
Comments
Other
Rev Date Author Line
4074 08 Jan 08 nicklas 1 /**
4074 08 Jan 08 nicklas 2   $Id$
4074 08 Jan 08 nicklas 3
4074 08 Jan 08 nicklas 4   Copyright (C) Authors contributing to this file.
4074 08 Jan 08 nicklas 5
4074 08 Jan 08 nicklas 6   This file is part of BASE - BioArray Software Environment.
4074 08 Jan 08 nicklas 7   Available at http://base.thep.lu.se/
4074 08 Jan 08 nicklas 8
4074 08 Jan 08 nicklas 9   BASE is free software; you can redistribute it and/or
4074 08 Jan 08 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
4074 08 Jan 08 nicklas 12   of the License, or (at your option) any later version.
4074 08 Jan 08 nicklas 13
4074 08 Jan 08 nicklas 14   BASE is distributed in the hope that it will be useful,
4074 08 Jan 08 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4074 08 Jan 08 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4074 08 Jan 08 nicklas 17   GNU General Public License for more details.
4074 08 Jan 08 nicklas 18
4074 08 Jan 08 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4074 08 Jan 08 nicklas 21 */
4074 08 Jan 08 nicklas 22 package net.sf.basedb.util;
4074 08 Jan 08 nicklas 23
4074 08 Jan 08 nicklas 24 import java.util.ArrayList;
4074 08 Jan 08 nicklas 25 import java.util.HashMap;
4074 08 Jan 08 nicklas 26 import java.util.List;
4074 08 Jan 08 nicklas 27 import java.util.Map;
4074 08 Jan 08 nicklas 28 import java.util.Set;
4074 08 Jan 08 nicklas 29 import java.util.regex.Matcher;
4074 08 Jan 08 nicklas 30 import java.util.regex.Pattern;
4074 08 Jan 08 nicklas 31
4074 08 Jan 08 nicklas 32 /**
4074 08 Jan 08 nicklas 33   Utiltity class for parsing string values in the form of 
4074 08 Jan 08 nicklas 34   HTTP request query parameters:
4074 08 Jan 08 nicklas 35   <p>
6898 12 May 15 nicklas 36   <code>key1=value1&amp;key2=value2...</code>
4074 08 Jan 08 nicklas 37   
4074 08 Jan 08 nicklas 38   @author nicklas
4074 08 Jan 08 nicklas 39   @version 2.6
4074 08 Jan 08 nicklas 40   @base.modified $Date$
4074 08 Jan 08 nicklas 41 */
4074 08 Jan 08 nicklas 42 public class QueryParameters
4074 08 Jan 08 nicklas 43 {
4074 08 Jan 08 nicklas 44   /**
6898 12 May 15 nicklas 45     Parses a string in HTTP query format (<code>key1=value1&amp;key2=value2...</code>)
4074 08 Jan 08 nicklas 46     and returns it as a QueryParameters object.
4074 08 Jan 08 nicklas 47     
4074 08 Jan 08 nicklas 48     @param query The string to parse
4074 08 Jan 08 nicklas 49     @return A {@link QueryParameters} object
4074 08 Jan 08 nicklas 50   */
4074 08 Jan 08 nicklas 51   public static QueryParameters parseQueryString(String query)
4074 08 Jan 08 nicklas 52   {
4074 08 Jan 08 nicklas 53     return new QueryParameters(query);
4074 08 Jan 08 nicklas 54   }
4074 08 Jan 08 nicklas 55
4074 08 Jan 08 nicklas 56   private Map<String, List<String>> parameters;
4074 08 Jan 08 nicklas 57     
4074 08 Jan 08 nicklas 58   private QueryParameters(String query)
4074 08 Jan 08 nicklas 59   {
4074 08 Jan 08 nicklas 60     parameters = new HashMap<String, List<String>>();
4074 08 Jan 08 nicklas 61     if (query != null)
4074 08 Jan 08 nicklas 62     {
4078 14 Jan 08 nicklas 63       Pattern p = Pattern.compile("([^=]+)=([^&]+)&?");
4074 08 Jan 08 nicklas 64       Matcher m = p.matcher(query);
4074 08 Jan 08 nicklas 65       while (m.find())
4074 08 Jan 08 nicklas 66       {
4074 08 Jan 08 nicklas 67         String key = m.group(1);
4074 08 Jan 08 nicklas 68         String value = m.group(2);
4074 08 Jan 08 nicklas 69         List<String> values = parameters.get(key);
4074 08 Jan 08 nicklas 70         if (values == null)
4074 08 Jan 08 nicklas 71         {
4074 08 Jan 08 nicklas 72           values = new ArrayList<String>();
4074 08 Jan 08 nicklas 73           parameters.put(key, values);
4074 08 Jan 08 nicklas 74         }
4074 08 Jan 08 nicklas 75         values.add(value);
4074 08 Jan 08 nicklas 76       }
4074 08 Jan 08 nicklas 77     }  
4074 08 Jan 08 nicklas 78   }
4074 08 Jan 08 nicklas 79   
4074 08 Jan 08 nicklas 80   /**
4074 08 Jan 08 nicklas 81     Get the (single) value for a key. If the query string contained 
4074 08 Jan 08 nicklas 82     more than one value for this key, the first value is returned.
4074 08 Jan 08 nicklas 83     @param key The key
4074 08 Jan 08 nicklas 84     @return The value, or null if no value was found for the key
4074 08 Jan 08 nicklas 85   */
4074 08 Jan 08 nicklas 86   public String getValue(String key)
4074 08 Jan 08 nicklas 87   {
4074 08 Jan 08 nicklas 88     List<String> values = getValues(key);
4074 08 Jan 08 nicklas 89     if (values != null && values.size() > 0)
4074 08 Jan 08 nicklas 90     {
4074 08 Jan 08 nicklas 91       return values.get(0);
4074 08 Jan 08 nicklas 92     }
4074 08 Jan 08 nicklas 93     else
4074 08 Jan 08 nicklas 94     {
4074 08 Jan 08 nicklas 95       return null;
4074 08 Jan 08 nicklas 96     }
4074 08 Jan 08 nicklas 97   }
4074 08 Jan 08 nicklas 98   
4074 08 Jan 08 nicklas 99   /**
4074 08 Jan 08 nicklas 100     Get all values for a key. 
4074 08 Jan 08 nicklas 101     @return A list of values, or null if no value was found for the key
4074 08 Jan 08 nicklas 102   */
4074 08 Jan 08 nicklas 103   public List<String> getValues(String key)
4074 08 Jan 08 nicklas 104   {
4074 08 Jan 08 nicklas 105     return parameters.get(key);
4074 08 Jan 08 nicklas 106   }
4074 08 Jan 08 nicklas 107   
4074 08 Jan 08 nicklas 108   /**
4074 08 Jan 08 nicklas 109     Get the number of values for a key.
4074 08 Jan 08 nicklas 110     @param key The key
4074 08 Jan 08 nicklas 111     @return The number of values
4074 08 Jan 08 nicklas 112   */
4074 08 Jan 08 nicklas 113   public int getNumValues(String key)
4074 08 Jan 08 nicklas 114   {
4074 08 Jan 08 nicklas 115     List<String> values = getValues(key);
4074 08 Jan 08 nicklas 116     return values == null ? 0 : values.size();
4074 08 Jan 08 nicklas 117   }
4074 08 Jan 08 nicklas 118   
4074 08 Jan 08 nicklas 119   /**
4074 08 Jan 08 nicklas 120     Get all keys that are present in the query string.
4074 08 Jan 08 nicklas 121     @return A set with the keys
4074 08 Jan 08 nicklas 122   */
4074 08 Jan 08 nicklas 123   public Set<String> getKeys()
4074 08 Jan 08 nicklas 124   {
4074 08 Jan 08 nicklas 125     return parameters.keySet();
4074 08 Jan 08 nicklas 126   }
4074 08 Jan 08 nicklas 127   
4074 08 Jan 08 nicklas 128 }