Search in sources :

Example 31 with FileItemIterator

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());
    }
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) JSONObject(org.json.simple.JSONObject) FileItemStream(org.apache.commons.fileupload.FileItemStream) JSONArray(org.json.simple.JSONArray) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) LinkedList(java.util.LinkedList) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) IngestException(org.opencastproject.ingest.api.IngestException) WebApplicationException(javax.ws.rs.WebApplicationException) MetadataParsingException(org.opencastproject.metadata.dublincore.MetadataParsingException) EventCommentException(org.opencastproject.event.comment.EventCommentException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException)

Example 32 with FileItemIterator

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;
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) IOException(java.io.IOException) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 33 with FileItemIterator

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);
    }
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) PropertyChangeListener(java.beans.PropertyChangeListener) WebApplicationException(javax.ws.rs.WebApplicationException) ProgressInputStream(org.opencastproject.util.ProgressInputStream) FileItemStream(org.apache.commons.fileupload.FileItemStream) NotFoundException(org.opencastproject.util.NotFoundException) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) ConfigurationException(org.osgi.service.cm.ConfigurationException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 34 with FileItemIterator

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();
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) URI(java.net.URI) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 35 with FileItemIterator

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();
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) URI(java.net.URI) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

FileItemIterator (org.apache.commons.fileupload.FileItemIterator)47 FileItemStream (org.apache.commons.fileupload.FileItemStream)45 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)37 IOException (java.io.IOException)29 InputStream (java.io.InputStream)21 FileUploadException (org.apache.commons.fileupload.FileUploadException)20 NotFoundException (org.opencastproject.util.NotFoundException)7 HashMap (java.util.HashMap)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 IngestException (org.opencastproject.ingest.api.IngestException)6 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 GZIPInputStream (java.util.zip.GZIPInputStream)5 POST (javax.ws.rs.POST)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)5 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)5 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)5