1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.utgenome.gwt.utgb.client.util;
26
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Set;
31
32
33
34
35
36 public class GETMethodURL {
37
38 protected String baseURL;
39
40 private final Map<String, String> keyAndValueMap = new HashMap<String, String>();
41
42 public static final GETMethodURL newInstance(final String url) {
43 final int questionIndex = url.indexOf('?');
44
45 if (questionIndex == -1) {
46 return new GETMethodURL(url);
47 }
48 else {
49 final String baseURL = url.substring(0, questionIndex);
50
51 final GETMethodURL _url = new GETMethodURL(baseURL);
52
53 final String parameterStr = url.substring(questionIndex + 1);
54
55 final String[] parameters = parameterStr.split("&");
56
57 for (int i = 0; i < parameters.length; i++) {
58 if (parameters[i].length() == 0)
59 continue;
60
61 final String[] elements = parameters[i].split("=");
62
63 if (elements.length == 2) {
64 _url.put(elements[0], elements[1]);
65 }
66 else if (elements.length == 1) {
67 if (parameters[i].endsWith("=")) {
68 _url.put(elements[0], "");
69 }
70 }
71 }
72
73 return _url;
74 }
75 }
76
77 protected GETMethodURL(final String baseURL) {
78 this.baseURL = baseURL;
79 }
80
81 public String getBaseURL() {
82 return baseURL;
83 }
84
85 public String getValue(final String key) {
86 return (keyAndValueMap.get(key));
87 }
88
89 public String put(final String key, final String value) {
90 if (keyAndValueMap.containsKey(key)) {
91 final String oldValue = (keyAndValueMap.get(key));
92
93 keyAndValueMap.put(key, value);
94 return oldValue;
95 }
96 else {
97 keyAndValueMap.put(key, value);
98 return null;
99 }
100 }
101
102 public String getURL() {
103 return getURL(new HashMap<String, String>());
104 }
105
106 public String getURL(final Map<String, String> parameterMap) {
107 final StringBuffer buf = new StringBuffer(getBaseURL());
108
109 char delimiter = '?';
110
111 {
112 final Set<Map.Entry<String, String>> defaultParameterSet = keyAndValueMap.entrySet();
113 final Iterator<Map.Entry<String, String>> defaultIt = defaultParameterSet.iterator();
114
115 while (defaultIt.hasNext()) {
116 final Map.Entry<String, String> defaultEntry = defaultIt.next();
117
118 final String defaultKey = (String) (defaultEntry.getKey());
119 final String defaultValue = (String) (defaultEntry.getValue());
120
121 String value = defaultValue;
122 if (parameterMap.containsKey(defaultKey)) {
123 value = (String) (parameterMap.get(defaultKey));
124 }
125
126 buf.append(delimiter);
127 delimiter = '&';
128 buf.append(defaultKey + "=" + value);
129 }
130 }
131 {
132 final Set<Map.Entry<String, String>> inputParameterSet = parameterMap.entrySet();
133 final Iterator<Map.Entry<String, String>> inputIt = inputParameterSet.iterator();
134
135 while (inputIt.hasNext()) {
136 final Map.Entry<String, String> inputEntry = inputIt.next();
137
138 final String inputKey = (String) (inputEntry.getKey());
139 final String inputValue = (String) (inputEntry.getValue());
140
141 if (!keyAndValueMap.containsKey(inputKey)) {
142
143 buf.append(delimiter);
144 delimiter = '&';
145 buf.append(inputKey + "=" + inputValue);
146 }
147 }
148 }
149
150 return buf.toString();
151 }
152 }