Search in sources :

Example 1 with MediaType

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();
}
Also used : MediaType(javax.ws.rs.core.MediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder)

Example 2 with MediaType

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());
}
Also used : MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 3 with MediaType

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);
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) MediaType(javax.ws.rs.core.MediaType)

Example 4 with MediaType

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());
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 5 with MediaType

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());
}
Also used : ContainerResponse(org.glassfish.jersey.server.ContainerResponse) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Aggregations

MediaType (javax.ws.rs.core.MediaType)477 Test (org.junit.Test)184 Path (javax.ws.rs.Path)44 Produces (javax.ws.rs.Produces)44 ByteArrayInputStream (java.io.ByteArrayInputStream)42 WebApplicationException (javax.ws.rs.WebApplicationException)41 IOException (java.io.IOException)40 Response (javax.ws.rs.core.Response)40 InputStream (java.io.InputStream)38 ArrayList (java.util.ArrayList)31 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)31 Type (java.lang.reflect.Type)30 Consumes (javax.ws.rs.Consumes)27 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)27 GET (javax.ws.rs.GET)25 OutputStream (java.io.OutputStream)23 Annotation (java.lang.annotation.Annotation)23 HashSet (java.util.HashSet)22 Locale (java.util.Locale)22 HashMap (java.util.HashMap)21