Ray Milkey
Committed by Gerrit Code Review

Refactor to use JAXRS URI builder and fix HTTP status for bad JSON

Change-Id: I696bdd4792b002391abe709c7a0e3e600ad50157
...@@ -17,8 +17,6 @@ package org.onosproject.rest.resources; ...@@ -17,8 +17,6 @@ package org.onosproject.rest.resources;
17 17
18 import java.io.IOException; 18 import java.io.IOException;
19 import java.io.InputStream; 19 import java.io.InputStream;
20 -import java.net.URI;
21 -import java.net.URISyntaxException;
22 import java.util.Objects; 20 import java.util.Objects;
23 import java.util.concurrent.CountDownLatch; 21 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit; 22 import java.util.concurrent.TimeUnit;
...@@ -30,8 +28,11 @@ import javax.ws.rs.POST; ...@@ -30,8 +28,11 @@ import javax.ws.rs.POST;
30 import javax.ws.rs.Path; 28 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam; 29 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces; 30 import javax.ws.rs.Produces;
31 +import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.MediaType; 32 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response; 33 import javax.ws.rs.core.Response;
34 +import javax.ws.rs.core.UriBuilder;
35 +import javax.ws.rs.core.UriInfo;
35 36
36 import org.onosproject.core.ApplicationId; 37 import org.onosproject.core.ApplicationId;
37 import org.onosproject.core.CoreService; 38 import org.onosproject.core.CoreService;
...@@ -59,6 +60,9 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -59,6 +60,9 @@ import static org.slf4j.LoggerFactory.getLogger;
59 60
60 @Path("intents") 61 @Path("intents")
61 public class IntentsWebResource extends AbstractWebResource { 62 public class IntentsWebResource extends AbstractWebResource {
63 + @Context
64 + UriInfo uriInfo;
65 +
62 private static final Logger log = getLogger(IntentsWebResource.class); 66 private static final Logger log = getLogger(IntentsWebResource.class);
63 private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5; 67 private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5;
64 68
...@@ -194,20 +198,21 @@ public class IntentsWebResource extends AbstractWebResource { ...@@ -194,20 +198,21 @@ public class IntentsWebResource extends AbstractWebResource {
194 @Consumes(MediaType.APPLICATION_JSON) 198 @Consumes(MediaType.APPLICATION_JSON)
195 @Produces(MediaType.APPLICATION_JSON) 199 @Produces(MediaType.APPLICATION_JSON)
196 public Response createIntent(InputStream stream) { 200 public Response createIntent(InputStream stream) {
197 - URI location;
198 try { 201 try {
199 IntentService service = get(IntentService.class); 202 IntentService service = get(IntentService.class);
200 ObjectNode root = (ObjectNode) mapper().readTree(stream); 203 ObjectNode root = (ObjectNode) mapper().readTree(stream);
201 Intent intent = codec(Intent.class).decode(root, this); 204 Intent intent = codec(Intent.class).decode(root, this);
202 service.submit(intent); 205 service.submit(intent);
203 - location = new URI(Short.toString(intent.appId().id()) + "/" 206 + UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
204 - + Long.toString(intent.id().fingerprint())); 207 + .path("intents")
205 - } catch (IOException | URISyntaxException ex) { 208 + .path(Short.toString(intent.appId().id()))
206 - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); 209 + .path(Long.toString(intent.id().fingerprint()));
207 - }
208 return Response 210 return Response
209 - .created(location) 211 + .created(locationBuilder.build())
210 .build(); 212 .build();
213 + } catch (IOException ioe) {
214 + throw new IllegalArgumentException(ioe);
215 + }
211 } 216 }
212 217
213 } 218 }
......