Search in sources :

Example 6 with Request

use of javax.ws.rs.core.Request in project cxf by apache.

the class EvaluatePreconditionsTest method testIfNoneMatch304.

@Test
public void testIfNoneMatch304() {
    final Request request = getRequest(HttpHeaders.IF_NONE_MATCH, ETAG_OLD.toString());
    final Response response = service.perform(request);
    Assert.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Request(javax.ws.rs.core.Request) Test(org.junit.Test)

Example 7 with Request

use of javax.ws.rs.core.Request in project cxf by apache.

the class EvaluatePreconditionsTest method testIfModified304.

@Test
public void testIfModified304() {
    final Request request = getRequest(HttpHeaders.IF_MODIFIED_SINCE, dateFormat.format(DATE_NEW));
    final Response response = service.perform(request);
    Assert.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Request(javax.ws.rs.core.Request) Test(org.junit.Test)

Example 8 with Request

use of javax.ws.rs.core.Request in project che by eclipse.

the class JAXRSDownloadFileResponseFilter method filter.

/**
     * JAX-RS Filter method called after a response has been provided for a request
     * <p>
     * Filters in the filter chain are ordered according to their {@code javax.annotation.Priority}
     * class-level annotation value.
     * </p>
     *
     * @param requestContext
     *         request context.
     * @param responseContext
     *         response context.
     * @throws IOException
     *         if an I/O exception occurs.
     */
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    // Apply header if all if correct
    Request request = requestContext.getRequest();
    String filename = getFileName(request, responseContext.getMediaType(), requestContext.getUriInfo(), responseContext.getStatus());
    if (filename != null) {
        if (hasCompliantEntity(responseContext.getEntity())) {
            responseContext.getHeaders().putSingle(CONTENT_DISPOSITION, "attachment; filename=" + filename);
        }
    }
}
Also used : Request(javax.ws.rs.core.Request)

Example 9 with Request

use of javax.ws.rs.core.Request in project che by eclipse.

the class ETagResponseFilter method doFilter.

/**
     * Filter the given container response
     *
     * @param containerResponse
     *         the response to use
     */
public void doFilter(GenericContainerResponse containerResponse) {
    // get entity of the response
    Object entity = containerResponse.getEntity();
    // no entity, skip
    if (entity == null) {
        return;
    }
    // Only handle JSON content
    if (!MediaType.APPLICATION_JSON_TYPE.equals(containerResponse.getContentType())) {
        return;
    }
    // Get the request
    ApplicationContext applicationContext = ApplicationContext.getCurrent();
    Request request = applicationContext.getRequest();
    // manage only GET requests
    if (!HttpMethod.GET.equals(request.getMethod())) {
        return;
    }
    // calculate hash with MD5
    HashFunction hashFunction = Hashing.md5();
    Hasher hasher = hashFunction.newHasher();
    boolean hashingSuccess = true;
    // Manage a list
    if (entity instanceof List) {
        List<?> entities = (List) entity;
        for (Object simpleEntity : entities) {
            hashingSuccess = addHash(simpleEntity, hasher);
            if (!hashingSuccess) {
                break;
            }
        }
    } else {
        hashingSuccess = addHash(entity, hasher);
    }
    // if we're able to handle the hash
    if (hashingSuccess) {
        // get result of the hash
        HashCode hashCode = hasher.hash();
        // Create the entity tag
        EntityTag entityTag = new EntityTag(hashCode.toString());
        // Check the etag
        Response.ResponseBuilder builder = request.evaluatePreconditions(entityTag);
        // not modified ?
        if (builder != null) {
            containerResponse.setResponse(builder.tag(entityTag).build());
        } else {
            // it has been changed, so send response with new ETag and entity
            Response.ResponseBuilder responseBuilder = Response.fromResponse(containerResponse.getResponse()).tag(entityTag);
            containerResponse.setResponse(responseBuilder.build());
        }
    }
}
Also used : GenericContainerResponse(org.everrest.core.GenericContainerResponse) Response(javax.ws.rs.core.Response) ApplicationContext(org.everrest.core.ApplicationContext) Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) HashFunction(com.google.common.hash.HashFunction) Request(javax.ws.rs.core.Request) List(java.util.List) EntityTag(javax.ws.rs.core.EntityTag)

Example 10 with Request

use of javax.ws.rs.core.Request in project che by eclipse.

the class EverrestDownloadFileResponseFilter method doFilter.

/**
     * Filter the given container response.
     *
     * @param containerResponse
     *         the response to use
     */
public void doFilter(GenericContainerResponse containerResponse) {
    containerResponse.getResponse();
    // Get the request
    ApplicationContext applicationContext = ApplicationContext.getCurrent();
    Request request = applicationContext.getRequest();
    // Apply header if all if correct
    String filename = getFileName(request, containerResponse.getContentType(), applicationContext, containerResponse.getStatus());
    if (filename != null) {
        if (hasCompliantEntity(containerResponse.getEntity())) {
            // it has been changed, so send response with updated header
            Response.ResponseBuilder responseBuilder = Response.fromResponse(containerResponse.getResponse()).header(CONTENT_DISPOSITION, "attachment; filename=" + filename);
            containerResponse.setResponse(responseBuilder.build());
        }
    }
}
Also used : GenericContainerResponse(org.everrest.core.GenericContainerResponse) Response(javax.ws.rs.core.Response) ApplicationContext(org.everrest.core.ApplicationContext) Request(javax.ws.rs.core.Request)

Aggregations

Request (javax.ws.rs.core.Request)19 Response (javax.ws.rs.core.Response)12 Test (org.junit.Test)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 IOException (java.io.IOException)4 UriInfo (javax.ws.rs.core.UriInfo)3 URISyntaxException (java.net.URISyntaxException)2 List (java.util.List)2 EntityTag (javax.ws.rs.core.EntityTag)2 ApplicationContext (org.everrest.core.ApplicationContext)2 GenericContainerResponse (org.everrest.core.GenericContainerResponse)2 Fault (org.ovirt.engine.api.model.Fault)2 HashCode (com.google.common.hash.HashCode)1 HashFunction (com.google.common.hash.HashFunction)1 Hasher (com.google.common.hash.Hasher)1 APIError (com.liferay.apio.architect.error.APIError)1 ApioLogger (com.liferay.apio.architect.logger.ApioLogger)1 ErrorMessageMapper (com.liferay.apio.architect.message.json.ErrorMessageMapper)1 ExceptionConverterManager (com.liferay.apio.architect.wiring.osgi.manager.ExceptionConverterManager)1 ErrorMessageMapperManager (com.liferay.apio.architect.wiring.osgi.manager.message.json.ErrorMessageMapperManager)1