1 package de.dlr.shepard.context.collection.endpoints; 2 3 import de.dlr.shepard.common.configuration.feature.toggles.VersioningFeatureToggle; 4 import de.dlr.shepard.common.util.Constants; 5 import de.dlr.shepard.context.version.entities.Version; 6 import de.dlr.shepard.context.version.io.VersionIO; 7 import de.dlr.shepard.context.version.services.VersionService; 8 import io.quarkus.arc.properties.IfBuildProperty; 9 import jakarta.enterprise.context.RequestScoped; 10 import jakarta.inject.Inject; 11 import jakarta.validation.Valid; 12 import jakarta.ws.rs.Consumes; 13 import jakarta.ws.rs.GET; 14 import jakarta.ws.rs.POST; 15 import jakarta.ws.rs.Path; 16 import jakarta.ws.rs.PathParam; 17 import jakarta.ws.rs.Produces; 18 import jakarta.ws.rs.core.Context; 19 import jakarta.ws.rs.core.MediaType; 20 import jakarta.ws.rs.core.Response; 21 import jakarta.ws.rs.core.Response.Status; 22 import jakarta.ws.rs.core.SecurityContext; 23 import java.util.ArrayList; 24 import java.util.List; 25 import java.util.UUID; 26 import org.eclipse.microprofile.openapi.annotations.Operation; 27 import org.eclipse.microprofile.openapi.annotations.enums.SchemaType; 28 import org.eclipse.microprofile.openapi.annotations.media.Content; 29 import org.eclipse.microprofile.openapi.annotations.media.Schema; 30 import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; 31 import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; 32 import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; 33 import org.eclipse.microprofile.openapi.annotations.tags.Tag; 34 35 @Path(Constants.COLLECTIONS) 36 @Produces(MediaType.APPLICATION_JSON) 37 @Consumes(MediaType.APPLICATION_JSON) 38 @RequestScoped 39 @IfBuildProperty(name = VersioningFeatureToggle.TOGGLE_PROPERTY, stringValue = "true") 40 public class CollectionVersioningRest { 41 42 @Inject 43 VersionService versionService; 44 45 @Context 46 private SecurityContext securityContext; 47 48 @GET 49 @Path("/{" + Constants.COLLECTION_ID + "}/" + Constants.VERSIONS) 50 @Tag(name = Constants.COLLECTION) 51 @Operation(description = "Get versions") 52 @APIResponse( 53 description = "ok", 54 responseCode = "200", 55 content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = VersionIO.class)) 56 ) 57 @APIResponse(description = "not found", responseCode = "404") 58 @Parameter(name = Constants.COLLECTION_ID) 59 public Response getVersions(@PathParam(Constants.COLLECTION_ID) long collectionId) { 60 List<Version> versions = versionService.getAllVersions(collectionId); 61 var result = new ArrayList<VersionIO>(versions.size()); 62 for (var version : versions) { 63 result.add(new VersionIO(version)); 64 } 65 return Response.ok(result).build(); 66 } 67 68 @GET 69 @Path("/{" + Constants.COLLECTION_ID + "}/" + Constants.VERSIONS + "/{" + Constants.VERSION_UID + "}") 70 @Tag(name = Constants.COLLECTION) 71 @Operation(description = "Get version") 72 @APIResponse( 73 description = "ok", 74 responseCode = "200", 75 content = @Content(schema = @Schema(implementation = VersionIO.class)) 76 ) 77 @APIResponse(description = "not found", responseCode = "404") 78 @Parameter(name = Constants.COLLECTION_ID) 79 @Parameter(name = Constants.VERSION_UID) 80 public Response getVersion( 81 @PathParam(Constants.COLLECTION_ID) long collectionId, 82 @PathParam(Constants.VERSION_UID) UUID versionUID 83 ) { 84 Version version = versionService.getVersion(versionUID); 85 return Response.ok(new VersionIO(version)).build(); 86 } 87 88 @POST 89 @Path("/{" + Constants.COLLECTION_ID + "}/" + Constants.VERSIONS) 90 @Tag(name = Constants.COLLECTION) 91 @Operation(description = "Create a new version") 92 @APIResponse( 93 description = "created", 94 responseCode = "201", 95 content = @Content(schema = @Schema(implementation = VersionIO.class)) 96 ) 97 @Parameter(name = Constants.COLLECTION_ID) 98 public Response createVersion( 99 @PathParam(Constants.COLLECTION_ID) long collectionId, 100 @RequestBody( 101 required = true, 102 content = @Content(schema = @Schema(implementation = VersionIO.class)) 103 ) @Valid VersionIO version 104 ) { 105 Version newVersion = versionService.createVersion( 106 collectionId, 107 version, 108 securityContext.getUserPrincipal().getName() 109 ); 110 return Response.ok(new VersionIO(newVersion)).status(Status.CREATED).build(); 111 } 112 }