Use requested media type in RestconfDocumentedExceptionMapper
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / RestconfDocumentedExceptionMapper.java
index 430f36641229e4e6bf70abfba2627a6e6dcbd858..9f25da0f0c9f421d08bca6871f2a5222fe5909ab 100644 (file)
@@ -18,6 +18,7 @@ import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import javax.ws.rs.core.Context;
@@ -88,25 +89,25 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
     @Context
     private HttpHeaders headers;
 
+    private final ControllerContext controllerContext;
+
+    public RestconfDocumentedExceptionMapper(ControllerContext controllerContext) {
+        this.controllerContext = Preconditions.checkNotNull(controllerContext);
+    }
+
     @Override
     public Response toResponse(final RestconfDocumentedException exception) {
 
         LOG.debug("In toResponse: {}", exception.getMessage());
 
-        final List<MediaType> accepts = headers.getAcceptableMediaTypes();
-        if (accepts != null) {
-            accepts.remove(MediaType.WILDCARD_TYPE);
+        final List<MediaType> mediaTypeList = new ArrayList<>();
+        if (headers.getMediaType() != null) {
+            mediaTypeList.add(headers.getMediaType());
         }
 
-        LOG.debug("Accept headers: {}", accepts);
-
-        final MediaType mediaType;
-        if (accepts != null && accepts.size() > 0) {
-            mediaType = accepts.get(0); // just pick the first one
-        } else {
-            // Default to the content type if there's no Accept header
-            mediaType = MediaType.APPLICATION_JSON_TYPE;
-        }
+        mediaTypeList.addAll(headers.getAcceptableMediaTypes());
+        final MediaType mediaType = mediaTypeList.stream().filter(type -> !type.equals(MediaType.WILDCARD_TYPE))
+                .findFirst().orElse(MediaType.APPLICATION_JSON_TYPE);
 
         LOG.debug("Using MediaType: {}", mediaType);
 
@@ -121,8 +122,8 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
 
         final int status = errors.iterator().next().getErrorTag().getStatusCode();
 
-        final ControllerContext context = ControllerContext.getInstance();
-        final DataNodeContainer errorsSchemaNode = (DataNodeContainer) context.getRestconfModuleErrorsSchemaNode();
+        final DataNodeContainer errorsSchemaNode =
+                (DataNodeContainer) controllerContext.getRestconfModuleErrorsSchemaNode();
 
         if (errorsSchemaNode == null) {
             return Response.status(status).type(MediaType.TEXT_PLAIN_TYPE).entity(exception.getMessage()).build();
@@ -148,7 +149,7 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
         errContBuild.withChild(listErorsBuilder.build());
 
         final NormalizedNodeContext errContext =  new NormalizedNodeContext(new InstanceIdentifierContext<>(null,
-                (DataSchemaNode) errorsSchemaNode, null, context.getGlobalSchema()), errContBuild.build());
+                (DataSchemaNode) errorsSchemaNode, null, controllerContext.getGlobalSchema()), errContBuild.build());
 
         Object responseBody;
         if (mediaType.getSubtype().endsWith("json")) {