Search in sources :

Example 1 with Request

use of jakarta.ws.rs.core.Request in project che-server by eclipse-che.

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
 * jakarta.annotation.Priority} class-level annotation value.
 *
 * @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(jakarta.ws.rs.core.Request)

Example 2 with Request

use of jakarta.ws.rs.core.Request in project che-server by eclipse-che.

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(jakarta.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(jakarta.ws.rs.core.Request) List(java.util.List) EntityTag(jakarta.ws.rs.core.EntityTag)

Example 3 with Request

use of jakarta.ws.rs.core.Request in project resteasy by resteasy.

the class SynchronousDispatcher method invoke.

/**
 * Invoke and write response.
 *
 * @param request http request
 * @param response http response
 * @param invoker resource invoker
 */
public void invoke(HttpRequest request, HttpResponse response, ResourceInvoker invoker) {
    RESTEasyTracingLogger tracingLogger = RESTEasyTracingLogger.getInstance(request);
    Response jaxrsResponse = null;
    try {
        request.getAsyncContext().initialRequestStarted();
        jaxrsResponse = invoker.invoke(request, response);
        request.getAsyncContext().initialRequestEnded();
        tracingLogger.log("DISPATCH_RESPONSE", jaxrsResponse);
        if (request.getAsyncContext().isSuspended()) {
            /**
             * Callback by the initial calling thread.  This callback will probably do nothing in an asynchronous environment
             * but will be used to simulate AsynchronousResponse in vanilla Servlet containers that do not support
             * asychronous HTTP.
             */
            request.getAsyncContext().getAsyncResponse().initialRequestThreadFinished();
            // we're handing response asynchronously
            jaxrsResponse = null;
        }
    } catch (CompletionException e) {
        // logger.error("invoke() failed mapping exception", e);
        writeException(request, response, e.getCause(), t -> {
        });
        return;
    } catch (Exception e) {
        // logger.error("invoke() failed mapping exception", e);
        invoker.getMethodStatisticsLogger().incFailureCnt();
        writeException(request, response, e, t -> {
        });
        return;
    }
    if (jaxrsResponse != null) {
        writeResponse(request, response, jaxrsResponse);
    }
}
Also used : Response(jakarta.ws.rs.core.Response) HttpResponse(org.jboss.resteasy.spi.HttpResponse) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse) Registry(org.jboss.resteasy.spi.Registry) HashMap(java.util.HashMap) ResteasyConfiguration(org.jboss.resteasy.spi.ResteasyConfiguration) ArrayList(java.util.ArrayList) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger) HashSet(java.util.HashSet) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) Messages(org.jboss.resteasy.resteasy_jaxrs.i18n.Messages) Response(jakarta.ws.rs.core.Response) RequestImpl(org.jboss.resteasy.specimpl.RequestImpl) ResourceInvoker(org.jboss.resteasy.spi.ResourceInvoker) LogMessages(org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter) Dispatcher(org.jboss.resteasy.spi.Dispatcher) Map(java.util.Map) Cleanable(org.jboss.resteasy.plugins.server.Cleanable) Request(jakarta.ws.rs.core.Request) Cleanables(org.jboss.resteasy.plugins.server.Cleanables) NotFoundException(jakarta.ws.rs.NotFoundException) Iterator(java.util.Iterator) UnhandledException(org.jboss.resteasy.spi.UnhandledException) Set(java.util.Set) HttpRequest(org.jboss.resteasy.spi.HttpRequest) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) UriInfo(jakarta.ws.rs.core.UriInfo) HttpRequestPreprocessor(org.jboss.resteasy.spi.HttpRequestPreprocessor) HttpResponse(org.jboss.resteasy.spi.HttpResponse) Consumer(java.util.function.Consumer) ResteasyAsynchronousContext(org.jboss.resteasy.spi.ResteasyAsynchronousContext) List(java.util.List) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse) HttpHeaders(jakarta.ws.rs.core.HttpHeaders) ResourceContext(jakarta.ws.rs.container.ResourceContext) Failure(org.jboss.resteasy.spi.Failure) InternalServerErrorException(org.jboss.resteasy.spi.InternalServerErrorException) Providers(jakarta.ws.rs.ext.Providers) PreMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext) CompletionException(java.util.concurrent.CompletionException) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger) NotFoundException(jakarta.ws.rs.NotFoundException) UnhandledException(org.jboss.resteasy.spi.UnhandledException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) InternalServerErrorException(org.jboss.resteasy.spi.InternalServerErrorException)

Example 4 with Request

use of jakarta.ws.rs.core.Request in project devspaces-images by redhat-developer.

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(jakarta.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(jakarta.ws.rs.core.Request) List(java.util.List) EntityTag(jakarta.ws.rs.core.EntityTag)

Example 5 with Request

use of jakarta.ws.rs.core.Request in project devspaces-images by redhat-developer.

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(jakarta.ws.rs.core.Response) ApplicationContext(org.everrest.core.ApplicationContext) Request(jakarta.ws.rs.core.Request)

Aggregations

Request (jakarta.ws.rs.core.Request)16 Response (jakarta.ws.rs.core.Response)11 UriInfo (jakarta.ws.rs.core.UriInfo)9 List (java.util.List)9 HttpHeaders (jakarta.ws.rs.core.HttpHeaders)8 Map (java.util.Map)8 NotFoundException (jakarta.ws.rs.NotFoundException)7 IOException (java.io.IOException)7 ResourceContext (jakarta.ws.rs.container.ResourceContext)6 Providers (jakarta.ws.rs.ext.Providers)6 HashMap (java.util.HashMap)6 ContainerRequestFilter (jakarta.ws.rs.container.ContainerRequestFilter)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Iterator (java.util.Iterator)4 Set (java.util.Set)4 CompletionException (java.util.concurrent.CompletionException)4 Consumer (java.util.function.Consumer)4 PreMatchContainerRequestContext (org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext)4 Cleanable (org.jboss.resteasy.plugins.server.Cleanable)4