use of org.apache.commons.fileupload.FileItemStream in project HongsCORE by ihongs.
the class BinaryUploader method save.
public static final State save(HttpServletRequest request, Map<String, Object> conf) {
FileItemStream fileStream = null;
boolean isAjaxUpload = request.getHeader("X_Requested_With") != null;
if (!ServletFileUpload.isMultipartContent(request)) {
return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
}
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
if (isAjaxUpload) {
upload.setHeaderEncoding("UTF-8");
}
try {
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
fileStream = iterator.next();
if (!fileStream.isFormField())
break;
fileStream = null;
}
if (fileStream == null) {
return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
}
String savePath = (String) conf.get("savePath");
String originFileName = fileStream.getName();
String suffix = FileType.getSuffixByFilename(originFileName);
originFileName = originFileName.substring(0, originFileName.length() - suffix.length());
savePath = savePath + suffix;
long maxSize = ((Long) conf.get("maxSize")).longValue();
if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
}
savePath = PathFormat.parse(savePath, originFileName);
// modified by Ternence
String rootPath = ConfigManager.getRootPath(request, conf);
String physicalPath = rootPath + savePath;
InputStream is = fileStream.openStream();
State storageState = StorageManager.saveFileByInputStream(is, physicalPath, maxSize);
is.close();
if (storageState.isSuccess()) {
storageState.putInfo("url", PathFormat.format(savePath));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
}
return storageState;
} catch (FileUploadException e) {
return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
} catch (IOException e) {
}
return new BaseState(false, AppInfo.IO_ERROR);
}
use of org.apache.commons.fileupload.FileItemStream in project opencast by opencast.
the class IngestRestService method addMediaPackageElement.
protected Response addMediaPackageElement(HttpServletRequest request, MediaPackageElement.Type type) {
MediaPackageElementFlavor flavor = null;
InputStream in = null;
try {
String fileName = null;
MediaPackage mp = null;
Long startTime = null;
String[] tags = null;
/* Only accept multipart/form-data */
if (!ServletFileUpload.isMultipartContent(request)) {
logger.trace("request isn't multipart-form-data");
return Response.serverError().status(Status.BAD_REQUEST).build();
}
boolean isDone = false;
for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
FileItemStream item = iter.next();
String fieldName = item.getFieldName();
if (item.isFormField()) {
if ("flavor".equals(fieldName)) {
String flavorString = Streams.asString(item.openStream(), "UTF-8");
logger.trace("flavor: {}", flavorString);
if (flavorString != null) {
flavor = MediaPackageElementFlavor.parseFlavor(flavorString);
}
} else if ("tags".equals(fieldName)) {
String tagsString = Streams.asString(item.openStream(), "UTF-8");
logger.trace("tags: {}", tagsString);
tags = tagsString.split(",");
} else if ("mediaPackage".equals(fieldName)) {
try {
String mediaPackageString = Streams.asString(item.openStream(), "UTF-8");
logger.trace("mediaPackage: {}", mediaPackageString);
mp = factory.newMediaPackageBuilder().loadFromXml(mediaPackageString);
} catch (MediaPackageException e) {
logger.debug("Unable to parse the 'mediaPackage' parameter: {}", ExceptionUtils.getMessage(e));
return Response.serverError().status(Status.BAD_REQUEST).build();
}
} else if ("startTime".equals(fieldName) && "/addPartialTrack".equals(request.getPathInfo())) {
String startTimeString = Streams.asString(item.openStream(), "UTF-8");
logger.trace("startTime: {}", startTime);
try {
startTime = Long.parseLong(startTimeString);
} catch (Exception e) {
logger.debug("Unable to parse the 'startTime' parameter: {}", ExceptionUtils.getMessage(e));
return Response.serverError().status(Status.BAD_REQUEST).build();
}
}
} else {
if (flavor == null) {
/* A flavor has to be specified in the request prior the video file */
logger.debug("A flavor has to be specified in the request prior to the content BODY");
return Response.serverError().status(Status.BAD_REQUEST).build();
}
fileName = item.getName();
in = item.openStream();
isDone = true;
}
if (isDone) {
break;
}
}
/*
* Check if we actually got a valid request including a message body and a valid mediapackage to attach the
* element to
*/
if (in == null || mp == null || MediaPackageSupport.sanityCheck(mp).isSome()) {
return Response.serverError().status(Status.BAD_REQUEST).build();
}
switch(type) {
case Attachment:
mp = ingestService.addAttachment(in, fileName, flavor, tags, mp);
break;
case Catalog:
mp = ingestService.addCatalog(in, fileName, flavor, tags, mp);
break;
case Track:
if (startTime == null) {
mp = ingestService.addTrack(in, fileName, flavor, tags, mp);
} else {
mp = ingestService.addPartialTrack(in, fileName, flavor, startTime, mp);
}
break;
default:
throw new IllegalStateException("Type must be one of track, catalog, or attachment");
}
return Response.ok(MediaPackageParser.getAsXml(mp)).build();
} catch (Exception e) {
logger.warn(e.getMessage(), e);
return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
} finally {
IOUtils.closeQuietly(in);
}
}
use of org.apache.commons.fileupload.FileItemStream in project opencast by opencast.
the class IngestRestService method ingestZippedMediaPackage.
private Response ingestZippedMediaPackage(HttpServletRequest request, String wdID, String wiID) {
if (isIngestLimitEnabled()) {
setIngestLimit(getIngestLimit() - 1);
logger.debug("An ingest has started so remaining ingest limit is " + getIngestLimit());
}
InputStream in = null;
Date started = new Date();
logger.info("Received new request from {} to ingest a zipped mediapackage", request.getRemoteHost());
try {
String workflowDefinitionId = wdID;
String workflowIdAsString = wiID;
Long workflowInstanceIdAsLong = null;
Map<String, String> workflowConfig = new HashMap<>();
if (ServletFileUpload.isMultipartContent(request)) {
boolean isDone = false;
for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
FileItemStream item = iter.next();
if (item.isFormField()) {
String fieldName = item.getFieldName();
String value = Streams.asString(item.openStream(), "UTF-8");
logger.trace("{}: {}", fieldName, value);
if (WORKFLOW_INSTANCE_ID_PARAM.equals(fieldName)) {
workflowIdAsString = value;
continue;
} else if (WORKFLOW_DEFINITION_ID_PARAM.equals(fieldName)) {
workflowDefinitionId = value;
continue;
} else {
logger.debug("Processing form field: " + fieldName);
workflowConfig.put(fieldName, value);
}
} else {
logger.debug("Processing file item");
// once the body gets read iter.hasNext must not be invoked or the stream can not be read
// MH-9579
in = item.openStream();
isDone = true;
}
if (isDone)
break;
}
} else {
logger.debug("Processing file item");
in = request.getInputStream();
}
// Adding ingest start time to workflow configuration
DateFormat formatter = new SimpleDateFormat(IngestService.UTC_DATE_FORMAT);
workflowConfig.put(IngestService.START_DATE_KEY, formatter.format(started));
/* Legacy support: Try to convert the workflowId to integer */
if (!StringUtils.isBlank(workflowIdAsString)) {
try {
workflowInstanceIdAsLong = Long.parseLong(workflowIdAsString);
} catch (NumberFormatException e) {
// The workflowId is not a long value and might be the media package identifier
workflowConfig.put(IngestServiceImpl.LEGACY_MEDIAPACKAGE_ID_KEY, workflowIdAsString);
}
}
if (StringUtils.isBlank(workflowDefinitionId)) {
workflowDefinitionId = defaultWorkflowDefinitionId;
}
WorkflowInstance workflow;
if (workflowInstanceIdAsLong != null) {
workflow = ingestService.addZippedMediaPackage(in, workflowDefinitionId, workflowConfig, workflowInstanceIdAsLong);
} else {
workflow = ingestService.addZippedMediaPackage(in, workflowDefinitionId, workflowConfig);
}
return Response.ok(WorkflowParser.toXml(workflow)).build();
} catch (NotFoundException e) {
logger.info(e.getMessage());
return Response.status(Status.NOT_FOUND).build();
} catch (MediaPackageException e) {
logger.warn(e.getMessage());
return Response.serverError().status(Status.BAD_REQUEST).build();
} catch (Exception e) {
logger.warn(e.getMessage(), e);
return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
} finally {
IOUtils.closeQuietly(in);
if (isIngestLimitEnabled()) {
setIngestLimit(getIngestLimit() + 1);
logger.debug("An ingest has finished so increased ingest limit to " + getIngestLimit());
}
}
}
use of org.apache.commons.fileupload.FileItemStream 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.FileItemStream 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;
}
Aggregations