1 package de.dlr.shepard.endpoints; 2 3 import java.io.InputStream; 4 import java.util.ArrayList; 5 6 import org.glassfish.jersey.media.multipart.FormDataContentDisposition; 7 import org.glassfish.jersey.media.multipart.FormDataParam; 8 9 import de.dlr.shepard.filters.Subscribable; 10 import de.dlr.shepard.neo4Core.io.FileContainerIO; 11 import de.dlr.shepard.neo4Core.io.PermissionsIO; 12 import de.dlr.shepard.neo4Core.orderBy.ContainerAttributes; 13 import de.dlr.shepard.neo4Core.services.FileContainerService; 14 import de.dlr.shepard.neo4Core.services.PermissionsService; 15 import de.dlr.shepard.security.PermissionsUtil; 16 import de.dlr.shepard.util.Constants; 17 import de.dlr.shepard.util.QueryParamHelper; 18 import jakarta.validation.Valid; 19 import jakarta.ws.rs.Consumes; 20 import jakarta.ws.rs.DELETE; 21 import jakarta.ws.rs.GET; 22 import jakarta.ws.rs.POST; 23 import jakarta.ws.rs.PUT; 24 import jakarta.ws.rs.Path; 25 import jakarta.ws.rs.PathParam; 26 import jakarta.ws.rs.Produces; 27 import jakarta.ws.rs.QueryParam; 28 import jakarta.ws.rs.core.Context; 29 import jakarta.ws.rs.core.MediaType; 30 import jakarta.ws.rs.core.Response; 31 import jakarta.ws.rs.core.Response.Status; 32 import jakarta.ws.rs.core.SecurityContext; 33 34 @Consumes(MediaType.APPLICATION_JSON) 35 @Produces(MediaType.APPLICATION_JSON) 36 @Path(Constants.FILES) 37 public class FileRestImpl implements FileRest { 38 private FileContainerService fileContainerService = new FileContainerService(); 39 private PermissionsService permissionsService = new PermissionsService(); 40 41 @Context 42 private SecurityContext securityContext; 43 44 @GET 45 @Override 46 public Response getAllFileContainers(@QueryParam(Constants.QP_NAME) String name, 47 @QueryParam(Constants.QP_PAGE) Integer page, @QueryParam(Constants.QP_SIZE) Integer size, 48 @QueryParam(Constants.QP_ORDER_BY_ATTRIBUTE) ContainerAttributes orderBy, 49 @QueryParam(Constants.QP_ORDER_DESC) Boolean orderDesc) { 50 var params = new QueryParamHelper(); 51 if (name != null) 52 params = params.withName(name); 53 if (page != null && size != null) 54 params = params.withPageAndSize(page, size); 55 if (orderBy != null) 56 params = params.withOrderByAttribute(orderBy, orderDesc); 57 var containers = fileContainerService.getAllContainers(params, securityContext.getUserPrincipal().getName()); 58 var result = new ArrayList<FileContainerIO>(containers.size()); 59 for (var container : containers) { 60 result.add(new FileContainerIO(container)); 61 } 62 return Response.ok(result).build(); 63 } 64 65 @GET 66 @Path("/{" + Constants.FILE_CONTAINER_ID + "}") 67 @Override 68 public Response getFileContainer(@PathParam(Constants.FILE_CONTAINER_ID) long fileContainerId) { 69 var result = fileContainerService.getContainer(fileContainerId); 70 return Response.ok(new FileContainerIO(result)).build(); 71 } 72 73 @POST 74 @Override 75 public Response createFileContainer(FileContainerIO fileContainer) { 76 var result = fileContainerService.createContainer(fileContainer, securityContext.getUserPrincipal().getName()); 77 return Response.ok(new FileContainerIO(result)).status(Status.CREATED).build(); 78 } 79 80 @DELETE 81 @Path("/{" + Constants.FILE_CONTAINER_ID + "}") 82 @Subscribable 83 @Override 84 public Response deleteFileContainer(@PathParam(Constants.FILE_CONTAINER_ID) long fileContainerId) { 85 var result = fileContainerService.deleteContainer(fileContainerId, 86 securityContext.getUserPrincipal().getName()); 87 return result ? Response.status(Status.NO_CONTENT).build() 88 : Response.status(Status.INTERNAL_SERVER_ERROR).build(); 89 } 90 91 @GET 92 @Path("/{" + Constants.FILE_CONTAINER_ID + "}/payload") 93 @Override 94 public Response getAllFiles(@PathParam(Constants.FILE_CONTAINER_ID) long fileContainerId) { 95 var payload = fileContainerService.getContainer(fileContainerId).getFiles(); 96 return Response.ok(payload).build(); 97 } 98 99 @GET 100 @Path("/{" + Constants.FILE_CONTAINER_ID + "}/payload/{" + Constants.OID + "}") 101 @Produces({ MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_JSON }) 102 @Override 103 public Response getFile(@PathParam(Constants.FILE_CONTAINER_ID) long fileContainerId, 104 @PathParam(Constants.OID) String oid) { 105 var payload = fileContainerService.getFile(fileContainerId, oid); 106 return payload != null 107 ? Response.ok(payload.getInputStream(), MediaType.APPLICATION_OCTET_STREAM) 108 .header("Content-Disposition", "attachment; filename=\"" + payload.getName() + "\"") 109 .header("Content-Length", payload.getSize()).build() 110 : Response.status(Status.NOT_FOUND).build(); 111 } 112 113 @DELETE 114 @Path("/{" + Constants.FILE_CONTAINER_ID + "}/payload/{" + Constants.OID + "}") 115 @Subscribable 116 @Override 117 public Response deleteFile(@PathParam(Constants.FILE_CONTAINER_ID) long fileContainerId, 118 @PathParam(Constants.OID) String oid) { 119 var result = fileContainerService.deleteFile(fileContainerId, oid); 120 return result ? Response.status(Status.NO_CONTENT).build() 121 : Response.status(Status.INTERNAL_SERVER_ERROR).build(); 122 } 123 124 @POST 125 @Consumes(MediaType.MULTIPART_FORM_DATA) 126 @Path("/{" + Constants.FILE_CONTAINER_ID + "}/payload") 127 @Subscribable 128 @Override 129 public Response createFile(@PathParam(Constants.FILE_CONTAINER_ID) long fileContainerId, 130 @FormDataParam(Constants.FILE) InputStream fileInputStream, 131 @FormDataParam(Constants.FILE) FormDataContentDisposition fileMetaData) { 132 String fileName = fileMetaData != null ? fileMetaData.getFileName() : null; 133 var result = fileContainerService.createFile(fileContainerId, fileName, fileInputStream); 134 return result != null ? Response.status(Status.CREATED).entity(result).build() 135 : Response.status(Status.INTERNAL_SERVER_ERROR).build(); 136 } 137 138 @GET 139 @Path("/{" + Constants.FILE_CONTAINER_ID + "}/" + Constants.PERMISSIONS) 140 @Override 141 public Response getFilePermissions(@PathParam(Constants.FILE_CONTAINER_ID) long fileContainerId) { 142 var perms = permissionsService.getPermissionsByEntity(fileContainerId); 143 return perms != null ? Response.ok(new PermissionsIO(perms)).build() 144 : Response.status(Status.NOT_FOUND).build(); 145 } 146 147 @PUT 148 @Path("/{" + Constants.FILE_CONTAINER_ID + "}/" + Constants.PERMISSIONS) 149 @Override 150 public Response editFilePermissions(@PathParam(Constants.FILE_CONTAINER_ID) long fileContainerId, 151 @Valid PermissionsIO permissions) { 152 var perms = permissionsService.updatePermissions(permissions, fileContainerId); 153 return perms != null ? Response.ok(new PermissionsIO(perms)).build() 154 : Response.status(Status.NOT_FOUND).build(); 155 } 156 157 @GET 158 @Path("/{" + Constants.FILE_CONTAINER_ID + "}/" + Constants.ROLES) 159 @Override 160 public Response getFileRoles(@PathParam(Constants.FILE_CONTAINER_ID) long fileContainerId) { 161 var roles = new PermissionsUtil().getRoles(fileContainerId, securityContext.getUserPrincipal().getName()); 162 return roles != null ? Response.ok(roles).build() : Response.status(Status.NOT_FOUND).build(); 163 } 164 165 }