use of javax.ws.rs.core.Response in project dropwizard by dropwizard.
the class LoggingExceptionMapperTest method handlesMethodNotAllowedWithHeaders.
@Test
public void handlesMethodNotAllowedWithHeaders() {
final Throwable thrown = catchThrowable(() -> target("/exception/json-mapping-exception").request(MediaType.APPLICATION_JSON).post(Entity.json("A"), String.class));
assertThat(thrown).isInstanceOf(WebApplicationException.class);
final Response resp = ((WebApplicationException) thrown).getResponse();
assertThat(resp.getStatus()).isEqualTo(405);
assertThat(resp.getHeaders()).contains(entry("Allow", ImmutableList.of("GET,OPTIONS")));
assertThat(resp.readEntity(String.class)).isEqualTo("{\"code\":405,\"message\":\"HTTP 405 Method Not Allowed\"}");
}
use of javax.ws.rs.core.Response in project druid by druid-io.
the class NamespaceLookupExtractorFactoryTest method validateNotFound.
private void validateNotFound(String method, LookupIntrospectHandler handler, Class<? extends LookupIntrospectHandler> clazz) throws Exception {
mockVerify();
mockReset();
expectEntryGetCacheStateOnce(versionedCache);
expectEmptyCache();
EasyMock.expect(versionedCache.getVersion()).andThrow(new ISE("some exception")).once();
mockReplay();
final Response response = (Response) clazz.getMethod(method).invoke(handler);
Assert.assertEquals(404, response.getStatus());
}
use of javax.ws.rs.core.Response in project druid by druid-io.
the class OverlordResource method taskPost.
@POST
@Path("/task")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response taskPost(final Task task, @Context final HttpServletRequest req) {
if (authConfig.isEnabled()) {
// This is an experimental feature, see - https://github.com/druid-io/druid/pull/2424
final String dataSource = task.getDataSource();
final AuthorizationInfo authorizationInfo = (AuthorizationInfo) req.getAttribute(AuthConfig.DRUID_AUTH_TOKEN);
Preconditions.checkNotNull(authorizationInfo, "Security is enabled but no authorization info found in the request");
Access authResult = authorizationInfo.isAuthorized(new Resource(dataSource, ResourceType.DATASOURCE), Action.WRITE);
if (!authResult.isAllowed()) {
return Response.status(Response.Status.FORBIDDEN).header("Access-Check-Result", authResult).build();
}
}
return asLeaderWith(taskMaster.getTaskQueue(), new Function<TaskQueue, Response>() {
@Override
public Response apply(TaskQueue taskQueue) {
try {
taskQueue.add(task);
return Response.ok(ImmutableMap.of("task", task.getId())).build();
} catch (EntryExistsException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(ImmutableMap.of("error", String.format("Task[%s] already exists!", task.getId()))).build();
}
}
});
}
use of javax.ws.rs.core.Response in project druid by druid-io.
the class SupervisorResourceTest method testShutdown.
@Test
public void testShutdown() throws Exception {
EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.of(supervisorManager)).times(2);
EasyMock.expect(supervisorManager.stopAndRemoveSupervisor("my-id")).andReturn(true);
EasyMock.expect(supervisorManager.stopAndRemoveSupervisor("my-id-2")).andReturn(false);
replayAll();
Response response = supervisorResource.shutdown("my-id");
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(ImmutableMap.of("id", "my-id"), response.getEntity());
response = supervisorResource.shutdown("my-id-2");
Assert.assertEquals(404, response.getStatus());
verifyAll();
resetAll();
EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.<SupervisorManager>absent());
replayAll();
response = supervisorResource.shutdown("my-id");
verifyAll();
Assert.assertEquals(503, response.getStatus());
}
use of javax.ws.rs.core.Response in project druid by druid-io.
the class SupervisorResourceTest method testSpecGetStatus.
@Test
public void testSpecGetStatus() throws Exception {
SupervisorReport report = new SupervisorReport("id", DateTime.now()) {
@Override
public Object getPayload() {
return null;
}
};
EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.of(supervisorManager)).times(2);
EasyMock.expect(supervisorManager.getSupervisorStatus("my-id")).andReturn(Optional.of(report));
EasyMock.expect(supervisorManager.getSupervisorStatus("my-id-2")).andReturn(Optional.<SupervisorReport>absent());
replayAll();
Response response = supervisorResource.specGetStatus("my-id");
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(report, response.getEntity());
response = supervisorResource.specGetStatus("my-id-2");
Assert.assertEquals(404, response.getStatus());
verifyAll();
resetAll();
EasyMock.expect(taskMaster.getSupervisorManager()).andReturn(Optional.<SupervisorManager>absent());
replayAll();
response = supervisorResource.specGetStatus("my-id");
verifyAll();
Assert.assertEquals(503, response.getStatus());
}
Aggregations