use of javax.ws.rs.core.MediaType in project Openfire by igniterealtime.
the class RESTExceptionMapper method toResponse.
/*
* (non-Javadoc)
*
* @see javax.ws.rs.ext.ExceptionMapper#toResponse(java.lang.Throwable)
*/
public Response toResponse(ServiceException exception) {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setRessource(exception.getRessource());
errorResponse.setMessage(exception.getMessage());
errorResponse.setException(exception.getException());
LOG.error(exception.getException() + ": " + exception.getMessage() + " with ressource " + exception.getRessource(), exception.getException());
ResponseBuilder responseBuilder = Response.status(exception.getStatus()).entity(errorResponse);
List<MediaType> accepts = headers.getAcceptableMediaTypes();
if (accepts != null && accepts.size() > 0) {
MediaType mediaType = accepts.get(0);
responseBuilder = responseBuilder.type(mediaType);
} else {
responseBuilder = responseBuilder.type(headers.getMediaType());
}
return responseBuilder.build();
}
use of javax.ws.rs.core.MediaType in project OpenAttestation by OpenAttestation.
the class ApiExceptionTest method testGetHttpReasonPhrase.
@Test
public void testGetHttpReasonPhrase() {
MediaType contentType = new MediaType();
byte[] content = new byte[] { 0x30, 0x31 };
ApiResponse response = new ApiResponse(404, "Not Found", contentType, content);
ApiException apiexception = new ApiException(response, "error!");
assertEquals("Not Found", apiexception.getHttpReasonPhrase());
}
use of javax.ws.rs.core.MediaType in project OpenAttestation by OpenAttestation.
the class ApacheHttpClient method readResponse.
private ApiResponse readResponse(HttpResponse response) throws IOException {
MediaType contentType = createMediaType(response);
byte[] content = null;
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream contentStream = entity.getContent();
if (contentStream != null) {
content = IOUtils.toByteArray(contentStream);
contentStream.close();
}
}
return new ApiResponse(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), contentType, content);
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class HeadSubResourceMethodTest method testGetWithProduceWithHead.
@Test
public void testGetWithProduceWithHead() throws Exception {
initiateWebApplication(ResourceGetWithProduceWithHead.class);
MediaType foo = MediaType.valueOf("application/foo");
ContainerResponse response = app.apply(RequestContextBuilder.from("/sub", "HEAD").accept(foo).build()).get();
assertEquals(200, response.getStatus());
assertFalse(response.hasEntity());
assertEquals(foo, response.getMediaType());
assertEquals("FOO-HEAD", response.getHeaders().getFirst("X-TEST").toString());
MediaType bar = MediaType.valueOf("application/bar");
response = app.apply(RequestContextBuilder.from("/sub", "HEAD").accept(bar).build()).get();
assertEquals(200, response.getStatus());
assertFalse(response.hasEntity());
assertEquals(bar, response.getMediaType());
assertEquals("BAR-HEAD", response.getHeaders().getFirst("X-TEST").toString());
}
use of javax.ws.rs.core.MediaType in project jersey by jersey.
the class HeadSubResourceMethodTest method testGetWithProduceNoHeadDifferentSub.
@Test
public void testGetWithProduceNoHeadDifferentSub() throws Exception {
initiateWebApplication(ResourceGetWithProduceNoHeadDifferentSub.class);
MediaType foo = MediaType.valueOf("application/foo");
ContainerResponse response = app.apply(RequestContextBuilder.from("/sub1", "HEAD").accept(foo).build()).get();
assertEquals(200, response.getStatus());
assertFalse(response.hasEntity());
assertEquals(foo, response.getMediaType());
MediaType bar = MediaType.valueOf("application/bar");
response = app.apply(RequestContextBuilder.from("/sub2", "HEAD").accept(bar).build()).get();
assertEquals(200, response.getStatus());
assertFalse(response.hasEntity());
assertEquals(bar, response.getMediaType());
}
Aggregations