/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE file at the root of the source
* tree and available online at
*
* https://github.com/keeps/roda
*/
package org.roda.wui.api.v1;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.transform.TransformerException;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.roda.core.common.EntityResponse;
import org.roda.core.common.StreamResponse;
import org.roda.core.common.UserUtility;
import org.roda.core.data.common.RodaConstants;
import org.roda.core.data.exceptions.RODAException;
import org.roda.core.data.v2.common.Pair;
import org.roda.core.data.v2.index.IndexResult;
import org.roda.core.data.v2.index.filter.Filter;
import org.roda.core.data.v2.index.select.SelectedItemsList;
import org.roda.core.data.v2.index.sort.Sorter;
import org.roda.core.data.v2.index.sublist.Sublist;
import org.roda.core.data.v2.ip.DIPFile;
import org.roda.core.data.v2.ip.DIPFiles;
import org.roda.core.data.v2.user.User;
import org.roda.wui.api.controllers.Browser;
import org.roda.wui.api.v1.utils.ApiResponseMessage;
import org.roda.wui.api.v1.utils.ApiUtils;
import org.roda.wui.api.v1.utils.ObjectResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
@Path(DIPFilesResource.ENDPOINT)
@Api(value = DIPFilesResource.SWAGGER_ENDPOINT)
public class DIPFilesResource {
public static final String ENDPOINT = "/v1/dipfiles";
public static final String SWAGGER_ENDPOINT = "v1 dipfiles";
@Context
private HttpServletRequest request;
@GET
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@ApiOperation(value = "List DIP Files", notes = "Gets a list of DIP files.", response = DIPFiles.class, responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful response", response = DIPFiles.class, responseContainer = "List"),
@ApiResponse(code = 404, message = "Not found", response = ApiResponseMessage.class)})
public Response list(
@ApiParam(value = "Index of the first element to return", defaultValue = "0") @QueryParam(RodaConstants.API_QUERY_KEY_START) String start,
@ApiParam(value = "Maximum number of elements to return", defaultValue = RodaConstants.DEFAULT_PAGINATION_STRING_VALUE) @QueryParam(RodaConstants.API_QUERY_KEY_LIMIT) String limit,
@ApiParam(value = "Choose format in which to get the DIP file", allowableValues = RodaConstants.API_LIST_MEDIA_TYPES, defaultValue = RodaConstants.API_QUERY_VALUE_ACCEPT_FORMAT_JSON) @QueryParam(RodaConstants.API_QUERY_KEY_ACCEPT_FORMAT) String acceptFormat)
throws RODAException {
String mediaType = ApiUtils.getMediaType(acceptFormat, request);
// get user
User user = UserUtility.getApiUser(request);
// delegate action to controller
boolean justActive = false;
Pair<Integer, Integer> pagingParams = ApiUtils.processPagingParams(start, limit);
IndexResult<DIPFile> result = Browser.find(DIPFile.class, Filter.NULL, Sorter.NONE,
new Sublist(pagingParams.getFirst(), pagingParams.getSecond()), null, user, justActive, new ArrayList<>());
return Response.ok(ApiUtils.indexedResultToRODAObjectList(DIPFile.class, result), mediaType).build();
}
@GET
@Path("/{" + RodaConstants.API_PATH_PARAM_DIP_FILE_UUID + "}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_OCTET_STREAM})
@ApiOperation(value = "Get DIP file", notes = "Get DIP file", response = DIPFile.class)
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = DIPFile.class),
@ApiResponse(code = 404, message = "Not found", response = ApiResponseMessage.class)})
public Response retrieve(
@ApiParam(value = "The UUID of the existing DIP file", required = true) @PathParam(RodaConstants.API_PATH_PARAM_DIP_FILE_UUID) String dipFileUUID,
@ApiParam(value = "Choose format in which to get the file", allowableValues = RodaConstants.API_GET_FILE_MEDIA_TYPES) @QueryParam(RodaConstants.API_QUERY_KEY_ACCEPT_FORMAT) String acceptFormat)
throws RODAException {
String mediaType = ApiUtils.getMediaType(acceptFormat, request);
// get user
User user = UserUtility.getApiUser(request);
// delegate action to controller
EntityResponse efile = Browser.retrieveDIPFile(user, dipFileUUID, acceptFormat);
if (efile instanceof ObjectResponse) {
ObjectResponse<DIPFile> file = (ObjectResponse<DIPFile>) efile;
return Response.ok(file.getObject(), mediaType).build();
} else {
return ApiUtils.okResponse((StreamResponse) efile);
}
}
@PUT
@ApiOperation(value = "Update DIP file", notes = "Update existing DIP file", response = DIPFile.class)
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = DIPFile.class),
@ApiResponse(code = 404, message = "Not found", response = ApiResponseMessage.class)})
public Response update(
@ApiParam(value = "The UUID of the DIP file") @QueryParam(RodaConstants.API_PATH_PARAM_DIP_FILE_UUID) String fileUUID,
@FormDataParam(RodaConstants.API_PARAM_UPLOAD) InputStream inputStream,
@FormDataParam(RodaConstants.API_PARAM_UPLOAD) FormDataContentDisposition fileDetail,
@ApiParam(value = "A new filename to the file") @QueryParam(RodaConstants.API_QUERY_KEY_FILENAME) String filename,
@ApiParam(value = "Choose format in which to get the DIP file", allowableValues = RodaConstants.API_POST_PUT_MEDIA_TYPES) @QueryParam(RodaConstants.API_QUERY_KEY_ACCEPT_FORMAT) String acceptFormat)
throws RODAException {
String mediaType = ApiUtils.getMediaType(acceptFormat, request);
// get user
User user = UserUtility.getApiUser(request);
String name = filename == null ? fileDetail.getFileName() : filename;
long size = fileDetail.getSize();
// delegate action to controller
try {
DIPFile updatedFile = Browser.updateDIPFile(user, fileUUID, name, size == -1 ? 0 : size, inputStream);
return Response.ok(updatedFile, mediaType).build();
} catch (IOException e) {
return ApiUtils.errorResponse(new TransformerException(e.getMessage()));
}
}
@POST
@ApiOperation(value = "Create DIP file", notes = "Create a new DIP file", response = DIPFile.class)
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = DIPFile.class),
@ApiResponse(code = 409, message = "Already exists", response = ApiResponseMessage.class)})
public Response createDIPFile(
@ApiParam(value = "The ID of the DIP where to create the file") @QueryParam(RodaConstants.API_PATH_PARAM_DIP_ID) String dipId,
@ApiParam(value = "The UUID of the parent folder") @QueryParam(RodaConstants.API_PATH_PARAM_FILE_UUID) String fileUUID,
@FormDataParam(RodaConstants.API_PARAM_UPLOAD) InputStream inputStream,
@FormDataParam(RodaConstants.API_PARAM_UPLOAD) FormDataContentDisposition fileDetail,
@ApiParam(value = "A new filename to the file") @QueryParam(RodaConstants.API_QUERY_KEY_FILENAME) String filename,
@ApiParam(value = "Choose format in which to get the file", allowableValues = RodaConstants.API_POST_PUT_MEDIA_TYPES) @QueryParam(RodaConstants.API_QUERY_KEY_ACCEPT_FORMAT) String acceptFormat)
throws RODAException {
String mediaType = ApiUtils.getMediaType(acceptFormat, request);
// get user
User user = UserUtility.getApiUser(request);
// delegate action to controller
try {
DIPFile file;
String name = filename == null ? fileDetail.getFileName() : filename;
long size = fileDetail.getSize();
if (fileUUID == null) {
file = Browser.createDIPFile(user, dipId, new ArrayList<>(), name, size == -1 ? 0 : size, inputStream);
} else {
file = Browser.createDIPFileWithParentUUID(user, fileUUID, name, size == -1 ? 0 : size, inputStream);
}
return Response.ok(file, mediaType).build();
} catch (IOException e) {
return ApiUtils.errorResponse(new TransformerException(e.getMessage()));
}
}
@DELETE
@Path("/{" + RodaConstants.API_PATH_PARAM_DIP_FILE_UUID + "}")
@ApiOperation(value = "Delete DIP file", notes = "Delete DIP file", response = Void.class)
@ApiResponses(value = {@ApiResponse(code = 204, message = "OK", response = Void.class),
@ApiResponse(code = 404, message = "Not found", response = ApiResponseMessage.class)})
public Response delete(
@ApiParam(value = "The UUID of the existing DIP file", required = true) @PathParam(RodaConstants.API_PATH_PARAM_DIP_FILE_UUID) String fileUUID,
@ApiParam(value = "Choose format in which to get the response", allowableValues = RodaConstants.API_DELETE_MEDIA_TYPES) @QueryParam(RodaConstants.API_QUERY_KEY_ACCEPT_FORMAT) String acceptFormat)
throws RODAException {
String mediaType = ApiUtils.getMediaType(acceptFormat, request);
// get user
User user = UserUtility.getApiUser(request);
// delegate action to controller
SelectedItemsList<DIPFile> list = SelectedItemsList.create(DIPFile.class, java.util.Arrays.asList(fileUUID));
Browser.deleteDIPFile(user, list);
return Response.ok(new ApiResponseMessage(ApiResponseMessage.OK, "DIP File deleted"), mediaType).build();
}
}