1
2
3
4
5
6
7 package org.mule.util;
8
9 import java.util.Map;
10 import java.util.TreeMap;
11
12
13
14
15
16
17 public class UriParamFilter
18 {
19 public String filterParamsByValue(String uri, String paramValue)
20 {
21 int i = uri.indexOf("?");
22 if (i == -1)
23 {
24 return uri;
25 }
26 String query = uri.substring(i + 1);
27 String base = uri.substring(0, i + 1);
28 StringBuffer newQuery = new StringBuffer();
29
30 TreeMap<String, String> p = getPropertiesFromQueryString(query);
31 for (Map.Entry<String, String> entry : p.entrySet())
32 {
33 if (!paramValue.equals(entry.getValue()))
34 {
35 newQuery.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
36 }
37 }
38 String result = base + newQuery.toString();
39 if (result.endsWith("?") || result.endsWith("&"))
40 {
41 result = result.substring(0, result.length() - 1);
42 }
43 return result;
44 }
45
46 private TreeMap<String, String> getPropertiesFromQueryString(String query)
47 {
48 TreeMap<String, String> props = new TreeMap<String, String>();
49
50 if (query == null)
51 {
52 return props;
53 }
54
55 query = new StringBuffer(query.length() + 1).append('&').append(query).toString();
56
57 int x = 0;
58 while ((x = addProperty(query, x, '&', props)) != -1)
59 {
60
61 }
62
63 return props;
64 }
65
66 private int addProperty(String query, int start, char separator, TreeMap<String, String> properties)
67 {
68 int i = query.indexOf(separator, start);
69 int i2 = query.indexOf(separator, i + 1);
70 String pair;
71 if (i > -1 && i2 > -1)
72 {
73 pair = query.substring(i + 1, i2);
74 }
75 else if (i > -1)
76 {
77 pair = query.substring(i + 1);
78 }
79 else
80 {
81 return -1;
82 }
83 int eq = pair.indexOf('=');
84
85 if (eq <= 0)
86 {
87 String value = "";
88 properties.put(pair, value);
89 }
90 else
91 {
92 String key = pair.substring(0, eq);
93 String value = (eq == pair.length() ? "" : pair.substring(eq + 1));
94 properties.put(key, value);
95 }
96 return i2;
97 }
98
99 }