Skip to content

Commit

Permalink
Don't have Platform ExceptionMappers pick up media types from resourc…
Browse files Browse the repository at this point in the history
…e method annotations.
  • Loading branch information
johngmyers committed Feb 14, 2017
1 parent 2ccee3e commit f80464d
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Platform 1.52

* [Bug] Fixed the Platform-provided ExceptionMapper classes to not pick up
media types from the resource method annotations.

Platform 1.51

* Trace tokens
Expand All @@ -17,6 +22,7 @@ Platform 1.51
* [Bug] The HttpServer.BusyThreads.Max metric leaked counts for calls
that threw uncaught exceptions.


* Library Upgrades

- Guava to 21.0 (from 20.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.proofpoint.jaxrs;

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 @@ -29,6 +30,7 @@ public class ParsingExceptionMapper implements ExceptionMapper<ParsingException>
public Response toResponse(ParsingException e)
{
return Response.status(Response.Status.BAD_REQUEST)
.type(MediaType.TEXT_PLAIN_TYPE)
.entity(e.getMessage())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.glassfish.jersey.server.ParamException.QueryParamException;

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 @@ -34,6 +35,7 @@ public class QueryParamExceptionMapper implements ExceptionMapper<QueryParamExce
public Response toResponse(QueryParamException e)
{
return Response.status(Response.Status.BAD_REQUEST)
.type(MediaType.TEXT_PLAIN_TYPE)
.entity(e.getMessage())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright 2015 Proofpoint, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.proofpoint.jaxrs;

import com.google.inject.Injector;
import com.proofpoint.bootstrap.LifeCycleManager;
import com.proofpoint.http.client.HttpClient;
import com.proofpoint.http.client.Request;
import com.proofpoint.http.client.StatusResponseHandler.StatusResponse;
import com.proofpoint.http.client.StringResponseHandler.StringResponse;
import com.proofpoint.http.client.jetty.JettyHttpClient;
import com.proofpoint.http.server.testing.TestingHttpServer;
import com.proofpoint.http.server.testing.TestingHttpServerModule;
import com.proofpoint.json.JsonModule;
import com.proofpoint.node.testing.TestingNodeModule;
import com.proofpoint.reporting.ReportingModule;
import com.proofpoint.testing.Closeables;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.management.MBeanServer;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import static com.google.common.base.Charsets.UTF_8;
import static com.proofpoint.bootstrap.Bootstrap.bootstrapTest;
import static com.proofpoint.http.client.StaticBodyGenerator.createStaticBodyGenerator;
import static com.proofpoint.http.client.StatusResponseHandler.createStatusResponseHandler;
import static com.proofpoint.http.client.StringResponseHandler.createStringResponseHandler;
import static com.proofpoint.jaxrs.JaxrsBinder.jaxrsBinder;
import static com.proofpoint.jaxrs.JaxrsModule.explicitJaxrsModule;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;

@Test(singleThreaded = true)
public class TestParsingExceptionMapper
{
private static final String GET = "GET";

private final HttpClient client = new JettyHttpClient();

private LifeCycleManager lifeCycleManager;
private TestingHttpServer server;

@BeforeMethod
public void setup()
throws Exception
{
createServer(new TestParsingResource());
}

@AfterMethod
public void teardown()
throws Exception
{
if (lifeCycleManager != null) {
lifeCycleManager.stop();
}
}

@AfterClass(alwaysRun = true)
public void teardownClass()
{
Closeables.closeQuietly(client);
}

@Test
public void testGetWithValidBodySucceeds()
throws Exception
{
StatusResponse response = client.execute(buildRequestWithBody("123"), createStatusResponseHandler());
assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
}

@Test
public void testGetWithInvalidBodyBadRequest()
throws Exception
{
StringResponse response = client.execute(buildRequestWithBody("string"), createStringResponseHandler());
assertEquals(response.getStatusCode(), Status.BAD_REQUEST.getStatusCode());
assertEquals(response.getHeader("Content-Type"), "text/plain");
}

private Request buildRequestWithBody(String override)
{
return Request.builder()
.setUri(server.getBaseUrl())
.setHeader("Content-Type", "application/json")
.setBodySource(createStaticBodyGenerator(override, UTF_8))
.setMethod(GET)
.build();
}

private void createServer(final TestParsingResource resource)
throws Exception
{
Injector injector = bootstrapTest()
.withModules(
new TestingNodeModule(),
explicitJaxrsModule(),
new JsonModule(),
new ReportingModule(),
binder -> binder.bind(MBeanServer.class).toInstance(mock(MBeanServer.class)),
new TestingHttpServerModule(),
binder -> jaxrsBinder(binder).bindInstance(resource))
.initialize();
lifeCycleManager = injector.getInstance(LifeCycleManager.class);
server = injector.getInstance(TestingHttpServer.class);
}

@Path("/")
public class TestParsingResource
{
@GET
@Produces(APPLICATION_JSON)
public Response get(Integer count)
{
return Response.ok().build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.proofpoint.http.client.HttpClient;
import com.proofpoint.http.client.Request;
import com.proofpoint.http.client.StatusResponseHandler.StatusResponse;
import com.proofpoint.http.client.StringResponseHandler.StringResponse;
import com.proofpoint.http.client.jetty.JettyHttpClient;
import com.proofpoint.http.server.testing.TestingHttpServer;
import com.proofpoint.http.server.testing.TestingHttpServerModule;
Expand All @@ -36,15 +37,18 @@
import javax.management.MBeanServer;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import static com.proofpoint.bootstrap.Bootstrap.bootstrapTest;
import static com.proofpoint.http.client.StatusResponseHandler.createStatusResponseHandler;
import static com.proofpoint.http.client.StringResponseHandler.createStringResponseHandler;
import static com.proofpoint.jaxrs.JaxrsBinder.jaxrsBinder;
import static com.proofpoint.jaxrs.JaxrsModule.explicitJaxrsModule;
import static java.lang.String.format;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;

Expand Down Expand Up @@ -84,21 +88,22 @@ public void teardownClass()
public void testGetWithValidQueryParamSucceeds()
throws Exception
{
StatusResponse response = client.execute(buildRequestWithQueryParam(GET, "123"), createStatusResponseHandler());
StatusResponse response = client.execute(buildRequestWithQueryParam("123"), createStatusResponseHandler());
assertEquals(response.getStatusCode(), Status.OK.getStatusCode());
}

@Test
public void testGetWithInvalidQueryParamReturnsBadRequest()
throws Exception
{
StatusResponse response = client.execute(buildRequestWithQueryParam(GET, "string"), createStatusResponseHandler());
StringResponse response = client.execute(buildRequestWithQueryParam("string"), createStringResponseHandler());
assertEquals(response.getStatusCode(), Status.BAD_REQUEST.getStatusCode());
assertEquals(response.getHeader("Content-Type"), "text/plain");
}

private Request buildRequestWithQueryParam(String type, String override)
private Request buildRequestWithQueryParam(String override)
{
return Request.builder().setUri(server.getBaseUrl().resolve(format("/?count=%s", override))).setMethod(type).build();
return Request.builder().setUri(server.getBaseUrl().resolve(format("/?count=%s", override))).setMethod(GET).build();
}

private void createServer(final TestQueryParamResource resource)
Expand All @@ -122,6 +127,7 @@ private void createServer(final TestQueryParamResource resource)
public class TestQueryParamResource
{
@GET
@Produces(APPLICATION_JSON)
public Response get(@QueryParam("count") Integer count)
{
return Response.ok().build();
Expand Down

0 comments on commit f80464d

Please sign in to comment.