use of jakarta.ws.rs.core.Request in project devspaces-images by redhat-developer.
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);
}
}
}
use of jakarta.ws.rs.core.Request in project jaxrs-api by eclipse-ee4j.
the class RequestFilter method getRequest.
public void getRequest() {
Request request = requestContext.getRequest();
String method = request.getMethod();
abortWithEntity(method);
}
use of jakarta.ws.rs.core.Request in project tomee by apache.
the class Contexts method bind.
/**
* Using a set ensures we don't set the thread local twice or more,
* there may be super classes with injection points of identical types
*
* Also allows us to get context references from other sources such as interceptors
*
* @param exchange Exchange
* @param types Collection
*/
public static void bind(final Exchange exchange, final Collection<Class<?>> types) {
// used in lazy mode by RESTResourceFinder if cdi beans uses @Context, === initThreadLocal
EXCHANGE.set(exchange);
CdiAppContextsService.pushRequestReleasable(CleanUpThreadLocal.INSTANCE);
for (final Class<?> type : types) {
if (Request.class.equals(type)) {
final Request binding = JAXRSUtils.createContextValue(exchange.getInMessage(), null, Request.class);
ThreadLocalContextManager.REQUEST.set(binding);
} else if (UriInfo.class.equals(type)) {
final UriInfo binding = JAXRSUtils.createContextValue(exchange.getInMessage(), null, UriInfo.class);
ThreadLocalContextManager.URI_INFO.set(binding);
} else if (HttpHeaders.class.equals(type)) {
final HttpHeaders binding = JAXRSUtils.createContextValue(exchange.getInMessage(), null, HttpHeaders.class);
ThreadLocalContextManager.HTTP_HEADERS.set(binding);
} else if (SecurityContext.class.equals(type)) {
final SecurityContext binding = JAXRSUtils.createContextValue(exchange.getInMessage(), null, SecurityContext.class);
ThreadLocalContextManager.SECURITY_CONTEXT.set(binding);
} else if (ContextResolver.class.equals(type)) {
final ContextResolver<?> binding = JAXRSUtils.createContextValue(exchange.getInMessage(), type, ContextResolver.class);
ThreadLocalContextManager.CONTEXT_RESOLVER.set(binding);
} else if (Providers.class.equals(type)) {
final Providers providers = JAXRSUtils.createContextValue(exchange.getInMessage(), null, Providers.class);
ThreadLocalContextManager.PROVIDERS.set(providers);
} else if (ServletRequest.class.equals(type)) {
ServletRequest servletRequest = JAXRSUtils.createContextValue(exchange.getInMessage(), null, ServletRequest.class);
if (servletRequest == null) {
// probably the case with CXF
servletRequest = JAXRSUtils.createContextValue(exchange.getInMessage(), null, HttpServletRequest.class);
}
ThreadLocalContextManager.SERVLET_REQUEST.set(servletRequest);
} else if (HttpServletRequest.class.equals(type)) {
final HttpServletRequest httpServletRequest = JAXRSUtils.createContextValue(exchange.getInMessage(), null, HttpServletRequest.class);
ThreadLocalContextManager.HTTP_SERVLET_REQUEST.set(httpServletRequest);
} else if (HttpServletResponse.class.equals(type)) {
final HttpServletResponse httpServletResponse = JAXRSUtils.createContextValue(exchange.getInMessage(), null, HttpServletResponse.class);
ThreadLocalContextManager.HTTP_SERVLET_RESPONSE.set(httpServletResponse);
} else if (ServletConfig.class.equals(type)) {
final ServletConfig servletConfig = JAXRSUtils.createContextValue(exchange.getInMessage(), null, ServletConfig.class);
ThreadLocalContextManager.SERVLET_CONFIG.set(servletConfig);
} else if (Configuration.class.equals(type)) {
final Configuration config = JAXRSUtils.createContextValue(exchange.getInMessage(), null, Configuration.class);
ThreadLocalContextManager.CONFIGURATION.set(config);
} else if (ResourceInfo.class.equals(type)) {
final ResourceInfo config = JAXRSUtils.createContextValue(exchange.getInMessage(), null, ResourceInfo.class);
ThreadLocalContextManager.RESOURCE_INFO.set(config);
} else if (ResourceContext.class.equals(type)) {
final ResourceContext config = JAXRSUtils.createContextValue(exchange.getInMessage(), null, ResourceContext.class);
ThreadLocalContextManager.RESOURCE_CONTEXT.set(config);
} else if (Application.class.equals(type)) {
final Application config = JAXRSUtils.createContextValue(exchange.getInMessage(), null, Application.class);
ThreadLocalContextManager.APPLICATION.set(config);
} else {
final Message message = exchange.getInMessage();
final ContextProvider<?> provider = ProviderFactory.getInstance(message).createContextProvider(type, message);
if (provider != null) {
final Object value = provider.createContext(message);
Map<String, Object> map = ThreadLocalContextManager.OTHERS.get();
if (map == null) {
map = new HashMap<>();
ThreadLocalContextManager.OTHERS.set(map);
}
map.put(type.getName(), value);
}
}
}
}
use of jakarta.ws.rs.core.Request in project mycore by MyCoRe-Org.
the class MCRRestObjects method listObjects.
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON + ";charset=UTF-8" })
@MCRCacheControl(maxAge = @MCRCacheControl.Age(time = 1, unit = TimeUnit.HOURS), sMaxAge = @MCRCacheControl.Age(time = 1, unit = TimeUnit.HOURS))
@Operation(summary = "Lists all objects in this repository", responses = @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = MCRObjectIDDate.class)))), tags = MCRRestUtils.TAG_MYCORE_OBJECT)
@XmlElementWrapper(name = "mycoreobjects")
@JacksonFeatures(serializationDisable = { SerializationFeature.WRITE_DATES_AS_TIMESTAMPS })
public Response listObjects() throws IOException {
Date lastModified = new Date(MCRXMLMetadataManager.instance().getLastModified());
Optional<Response> cachedResponse = MCRRestUtils.getCachedResponse(request, lastModified);
if (cachedResponse.isPresent()) {
return cachedResponse.get();
}
List<MCRRestObjectIDDate> idDates = MCRXMLMetadataManager.instance().listObjectDates().stream().filter(oid -> !oid.getId().contains("_derivate_")).map(x -> new MCRRestObjectIDDate(x)).collect(Collectors.toList());
return Response.ok(new GenericEntity<List<MCRRestObjectIDDate>>(idDates) {
}).lastModified(lastModified).build();
}
use of jakarta.ws.rs.core.Request in project mycore by MyCoRe-Org.
the class MCRRestObjects method getThumbnail.
private Response getThumbnail(String id, int size, String ext) {
List<MCRPath> mainDocs = MCRMetadataManager.getDerivateIds(MCRObjectID.getInstance(id), 1, TimeUnit.MINUTES).stream().filter(d -> MCRAccessManager.checkDerivateContentPermission(d, MCRAccessManager.PERMISSION_READ)).map(d -> {
String nameOfMainFile = MCRMetadataManager.retrieveMCRDerivate(d).getDerivate().getInternals().getMainDoc();
return nameOfMainFile.isEmpty() ? null : MCRPath.getPath(d.toString(), '/' + nameOfMainFile);
}).filter(Objects::nonNull).collect(Collectors.toList());
if (mainDocs.isEmpty()) {
throw new NotFoundException();
}
for (MCRPath mainDoc : mainDocs) {
Optional<MCRThumbnailGenerator> thumbnailGenerator = THUMBNAIL_GENERATORS.stream().filter(g -> g.matchesFileType(context.getMimeType(mainDoc.getFileName().toString()), mainDoc)).findFirst();
if (thumbnailGenerator.isPresent()) {
try {
FileTime lastModified = Files.getLastModifiedTime(mainDoc);
Date lastModifiedDate = new Date(lastModified.toMillis());
Optional<Response> cachedResponse = MCRRestUtils.getCachedResponse(request, lastModifiedDate);
return cachedResponse.orElseGet(() -> {
Optional<BufferedImage> thumbnail = null;
try {
thumbnail = thumbnailGenerator.get().getThumbnail(mainDoc, size);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return thumbnail.map(b -> {
String type = "image/png";
if ("jpg".equals(ext) || "jpeg".equals(ext)) {
type = "image/jpeg";
}
return Response.ok(b).lastModified(lastModifiedDate).type(type).build();
}).orElseGet(() -> Response.status(Response.Status.NOT_FOUND).build());
});
} catch (FileNotFoundException e) {
// try another mainDoc if present
continue;
} catch (IOException e) {
throw new InternalServerErrorException(e);
} catch (UncheckedIOException e) {
throw new InternalServerErrorException(e.getCause());
}
}
}
throw new NotFoundException();
}
Aggregations