Search in sources :

Example 1 with CONTENT_LOCATION

use of javax.ws.rs.core.HttpHeaders.CONTENT_LOCATION in project aries-jax-rs-whiteboard by apache.

the class FrameworkBundlesResource method bundles.

// 137.3.2.2
@POST
@Consumes(TEXT_PLAIN)
@Produces({ APPLICATION_BUNDLE_JSON, APPLICATION_BUNDLE_XML })
@Path("framework/bundles{ext:(\\.json|\\.xml)*}")
@Operation(operationId = "POST/bundles", summary = "Installs a new bundle to the managed framework and thereby logically appends it to the bundles resource", requestBody = @RequestBody(description = "One of the following contents is allowed; a location string with content-type type 'text/plain' OR a bundle jar with content-type 'application/vnd.osgi.bundle'", required = true, content = { @Content(mediaType = TEXT_PLAIN, schema = @Schema(description = "a location string", type = "string")), @Content(mediaType = APPLICATION_VNDOSGIBUNDLE, schema = @Schema(description = "a bundle jar", type = "string", format = "binary")) }), responses = { @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = BundleSchema.class))), @ApiResponse(responseCode = "400", description = "the REST management service received a BundleException when trying to install", content = { @Content(mediaType = APPLICATION_BUNDLEEXCEPTION_JSON, schema = @Schema(implementation = BundleExceptionSchema.class)), @Content(mediaType = APPLICATION_BUNDLEEXCEPTION_XML, schema = @Schema(implementation = BundleExceptionSchema.class)) }), @ApiResponse(responseCode = "406", description = "The REST management service does not support any of the requested representations") }, parameters = { @Parameter(name = CONTENT_LOCATION, in = ParameterIn.HEADER, description = "When posting a bundle jar a location string can be specified using the 'Content-Location' header", required = false) })
public Response bundles(String location, @Parameter(allowEmptyValue = true, description = "File extension", schema = @Schema(allowableValues = { ".json", ".xml" })) @PathParam("ext") String ext, @HeaderParam(CONTENT_TYPE) MediaType contentType) {
    try {
        Instant now = Instant.now().minusMillis(2);
        Bundle bundle = bundleContext.installBundle(location);
        if (!now.isBefore(Instant.ofEpochMilli(bundle.getLastModified()))) {
            throw new WebApplicationException(location, 409);
        }
        ResponseBuilder builder = Response.ok(bundleSchema(bundle));
        return Optional.ofNullable(ext).map(String::trim).map(e -> ".json".equals(e) ? APPLICATION_BUNDLE_JSON : APPLICATION_BUNDLE_XML).map(type -> builder.type(type)).orElse(builder).build();
    } catch (BundleException exception) {
        return Response.status(400).type(contentType.equals(APPLICATION_BUNDLE_JSON_TYPE) ? APPLICATION_BUNDLEEXCEPTION_JSON_TYPE : APPLICATION_BUNDLEEXCEPTION_XML_TYPE).entity(BundleExceptionSchema.build(exception.getType(), exception.getMessage())).build();
    }
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) APPLICATION_BUNDLEEXCEPTION_XML_TYPE(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLEEXCEPTION_XML_TYPE) PathParam(javax.ws.rs.PathParam) APPLICATION_BUNDLE_JSON_TYPE(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLE_JSON_TYPE) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) APPLICATION_BUNDLE_XML(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLE_XML) APPLICATION_BUNDLESTATE_XML(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLESTATE_XML) APPLICATION_BUNDLES_XML(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLES_XML) ParameterIn(io.swagger.v3.oas.annotations.enums.ParameterIn) Content(io.swagger.v3.oas.annotations.media.Content) MediaType(javax.ws.rs.core.MediaType) Operation(io.swagger.v3.oas.annotations.Operation) Consumes(javax.ws.rs.Consumes) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) HeaderParam(javax.ws.rs.HeaderParam) Bundle(org.osgi.framework.Bundle) APPLICATION_BUNDLESTATE_JSON(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLESTATE_JSON) CONTENT_LOCATION(javax.ws.rs.core.HttpHeaders.CONTENT_LOCATION) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) BundleException(org.osgi.framework.BundleException) APPLICATION_BUNDLEEXCEPTION_JSON(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLEEXCEPTION_JSON) BundleListSchema(org.apache.aries.jax.rs.rest.management.schema.BundleListSchema) Schema(io.swagger.v3.oas.annotations.media.Schema) BundleSchema(org.apache.aries.jax.rs.rest.management.schema.BundleSchema) POST(javax.ws.rs.POST) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) Predicate(java.util.function.Predicate) UUID(java.util.UUID) APPLICATION_BUNDLEEXCEPTION_XML(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLEEXCEPTION_XML) Instant(java.time.Instant) BundleContext(org.osgi.framework.BundleContext) APPLICATION_VNDOSGIBUNDLE(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_VNDOSGIBUNDLE) Parameter(io.swagger.v3.oas.annotations.Parameter) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) APPLICATION_BUNDLES_JSON(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLES_JSON) Response(javax.ws.rs.core.Response) BundleExceptionSchema(org.apache.aries.jax.rs.rest.management.schema.BundleExceptionSchema) APPLICATION_BUNDLE_JSON(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLE_JSON) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) APPLICATION_BUNDLEEXCEPTION_JSON_TYPE(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLEEXCEPTION_JSON_TYPE) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) InputStream(java.io.InputStream) WebApplicationException(javax.ws.rs.WebApplicationException) Bundle(org.osgi.framework.Bundle) Instant(java.time.Instant) BundleException(org.osgi.framework.BundleException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation)

Example 2 with CONTENT_LOCATION

use of javax.ws.rs.core.HttpHeaders.CONTENT_LOCATION in project aries-jax-rs-whiteboard by apache.

the class FrameworkBundlesResource method bundles.

// 137.3.2.3
@POST
@Consumes(APPLICATION_VNDOSGIBUNDLE)
@Produces({ APPLICATION_BUNDLE_JSON, APPLICATION_BUNDLE_XML })
@Path("framework/bundles{ext:(\\.json|\\.xml)*}")
@Operation(hidden = true)
public Response bundles(InputStream inputStream, @Parameter(allowEmptyValue = true, description = "File extension", schema = @Schema(allowableValues = { ".json", ".xml" })) @PathParam("ext") String ext, @HeaderParam(CONTENT_LOCATION) String location, @HeaderParam(CONTENT_TYPE) MediaType contentType) {
    try {
        location = Optional.ofNullable(location).orElseGet(() -> "org.apache.aries.jax.rs.whiteboard:".concat(UUID.randomUUID().toString()));
        Instant now = Instant.now().minusMillis(2);
        Bundle bundle = bundleContext.installBundle(location, inputStream);
        if (!now.isBefore(Instant.ofEpochMilli(bundle.getLastModified()))) {
            throw new WebApplicationException(location, 409);
        }
        ResponseBuilder builder = Response.ok(bundleSchema(bundle));
        return Optional.ofNullable(ext).map(String::trim).map(e -> ".json".equals(e) ? APPLICATION_BUNDLE_JSON : APPLICATION_BUNDLE_XML).map(type -> builder.type(type)).orElse(builder).build();
    } catch (BundleException exception) {
        return Response.status(400).type(contentType.equals(APPLICATION_BUNDLE_JSON_TYPE) ? APPLICATION_BUNDLEEXCEPTION_JSON_TYPE : APPLICATION_BUNDLEEXCEPTION_XML_TYPE).entity(BundleExceptionSchema.build(exception.getType(), exception.getMessage())).build();
    }
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) APPLICATION_BUNDLEEXCEPTION_XML_TYPE(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLEEXCEPTION_XML_TYPE) PathParam(javax.ws.rs.PathParam) APPLICATION_BUNDLE_JSON_TYPE(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLE_JSON_TYPE) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) APPLICATION_BUNDLE_XML(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLE_XML) APPLICATION_BUNDLESTATE_XML(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLESTATE_XML) APPLICATION_BUNDLES_XML(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLES_XML) ParameterIn(io.swagger.v3.oas.annotations.enums.ParameterIn) Content(io.swagger.v3.oas.annotations.media.Content) MediaType(javax.ws.rs.core.MediaType) Operation(io.swagger.v3.oas.annotations.Operation) Consumes(javax.ws.rs.Consumes) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) HeaderParam(javax.ws.rs.HeaderParam) Bundle(org.osgi.framework.Bundle) APPLICATION_BUNDLESTATE_JSON(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLESTATE_JSON) CONTENT_LOCATION(javax.ws.rs.core.HttpHeaders.CONTENT_LOCATION) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) BundleException(org.osgi.framework.BundleException) APPLICATION_BUNDLEEXCEPTION_JSON(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLEEXCEPTION_JSON) BundleListSchema(org.apache.aries.jax.rs.rest.management.schema.BundleListSchema) Schema(io.swagger.v3.oas.annotations.media.Schema) BundleSchema(org.apache.aries.jax.rs.rest.management.schema.BundleSchema) POST(javax.ws.rs.POST) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) Predicate(java.util.function.Predicate) UUID(java.util.UUID) APPLICATION_BUNDLEEXCEPTION_XML(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLEEXCEPTION_XML) Instant(java.time.Instant) BundleContext(org.osgi.framework.BundleContext) APPLICATION_VNDOSGIBUNDLE(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_VNDOSGIBUNDLE) Parameter(io.swagger.v3.oas.annotations.Parameter) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) APPLICATION_BUNDLES_JSON(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLES_JSON) Response(javax.ws.rs.core.Response) BundleExceptionSchema(org.apache.aries.jax.rs.rest.management.schema.BundleExceptionSchema) APPLICATION_BUNDLE_JSON(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLE_JSON) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) APPLICATION_BUNDLEEXCEPTION_JSON_TYPE(org.apache.aries.jax.rs.rest.management.RestManagementConstants.APPLICATION_BUNDLEEXCEPTION_JSON_TYPE) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) InputStream(java.io.InputStream) WebApplicationException(javax.ws.rs.WebApplicationException) Bundle(org.osgi.framework.Bundle) Instant(java.time.Instant) BundleException(org.osgi.framework.BundleException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)2 Parameter (io.swagger.v3.oas.annotations.Parameter)2 ParameterIn (io.swagger.v3.oas.annotations.enums.ParameterIn)2 Content (io.swagger.v3.oas.annotations.media.Content)2 ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)2 Schema (io.swagger.v3.oas.annotations.media.Schema)2 RequestBody (io.swagger.v3.oas.annotations.parameters.RequestBody)2 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)2 InputStream (java.io.InputStream)2 Instant (java.time.Instant)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Predicate (java.util.function.Predicate)2 Collectors.toList (java.util.stream.Collectors.toList)2 Stream (java.util.stream.Stream)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 HeaderParam (javax.ws.rs.HeaderParam)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2