use of org.apache.commons.fileupload.FileItemIterator in project ninja by ninjaframework.
the class UploadController method uploadFinish.
/**
* This upload method expects a file and simply displays the file in the
* multipart upload again to the user (in the correct mime encoding).
*
* @param context
* @return
* @throws Exception
*/
public Result uploadFinish(Context context) throws Exception {
// we are using a renderable inner class to stream the input again to
// the user
Renderable renderable = new Renderable() {
@Override
public void render(Context context, Result result) {
try {
// make sure the context really is a multipart context...
if (context.isMultipart()) {
// This is the iterator we can use to iterate over the
// contents
// of the request.
FileItemIterator fileItemIterator = context.getFileItemIterator();
while (fileItemIterator.hasNext()) {
FileItemStream item = fileItemIterator.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
String contentType = item.getContentType();
if (contentType != null) {
result.contentType(contentType);
} else {
contentType = mimeTypes.getMimeType(name);
}
ResponseStreams responseStreams = context.finalizeHeaders(result);
if (item.isFormField()) {
System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
// Process the input stream
ByteStreams.copy(stream, responseStreams.getOutputStream());
}
}
}
} catch (IOException | FileUploadException exception) {
throw new InternalServerErrorException(exception);
}
}
};
return new Result(200).render(renderable);
}
use of org.apache.commons.fileupload.FileItemIterator in project ninja by ninjaframework.
the class NinjaServletContext method getFileItemIterator.
@Override
public FileItemIterator getFileItemIterator() {
long maxFileSize = ninjaProperties.getIntegerWithDefault(NinjaConstant.UPLOADS_MAX_FILE_SIZE, -1);
long maxTotalSize = ninjaProperties.getIntegerWithDefault(NinjaConstant.UPLOADS_MAX_TOTAL_SIZE, -1);
ServletFileUpload upload = new ServletFileUpload();
upload.setFileSizeMax(maxFileSize);
upload.setSizeMax(maxTotalSize);
FileItemIterator fileItemIterator = null;
try {
fileItemIterator = upload.getItemIterator(httpServletRequest);
} catch (FileUploadException | IOException e) {
logger.error("Error while trying to process mulitpart file upload", e);
}
return fileItemIterator;
}
use of org.apache.commons.fileupload.FileItemIterator in project ninja by ninjaframework.
the class NinjaServletContext method processFormFields.
private void processFormFields() {
if (formFieldsProcessed)
return;
formFieldsProcessed = true;
// return if not multipart
if (!ServletFileUpload.isMultipartContent(httpServletRequest))
return;
// get fileProvider from route method/class, or defaults to an injected one
// if none injected, then we do not process form fields this way and let the user
// call classic getFileItemIterator() by himself
FileProvider fileProvider = null;
if (route != null) {
if (fileProvider == null) {
fileProvider = route.getControllerMethod().getAnnotation(FileProvider.class);
}
if (fileProvider == null) {
fileProvider = route.getControllerClass().getAnnotation(FileProvider.class);
}
}
// get file item provider from file provider or default one
FileItemProvider fileItemProvider = null;
if (fileProvider == null) {
fileItemProvider = injector.getInstance(FileItemProvider.class);
} else {
fileItemProvider = injector.getInstance(fileProvider.value());
}
if (fileItemProvider instanceof NoFileItemProvider)
return;
// Initialize maps and other constants
ArrayListMultimap<String, String> formMap = ArrayListMultimap.create();
ArrayListMultimap<String, FileItem> fileMap = ArrayListMultimap.create();
// Include any query string parameters as part of "formMap"
final Map<String, String[]> parameterMap = this.httpServletRequest.getParameterMap();
if (parameterMap != null) {
parameterMap.forEach((parameter, values) -> {
for (String value : values) {
formMap.put(parameter, value);
}
});
}
// This is the iterator we can use to iterate over the contents of the request.
try {
FileItemIterator fileItemIterator = getFileItemIterator();
while (fileItemIterator.hasNext()) {
FileItemStream item = fileItemIterator.next();
if (item.isFormField()) {
String charset = NinjaConstant.UTF_8;
String contentType = item.getContentType();
if (contentType != null) {
charset = HttpHeaderUtils.getCharsetOfContentTypeOrUtf8(contentType);
}
// save the form field for later use from getParameter
String value = Streams.asString(item.openStream(), charset);
formMap.put(item.getFieldName(), value);
} else {
// process file as input stream and save for later use in getParameterAsFile or getParameterAsInputStream
FileItem fileItem = fileItemProvider.create(item);
fileMap.put(item.getFieldName(), fileItem);
}
}
} catch (FileUploadException | IOException e) {
throw new RuntimeException("Failed to parse multipart request data", e);
}
// convert both multimap<K,V> to map<K,List<V>>
formFieldsMap = toUnmodifiableMap(formMap);
fileFieldsMap = toUnmodifiableMap(fileMap);
}
use of org.apache.commons.fileupload.FileItemIterator in project ninja by ninjaframework.
the class FileItemProviderImpl2 method init.
protected final void init(final Class<? extends FileItemProvider> fileItemProviderClass) throws IOException, ServletException {
// default setup for httpServlet request.
// According to servlet spec the following will be returned:
when(httpServletRequest.getContextPath()).thenReturn("");
when(httpServletRequest.getRequestURI()).thenReturn("/");
when(httpServletRequest.getContentType()).thenReturn("multipart/form-data");
when(httpServletRequest.getMethod()).thenReturn("POST");
ninjaProperties = NinjaPropertiesImpl.builder().withMode(NinjaMode.test).build();
FileItemIterator fileItemIterator = makeFileItemsIterator();
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(NinjaProperties.class).toInstance(ninjaProperties);
bind(FileItemProvider.class).to(fileItemProviderClass);
}
});
context = new NinjaServletContext(bodyParserEngineManager, flashCookie, ninjaProperties, resultHandler, sessionCookie, validation, injector, new ParamParsers(new HashSet<>())) {
public FileItemIterator getFileItemIterator() {
return fileItemIterator;
}
};
}
use of org.apache.commons.fileupload.FileItemIterator in project stanbol by apache.
the class ContentItemReader method readFrom.
@Override
public ContentItem readFrom(Class<ContentItem> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
// boolean withMetadata = withMetadata(httpHeaders);
ContentItem contentItem = null;
IRI contentItemId = getContentItemId();
if (log.isTraceEnabled()) {
// NOTE: enabling TRACE level logging will copy the parsed content
// into a BYTE array
log.trace("Parse ContentItem from");
log.trace(" - MediaType: {}", mediaType);
log.trace(" - Headers:");
for (Entry<String, List<String>> header : httpHeaders.entrySet()) {
log.trace(" {}: {}", header.getKey(), header.getValue());
}
byte[] content = IOUtils.toByteArray(entityStream);
log.trace("content: \n{}", new String(content, "UTF-8"));
IOUtils.closeQuietly(entityStream);
entityStream = new ByteArrayInputStream(content);
}
Set<String> parsedContentIds = new HashSet<String>();
if (mediaType.isCompatible(MULTIPART)) {
log.debug(" - parse Multipart MIME ContentItem");
// try to read ContentItem from "multipart/from-data"
Graph metadata = null;
FileItemIterator fileItemIterator;
try {
fileItemIterator = fu.getItemIterator(new MessageBodyReaderContext(entityStream, mediaType));
while (fileItemIterator.hasNext()) {
FileItemStream fis = fileItemIterator.next();
if (fis.getFieldName().equals("metadata")) {
if (contentItem != null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("The Multipart MIME part with the 'metadata' " + "MUST BE before the MIME part containing the " + "'content'!").build());
}
// only used if not parsed as query param
if (contentItemId == null && fis.getName() != null && !fis.getName().isEmpty()) {
contentItemId = new IRI(fis.getName());
}
metadata = new IndexedGraph();
try {
getParser().parse(metadata, fis.openStream(), fis.getContentType());
} catch (Exception e) {
throw new WebApplicationException(e, Response.status(Response.Status.BAD_REQUEST).entity(String.format("Unable to parse Metadata " + "from Multipart MIME part '%s' (" + "contentItem: %s| contentType: %s)", fis.getFieldName(), fis.getName(), fis.getContentType())).build());
}
} else if (fis.getFieldName().equals("content")) {
contentItem = createContentItem(contentItemId, metadata, fis, parsedContentIds);
} else if (fis.getFieldName().equals("properties") || fis.getFieldName().equals(REQUEST_PROPERTIES_URI.getUnicodeString())) {
// parse the RequestProperties
if (contentItem == null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Multipart MIME parts for " + "Request Properties MUST BE after the " + "MIME parts for 'metadata' AND 'content'").build());
}
MediaType propMediaType = MediaType.valueOf(fis.getContentType());
if (!APPLICATION_JSON_TYPE.isCompatible(propMediaType)) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Request Properties (Multipart MIME parts" + "with the name '" + fis.getFieldName() + "') MUST " + "BE encoded as 'appicaltion/json' (encountered: '" + fis.getContentType() + "')!").build());
}
String propCharset = propMediaType.getParameters().get("charset");
if (propCharset == null) {
propCharset = "UTF-8";
}
Map<String, Object> reqProp = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
try {
reqProp.putAll(toMap(new JSONObject(IOUtils.toString(fis.openStream(), propCharset))));
} catch (JSONException e) {
throw new WebApplicationException(e, Response.status(Response.Status.BAD_REQUEST).entity("Unable to parse Request Properties from" + "Multipart MIME parts with the name 'properties'!").build());
}
} else {
// additional metadata as serialised RDF
if (contentItem == null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Multipart MIME parts for additional " + "contentParts MUST BE after the MIME " + "parts for 'metadata' AND 'content'").build());
}
if (fis.getFieldName() == null || fis.getFieldName().isEmpty()) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Multipart MIME parts representing " + "ContentParts for additional RDF metadata" + "MUST define the contentParts URI as" + "'name' of the MIME part!").build());
}
Graph graph = new IndexedGraph();
try {
getParser().parse(graph, fis.openStream(), fis.getContentType());
} catch (Exception e) {
throw new WebApplicationException(e, Response.status(Response.Status.BAD_REQUEST).entity(String.format("Unable to parse RDF " + "for ContentPart '%s' ( contentType: %s)", fis.getName(), fis.getContentType())).build());
}
IRI contentPartId = new IRI(fis.getFieldName());
contentItem.addPart(contentPartId, graph);
}
}
if (contentItem == null) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("The parsed multipart content item does not contain " + "any content. The content is expected to be contained " + "in a MIME part with the name 'content'. This part can " + " be also a 'multipart/alternate' if multiple content " + "parts need to be included in requests.").build());
}
} catch (FileUploadException e) {
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
}
} else {
// normal content
ContentItemFactory ciFactory = getContentItemFactory();
contentItem = ciFactory.createContentItem(contentItemId, new StreamSource(entityStream, mediaType.toString()));
// add the URI of the main content
parsedContentIds.add(contentItem.getPartUri(0).getUnicodeString());
}
// set the parsed contentIDs to the EnhancementProperties
Map<String, Object> ep = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
parseEnhancementPropertiesFromParameters(ep);
ep.put(PARSED_CONTENT_URIS, Collections.unmodifiableSet(parsedContentIds));
// STANBOL-660: set the language of the content if explicitly parsed in the request
String contentLanguage = getContentLanguage();
if (!StringUtils.isBlank(contentLanguage)) {
// language codes are case insensitive ... so we convert to lower case
contentLanguage = contentLanguage.toLowerCase(Locale.ROOT);
createParsedLanguageAnnotation(contentItem, contentLanguage);
// previously only the dc:language property was set to the contentItem. However this
// information is only used as fallback if no Language annotation is present. However
// if a user explicitly parses the language he expects this language to be used
// so this was change with STANBOL-1417
// EnhancementEngineHelper.set(contentItem.getMetadata(), contentItem.getUri(),
// DC_LANGUAGE, new PlainLiteralImpl(contentLanguage));
}
return contentItem;
}
Aggregations