use of org.opencastproject.fileupload.api.exception.FileUploadException 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();
}
}
use of org.opencastproject.fileupload.api.exception.FileUploadException 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();
}
}
use of org.opencastproject.fileupload.api.exception.FileUploadException 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);
}
}
}
use of org.opencastproject.fileupload.api.exception.FileUploadException in project opencast by opencast.
the class FileUploadServiceImpl method createJob.
// </editor-fold>
/**
* {@inheritDoc}
*
* @see org.opencastproject.fileupload.api.FileUploadService#createJob(String, long, int,
* org.opencastproject.mediapackage.MediaPackage, org.opencastproject.mediapackage.MediaPackageElementFlavor)
*/
@Override
public FileUploadJob createJob(String filename, long filesize, int chunksize, MediaPackage mp, MediaPackageElementFlavor flavor) throws FileUploadException {
FileUploadJob job = new FileUploadJob(filename, filesize, chunksize, mp, flavor);
logger.info("Creating new upload job: {}", job);
try {
// create working dir
File jobDir = getJobDir(job.getId());
FileUtils.forceMkdir(jobDir);
// create empty payload file
ensureExists(getPayloadFile(job.getId()));
// create job file
storeJob(job);
} catch (FileUploadException e) {
deleteJob(job.getId());
throw fileUploadException(Severity.error, "Could not create job file in " + workRoot.getAbsolutePath(), e);
} catch (IOException e) {
deleteJob(job.getId());
throw fileUploadException(Severity.error, "Could not create upload job directory in " + workRoot.getAbsolutePath(), e);
}
return job;
}
use of org.opencastproject.fileupload.api.exception.FileUploadException in project opencast by opencast.
the class FileUploadServiceImpl method putPayloadIntoMediaPackage.
/**
* Puts the payload of an upload job into a MediaPackage in the WFR, adds the files as a track to the MediaPackage and
* returns the files URL in the WFR.
*
* @param job
* @return URL of the file in the WFR
* @throws FileUploadException
*/
private URL putPayloadIntoMediaPackage(FileUploadJob job) throws FileUploadException {
MediaPackage mediaPackage = job.getPayload().getMediaPackage();
MediaPackageElementFlavor flavor = job.getPayload().getFlavor();
List<Track> excludeTracks = Arrays.asList(mediaPackage.getTracks(flavor));
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(getPayloadFile(job.getId()));
MediaPackage mp = ingestService.addTrack(fileInputStream, job.getPayload().getFilename(), job.getPayload().getFlavor(), mediaPackage);
List<Track> tracks = new ArrayList<Track>(Arrays.asList(mp.getTracks(flavor)));
tracks.removeAll(excludeTracks);
if (tracks.size() != 1)
throw new FileUploadException("Ingested track not found");
return tracks.get(0).getURI().toURL();
} catch (Exception e) {
throw fileUploadException(Severity.error, "Failed to add payload to MediaPackage.", e);
} finally {
IOUtils.closeQuietly(fileInputStream);
}
}
Aggregations