Committed by
Gerrit Code Review
ONOS-5107 and ONOS-5108: fixing an issue in the GET operation implemented by REST SBI protocol
Also, changing the visibility of one of its APIs from "private" to "protected" so that it can gets called from inherrited subclasses. Change-Id: I195b3f4d851f8523f33031b199edc5cc7b58535d
Showing
1 changed file
with
38 additions
and
23 deletions
... | @@ -150,68 +150,65 @@ public class RestSBControllerImpl implements RestSBController { | ... | @@ -150,68 +150,65 @@ public class RestSBControllerImpl implements RestSBController { |
150 | } | 150 | } |
151 | 151 | ||
152 | private Response getResponse(DeviceId device, String request, InputStream payload, String mediaType) { | 152 | private Response getResponse(DeviceId device, String request, InputStream payload, String mediaType) { |
153 | + String type = typeOfMediaType(mediaType); | ||
154 | + | ||
153 | WebTarget wt = getWebTarget(device, request); | 155 | WebTarget wt = getWebTarget(device, request); |
154 | 156 | ||
155 | Response response = null; | 157 | Response response = null; |
156 | if (payload != null) { | 158 | if (payload != null) { |
157 | try { | 159 | try { |
158 | - response = wt.request(mediaType) | 160 | + response = wt.request(type) |
159 | - .post(Entity.entity(IOUtils.toString(payload, StandardCharsets.UTF_8), mediaType)); | 161 | + .post(Entity.entity(IOUtils.toString(payload, StandardCharsets.UTF_8), type)); |
160 | } catch (IOException e) { | 162 | } catch (IOException e) { |
161 | log.error("Cannot do POST {} request on device {} because can't read payload", | 163 | log.error("Cannot do POST {} request on device {} because can't read payload", |
162 | request, device); | 164 | request, device); |
163 | } | 165 | } |
164 | } else { | 166 | } else { |
165 | - response = wt.request(mediaType).post(Entity.entity(null, mediaType)); | 167 | + response = wt.request(type).post(Entity.entity(null, type)); |
166 | } | 168 | } |
167 | return response; | 169 | return response; |
168 | } | 170 | } |
169 | 171 | ||
170 | @Override | 172 | @Override |
171 | public boolean put(DeviceId device, String request, InputStream payload, String mediaType) { | 173 | public boolean put(DeviceId device, String request, InputStream payload, String mediaType) { |
174 | + String type = typeOfMediaType(mediaType); | ||
172 | 175 | ||
173 | WebTarget wt = getWebTarget(device, request); | 176 | WebTarget wt = getWebTarget(device, request); |
177 | + | ||
174 | Response response = null; | 178 | Response response = null; |
175 | if (payload != null) { | 179 | if (payload != null) { |
176 | try { | 180 | try { |
177 | - response = wt.request(mediaType) | 181 | + response = wt.request(type) |
178 | - .put(Entity.entity(IOUtils.toString(payload, StandardCharsets.UTF_8), mediaType)); | 182 | + .put(Entity.entity(IOUtils.toString(payload, StandardCharsets.UTF_8), type)); |
179 | } catch (IOException e) { | 183 | } catch (IOException e) { |
180 | log.error("Cannot do PUT {} request on device {} because can't read payload", | 184 | log.error("Cannot do PUT {} request on device {} because can't read payload", |
181 | request, device); | 185 | request, device); |
182 | } | 186 | } |
183 | } else { | 187 | } else { |
184 | - response = wt.request(mediaType).put(Entity.entity(null, mediaType)); | 188 | + response = wt.request(type).put(Entity.entity(null, type)); |
185 | } | 189 | } |
186 | return checkReply(response); | 190 | return checkReply(response); |
187 | } | 191 | } |
188 | 192 | ||
189 | @Override | 193 | @Override |
190 | public InputStream get(DeviceId device, String request, String mediaType) { | 194 | public InputStream get(DeviceId device, String request, String mediaType) { |
191 | - WebTarget wt = getWebTarget(device, request); | 195 | + String type = typeOfMediaType(mediaType); |
192 | - String type; | ||
193 | - switch (mediaType) { | ||
194 | - case XML: | ||
195 | - type = MediaType.APPLICATION_XML; | ||
196 | - break; | ||
197 | - case JSON: | ||
198 | - type = MediaType.APPLICATION_JSON; | ||
199 | - break; | ||
200 | - default: | ||
201 | - throw new IllegalArgumentException("Unsupported media type " + mediaType); | ||
202 | 196 | ||
203 | - } | 197 | + WebTarget wt = getWebTarget(device, request); |
204 | 198 | ||
205 | Response s = wt.request(type).get(); | 199 | Response s = wt.request(type).get(); |
200 | + | ||
206 | if (checkReply(s)) { | 201 | if (checkReply(s)) { |
207 | - return new ByteArrayInputStream(wt.request(type) | 202 | + return new ByteArrayInputStream(s.readEntity((String.class)) |
208 | - .get(String.class).getBytes(StandardCharsets.UTF_8)); | 203 | + .getBytes(StandardCharsets.UTF_8)); |
209 | } | 204 | } |
210 | return null; | 205 | return null; |
211 | } | 206 | } |
212 | 207 | ||
213 | @Override | 208 | @Override |
214 | public boolean patch(DeviceId device, String request, InputStream payload, String mediaType) { | 209 | public boolean patch(DeviceId device, String request, InputStream payload, String mediaType) { |
210 | + String type = typeOfMediaType(mediaType); | ||
211 | + | ||
215 | try { | 212 | try { |
216 | log.debug("Url request {} ", getUrlString(device, request)); | 213 | log.debug("Url request {} ", getUrlString(device, request)); |
217 | HttpPatch httprequest = new HttpPatch(getUrlString(device, request)); | 214 | HttpPatch httprequest = new HttpPatch(getUrlString(device, request)); |
... | @@ -223,7 +220,7 @@ public class RestSBControllerImpl implements RestSBController { | ... | @@ -223,7 +220,7 @@ public class RestSBControllerImpl implements RestSBController { |
223 | } | 220 | } |
224 | if (payload != null) { | 221 | if (payload != null) { |
225 | StringEntity input = new StringEntity(IOUtils.toString(payload, StandardCharsets.UTF_8)); | 222 | StringEntity input = new StringEntity(IOUtils.toString(payload, StandardCharsets.UTF_8)); |
226 | - input.setContentType(mediaType); | 223 | + input.setContentType(type); |
227 | httprequest.setEntity(input); | 224 | httprequest.setEntity(input); |
228 | } | 225 | } |
229 | CloseableHttpClient httpClient; | 226 | CloseableHttpClient httpClient; |
... | @@ -246,20 +243,38 @@ public class RestSBControllerImpl implements RestSBController { | ... | @@ -246,20 +243,38 @@ public class RestSBControllerImpl implements RestSBController { |
246 | 243 | ||
247 | @Override | 244 | @Override |
248 | public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) { | 245 | public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) { |
246 | + String type = typeOfMediaType(mediaType); | ||
247 | + | ||
249 | WebTarget wt = getWebTarget(device, request); | 248 | WebTarget wt = getWebTarget(device, request); |
250 | 249 | ||
251 | // FIXME: do we need to delete an entry by enclosing data in DELETE request? | 250 | // FIXME: do we need to delete an entry by enclosing data in DELETE request? |
252 | // wouldn't it be nice to use PUT to implement the similar concept? | 251 | // wouldn't it be nice to use PUT to implement the similar concept? |
253 | - Response response = wt.request(mediaType).delete(); | 252 | + Response response = wt.request(type).delete(); |
254 | 253 | ||
255 | return checkReply(response); | 254 | return checkReply(response); |
256 | } | 255 | } |
257 | 256 | ||
257 | + private String typeOfMediaType(String mediaType) { | ||
258 | + String type; | ||
259 | + switch (mediaType) { | ||
260 | + case XML: | ||
261 | + type = MediaType.APPLICATION_XML; | ||
262 | + break; | ||
263 | + case JSON: | ||
264 | + type = MediaType.APPLICATION_JSON; | ||
265 | + break; | ||
266 | + default: | ||
267 | + throw new IllegalArgumentException("Unsupported media type " + mediaType); | ||
268 | + | ||
269 | + } | ||
270 | + return type; | ||
271 | + } | ||
272 | + | ||
258 | private void authenticate(Client client, String username, String password) { | 273 | private void authenticate(Client client, String username, String password) { |
259 | client.register(HttpAuthenticationFeature.basic(username, password)); | 274 | client.register(HttpAuthenticationFeature.basic(username, password)); |
260 | } | 275 | } |
261 | 276 | ||
262 | - private WebTarget getWebTarget(DeviceId device, String request) { | 277 | + protected WebTarget getWebTarget(DeviceId device, String request) { |
263 | log.debug("Sending request to URL {} ", getUrlString(device, request)); | 278 | log.debug("Sending request to URL {} ", getUrlString(device, request)); |
264 | return clientMap.get(device).target(getUrlString(device, request)); | 279 | return clientMap.get(device).target(getUrlString(device, request)); |
265 | } | 280 | } | ... | ... |
-
Please register or login to post a comment