Fix RestconfDocumentedExceptionMapper exception 25/72525/1
authorTom Pantelis <tompantelis@gmail.com>
Thu, 31 May 2018 13:34:52 +0000 (09:34 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Thu, 31 May 2018 13:34:52 +0000 (09:34 -0400)
Seen in CSIT run after jersey 2 upgrade:

2018-05-31T08:47:19,494 | ERROR | qtp315470863-147 | ServerRuntime$Responder          | 190 - org.glassfish.jersey.core.jersey-server - 2.25.1 | An exception has been thrown from an exception mapper class org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper.
java.lang.UnsupportedOperationException: null
at java.util.Collections$UnmodifiableList$1.remove(Collections.java:1349) [?:?]
at jersey.repackaged.com.google.common.collect.TransformedIterator.remove(TransformedIterator.java:53) [185:org.glassfish.jersey.bundles.repackaged.jersey-guava:2.25.1]
at java.util.AbstractCollection.remove(AbstractCollection.java:293) [?:?]
at org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper.toResponse(RestconfDocumentedExceptionMapper.java:104) [346:org.opendaylight.netconf.restconf-nb-bierman02:1.8.0.SNAPSHOT]
at org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper.toResponse(RestconfDocumentedExceptionMapper.java:76) [346:org.opendaylight.netconf.restconf-nb-bierman02:1.8.0.SNAPSHOT]

headers.getAcceptableMediaTypes() now returns an immutable list so rework
the code to use stream -> filter - findFirst.

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

index c271bf33d2d28c856df14065f5574525ae40042d..b4b2890954284b42da9d46bbeda7192f9cbaeabb 100644 (file)
@@ -99,20 +99,8 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
 
         LOG.debug("In toResponse: {}", exception.getMessage());
 
-        final List<MediaType> accepts = headers.getAcceptableMediaTypes();
-        if (accepts != null) {
-            accepts.remove(MediaType.WILDCARD_TYPE);
-        }
-
-        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;
-        }
+        final MediaType mediaType = headers.getAcceptableMediaTypes().stream()
+                .filter(type -> type != MediaType.WILDCARD_TYPE).findFirst().orElse(MediaType.APPLICATION_JSON_TYPE);
 
         LOG.debug("Using MediaType: {}", mediaType);