Committed by
Gerrit Code Review
Adding post method with String return type
Change-Id: I4483ad2494ccaca08f7460de212e432d46cba710
Showing
2 changed files
with
32 additions
and
3 deletions
... | @@ -78,6 +78,20 @@ public interface RestSBController { | ... | @@ -78,6 +78,20 @@ public interface RestSBController { |
78 | boolean post(DeviceId device, String request, InputStream payload, String mediaType); | 78 | boolean post(DeviceId device, String request, InputStream payload, String mediaType); |
79 | 79 | ||
80 | /** | 80 | /** |
81 | + * Does a REST POST request with specified parameters to the device. | ||
82 | + * | ||
83 | + * @param device device to make the request to | ||
84 | + * @param request url of the request | ||
85 | + * @param payload payload of the request as an InputStream | ||
86 | + * @param mediaType type of content in the payload i.e. application/json | ||
87 | + * @param responseClass the type of response object we are interested in, | ||
88 | + * such as String, InputStream. | ||
89 | + * @return Object of type requested via responseClass. | ||
90 | + */ | ||
91 | + <T> T post(DeviceId device, String request, InputStream payload, | ||
92 | + String mediaType, Class<T> responseClass); | ||
93 | + | ||
94 | + /** | ||
81 | * Does a REST PUT request with specified parameters to the device. | 95 | * Does a REST PUT request with specified parameters to the device. |
82 | * | 96 | * |
83 | * @param device device to make the request to | 97 | * @param device device to make the request to | ... | ... |
... | @@ -134,6 +134,22 @@ public class RestSBControllerImpl implements RestSBController { | ... | @@ -134,6 +134,22 @@ public class RestSBControllerImpl implements RestSBController { |
134 | 134 | ||
135 | @Override | 135 | @Override |
136 | public boolean post(DeviceId device, String request, InputStream payload, String mediaType) { | 136 | public boolean post(DeviceId device, String request, InputStream payload, String mediaType) { |
137 | + Response response = getResponse(device, request, payload, mediaType); | ||
138 | + return checkReply(response); | ||
139 | + } | ||
140 | + | ||
141 | + @Override | ||
142 | + public <T> T post(DeviceId device, String request, InputStream payload, | ||
143 | + String mediaType, Class<T> responseClass) { | ||
144 | + Response response = getResponse(device, request, payload, mediaType); | ||
145 | + if (response.hasEntity()) { | ||
146 | + return response.readEntity(responseClass); | ||
147 | + } | ||
148 | + log.error("Response from device {} for request {} contains no entity", device, request); | ||
149 | + return null; | ||
150 | + } | ||
151 | + | ||
152 | + private Response getResponse(DeviceId device, String request, InputStream payload, String mediaType) { | ||
137 | WebTarget wt = getWebTarget(device, request); | 153 | WebTarget wt = getWebTarget(device, request); |
138 | 154 | ||
139 | Response response = null; | 155 | Response response = null; |
... | @@ -148,7 +164,7 @@ public class RestSBControllerImpl implements RestSBController { | ... | @@ -148,7 +164,7 @@ public class RestSBControllerImpl implements RestSBController { |
148 | } else { | 164 | } else { |
149 | response = wt.request(mediaType).post(Entity.entity(null, mediaType)); | 165 | response = wt.request(mediaType).post(Entity.entity(null, mediaType)); |
150 | } | 166 | } |
151 | - return checkReply(response); | 167 | + return response; |
152 | } | 168 | } |
153 | 169 | ||
154 | @Override | 170 | @Override |
... | @@ -189,7 +205,7 @@ public class RestSBControllerImpl implements RestSBController { | ... | @@ -189,7 +205,7 @@ public class RestSBControllerImpl implements RestSBController { |
189 | Response s = wt.request(type).get(); | 205 | Response s = wt.request(type).get(); |
190 | if (checkReply(s)) { | 206 | if (checkReply(s)) { |
191 | return new ByteArrayInputStream(wt.request(type) | 207 | return new ByteArrayInputStream(wt.request(type) |
192 | - .get(String.class).getBytes(StandardCharsets.UTF_8)); | 208 | + .get(String.class).getBytes(StandardCharsets.UTF_8)); |
193 | } | 209 | } |
194 | return null; | 210 | return null; |
195 | } | 211 | } |
... | @@ -305,7 +321,6 @@ public class RestSBControllerImpl implements RestSBController { | ... | @@ -305,7 +321,6 @@ public class RestSBControllerImpl implements RestSBController { |
305 | public X509Certificate[] getAcceptedIssuers() { | 321 | public X509Certificate[] getAcceptedIssuers() { |
306 | return new X509Certificate[0]; | 322 | return new X509Certificate[0]; |
307 | } | 323 | } |
308 | - | ||
309 | } }, new java.security.SecureRandom()); | 324 | } }, new java.security.SecureRandom()); |
310 | } catch (NoSuchAlgorithmException | KeyManagementException e) { | 325 | } catch (NoSuchAlgorithmException | KeyManagementException e) { |
311 | e.printStackTrace(); | 326 | e.printStackTrace(); | ... | ... |
-
Please register or login to post a comment