View Javadoc

1   /*
2    * $Id: UriParamFilter.java 20320 2010-11-24 15:03:31Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.util;
11  
12  import java.util.Map;
13  import java.util.TreeMap;
14  
15  /**
16   * filters key value pairs out of a query string
17   */
18  //TODO: this should really be done with RegEx but I was having a lot of difficulty. See the history for this file for a solution
19  // that almost works, maybe someone else can figure out the last piece
20  public class UriParamFilter
21  {
22      public String filterParamsByValue(String uri, String paramValue)
23      {
24          int i = uri.indexOf("?");
25          if (i == -1)
26          {
27              return uri;
28          }
29          String query = uri.substring(i + 1);
30          String base = uri.substring(0, i + 1);
31          StringBuffer newQuery = new StringBuffer();
32  
33          TreeMap<String, String> p = getPropertiesFromQueryString(query);
34          for (Map.Entry<String, String> entry : p.entrySet())
35          {
36              if (!paramValue.equals(entry.getValue()))
37              {
38                  newQuery.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
39              }
40          }
41          String result = base + newQuery.toString();
42          if (result.endsWith("?") || result.endsWith("&"))
43          {
44              result = result.substring(0, result.length() - 1);
45          }
46          return result;
47      }
48  
49      private TreeMap<String, String> getPropertiesFromQueryString(String query)
50      {
51          TreeMap<String, String> props = new TreeMap<String, String>();
52  
53          if (query == null)
54          {
55              return props;
56          }
57  
58          query = new StringBuffer(query.length() + 1).append('&').append(query).toString();
59  
60          int x = 0;
61          while ((x = addProperty(query, x, '&', props)) != -1)
62          {
63              // run
64          }
65  
66          return props;
67      }
68  
69      private int addProperty(String query, int start, char separator, TreeMap<String, String> properties)
70      {
71          int i = query.indexOf(separator, start);
72          int i2 = query.indexOf(separator, i + 1);
73          String pair;
74          if (i > -1 && i2 > -1)
75          {
76              pair = query.substring(i + 1, i2);
77          }
78          else if (i > -1)
79          {
80              pair = query.substring(i + 1);
81          }
82          else
83          {
84              return -1;
85          }
86          int eq = pair.indexOf('=');
87  
88          if (eq <= 0)
89          {
90              String value = "";
91              properties.put(pair, value);
92          }
93          else
94          {
95              String key = pair.substring(0, eq);
96              String value = (eq == pair.length() ? "" : pair.substring(eq + 1));
97              properties.put(key, value);
98          }
99          return i2;
100     }
101 
102 }