View Javadoc
1   package de.dlr.shepard.context.references.structureddata.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.structureddata.entities.StructuredDataReference;
6   import de.dlr.shepard.context.references.structureddata.io.StructuredDataReferenceIO;
7   import de.dlr.shepard.context.references.structureddata.services.StructuredDataReferenceService;
8   import de.dlr.shepard.data.structureddata.entities.StructuredDataPayload;
9   import jakarta.enterprise.context.RequestScoped;
10  import jakarta.inject.Inject;
11  import jakarta.validation.Valid;
12  import jakarta.validation.constraints.NotBlank;
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.STRUCTURED_DATA_REFERENCES
52  )
53  @RequestScoped
54  public class StructuredDataReferenceRest {
55  
56    @Inject
57    StructuredDataReferenceService structuredDataReferenceService;
58  
59    @Context
60    private SecurityContext securityContext;
61  
62    @GET
63    @Tag(name = Constants.STRUCTURED_DATA_REFERENCE)
64    @Operation(description = "Get all structuredData references")
65    @APIResponse(
66      description = "ok",
67      responseCode = "200",
68      content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = StructuredDataReferenceIO.class))
69    )
70    @APIResponse(responseCode = "400", description = "bad request")
71    @APIResponse(responseCode = "401", description = "not authorized")
72    @APIResponse(responseCode = "404", description = "not found")
73    @Parameter(name = Constants.COLLECTION_ID)
74    @Parameter(name = Constants.DATA_OBJECT_ID)
75    @Parameter(name = Constants.VERSION_UID)
76    public Response getAllStructuredDataReferences(
77      @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
78      @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
79      @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID
80    ) {
81      UUID versionUUID = null;
82      if (versionUID != null) {
83        versionUUID = UUID.fromString(versionUID);
84      }
85      var references = structuredDataReferenceService.getAllReferencesByDataObjectId(
86        collectionId,
87        dataObjectId,
88        versionUUID
89      );
90      List<StructuredDataReferenceIO> result = new ArrayList<StructuredDataReferenceIO>(references.size());
91      for (var ref : references) {
92        result.add(new StructuredDataReferenceIO(ref));
93      }
94      return Response.ok(result).build();
95    }
96  
97    @GET
98    @Path("/{" + Constants.STRUCTURED_DATA_REFERENCE_ID + "}")
99    @Tag(name = Constants.STRUCTURED_DATA_REFERENCE)
100   @Operation(description = "Get structuredData reference")
101   @APIResponse(
102     description = "ok",
103     responseCode = "200",
104     content = @Content(schema = @Schema(implementation = StructuredDataReferenceIO.class))
105   )
106   @APIResponse(responseCode = "400", description = "bad request")
107   @APIResponse(responseCode = "401", description = "not authorized")
108   @APIResponse(responseCode = "403", description = "forbidden")
109   @APIResponse(responseCode = "404", description = "not found")
110   @Parameter(name = Constants.COLLECTION_ID)
111   @Parameter(name = Constants.DATA_OBJECT_ID)
112   @Parameter(name = Constants.STRUCTURED_DATA_REFERENCE_ID)
113   @Parameter(name = Constants.VERSION_UID)
114   public Response getStructuredDataReference(
115     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
116     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
117     @PathParam(Constants.STRUCTURED_DATA_REFERENCE_ID) @NotNull @PositiveOrZero Long referenceId,
118     @QueryParam(Constants.VERSION_UID) @org.hibernate.validator.constraints.UUID String versionUID
119   ) {
120     UUID versionUUID = null;
121     if (versionUID != null) {
122       versionUUID = UUID.fromString(versionUID);
123     }
124     StructuredDataReference ref = structuredDataReferenceService.getReference(
125       collectionId,
126       dataObjectId,
127       referenceId,
128       versionUUID
129     );
130     return Response.ok(new StructuredDataReferenceIO(ref)).build();
131   }
132 
133   @POST
134   @Subscribable
135   @Tag(name = Constants.STRUCTURED_DATA_REFERENCE)
136   @Operation(description = "Create a new structuredData reference")
137   @APIResponse(
138     description = "created",
139     responseCode = "201",
140     content = @Content(schema = @Schema(implementation = StructuredDataReferenceIO.class))
141   )
142   @APIResponse(responseCode = "400", description = "bad request")
143   @APIResponse(responseCode = "401", description = "not authorized")
144   @APIResponse(responseCode = "404", description = "not found")
145   @Parameter(name = Constants.COLLECTION_ID)
146   @Parameter(name = Constants.DATA_OBJECT_ID)
147   public Response createStructuredDataReference(
148     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
149     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
150     @RequestBody(
151       required = true,
152       content = @Content(schema = @Schema(implementation = StructuredDataReferenceIO.class))
153     ) @Valid StructuredDataReferenceIO structuredDataReference
154   ) {
155     StructuredDataReference ref = structuredDataReferenceService.createReference(
156       collectionId,
157       dataObjectId,
158       structuredDataReference
159     );
160     return Response.ok(new StructuredDataReferenceIO(ref)).status(Status.CREATED).build();
161   }
162 
163   @DELETE
164   @Path("/{" + Constants.STRUCTURED_DATA_REFERENCE_ID + "}")
165   @Subscribable
166   @Tag(name = Constants.STRUCTURED_DATA_REFERENCE)
167   @Operation(description = "Delete structuredData reference")
168   @APIResponse(description = "deleted", responseCode = "204")
169   @APIResponse(responseCode = "400", description = "bad request")
170   @APIResponse(responseCode = "401", description = "not authorized")
171   @APIResponse(responseCode = "403", description = "forbidden")
172   @APIResponse(responseCode = "404", description = "not found")
173   @Parameter(name = Constants.COLLECTION_ID)
174   @Parameter(name = Constants.DATA_OBJECT_ID)
175   @Parameter(name = Constants.STRUCTURED_DATA_REFERENCE_ID)
176   public Response deleteStructuredDataReference(
177     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
178     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
179     @PathParam(Constants.STRUCTURED_DATA_REFERENCE_ID) @NotNull @PositiveOrZero Long structuredDataReferenceId
180   ) {
181     structuredDataReferenceService.deleteReference(collectionId, dataObjectId, structuredDataReferenceId);
182     return Response.status(Status.NO_CONTENT).build();
183   }
184 
185   @GET
186   @Path("/{" + Constants.STRUCTURED_DATA_REFERENCE_ID + "}/payload")
187   @Tag(name = Constants.STRUCTURED_DATA_REFERENCE)
188   @Operation(description = "Get structured data payload")
189   @APIResponse(
190     description = "ok",
191     responseCode = "200",
192     content = @Content(schema = @Schema(type = SchemaType.ARRAY, implementation = StructuredDataPayload.class))
193   )
194   @APIResponse(responseCode = "400", description = "bad request")
195   @APIResponse(responseCode = "401", description = "not authorized")
196   @APIResponse(responseCode = "403", description = "forbidden")
197   @APIResponse(responseCode = "404", description = "not found")
198   @Parameter(name = Constants.COLLECTION_ID)
199   @Parameter(name = Constants.DATA_OBJECT_ID)
200   @Parameter(name = Constants.STRUCTURED_DATA_REFERENCE_ID)
201   public Response getStructuredDataPayload(
202     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
203     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
204     @PathParam(Constants.STRUCTURED_DATA_REFERENCE_ID) @NotNull @PositiveOrZero Long structuredDataId
205   ) {
206     List<StructuredDataPayload> payload = structuredDataReferenceService.getAllPayloads(
207       collectionId,
208       dataObjectId,
209       structuredDataId
210     );
211     return Response.ok(payload).build();
212   }
213 
214   @GET
215   @Path("/{" + Constants.STRUCTURED_DATA_REFERENCE_ID + "}/payload/{" + Constants.OID + "}")
216   @Tag(name = Constants.STRUCTURED_DATA_REFERENCE)
217   @Operation(description = "Get a specific structured data payload")
218   @APIResponse(
219     description = "ok",
220     responseCode = "200",
221     content = @Content(schema = @Schema(implementation = StructuredDataPayload.class))
222   )
223   @APIResponse(responseCode = "400", description = "bad request")
224   @APIResponse(responseCode = "401", description = "not authorized")
225   @APIResponse(responseCode = "403", description = "forbidden")
226   @APIResponse(responseCode = "404", description = "not found")
227   @Parameter(name = Constants.COLLECTION_ID)
228   @Parameter(name = Constants.DATA_OBJECT_ID)
229   @Parameter(name = Constants.STRUCTURED_DATA_CONTAINER_ID)
230   @Parameter(name = Constants.OID)
231   public Response getSpecificStructuredDataPayload(
232     @PathParam(Constants.COLLECTION_ID) @NotNull @PositiveOrZero Long collectionId,
233     @PathParam(Constants.DATA_OBJECT_ID) @NotNull @PositiveOrZero Long dataObjectId,
234     @PathParam(Constants.STRUCTURED_DATA_REFERENCE_ID) @NotNull @PositiveOrZero Long structuredDataId,
235     @PathParam(Constants.OID) @NotBlank String oid
236   ) {
237     StructuredDataPayload payload = structuredDataReferenceService.getPayload(
238       collectionId,
239       dataObjectId,
240       structuredDataId,
241       oid
242     );
243     return Response.ok(payload).build();
244   }
245 }