1 package de.dlr.shepard.context.references.uri.endpoints; 2 3 import de.dlr.shepard.common.filters.Subscribable; 4 import de.dlr.shepard.common.util.Constants; 5 import de.dlr.shepard.context.references.uri.io.URIReferenceIO; 6 import de.dlr.shepard.context.references.uri.services.URIReferenceService; 7 import jakarta.enterprise.context.RequestScoped; 8 import jakarta.inject.Inject; 9 import jakarta.validation.Valid; 10 import jakarta.validation.constraints.NotNull; 11 import jakarta.validation.constraints.PositiveOrZero; 12 import jakarta.ws.rs.Consumes; 13 import jakarta.ws.rs.DELETE; 14 import jakarta.ws.rs.GET; 15 import jakarta.ws.rs.POST; 16 import jakarta.ws.rs.Path; 17 import jakarta.ws.rs.PathParam; 18 import jakarta.ws.rs.Produces; 19 import jakarta.ws.rs.QueryParam; 20 import jakarta.ws.rs.core.Context; 21 import jakarta.ws.rs.core.MediaType; 22 import jakarta.ws.rs.core.Response; 23 import jakarta.ws.rs.core.Response.Status; 24 import jakarta.ws.rs.core.SecurityContext; 25 import java.util.ArrayList; 26 import java.util.UUID; 27 import org.eclipse.microprofile.openapi.annotations.Operation; 28 import org.eclipse.microprofile.openapi.annotations.enums.SchemaType; 29 import org.eclipse.microprofile.openapi.annotations.media.Content; 30 import org.eclipse.microprofile.openapi.annotations.media.Schema; 31 import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; 32 import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; 33 import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; 34 import org.eclipse.microprofile.openapi.annotations.tags.Tag; 35 36 @Consumes(MediaType.APPLICATION_JSON) 37 @Produces(MediaType.APPLICATION_JSON) 38 @Path( 39 Constants.COLLECTIONS + 40 "/{" + 41 Constants.COLLECTION_ID + 42 "}/" + 43 Constants.DATA_OBJECTS + 44 "/{" + 45 Constants.DATA_OBJECT_ID + 46 "}/" + 47 Constants.URI_REFERENCES 48 ) 49 @RequestScoped 50 public class URIReferenceRest { 51 52 @Inject 53 URIReferenceService uriReferenceService; 54 55 @Context 56 private SecurityContext securityContext; 57 58 @GET 59 @Tag(name = Constants.URI_REFERENCE) 60 @Operation(description = "Get all uri references") 61 @APIResponse( 62 description = "ok", 63 responseCode = "200", 64 content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = URIReferenceIO.class)) 65 ) 66 @APIResponse(responseCode = "400", description = "bad request") 67 @APIResponse(responseCode = "401", description = "not authorized") 68 @APIResponse(responseCode = "403", description = "forbidden") 69 @APIResponse(responseCode = "404", description = "not found") 70 @Parameter(name = Constants.COLLECTION_ID) 71 @Parameter(name = Constants.DATA_OBJECT_ID) 72 @Parameter(name = Constants.VERSION_UID) 73 public Response getAllUriReferences( 74 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 75 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 76 @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID 77 ) { 78 UUID versionUUID = null; 79 if (versionUID != null) { 80 versionUUID = UUID.fromString(versionUID); 81 } 82 var references = uriReferenceService.getAllReferencesByDataObjectId(collectionId, dataObjectId, versionUUID); 83 var result = new ArrayList<URIReferenceIO>(references.size()); 84 for (var ref : references) { 85 result.add(new URIReferenceIO(ref)); 86 } 87 return Response.ok(result).build(); 88 } 89 90 @GET 91 @Path("/{" + Constants.URI_REFERENCE_ID + "}") 92 @Tag(name = Constants.URI_REFERENCE) 93 @Operation(description = "Get uri reference") 94 @APIResponse( 95 description = "ok", 96 responseCode = "200", 97 content = @Content(schema = @Schema(implementation = URIReferenceIO.class)) 98 ) 99 @APIResponse(responseCode = "400", description = "bad request") 100 @APIResponse(responseCode = "401", description = "not authorized") 101 @APIResponse(responseCode = "403", description = "forbidden") 102 @APIResponse(responseCode = "404", description = "not found") 103 @Parameter(name = Constants.COLLECTION_ID) 104 @Parameter(name = Constants.DATA_OBJECT_ID) 105 @Parameter(name = Constants.URI_REFERENCE_ID) 106 @Parameter(name = Constants.VERSION_UID) 107 public Response getUriReference( 108 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 109 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 110 @PathParam(Constants.URI_REFERENCE_ID) @NotNull @PositiveOrZero Long referenceId, 111 @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID 112 ) { 113 UUID versionUUID = null; 114 if (versionUID != null) { 115 versionUUID = UUID.fromString(versionUID); 116 } 117 var reference = uriReferenceService.getReference(collectionId, dataObjectId, referenceId, versionUUID); 118 return Response.ok(new URIReferenceIO(reference)).build(); 119 } 120 121 @POST 122 @Subscribable 123 @Tag(name = Constants.URI_REFERENCE) 124 @Operation(description = "Create a new uri reference") 125 @APIResponse( 126 description = "created", 127 responseCode = "201", 128 content = @Content(schema = @Schema(implementation = URIReferenceIO.class)) 129 ) 130 @APIResponse(responseCode = "400", description = "bad request") 131 @APIResponse(responseCode = "401", description = "not authorized") 132 @APIResponse(responseCode = "403", description = "forbidden") 133 @APIResponse(responseCode = "404", description = "not found") 134 @Parameter(name = Constants.COLLECTION_ID) 135 @Parameter(name = Constants.DATA_OBJECT_ID) 136 public Response createUriReference( 137 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 138 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 139 @RequestBody( 140 required = true, 141 content = @Content(schema = @Schema(implementation = URIReferenceIO.class)) 142 ) @Valid URIReferenceIO timeseriesReference 143 ) { 144 var result = uriReferenceService.createReference(collectionId, dataObjectId, timeseriesReference); 145 return Response.ok(new URIReferenceIO(result)).status(Status.CREATED).build(); 146 } 147 148 @DELETE 149 @Path("/{" + Constants.URI_REFERENCE_ID + "}") 150 @Subscribable 151 @Tag(name = Constants.URI_REFERENCE) 152 @Operation(description = "Delete uri reference") 153 @APIResponse(description = "deleted", responseCode = "204") 154 @APIResponse(responseCode = "400", description = "bad request") 155 @APIResponse(responseCode = "401", description = "not authorized") 156 @APIResponse(responseCode = "403", description = "forbidden") 157 @APIResponse(responseCode = "404", description = "not found") 158 @Parameter(name = Constants.COLLECTION_ID) 159 @Parameter(name = Constants.DATA_OBJECT_ID) 160 @Parameter(name = Constants.URI_REFERENCE_ID) 161 public Response deleteUriReference( 162 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 163 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 164 @PathParam(Constants.URI_REFERENCE_ID) @NotNull @PositiveOrZero Long referenceId 165 ) { 166 uriReferenceService.deleteReference(collectionId, dataObjectId, referenceId); 167 return Response.status(Status.NO_CONTENT).build(); 168 } 169 }