Skip to content

Commit

Permalink
Allow the user for overriding the default Viewable MediaType
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Supol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed May 14, 2020
1 parent 11d6c11 commit da3473b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
}

0 comments on commit da3473b

Please sign in to comment.