1 package de.dlr.shepard.endpoints; 2 3 import java.util.ArrayList; 4 5 import de.dlr.shepard.filters.Subscribable; 6 import de.dlr.shepard.mongoDB.StructuredDataPayload; 7 import de.dlr.shepard.neo4Core.io.PermissionsIO; 8 import de.dlr.shepard.neo4Core.io.StructuredDataContainerIO; 9 import de.dlr.shepard.neo4Core.orderBy.ContainerAttributes; 10 import de.dlr.shepard.neo4Core.services.PermissionsService; 11 import de.dlr.shepard.neo4Core.services.StructuredDataContainerService; 12 import de.dlr.shepard.security.PermissionsUtil; 13 import de.dlr.shepard.util.Constants; 14 import de.dlr.shepard.util.QueryParamHelper; 15 import jakarta.validation.Valid; 16 import jakarta.ws.rs.Consumes; 17 import jakarta.ws.rs.DELETE; 18 import jakarta.ws.rs.GET; 19 import jakarta.ws.rs.POST; 20 import jakarta.ws.rs.PUT; 21 import jakarta.ws.rs.Path; 22 import jakarta.ws.rs.PathParam; 23 import jakarta.ws.rs.Produces; 24 import jakarta.ws.rs.QueryParam; 25 import jakarta.ws.rs.core.Context; 26 import jakarta.ws.rs.core.MediaType; 27 import jakarta.ws.rs.core.Response; 28 import jakarta.ws.rs.core.Response.Status; 29 import jakarta.ws.rs.core.SecurityContext; 30 31 @Consumes(MediaType.APPLICATION_JSON) 32 @Produces(MediaType.APPLICATION_JSON) 33 @Path(Constants.STRUCTUREDDATAS) 34 public class StructuredDataRestImpl implements StructuredDataRest { 35 private StructuredDataContainerService structuredDataContainerService = new StructuredDataContainerService(); 36 private PermissionsService permissionsService = new PermissionsService(); 37 38 @Context 39 private SecurityContext securityContext; 40 41 @GET 42 @Override 43 public Response getAllStructuredDataContainers(@QueryParam(Constants.QP_NAME) String name, 44 @QueryParam(Constants.QP_PAGE) Integer page, @QueryParam(Constants.QP_SIZE) Integer size, 45 @QueryParam(Constants.QP_ORDER_BY_ATTRIBUTE) ContainerAttributes orderBy, 46 @QueryParam(Constants.QP_ORDER_DESC) Boolean orderDesc) { 47 var params = new QueryParamHelper(); 48 if (name != null) 49 params = params.withName(name); 50 if (page != null && size != null) 51 params = params.withPageAndSize(page, size); 52 if (orderBy != null) 53 params = params.withOrderByAttribute(orderBy, orderDesc); 54 var containers = structuredDataContainerService.getAllContainers(params, 55 securityContext.getUserPrincipal().getName()); 56 var result = new ArrayList<StructuredDataContainerIO>(containers.size()); 57 for (var container : containers) { 58 result.add(new StructuredDataContainerIO(container)); 59 } 60 return Response.ok(result).build(); 61 } 62 63 @GET 64 @Path("/{" + Constants.STRUCTUREDDATA_CONTAINER_ID + "}") 65 @Override 66 public Response getStructuredDataContainer( 67 @PathParam(Constants.STRUCTUREDDATA_CONTAINER_ID) long structuredDataId) { 68 var result = structuredDataContainerService.getContainer(structuredDataId); 69 return Response.ok(new StructuredDataContainerIO(result)).build(); 70 } 71 72 @DELETE 73 @Path("/{" + Constants.STRUCTUREDDATA_CONTAINER_ID + "}") 74 @Subscribable 75 @Override 76 public Response deleteStructuredDataContainer( 77 @PathParam(Constants.STRUCTUREDDATA_CONTAINER_ID) long structuredDataId) { 78 var result = structuredDataContainerService.deleteContainer(structuredDataId, 79 securityContext.getUserPrincipal().getName()); 80 return result ? Response.status(Status.NO_CONTENT).build() 81 : Response.status(Status.INTERNAL_SERVER_ERROR).build(); 82 } 83 84 @POST 85 @Override 86 public Response createStructuredDataContainer(StructuredDataContainerIO structuredDataContainerIO) { 87 var result = structuredDataContainerService.createContainer(structuredDataContainerIO, 88 securityContext.getUserPrincipal().getName()); 89 return Response.ok(new StructuredDataContainerIO(result)).status(Status.CREATED).build(); 90 } 91 92 @POST 93 @Path("/{" + Constants.STRUCTUREDDATA_CONTAINER_ID + "}/payload") 94 @Subscribable 95 @Override 96 public Response createStructuredData(@PathParam(Constants.STRUCTUREDDATA_CONTAINER_ID) long structuredDataId, 97 StructuredDataPayload payload) { 98 var result = structuredDataContainerService.createStructuredData(structuredDataId, payload); 99 return result != null ? Response.status(Status.CREATED).entity(result).build() 100 : Response.status(Status.INTERNAL_SERVER_ERROR).build(); 101 } 102 103 @GET 104 @Path("/{" + Constants.STRUCTUREDDATA_CONTAINER_ID + "}/payload") 105 @Override 106 public Response getAllStructuredDatas(@PathParam(Constants.STRUCTUREDDATA_CONTAINER_ID) long structuredDataId) { 107 var result = structuredDataContainerService.getContainer(structuredDataId).getStructuredDatas(); 108 return Response.ok(result).build(); 109 } 110 111 @GET 112 @Path("/{" + Constants.STRUCTUREDDATA_CONTAINER_ID + "}/payload/{" + Constants.OID + "}") 113 @Override 114 public Response getStructuredData(@PathParam(Constants.STRUCTUREDDATA_CONTAINER_ID) long structuredDataId, 115 @PathParam(Constants.OID) String oid) { 116 var result = structuredDataContainerService.getStructuredData(structuredDataId, oid); 117 return result != null ? Response.ok(result).build() : Response.status(Status.NOT_FOUND).build(); 118 } 119 120 @DELETE 121 @Path("/{" + Constants.STRUCTUREDDATA_CONTAINER_ID + "}/payload/{" + Constants.OID + "}") 122 @Subscribable 123 @Override 124 public Response deleteStructuredData(@PathParam(Constants.STRUCTUREDDATA_CONTAINER_ID) long structuredDataId, 125 @PathParam(Constants.OID) String oid) { 126 var result = structuredDataContainerService.deleteStructuredData(structuredDataId, oid); 127 return result ? Response.status(Status.NO_CONTENT).build() 128 : Response.status(Status.INTERNAL_SERVER_ERROR).build(); 129 } 130 131 @GET 132 @Path("/{" + Constants.STRUCTUREDDATA_CONTAINER_ID + "}/" + Constants.PERMISSIONS) 133 @Override 134 public Response getStructuredDataPermissions( 135 @PathParam(Constants.STRUCTUREDDATA_CONTAINER_ID) long structuredDataId) { 136 var perms = permissionsService.getPermissionsByEntity(structuredDataId); 137 return perms != null ? Response.ok(new PermissionsIO(perms)).build() 138 : Response.status(Status.NOT_FOUND).build(); 139 } 140 141 @PUT 142 @Path("/{" + Constants.STRUCTUREDDATA_CONTAINER_ID + "}/" + Constants.PERMISSIONS) 143 @Override 144 public Response editStructuredDataPermissions( 145 @PathParam(Constants.STRUCTUREDDATA_CONTAINER_ID) long structuredDataId, @Valid PermissionsIO permissions) { 146 var perms = permissionsService.updatePermissions(permissions, structuredDataId); 147 return perms != null ? Response.ok(new PermissionsIO(perms)).build() 148 : Response.status(Status.NOT_FOUND).build(); 149 } 150 151 @GET 152 @Path("/{" + Constants.STRUCTUREDDATA_CONTAINER_ID + "}/" + Constants.ROLES) 153 @Override 154 public Response getStructuredDataRoles(@PathParam(Constants.STRUCTUREDDATA_CONTAINER_ID) long structuredDataId) { 155 var roles = new PermissionsUtil().getRoles(structuredDataId, securityContext.getUserPrincipal().getName()); 156 return roles != null ? Response.ok(roles).build() : Response.status(Status.NOT_FOUND).build(); 157 } 158 159 }