From 52d04d3087060414b742d8164651e9bc730678b7 Mon Sep 17 00:00:00 2001 From: Jan Supol Date: Mon, 11 May 2020 16:06:17 +0200 Subject: [PATCH] Allow the user for overriding the default Viewable MediaType Signed-off-by: Jan Supol --- .../mvc/internal/ViewableMessageBodyWriter.java | 6 ++++-- .../e2e/server/mvc/ExceptionViewProcessorTest.java | 13 +++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ext/mvc/src/main/java/org/glassfish/jersey/server/mvc/internal/ViewableMessageBodyWriter.java b/ext/mvc/src/main/java/org/glassfish/jersey/server/mvc/internal/ViewableMessageBodyWriter.java index 61820ef1a3..0949838e16 100644 --- a/ext/mvc/src/main/java/org/glassfish/jersey/server/mvc/internal/ViewableMessageBodyWriter.java +++ b/ext/mvc/src/main/java/org/glassfish/jersey/server/mvc/internal/ViewableMessageBodyWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -102,7 +102,9 @@ public void writeTo(final Viewable viewable, throw new WebApplicationException(new ProcessingException(message), Response.Status.NOT_FOUND); } - httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, resolvedViewable.getMediaType()); + if (!httpHeaders.containsKey(HttpHeaders.CONTENT_TYPE)) { + httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, resolvedViewable.getMediaType()); + } resolvedViewable.writeTo(entityStream, httpHeaders); } catch (ViewableContextException vce) { throw new NotFoundException(vce); diff --git a/tests/e2e-server/src/test/java/org/glassfish/jersey/tests/e2e/server/mvc/ExceptionViewProcessorTest.java b/tests/e2e-server/src/test/java/org/glassfish/jersey/tests/e2e/server/mvc/ExceptionViewProcessorTest.java index 772c0994c0..287b614a84 100644 --- a/tests/e2e-server/src/test/java/org/glassfish/jersey/tests/e2e/server/mvc/ExceptionViewProcessorTest.java +++ b/tests/e2e-server/src/test/java/org/glassfish/jersey/tests/e2e/server/mvc/ExceptionViewProcessorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -26,6 +26,7 @@ import javax.ws.rs.WebApplicationException; import javax.ws.rs.client.Invocation; import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @@ -37,6 +38,7 @@ import org.glassfish.jersey.test.TestProperties; import org.glassfish.jersey.tests.e2e.server.mvc.provider.TestViewProcessor; +import org.junit.Assert; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -71,7 +73,9 @@ public Response toResponse(WebApplicationException exception) { // Relative. if (exception.getResponse().getStatus() == 406) { - return Response.status(406).entity( + return Response.status(406) + .type(MediaType.TEXT_PLAIN_TYPE) + .entity( new Viewable( "/org/glassfish/jersey/tests/e2e/server/mvc/ExceptionViewProcessorTest/WebAppExceptionMapper/406", "406")).build(); @@ -102,6 +106,8 @@ public void testAbsoluteExplicitTemplate() throws IOException { p.load(cr.readEntity(InputStream.class)); assertEquals("/org/glassfish/jersey/tests/e2e/server/mvc/ExceptionViewProcessorTest/404.testp", p.getProperty("path")); assertEquals("404", p.getProperty("model")); + + cr.close(); } @Test @@ -116,5 +122,8 @@ public void testResolvingClassExplicitTemplate() throws IOException { assertEquals("/org/glassfish/jersey/tests/e2e/server/mvc/ExceptionViewProcessorTest/WebAppExceptionMapper/406.testp", p.getProperty("path")); assertEquals("406", p.getProperty("model")); + + Assert.assertEquals(MediaType.TEXT_PLAIN_TYPE, cr.getMediaType()); + cr.close(); } }