@Override public void filter(ContainerRequestContext requestContext) throws IOException { // answer OPTIONS requests early so we don't have jersey produce WADL responses for them (we only use them for CORS preflight) if ("options".equalsIgnoreCase(requestContext.getRequest().getMethod())) { final Response.ResponseBuilder options = Response.noContent(); String origin = requestContext.getHeaders().getFirst("Origin"); if (origin != null && !origin.isEmpty()) { options.header("Access-Control-Allow-Origin", origin); options.header("Access-Control-Allow-Credentials", true); options.header("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Graylog-No-Session-Extension, X-Requested-With, X-Requested-By"); options.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // In order to avoid redoing the preflight thingy for every request, see http://stackoverflow.com/a/12021982/1088469 options.header("Access-Control-Max-Age", "600"); // 10 minutes seems to be the maximum allowable value requestContext.abortWith(options.build()); } } } }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { // we have already added the necessary headers for OPTIONS requests below if ("options".equalsIgnoreCase(requestContext.getRequest().getMethod())) { if(Response.Status.Family.familyOf(responseContext.getStatus()) == Response.Status.Family.SUCCESSFUL) { return; } responseContext.setStatus(Response.Status.NO_CONTENT.getStatusCode()); responseContext.setEntity(""); } String origin = requestContext.getHeaders().getFirst("Origin"); if (origin != null && !origin.isEmpty()) { responseContext.getHeaders().add("Access-Control-Allow-Origin", origin); responseContext.getHeaders().add("Access-Control-Allow-Credentials", true); responseContext.getHeaders().add("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Graylog-No-Session-Extension, X-Requested-With, X-Requested-By"); responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // In order to avoid redoing the preflight thingy for every request, see http://stackoverflow.com/a/12021982/1088469 responseContext.getHeaders().add("Access-Control-Max-Age", "600"); // 10 minutes seems to be the maximum allowable value } }
@Override public Response apply(ContainerRequestContext data) { Map<String, Object> arguments = new HashMap<>(); if (this.readBody) { arguments.putAll(extractBodyArguments(data)); } arguments.putAll(extractPathParameters(data)); arguments.putAll(extractQueryParameters(data)); try { Object response = this.operation.invoke(new InvocationContext( new JerseySecurityContext(data.getSecurityContext()), arguments)); return convertToJaxRsResponse(response, data.getRequest().getMethod()); } catch (InvalidEndpointRequestException ex) { return Response.status(Status.BAD_REQUEST).build(); } }
@Override public Request getRequest() { return context.getRequest(); }
@Override public Request getRequest() { return request.getRequest(); } @Override public String getMethod() { return request.getMethod(); }
@Override public Request getRequest() { return request.getRequest(); } @Override public String getMethod() { return request.getMethod(); }
@Provides @RequestScoped public Request providesRequest() { return filter.getContainerRequestContext().getRequest(); }
@Provides @RequestScoped public HttpHeaders providesHttpHeaders() { ContainerRequestContext context = filter.getContainerRequestContext(); return (context != null) ? (ContainerRequest)context.getRequest() : null; }
@Override public void filter(ContainerRequestContext requestContext) throws IOException { // answer OPTIONS requests early so we don't have jersey produce WADL responses for them (we only use them for CORS preflight) if ("options".equalsIgnoreCase(requestContext.getRequest().getMethod())) { final Response.ResponseBuilder options = Response.noContent(); String origin = requestContext.getHeaders().getFirst("Origin"); if (origin != null && !origin.isEmpty()) { options.header("Access-Control-Allow-Origin", origin); options.header("Access-Control-Allow-Credentials", true); options.header("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Graylog-No-Session-Extension, X-Requested-With, X-Requested-By"); options.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // In order to avoid redoing the preflight thingy for every request, see http://stackoverflow.com/a/12021982/1088469 options.header("Access-Control-Max-Age", "600"); // 10 minutes seems to be the maximum allowable value requestContext.abortWith(options.build()); } } } }
@Override public void filter(ContainerRequestContext rc) throws IOException { if (HttpMethod.GET.equals(rc.getRequest().getMethod())) { UriInfo ui = rc.getUriInfo(); String path = "/" + ui.getPath(); if (PATTERN.matcher(path).matches() && locator.exists(path)) { rc.setRequestUri(URI.create("api-docs" + path)); } } } }
@Override public void filter(ContainerRequestContext rc) throws IOException { if (HttpMethod.GET.equals(rc.getRequest().getMethod())) { UriInfo ui = rc.getUriInfo(); String path = ui.getPath(); int uiPathIndex = path.lastIndexOf("api-docs"); if (uiPathIndex >= 0) { String resourcePath = uiPathIndex + 8 < path.length() ? path.substring(uiPathIndex + 8) : ""; rc.abortWith(uiService.getResource(ui, resourcePath)); } } } }
@Override public void filter(ContainerRequestContext rc) throws IOException { if (HttpMethod.GET.equals(rc.getRequest().getMethod())) { UriInfo ui = rc.getUriInfo(); String path = "/" + ui.getPath(); if (PATTERN.matcher(path).matches() && locator.exists(path)) { rc.setRequestUri(URI.create("api-docs" + path)); } } } }
@Override public void filter(final ContainerRequestContext rc) { if (!HttpMethod.GET.equals(rc.getRequest().getMethod())) { return; } final String path = rc.getUriInfo().getPath(); if ("openapi".equals(path)) { final List<MediaType> mediaTypes = rc.getAcceptableMediaTypes(); rc.abortWith(Response.ok(openApi).type(selectType(mediaTypes)).build()); } if ("openapi.json".equals(path)) { rc.abortWith(Response.ok(openApi).type(APPLICATION_JSON_TYPE).build()); } if ("openapi.yml".equals(path) || "openapi.yaml".equals(path)) { rc.abortWith(Response.ok(openApi).type("text/vnd.yaml").build()); } }
@Provides @RequestScoped public MessageBodyWorkers providesMessageBodyWorkers() { ContainerRequestContext context = filter.getContainerRequestContext(); return (context != null) ? ((ContainerRequest)context.getRequest()).getWorkers() : null; }
@Override public void filter(ContainerRequestContext rc) throws IOException { if (HttpMethod.GET.equals(rc.getRequest().getMethod())) { UriInfo ui = rc.getUriInfo(); String path = ui.getPath(); int uiPathIndex = path.lastIndexOf("api-docs"); if (uiPathIndex >= 0) { String resourcePath = uiPathIndex + 8 < path.length() ? path.substring(uiPathIndex + 8) : ""; rc.abortWith(uiService.getResource(ui, resourcePath)); } } } }
@Override public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException { String method = containerRequestContext.getRequest().getMethod(); if (method.equals("POST") && containerResponseContext.getStatus() == Response.Status.OK.getStatusCode()) { containerResponseContext.setStatus(Response.Status.CREATED.getStatusCode()); RestEntity entity = (RestEntity) containerResponseContext.getEntity(); String id = entity.getId(); containerResponseContext.getHeaders().add("Location", containerRequestContext.getUriInfo().getAbsolutePathBuilder().path(id).build().toString()); } //containerResponseContext.getHeaders().put("Location") } }
private Response getClassification(String classId, Function<MCRCategoryDAO, MCRCategory> categorySupplier) { MCRCategoryDAO categoryDAO = MCRCategoryDAOFactory.getInstance(); Date lastModified = getLastModifiedDate(classId, categoryDAO); Optional<Response> cachedResponse = MCRRestUtils.getCachedResponse(request.getRequest(), lastModified); if (cachedResponse.isPresent()) { return cachedResponse.get(); } MCRCategory classification = categorySupplier.apply(categoryDAO); if (classification == null) { throw new NotFoundException(); } return Response.ok() .entity(classification.isClassification() ? MCRClass.getClassification(classification) : MCRClassCategory.getInstance(classification)) .lastModified(lastModified) .build(); }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { // we have already added the necessary headers for OPTIONS requests below if ("options".equalsIgnoreCase(requestContext.getRequest().getMethod())) { if(Response.Status.Family.familyOf(responseContext.getStatus()) == Response.Status.Family.SUCCESSFUL) { return; } responseContext.setStatus(Response.Status.NO_CONTENT.getStatusCode()); responseContext.setEntity(""); } String origin = requestContext.getHeaders().getFirst("Origin"); if (origin != null && !origin.isEmpty()) { responseContext.getHeaders().add("Access-Control-Allow-Origin", origin); responseContext.getHeaders().add("Access-Control-Allow-Credentials", true); responseContext.getHeaders().add("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Graylog-No-Session-Extension, X-Requested-With, X-Requested-By"); responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); // In order to avoid redoing the preflight thingy for every request, see http://stackoverflow.com/a/12021982/1088469 responseContext.getHeaders().add("Access-Control-Max-Age", "600"); // 10 minutes seems to be the maximum allowable value } }
@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 classifications in this repository", responses = @ApiResponse( content = @Content(array = @ArraySchema(schema = @Schema(implementation = MCRClass.class)))), tags = MCRRestUtils.TAG_MYCORE_CLASSIFICATION) @XmlElementWrapper(name = "classifications") public Response listClassifications() { MCRCategoryDAO categoryDAO = MCRCategoryDAOFactory.getInstance(); Date lastModified = new Date(categoryDAO.getLastModified()); Optional<Response> cachedResponse = MCRRestUtils.getCachedResponse(request.getRequest(), lastModified); if (cachedResponse.isPresent()) { return cachedResponse.get(); } GenericEntity<List<MCRClass>> entity = new GenericEntity<List<MCRClass>>( categoryDAO.getRootCategories() .stream() .map(MCRRestClassifications::convertToClass) .collect(Collectors.toList())) { }; return Response.ok(entity) .lastModified(lastModified) .build(); }
private Response updateOrCreateFile(InputStream contents, MCRPath mcrPath) { MCRFileAttributes fileAttributes; try { fileAttributes = Files.readAttributes(mcrPath, MCRFileAttributes.class); if (!fileAttributes.isRegularFile()) { throw new BadRequestException("Overwrite directory with file: " + mcrPath); } } catch (IOException e) { //does not exist return createFile(contents, mcrPath); } //file does already exist Date lastModified = new Date(fileAttributes.lastModifiedTime().toMillis()); EntityTag eTag = getETag(fileAttributes); Optional<Response> cachedResponse = MCRRestUtils.getCachedResponse(request.getRequest(), lastModified, eTag); if (cachedResponse.isPresent()) { return cachedResponse.get(); } return updateFile(contents, mcrPath); }