// Multipart form encoding?
if (ServletFileUpload.isMultipartContent(request)) {
try {
ServletFileUpload payload = new ServletFileUpload();
for (FileItemIterator iter = payload.getItemIterator(request); iter.hasNext();) {
FileItemStream item = iter.next();
String fieldName = item.getFieldName();
if (item.isFormField()) {
String fieldValue = Streams.asString(item.openStream());
if (StringUtils.isBlank(fieldValue))
continue;
if (OPT_PATH.equals(fieldName)) {
path = fieldValue;
} else if (OPT_LANGUAGE.equals(fieldName)) {
try {
language = LanguageUtils.getLanguage(fieldValue);
} catch (UnknownLanguageException e) {
throw new WebApplicationException(Status.BAD_REQUEST);
}
} else if (OPT_MIMETYPE.equals(fieldName)) {
mimeType = fieldValue;
}
} else {
// once the body gets read iter.hasNext must not be invoked
// or the stream can not be read
fileName = StringUtils.trim(item.getName());
mimeType = StringUtils.trim(item.getContentType());
uploadedFile = File.createTempFile("upload-", null);
FileOutputStream fos = new FileOutputStream(uploadedFile);
try {
IOUtils.copy(item.openStream(), fos);
} catch (IOException e) {
throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
} finally {
IOUtils.closeQuietly(fos);
}