Announcement Announcement Module
Collapse
No announcement yet.
ProgressListener & CommonsMultipartResolver Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ProgressListener & CommonsMultipartResolver

    Hi, I'm building a GWT app that has a file upload feature. I would like to add a progress bar to show the user file upload progress.

    I've read some of the threads in the forum, and I'm able to upload a file ok, and I have stored a ThreadLocal ProgressListener in the HttpSession, but I'm not able to get it when I poll the server for progress.

    Here's my overriden CommonsMultipartResolver:

    Code:
    public class AjaxMultipartResolver extends CommonsMultipartResolver {
    	
    	private static ThreadLocal<UploadProgressListener> progressListener = new ThreadLocal<UploadProgressListener>();
    
        @Override
        public void cleanupMultipart(MultipartHttpServletRequest request) {
            super.cleanupMultipart(request);
        }
    
        @Override
        protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {
            FileUpload fileUpload = super.newFileUpload(fileItemFactory);
            fileUpload.setProgressListener(progressListener.get());
            return fileUpload;
        }
       
        @Override
        public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
            progressListener.set(new UploadProgressListener(request));
            return super.resolveMultipart(request);
        } 
    }
    Here's my progress listener:

    Code:
    public class UploadProgressListener implements ProgressListener {
    
    	private double percentDone;
    	
    	private static final Logger LOGGER = Logger.getLogger(UploadProgressListener.class);
    	
    	public UploadProgressListener(HttpServletRequest request) {
    		final HttpSession session = request.getSession();
    		LOGGER.info("Setting progress listener attribute on session: "+session.getId());
            session.setAttribute("progress", this);
        } 
    
    	/*
    	 * (non-Javadoc)
    	 * @see org.apache.commons.fileupload.ProgressListener#update(long, long, int)
    	 */
    	@Override
    	public void update(long bytesRead, long contentLength, int pItems) {	
    		percentDone = (100 * bytesRead) / contentLength;
    	}
    	
    	/**
    	 * Get the percent done
    	 * @return the percent done
    	 */
    	public double getPercentDone() {
    		return percentDone;
    	}
    }
    For file upload, I extend SimpleFormController, and I defined an UploadBean for my command class. This is working just fine...

    Code:
    public class FileUploadController extends SimpleFormController {
    
    	/*
    	 * (non-Javadoc)
    	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
    	 */
    	@Override
        protected ModelAndView onSubmit(
        		final HttpServletRequest request, 
        		final HttpServletResponse response,
                final Object command, 
                final BindException errors) throws ServletException {
        	
    		// this is our command object that should have its params mapped from the form
            FileUploadBean bean;
            
            try {
            	// cast the bean
                bean = (FileUploadBean) command;
    This works fine for FileUpload -- I guess where I am stuck is how to poll for the progress. Since I extend SimpleFormController, I don't think I can issue GET requests (can I?)

    I tried implementing a standard controller like:

    Code:
    public class StatusController implements Controller {
           public ModelAndView handleRequest(HttpServletRequest req,
    			HttpServletResponse res) throws Exception {
           Object o = request.getAttribute("progress");
    }
    ...but this is always null -- I guess b/c the session is different since it is not a Multipart request.

    How can I resolve this?

    Thanks in advance,
    Davis

  • #2
    I tried implementing a standard controller like:

    Code:
    public class StatusController implements Controller {
           public ModelAndView handleRequest(HttpServletRequest req,
    			HttpServletResponse res) throws Exception {
           Object o = request.getAttribute("progress");
    }
    ...but this is always null -- I guess b/c the session is different since it is not a Multipart request.

    How can I resolve this?

    Thanks in advance,
    Davis
    You are getting attribute from request scope but you set listener object in session scope.

    You should do :
    Code:
           Object o = request.getSession().getAttribute("progress");
    Also check this thread :
    http://forum.springsource.org/showthread.php?t=32367

    I'm trying to develop a progress bar using spring 3, ajax controller and jquery , when i finish i will post my experience.

    Comment


    • #3
      Thanks for the response. I had this same requirement in a project I'm working on and this thread really helped me out.

      Alex


      Originally posted by l.pasquinelli View Post
      You are getting attribute from request scope but you set listener object in session scope.

      You should do :
      Code:
             Object o = request.getSession().getAttribute("progress");
      Also check this thread :
      http://forum.springsource.org/showthread.php?t=32367

      I'm trying to develop a progress bar using spring 3, ajax controller and jquery , when i finish i will post my experience.

      Comment

      Working...
      X