1 package de.dlr.shepard.context.references.dataobject.endpoints; 2 3 import de.dlr.shepard.common.filters.Subscribable; 4 import de.dlr.shepard.common.util.Constants; 5 import de.dlr.shepard.context.collection.entities.DataObject; 6 import de.dlr.shepard.context.collection.io.DataObjectIO; 7 import de.dlr.shepard.context.references.dataobject.entities.DataObjectReference; 8 import de.dlr.shepard.context.references.dataobject.io.DataObjectReferenceIO; 9 import de.dlr.shepard.context.references.dataobject.services.DataObjectReferenceService; 10 import jakarta.enterprise.context.RequestScoped; 11 import jakarta.inject.Inject; 12 import jakarta.validation.Valid; 13 import jakarta.validation.constraints.NotNull; 14 import jakarta.validation.constraints.PositiveOrZero; 15 import jakarta.ws.rs.Consumes; 16 import jakarta.ws.rs.DELETE; 17 import jakarta.ws.rs.GET; 18 import jakarta.ws.rs.POST; 19 import jakarta.ws.rs.Path; 20 import jakarta.ws.rs.PathParam; 21 import jakarta.ws.rs.Produces; 22 import jakarta.ws.rs.QueryParam; 23 import jakarta.ws.rs.core.Context; 24 import jakarta.ws.rs.core.MediaType; 25 import jakarta.ws.rs.core.Response; 26 import jakarta.ws.rs.core.Response.Status; 27 import jakarta.ws.rs.core.SecurityContext; 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.UUID; 31 import org.eclipse.microprofile.openapi.annotations.Operation; 32 import org.eclipse.microprofile.openapi.annotations.enums.SchemaType; 33 import org.eclipse.microprofile.openapi.annotations.media.Content; 34 import org.eclipse.microprofile.openapi.annotations.media.Schema; 35 import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; 36 import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; 37 import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; 38 import org.eclipse.microprofile.openapi.annotations.tags.Tag; 39 40 @Consumes(MediaType.APPLICATION_JSON) 41 @Produces(MediaType.APPLICATION_JSON) 42 @Path( 43 Constants.COLLECTIONS + 44 "/{" + 45 Constants.COLLECTION_ID + 46 "}/" + 47 Constants.DATA_OBJECTS + 48 "/{" + 49 Constants.DATA_OBJECT_ID + 50 "}/" + 51 Constants.DATAOBJECT_REFERENCES 52 ) 53 @RequestScoped 54 public class DataObjectReferenceRest { 55 56 @Inject 57 DataObjectReferenceService dataObjectReferenceService; 58 59 @Context 60 private SecurityContext securityContext; 61 62 @GET 63 @Tag(name = Constants.DATAOBJECT_REFERENCE) 64 @Operation(description = "Get all dataObject references") 65 @APIResponse( 66 description = "ok", 67 responseCode = "200", 68 content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = DataObjectReferenceIO.class)) 69 ) 70 @APIResponse(responseCode = "400", description = "bad request") 71 @APIResponse(responseCode = "401", description = "not authorized") 72 @APIResponse(responseCode = "403", description = "forbidden") 73 @APIResponse(responseCode = "404", description = "not found") 74 @Parameter(name = Constants.COLLECTION_ID) 75 @Parameter(name = Constants.DATA_OBJECT_ID) 76 @Parameter(name = Constants.VERSION_UID) 77 public Response getAllDataObjectReferences( 78 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 79 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 80 @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID 81 ) { 82 UUID versionUUID = null; 83 if (versionUID != null) { 84 versionUUID = UUID.fromString(versionUID); 85 } 86 List<DataObjectReference> references = dataObjectReferenceService.getAllReferencesByDataObjectId( 87 collectionId, 88 dataObjectId, 89 versionUUID 90 ); 91 var result = new ArrayList<DataObjectReferenceIO>(references.size()); 92 for (var reference : references) { 93 result.add(new DataObjectReferenceIO(reference)); 94 } 95 return Response.ok(result).build(); 96 } 97 98 @GET 99 @Path("/{" + Constants.DATAOBJECT_REFERENCE_ID + "}") 100 @Tag(name = Constants.DATAOBJECT_REFERENCE) 101 @Operation(description = "Get dataObject reference") 102 @APIResponse( 103 description = "ok", 104 responseCode = "200", 105 content = @Content(schema = @Schema(implementation = DataObjectReferenceIO.class)) 106 ) 107 @APIResponse(responseCode = "400", description = "bad request") 108 @APIResponse(responseCode = "401", description = "not authorized") 109 @APIResponse(responseCode = "403", description = "forbidden") 110 @APIResponse(responseCode = "404", description = "not found") 111 @Parameter(name = Constants.COLLECTION_ID) 112 @Parameter(name = Constants.DATA_OBJECT_ID) 113 @Parameter(name = Constants.DATAOBJECT_REFERENCE_ID) 114 @Parameter(name = Constants.VERSION_UID) 115 public Response getDataObjectReference( 116 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 117 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 118 @PathParam(Constants.DATAOBJECT_REFERENCE_ID) @NotNull @PositiveOrZero Long dataObjectReferenceId, 119 @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID 120 ) { 121 UUID versionUUID = null; 122 if (versionUID != null) { 123 versionUUID = UUID.fromString(versionUID); 124 } 125 DataObjectReference result = dataObjectReferenceService.getReference( 126 collectionId, 127 dataObjectId, 128 dataObjectReferenceId, 129 versionUUID 130 ); 131 return Response.ok(new DataObjectReferenceIO(result)).build(); 132 } 133 134 @POST 135 @Subscribable 136 @Tag(name = Constants.DATAOBJECT_REFERENCE) 137 @Operation(description = "Create a new dataObject reference") 138 @APIResponse( 139 description = "created", 140 responseCode = "201", 141 content = @Content(schema = @Schema(implementation = DataObjectReferenceIO.class)) 142 ) 143 @APIResponse(responseCode = "400", description = "bad request") 144 @APIResponse(responseCode = "401", description = "not authorized") 145 @APIResponse(responseCode = "403", description = "forbidden") 146 @APIResponse(responseCode = "404", description = "not found") 147 @Parameter(name = Constants.COLLECTION_ID) 148 @Parameter(name = Constants.DATA_OBJECT_ID) 149 public Response createDataObjectReference( 150 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 151 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 152 @RequestBody( 153 required = true, 154 content = @Content(schema = @Schema(implementation = DataObjectReferenceIO.class)) 155 ) @Valid DataObjectReferenceIO dataObjectReference 156 ) { 157 DataObjectReference result = dataObjectReferenceService.createReference( 158 collectionId, 159 dataObjectId, 160 dataObjectReference 161 ); 162 return Response.ok(new DataObjectReferenceIO(result)).status(Status.CREATED).build(); 163 } 164 165 @DELETE 166 @Path("/{" + Constants.DATAOBJECT_REFERENCE_ID + "}") 167 @Subscribable 168 @Tag(name = Constants.DATAOBJECT_REFERENCE) 169 @Operation(description = "Delete dataObject reference") 170 @APIResponse(description = "deleted", responseCode = "204") 171 @APIResponse(responseCode = "400", description = "bad request") 172 @APIResponse(responseCode = "401", description = "not authorized") 173 @APIResponse(responseCode = "403", description = "forbidden") 174 @APIResponse(responseCode = "404", description = "not found") 175 @Parameter(name = Constants.COLLECTION_ID) 176 @Parameter(name = Constants.DATA_OBJECT_ID) 177 @Parameter(name = Constants.DATAOBJECT_REFERENCE_ID) 178 public Response deleteDataObjectReference( 179 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 180 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 181 @PathParam(Constants.DATAOBJECT_REFERENCE_ID) @NotNull @PositiveOrZero Long dataObjectReferenceId 182 ) { 183 dataObjectReferenceService.deleteReference(collectionId, dataObjectId, dataObjectReferenceId); 184 return Response.status(Status.NO_CONTENT).build(); 185 } 186 187 @GET 188 @Path("/{" + Constants.DATAOBJECT_REFERENCE_ID + "}/payload") 189 @Tag(name = Constants.DATAOBJECT_REFERENCE) 190 @Operation(description = "Get dataObject reference payload") 191 @APIResponse( 192 description = "ok", 193 responseCode = "200", 194 content = @Content(schema = @Schema(implementation = DataObjectIO.class)) 195 ) 196 @APIResponse(responseCode = "400", description = "bad request") 197 @APIResponse(responseCode = "401", description = "not authorized") 198 @APIResponse(responseCode = "403", description = "forbidden") 199 @APIResponse(responseCode = "404", description = "not found") 200 @Parameter(name = Constants.COLLECTION_ID) 201 @Parameter(name = Constants.DATA_OBJECT_ID) 202 @Parameter(name = Constants.DATAOBJECT_REFERENCE_ID) 203 @Parameter(name = Constants.VERSION_UID) 204 public Response getDataObjectReferencePayload( 205 @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId, 206 @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId, 207 @PathParam(Constants.DATAOBJECT_REFERENCE_ID) @NotNull @PositiveOrZero Long dataObjectReferenceId, 208 @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID 209 ) { 210 UUID versionUUID = null; 211 if (versionUID != null) { 212 versionUUID = UUID.fromString(versionUID); 213 } 214 DataObject payload = dataObjectReferenceService.getPayload( 215 collectionId, 216 dataObjectId, 217 dataObjectReferenceId, 218 versionUUID 219 ); 220 return Response.ok(new DataObjectIO(payload)).build(); 221 } 222 }