Search in sources :

Example 1 with FileUploadJob

use of org.opencastproject.fileupload.api.job.FileUploadJob in project opencast by opencast.

the class FileUploadRestService method getNewJob.

// </editor-fold>
@POST
@Produces(MediaType.TEXT_PLAIN)
@Path("newjob")
@RestQuery(name = "newjob", description = "Creates a new upload job and returns the jobs ID.", restParameters = { @RestParameter(description = "The name of the file that will be uploaded", isRequired = false, name = REQUESTFIELD_FILENAME, type = RestParameter.Type.STRING), @RestParameter(description = "The size of the file that will be uploaded", isRequired = false, name = REQUESTFIELD_FILESIZE, type = RestParameter.Type.STRING), @RestParameter(description = "The size of the chunks that will be uploaded", isRequired = false, name = REQUESTFIELD_CHUNKSIZE, type = RestParameter.Type.STRING), @RestParameter(description = "The flavor of this track", isRequired = false, name = REQUESTFIELD_FLAVOR, type = RestParameter.Type.STRING), @RestParameter(description = "The mediapackage the file should belong to", isRequired = false, name = REQUESTFIELD_MEDIAPACKAGE, type = RestParameter.Type.TEXT) }, reponses = { @RestResponse(description = "job was successfully created", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "upload service gave an error", responseCode = HttpServletResponse.SC_NO_CONTENT) }, returnDescription = "The ID of the newly created upload job")
public Response getNewJob(@FormParam(REQUESTFIELD_FILENAME) String filename, @FormParam(REQUESTFIELD_FILESIZE) long filesize, @FormParam(REQUESTFIELD_CHUNKSIZE) int chunksize, @FormParam(REQUESTFIELD_MEDIAPACKAGE) String mediapackage, @FormParam(REQUESTFIELD_FLAVOR) String flav) {
    try {
        if (StringUtils.isBlank(filename)) {
            filename = "john.doe";
        }
        if (filesize < 1) {
            filesize = -1;
        }
        if (chunksize < 1) {
            chunksize = -1;
        }
        MediaPackage mp = null;
        if (StringUtils.isNotBlank(mediapackage)) {
            mp = factory.newMediaPackageBuilder().loadFromXml(mediapackage);
        }
        MediaPackageElementFlavor flavor = null;
        if (StringUtils.isNotBlank(flav)) {
            flavor = new MediaPackageElementFlavor(flav.split("/")[0], flav.split("/")[1]);
        }
        FileUploadJob job = uploadService.createJob(filename, filesize, chunksize, mp, flavor);
        return Response.ok(job.getId()).build();
    } catch (FileUploadException e) {
        log.error(e.getMessage(), e);
        return Response.status(Response.Status.NO_CONTENT).entity(e.getMessage()).build();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return Response.serverError().entity(buildUnexpectedErrorMessage(e)).build();
    }
}
Also used : FileUploadJob(org.opencastproject.fileupload.api.job.FileUploadJob) MediaPackage(org.opencastproject.mediapackage.MediaPackage) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 2 with FileUploadJob

use of org.opencastproject.fileupload.api.job.FileUploadJob in project opencast by opencast.

the class FileUploadRestService method postPayload.

@POST
@Produces(MediaType.APPLICATION_XML)
@Path("job/{jobID}")
@RestQuery(name = "newjob", description = "Appends the next chunk of data to the file on the server.", pathParameters = { @RestParameter(description = "The ID of the upload job", isRequired = false, name = "jobID", type = RestParameter.Type.STRING) }, restParameters = { @RestParameter(description = "The number of the current chunk", isRequired = false, name = "chunknumber", type = RestParameter.Type.STRING), @RestParameter(description = "The payload", isRequired = false, name = "filedata", type = RestParameter.Type.FILE) }, reponses = { @RestResponse(description = "the chunk data was successfully appended to file on server", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "the upload job was not found", responseCode = HttpServletResponse.SC_NOT_FOUND), @RestResponse(description = "the request was malformed", responseCode = HttpServletResponse.SC_BAD_REQUEST) }, returnDescription = "The XML representation of the updated upload job")
public Response postPayload(@PathParam("jobID") String jobId, @Context HttpServletRequest request) {
    try {
        if (!ServletFileUpload.isMultipartContent(request)) {
            // make sure request is "multipart/form-data"
            throw new FileUploadException("Request is not of type multipart/form-data");
        }
        if (uploadService.hasJob(jobId)) {
            // testing for existence of job here already so we can generate a 404 early
            long chunkNum = 0;
            FileUploadJob job = uploadService.getJob(jobId);
            ServletFileUpload upload = new ServletFileUpload();
            for (FileItemIterator iter = upload.getItemIterator(request); iter.hasNext(); ) {
                FileItemStream item = iter.next();
                if (item.isFormField()) {
                    String name = item.getFieldName();
                    if (REQUESTFIELD_CHUNKNUM.equalsIgnoreCase(name)) {
                        chunkNum = Long.parseLong(Streams.asString(item.openStream()));
                    }
                } else if (REQUESTFIELD_DATA.equalsIgnoreCase(item.getFieldName())) {
                    uploadService.acceptChunk(job, chunkNum, item.openStream());
                    return Response.ok(job).build();
                }
            }
            throw new FileUploadException("No payload!");
        } else {
            log.warn("Upload job not found: " + jobId);
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (FileUploadException e) {
        log.error(e.getMessage(), e);
        return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return Response.serverError().entity(buildUnexpectedErrorMessage(e)).build();
    }
}
Also used : FileUploadJob(org.opencastproject.fileupload.api.job.FileUploadJob) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 3 with FileUploadJob

use of org.opencastproject.fileupload.api.job.FileUploadJob in project opencast by opencast.

the class FileUploadServiceImpl method getJob.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.fileupload.api.FileUploadService#getJob(String id)
 */
@Override
public FileUploadJob getJob(String id) throws FileUploadException {
    if (jobCache.containsKey(id)) {
        // job already cached?
        return jobCache.get(id);
    } else {
        // job not in cache?
        try {
            // try to load job from filesystem
            synchronized (this) {
                File jobFile = getJobFile(id);
                FileUploadJob job = (FileUploadJob) jobUnmarshaller.unmarshal(jobFile);
                // get last modified time from job file
                job.setLastModified(jobFile.lastModified());
                return job;
            }
        // if loading from fs also fails
        } catch (Exception e) {
            // we could not find the job and throw an Exception
            throw fileUploadException(Severity.warn, "Failed to load job " + id + " from file.", e);
        }
    }
}
Also used : FileUploadJob(org.opencastproject.fileupload.api.job.FileUploadJob) File(java.io.File) ConfigurationException(org.osgi.service.cm.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException)

Example 4 with FileUploadJob

use of org.opencastproject.fileupload.api.job.FileUploadJob in project opencast by opencast.

the class FileUploadRestService method getJob.

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("job/{jobID}.{format:xml|json}")
@RestQuery(name = "job", description = "Returns the XML or the JSON representation of an upload job.", pathParameters = { @RestParameter(description = "The ID of the upload job", isRequired = false, name = "jobID", type = RestParameter.Type.STRING), @RestParameter(description = "The output format (json or xml) of the response body.", isRequired = true, name = "format", type = RestParameter.Type.STRING) }, reponses = { @RestResponse(description = "the job was successfully retrieved.", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "the job was not found.", responseCode = HttpServletResponse.SC_NOT_FOUND) }, returnDescription = "The XML representation of the requested upload job.")
public Response getJob(@PathParam("jobID") String id, @PathParam("format") String format) {
    try {
        if (uploadService.hasJob(id)) {
            // Return the results using the requested format
            FileUploadJob job = uploadService.getJob(id);
            final String type = "json".equals(format) ? MediaType.APPLICATION_JSON : MediaType.APPLICATION_XML;
            return Response.ok().entity(job).type(type).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return Response.serverError().entity(buildUnexpectedErrorMessage(e)).build();
    }
}
Also used : FileUploadJob(org.opencastproject.fileupload.api.job.FileUploadJob) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 5 with FileUploadJob

use of org.opencastproject.fileupload.api.job.FileUploadJob in project opencast by opencast.

the class FileUploadRestService method getPayload.

@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("job/{jobID}/{filename}")
@RestQuery(name = "payload", description = "Returns the payload of the upload job.", pathParameters = { @RestParameter(description = "The ID of the upload job to retrieve the file from", isRequired = false, name = "jobID", type = RestParameter.Type.STRING), @RestParameter(description = "The name of the payload file", isRequired = false, name = "filename", type = RestParameter.Type.STRING) }, reponses = { @RestResponse(description = "the job and file have been found.", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "the job or file were not found.", responseCode = HttpServletResponse.SC_NOT_FOUND) }, returnDescription = "The payload of the upload job")
public Response getPayload(@PathParam("jobID") String id, @PathParam("filename") String filename) {
    try {
        if (uploadService.hasJob(id)) {
            FileUploadJob job = uploadService.getJob(id);
            InputStream payload = uploadService.getPayload(job);
            // TODO use AutoDetectParser to guess Content-Type header
            return Response.ok(payload).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return Response.serverError().entity(buildUnexpectedErrorMessage(e)).build();
    }
}
Also used : FileUploadJob(org.opencastproject.fileupload.api.job.FileUploadJob) InputStream(java.io.InputStream) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

FileUploadException (org.opencastproject.fileupload.api.exception.FileUploadException)7 FileUploadJob (org.opencastproject.fileupload.api.job.FileUploadJob)7 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 RestQuery (org.opencastproject.util.doc.rest.RestQuery)4 File (java.io.File)3 IOException (java.io.IOException)3 FileNotFoundException (java.io.FileNotFoundException)2 MalformedURLException (java.net.MalformedURLException)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 InputStream (java.io.InputStream)1 Calendar (java.util.Calendar)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 MediaPackage (org.opencastproject.mediapackage.MediaPackage)1 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)1