Use requested media type in RestconfDocumentedExceptionMapper 86/72586/1
authorTom Pantelis <tompantelis@gmail.com>
Fri, 1 Jun 2018 21:16:46 +0000 (17:16 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Fri, 1 Jun 2018 21:16:46 +0000 (17:16 -0400)
The code uses headers.getAcceptableMediaTypes() to select the media type
for the response but, from what I've seen, that only contains the wildcard "\",
in which case we always return json. We should use the requested media type
via headers.getMediaType(), if set, and fallback to getAcceptableMediaTypes()
or json as default.

Change-Id: I391cf054ddab18ddc2855d8d4337cac376d71825
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfDocumentedExceptionMapper.java

index aa6b7c0b697b80a8e4d7dd9371e29fa91aa05729..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;
@@ -99,9 +100,14 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
 
         LOG.debug("In toResponse: {}", exception.getMessage());
 
-        final MediaType mediaType = headers.getAcceptableMediaTypes().stream()
-                .filter(type -> !type.equals(MediaType.WILDCARD_TYPE)).findFirst()
-                        .orElse(MediaType.APPLICATION_JSON_TYPE);
+        final List<MediaType> mediaTypeList = new ArrayList<>();
+        if (headers.getMediaType() != null) {
+            mediaTypeList.add(headers.getMediaType());
+        }
+
+        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);