Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
URLBuilder |
|
| 2.0;2 |
1 | package org.codeforamerica.open311.internals.network; | |
2 | ||
3 | import java.net.MalformedURLException; | |
4 | import java.net.URL; | |
5 | import java.util.HashMap; | |
6 | import java.util.List; | |
7 | import java.util.Map; | |
8 | ||
9 | import org.codeforamerica.open311.facade.data.Attribute; | |
10 | ||
11 | /** | |
12 | * Builds the necessary URLs to communicate with the endpoints. | |
13 | * | |
14 | * @author Santiago MunĂn <santimunin@gmail.com> | |
15 | * | |
16 | */ | |
17 | public class URLBuilder { | |
18 | ||
19 | private static final String GET_SERVICE_LIST = "services"; | |
20 | private static final String GET_SERVICE_DEFINITION = "services"; | |
21 | private static final String POST_SERVICE_REQUEST = "requests"; | |
22 | private static final String GET_SERVICE_REQUEST_FROM_A_TOKEN = "tokens"; | |
23 | private static final String GET_SERVICE_REQUESTS = "requests"; | |
24 | private String baseUrl; | |
25 | private String jurisdictionId; | |
26 | private String format; | |
27 | ||
28 | 12 | public URLBuilder(String baseUrl, String jurisdictionId, String format) { |
29 | 12 | this.baseUrl = baseUrl; |
30 | 12 | this.jurisdictionId = jurisdictionId; |
31 | 12 | this.format = format; |
32 | 12 | } |
33 | ||
34 | /** | |
35 | * Builds a GET Service List URL. | |
36 | * | |
37 | * @return An URL ready to be accessed. | |
38 | * @throws MalformedURLException | |
39 | * If one of the parts of the url (endpoint's base url, | |
40 | * format...) is not correct. | |
41 | */ | |
42 | public URL buildGetServiceListUrl() throws MalformedURLException { | |
43 | 4 | String url = baseUrl + "/" + GET_SERVICE_LIST + "." + format; |
44 | 4 | return new URL(addJurisdictionId(url, jurisdictionId)); |
45 | } | |
46 | ||
47 | /** | |
48 | * Builds a GET Service Definition URL. | |
49 | * | |
50 | * @param serviceCode | |
51 | * Code of the service to find the definition. | |
52 | * @return An URL ready to be accessed. | |
53 | * @throws MalformedURLException | |
54 | * If one of the parts of the url (endpoint's base url, | |
55 | * format...) is not correct. | |
56 | */ | |
57 | public URL buildGetServiceDefinitionUrl(String serviceCode) | |
58 | throws MalformedURLException { | |
59 | 4 | String url = baseUrl + "/" + GET_SERVICE_DEFINITION + "/" + serviceCode |
60 | + "." + format; | |
61 | 4 | return new URL(addJurisdictionId(url, jurisdictionId)); |
62 | } | |
63 | ||
64 | /** | |
65 | * Builds a POST Service Request URL. | |
66 | * | |
67 | * @return An URL ready to be accessed. | |
68 | * @throws MalformedURLException | |
69 | * If one of the parts of the url (endpoint's base url, | |
70 | * format...) is not correct. | |
71 | */ | |
72 | public URL buildPostServiceRequestUrl() throws MalformedURLException { | |
73 | 6 | String base = baseUrl + "/" + POST_SERVICE_REQUEST + "." + format; |
74 | 6 | return new URL(base); |
75 | } | |
76 | ||
77 | /** | |
78 | * Builds a GET Service Request ID From Token URL. | |
79 | * | |
80 | * @param token | |
81 | * Token associated to a service request. | |
82 | * @return An URL ready to be accessed. | |
83 | * @throws MalformedURLException | |
84 | * If one of the parts of the url (endpoint's base url, | |
85 | * format...) is not correct. | |
86 | */ | |
87 | public URL buildGetServiceRequestIdFromATokenUrl(String token) | |
88 | throws MalformedURLException { | |
89 | 2 | String url = baseUrl + "/" + GET_SERVICE_REQUEST_FROM_A_TOKEN + "/" |
90 | + token + "." + format; | |
91 | 2 | return new URL(addJurisdictionId(url, jurisdictionId)); |
92 | } | |
93 | ||
94 | /** | |
95 | * Builds a GET Service Requests URL. | |
96 | * | |
97 | * @param arguments | |
98 | * Pairs (key, value) of optional arguments | |
99 | * @return An URL ready to be accessed. | |
100 | * @throws MalformedURLException | |
101 | * If one of the parts of the url (endpoint's base url, | |
102 | * format...) is not correct. | |
103 | */ | |
104 | public URL buildGetServiceRequests(Map<String, String> arguments) | |
105 | throws MalformedURLException { | |
106 | 3 | if (jurisdictionId.length() > 0) { |
107 | 2 | arguments.put("jurisdiction_id", jurisdictionId); |
108 | } | |
109 | 3 | return buildUrl(baseUrl + "/" + GET_SERVICE_REQUESTS + "." + format, |
110 | arguments); | |
111 | } | |
112 | ||
113 | /** | |
114 | * Builds a GET Service Request URL. | |
115 | * | |
116 | * @param serviceId | |
117 | * Id of the service. | |
118 | * @return An URL ready to be accessed. | |
119 | * @throws MalformedURLException | |
120 | * If one of the parts of the url (endpoint's base url, | |
121 | * format...) is not correct. | |
122 | */ | |
123 | public URL buildGetServiceRequest(String serviceId) | |
124 | throws MalformedURLException { | |
125 | 3 | String url = baseUrl + "/" + GET_SERVICE_REQUESTS + "/" + serviceId |
126 | + "." + format; | |
127 | 3 | url = addJurisdictionId(url, jurisdictionId); |
128 | 3 | return buildUrl(url, null); |
129 | } | |
130 | ||
131 | /** | |
132 | * Builds the list of arguments of a POST service request. | |
133 | * | |
134 | * @param arguments | |
135 | * List of (key, value) pairs. | |
136 | * @param attributes | |
137 | * List of attributes. | |
138 | * @return A list of (key, value) pairs with all the given data (arguments | |
139 | * and attributes). | |
140 | * @throws MalformedURLException | |
141 | * If any of the arguments given is not allowed. | |
142 | */ | |
143 | public Map<String, String> buildPostServiceRequestBody( | |
144 | Map<String, String> arguments, List<Attribute> attributes) | |
145 | throws MalformedURLException { | |
146 | 6 | Map<String, String> attributesMap = buildAttributes(attributes); |
147 | 6 | arguments = arguments == null ? new HashMap<String, String>() |
148 | : arguments; | |
149 | 6 | arguments.putAll(attributesMap); |
150 | 6 | return arguments; |
151 | } | |
152 | ||
153 | /** | |
154 | * Builds a map of (key, value) pairs from the attributes. | |
155 | * | |
156 | * @param attributes | |
157 | * Request attributes. | |
158 | * @return Map of (key, value) pairs. | |
159 | */ | |
160 | private Map<String, String> buildAttributes(List<Attribute> attributes) { | |
161 | 6 | Map<String, String> result = new HashMap<String, String>(); |
162 | 6 | if (attributes != null) { |
163 | 6 | for (Attribute attribute : attributes) { |
164 | 3 | result.putAll(attribute.generatePOSTRequestParameter()); |
165 | } | |
166 | } | |
167 | 6 | return result; |
168 | } | |
169 | ||
170 | /** | |
171 | * Builds a &-separated parameter string. | |
172 | * | |
173 | * @param parameters | |
174 | * List of pairs (key, value). | |
175 | * @return A string of the form (key=value&key2=value2&...). | |
176 | */ | |
177 | private String buildParameterString(Map<String, String> parameters) { | |
178 | 3 | boolean first = true; |
179 | 3 | if (parameters != null) { |
180 | 3 | StringBuilder builder = new StringBuilder(); |
181 | 3 | for (String key : parameters.keySet()) { |
182 | 6 | if (first) { |
183 | 2 | first = false; |
184 | } else { | |
185 | 4 | builder.append("&"); |
186 | } | |
187 | 6 | builder.append(key + "=" + parameters.get(key)); |
188 | } | |
189 | 3 | return builder.toString(); |
190 | } | |
191 | 0 | return ""; |
192 | } | |
193 | ||
194 | /** | |
195 | * Builds an URL from a base plus a list of arguments. | |
196 | * | |
197 | * @param base | |
198 | * Host address. | |
199 | * @param arguments | |
200 | * List of pairs (key, value). | |
201 | * @return a URL built from the given data. | |
202 | * @throws MalformedURLException | |
203 | * If there was any problem building the URL. | |
204 | */ | |
205 | private URL buildUrl(String base, Map<String, String> arguments) | |
206 | throws MalformedURLException { | |
207 | 6 | if (arguments == null) { |
208 | 3 | return new URL(base); |
209 | } else { | |
210 | 3 | return new URL(base + "?" + buildParameterString(arguments)); |
211 | } | |
212 | } | |
213 | ||
214 | /** | |
215 | * Adds a jurisdictionId to an url. | |
216 | * | |
217 | * @param url | |
218 | * Base url. | |
219 | * @param jurisdictionId | |
220 | * Given jurisdiction id. | |
221 | * @return The new url. | |
222 | */ | |
223 | private String addJurisdictionId(String url, String jurisdictionId) { | |
224 | 13 | if (jurisdictionId.length() > 0) { |
225 | 7 | return url += "?jurisdiction_id=" + jurisdictionId; |
226 | } | |
227 | 6 | return url; |
228 | } | |
229 | } |