List of usage examples for org.apache.commons.fileupload.portlet PortletRequestContext PortletRequestContext
public PortletRequestContext(ActionRequest request)
From source file:org.apache.myfaces.webapp.filter.portlet.PortletChacheFileSizeErrorsFileUpload.java
/** * Similar to {@link ServletFileUpload#parseRequest(RequestContext)} but will * catch and swallow FileSizeLimitExceededExceptions in order to return as * many usable items as possible./* w w w .j a v a 2 s . c o m*/ * * @param fileUpload * @return List of {@link FileItem} excluding any that exceed max size. * @throws FileUploadException */ public List parseRequestCatchingFileSizeErrors(ActionRequest request, FileUpload fileUpload) throws FileUploadException { try { List items = new ArrayList(); // The line below throws a SizeLimitExceededException (wrapped by a // FileUploadIOException) if the request is longer than the max size // allowed by fileupload requests (FileUpload.getSizeMax) // But note that if the request does not send proper headers this check // just will not do anything and we still have to check it again. FileItemIterator iter = fileUpload.getItemIterator(new PortletRequestContext(request)); FileItemFactory fac = fileUpload.getFileItemFactory(); if (fac == null) { throw new NullPointerException("No FileItemFactory has been set."); } long maxFileSize = this.getFileSizeMax(); long maxSize = this.getSizeMax(); boolean checkMaxSize = false; if (maxFileSize == -1L) { //The max allowed file size should be approximate to the maxSize maxFileSize = maxSize; } if (maxSize != -1L) { checkMaxSize = true; } while (iter.hasNext()) { final FileItemStream item = iter.next(); FileItem fileItem = fac.createItem(item.getFieldName(), item.getContentType(), item.isFormField(), item.getName()); long allowedLimit = 0L; try { if (maxFileSize != -1L || checkMaxSize) { if (checkMaxSize) { allowedLimit = maxSize > maxFileSize ? maxFileSize : maxSize; } else { //Just put the limit allowedLimit = maxFileSize; } long contentLength = getContentLength(item.getHeaders()); //If we have a content length in the header we can use it if (contentLength != -1L && contentLength > allowedLimit) { throw new FileUploadIOException(new FileSizeLimitExceededException( "The field " + item.getFieldName() + " exceeds its maximum permitted " + " size of " + allowedLimit + " characters.", contentLength, allowedLimit)); } //Otherwise we must limit the input as it arrives (NOTE: we cannot rely //on commons upload to throw this exception as it will close the //underlying stream final InputStream itemInputStream = item.openStream(); InputStream limitedInputStream = new LimitedInputStream(itemInputStream, allowedLimit) { protected void raiseError(long pSizeMax, long pCount) throws IOException { throw new FileUploadIOException(new FileSizeLimitExceededException( "The field " + item.getFieldName() + " exceeds its maximum permitted " + " size of " + pSizeMax + " characters.", pCount, pSizeMax)); } }; //Copy from the limited stream long bytesCopied = Streams.copy(limitedInputStream, fileItem.getOutputStream(), true); // Decrement the bytesCopied values from maxSize, so the next file copied // takes into account this value when allowedLimit var is calculated // Note the invariant before the line is maxSize >= bytesCopied, since if this // is not true a FileUploadIOException is thrown first. maxSize -= bytesCopied; } else { //We can just copy the data Streams.copy(item.openStream(), fileItem.getOutputStream(), true); } } catch (FileUploadIOException e) { try { throw (FileUploadException) e.getCause(); } catch (FileUploadBase.FileSizeLimitExceededException se) { request.setAttribute("org.apache.myfaces.custom.fileupload.exception", "fileSizeLimitExceeded"); String fieldName = fileItem.getFieldName(); request.setAttribute("org.apache.myfaces.custom.fileupload." + fieldName + ".maxSize", new Integer((int) allowedLimit)); } } catch (IOException e) { throw new IOFileUploadException("Processing of " + FileUploadBase.MULTIPART_FORM_DATA + " request failed. " + e.getMessage(), e); } if (fileItem instanceof FileItemHeadersSupport) { final FileItemHeaders fih = item.getHeaders(); ((FileItemHeadersSupport) fileItem).setHeaders(fih); } if (fileItem != null) { items.add(fileItem); } } return items; } catch (FileUploadIOException e) { throw (FileUploadException) e.getCause(); } catch (IOException e) { throw new FileUploadException(e.getMessage(), e); } }
From source file:org.jasig.portlet.cms.controller.EditPostController.java
private void processPostAttachments(final ActionRequest request, final Post post) throws Exception { if (FileUploadBase.isMultipartContent(new PortletRequestContext(request))) { /*//from w ww . j a va2s .c o m * Attachments may have been removed in the edit mode. We must * refresh the session-bound post before updating attachments. */ final PortletPreferencesWrapper pref = new PortletPreferencesWrapper(request); final Post originalPost = getRepositoryDao().getPost(pref.getPortletRepositoryRoot()); if (originalPost != null) { post.getAttachments().clear(); post.getAttachments().addAll(originalPost.getAttachments()); } final MultipartActionRequest multipartRequest = (MultipartActionRequest) request; for (int index = 0; index < multipartRequest.getFileMap().size(); index++) { final MultipartFile file = multipartRequest.getFile("attachment" + index); if (!file.isEmpty()) { logDebug("Uploading attachment file: " + file.getOriginalFilename()); logDebug("Attachment file size: " + file.getSize()); final Calendar cldr = Calendar.getInstance(request.getLocale()); cldr.setTime(new Date()); final Attachment attachment = Attachment.fromFile(file.getOriginalFilename(), file.getContentType(), cldr, file.getBytes()); final String title = multipartRequest.getParameter("attachmentTitle" + index); attachment.setTitle(title); post.getAttachments().add(attachment); } } } }