use of org.apache.commons.fileupload.FileItemIterator in project opencast by opencast.
the class IndexServiceImpl method updateEventAssets.
@Override
public String updateEventAssets(MediaPackage mp, HttpServletRequest request) throws IndexServiceException {
JSONObject metadataJson = null;
// regex for form field name matching an attachment or a catalog
// The first sub items identifies if the file is an attachment or catalog
// The second is the item flavor
// Example form field names: "catalog/captions/timedtext" and "attachment/captions/vtt"
// The prefix of field name for attachment and catalog
// The metadata is expected to contain a workflow definition id and
// asset metadata mapped to the asset field id.
List<String> assetList = new LinkedList<String>();
// 1. save assets with temporary flavors
try {
if (!ServletFileUpload.isMultipartContent(request)) {
throw new IllegalArgumentException("No multipart content");
}
for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
FileItemStream item = iter.next();
String fieldName = item.getFieldName();
if (item.isFormField()) {
if ("metadata".equals(fieldName)) {
String metadata = Streams.asString(item.openStream());
try {
metadataJson = (JSONObject) parser.parse(metadata);
} catch (Exception e) {
logger.warn("Unable to parse metadata {}", metadata);
throw new IllegalArgumentException("Unable to parse metadata");
}
}
} else {
if (item.getFieldName().toLowerCase().matches(attachmentRegex)) {
assetList.add(item.getFieldName());
// Add attachment with field name as temporary flavor
mp = ingestService.addAttachment(item.openStream(), item.getName(), new MediaPackageElementFlavor(item.getFieldName(), "*"), mp);
} else if (item.getFieldName().toLowerCase().matches(catalogRegex)) {
assetList.add(item.getFieldName());
// Add catalog with field name as temporary flavor
mp = ingestService.addCatalog(item.openStream(), item.getName(), new MediaPackageElementFlavor(item.getFieldName(), "*"), mp);
} else {
logger.warn("Unknown field name found {}", item.getFieldName());
}
}
}
// and correct the temporary flavor to the new flavor.
try {
JSONArray assetMetadata = (JSONArray) ((JSONObject) metadataJson.get("assets")).get("options");
if (assetMetadata != null) {
mp = updateMpAssetFlavor(assetList, mp, assetMetadata, isOverwriteExistingAsset);
} else {
logger.warn("The asset option mapping parameter was not found");
throw new IndexServiceException("The asset option mapping parameter was not found");
}
} catch (Exception e) {
// Assuming a parse error versus a file error and logging the error type
logger.warn("Unable to process asset metadata {}", metadataJson.get("assets"), e);
throw new IllegalArgumentException("Unable to parse metadata", e);
}
return startAddAssetWorkflow(metadataJson, mp);
} catch (Exception e) {
logger.error("Unable to create event: {}", getStackTrace(e));
throw new IndexServiceException(e.getMessage());
}
}
use of org.apache.commons.fileupload.FileItemIterator in project opencast by opencast.
the class EventHttpServletRequest method updateFromHttpServletRequest.
/**
* Load the details of updating an event.
*
* @param event
* The event to update.
* @param request
* The multipart request that has the data to load the updated event.
* @param eventCatalogUIAdapters
* The list of catalog ui adapters to use to load the event metadata.
* @param startDatePattern
* The pattern to use to parse the start date from the request.
* @param startTimePattern
* The pattern to use to parse the start time from the request.
* @return The data for the event update
* @throws IllegalArgumentException
* Thrown if the request to update the event is malformed.
* @throws IndexServiceException
* Thrown if something is unable to load the event data.
* @throws NotFoundException
* Thrown if unable to find a metadata catalog or field that matches an input catalog or field.
*/
public static EventHttpServletRequest updateFromHttpServletRequest(Event event, HttpServletRequest request, List<EventCatalogUIAdapter> eventCatalogUIAdapters, Opt<String> startDatePattern, Opt<String> startTimePattern) throws IllegalArgumentException, IndexServiceException, NotFoundException {
EventHttpServletRequest eventHttpServletRequest = new EventHttpServletRequest();
if (ServletFileUpload.isMultipartContent(request)) {
try {
for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
FileItemStream item = iter.next();
String fieldName = item.getFieldName();
if (item.isFormField()) {
setFormField(eventCatalogUIAdapters, eventHttpServletRequest, item, fieldName, startDatePattern, startTimePattern);
}
}
} catch (IOException e) {
throw new IndexServiceException("Unable to update event", e);
} catch (FileUploadException e) {
throw new IndexServiceException("Unable to update event", e);
}
} else {
throw new IllegalArgumentException("No multipart content");
}
return eventHttpServletRequest;
}
use of org.apache.commons.fileupload.FileItemIterator in project opencast by opencast.
the class StaticFileRestService method postStaticFile.
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Path("")
@RestQuery(name = "postStaticFile", description = "Post a new static resource", bodyParameter = @RestParameter(description = "The static resource file", isRequired = true, name = "BODY", type = RestParameter.Type.FILE), reponses = { @RestResponse(description = "Returns the id of the uploaded static resource", responseCode = HttpServletResponse.SC_CREATED), @RestResponse(description = "No filename or file to upload found", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "The upload size is too big", responseCode = HttpServletResponse.SC_BAD_REQUEST) }, returnDescription = "")
public Response postStaticFile(@Context HttpServletRequest request) {
if (maxUploadSize > 0 && request.getContentLength() > maxUploadSize) {
logger.warn("Preventing upload of static file as its size {} is larger than the max size allowed {}", request.getContentLength(), maxUploadSize);
return Response.status(Status.BAD_REQUEST).build();
}
ProgressInputStream inputStream = null;
try {
String filename = null;
if (ServletFileUpload.isMultipartContent(request)) {
boolean isDone = false;
for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
FileItemStream item = iter.next();
if (item.isFormField()) {
continue;
} else {
logger.debug("Processing file item");
filename = item.getName();
inputStream = new ProgressInputStream(item.openStream());
inputStream.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
long totalNumBytesRead = (Long) evt.getNewValue();
if (totalNumBytesRead > maxUploadSize) {
logger.warn("Upload limit of {} bytes reached, returning a bad request.", maxUploadSize);
throw new WebApplicationException(Status.BAD_REQUEST);
}
}
});
isDone = true;
}
if (isDone)
break;
}
} else {
logger.warn("Request is not multi part request, returning a bad request.");
return Response.status(Status.BAD_REQUEST).build();
}
if (filename == null) {
logger.warn("Request was missing the filename, returning a bad request.");
return Response.status(Status.BAD_REQUEST).build();
}
if (inputStream == null) {
logger.warn("Request was missing the file, returning a bad request.");
return Response.status(Status.BAD_REQUEST).build();
}
String uuid = staticFileService.storeFile(filename, inputStream);
try {
return Response.created(getStaticFileURL(uuid)).entity(uuid).build();
} catch (NotFoundException e) {
logger.error("Previous stored file with uuid {} couldn't beren found: {}", uuid, ExceptionUtils.getStackTrace(e));
return Response.serverError().build();
}
} catch (WebApplicationException e) {
return e.getResponse();
} catch (Exception e) {
logger.error("Unable to store file because: {}", ExceptionUtils.getStackTrace(e));
return Response.serverError().build();
} finally {
IOUtils.closeQuietly(inputStream);
}
}
use of org.apache.commons.fileupload.FileItemIterator in project opencast by opencast.
the class WorkingFileRepositoryRestEndpoint method restPutInCollection.
@POST
@Produces(MediaType.TEXT_HTML)
@Path(WorkingFileRepository.COLLECTION_PATH_PREFIX + "{collectionId}")
@RestQuery(name = "putInCollection", description = "Store a file in working repository under ./collectionId/filename", returnDescription = "The URL to access the stored file", pathParameters = { @RestParameter(name = "collectionId", description = "the colection identifier", isRequired = true, type = STRING) }, restParameters = { @RestParameter(name = "file", description = "the filename", isRequired = true, type = FILE) }, reponses = { @RestResponse(responseCode = SC_OK, description = "OK, file stored") })
public Response restPutInCollection(@PathParam("collectionId") String collectionId, @Context HttpServletRequest request) throws Exception {
if (ServletFileUpload.isMultipartContent(request)) {
for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
FileItemStream item = iter.next();
if (item.isFormField()) {
continue;
}
URI url = this.putInCollection(collectionId, item.getName(), item.openStream());
return Response.ok(url.toString()).build();
}
}
return Response.serverError().status(400).build();
}
use of org.apache.commons.fileupload.FileItemIterator in project opencast by opencast.
the class WorkingFileRepositoryRestEndpoint method restPut.
@POST
@Produces(MediaType.TEXT_HTML)
@Path(WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX + "{mediaPackageID}/{mediaPackageElementID}")
@RestQuery(name = "put", description = "Store a file in working repository under ./mediaPackageID/mediaPackageElementID", returnDescription = "The URL to access the stored file", pathParameters = { @RestParameter(name = "mediaPackageID", description = "the mediapackage identifier", isRequired = true, type = STRING), @RestParameter(name = "mediaPackageElementID", description = "the mediapackage element identifier", isRequired = true, type = STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "OK, file stored") }, restParameters = { @RestParameter(name = "file", description = "the filename", isRequired = true, type = FILE) })
public Response restPut(@PathParam("mediaPackageID") String mediaPackageID, @PathParam("mediaPackageElementID") String mediaPackageElementID, @Context HttpServletRequest request) throws Exception {
if (ServletFileUpload.isMultipartContent(request)) {
for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
FileItemStream item = iter.next();
if (item.isFormField()) {
continue;
}
URI url = this.put(mediaPackageID, mediaPackageElementID, item.getName(), item.openStream());
return Response.ok(url.toString()).build();
}
}
return Response.serverError().status(400).build();
}
Aggregations