<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Geekdev101</title>
	<atom:link href="http://geekdev101.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://geekdev101.wordpress.com</link>
	<description>Just another geek on blogging</description>
	<lastBuildDate>Sun, 25 Oct 2009 16:18:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='geekdev101.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Geekdev101</title>
		<link>http://geekdev101.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://geekdev101.wordpress.com/osd.xml" title="Geekdev101" />
	<atom:link rel='hub' href='http://geekdev101.wordpress.com/?pushpress=hub'/>
		<item>
		<title>[Java] File uploads with progress bar in Spring framework</title>
		<link>http://geekdev101.wordpress.com/2009/10/25/java-file-uploads-with-progress-bar-in-spring-framework/</link>
		<comments>http://geekdev101.wordpress.com/2009/10/25/java-file-uploads-with-progress-bar-in-spring-framework/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 16:18:44 +0000</pubDate>
		<dc:creator>geekdev101</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://geekdev101.wordpress.com/?p=61</guid>
		<description><![CDATA[File uploads with Spring framework are straight forward, however what happens if you would like to show a progress bar while uploading large files? Spring framework default multipart implementation depends on Apache Commons Fileupload library and after looking at the user guide there is a way of attaching progress listener so it is possible to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=geekdev101.wordpress.com&amp;blog=8926028&amp;post=61&amp;subd=geekdev101&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>File uploads with Spring framework are straight forward, however what happens if you would like to show a progress bar while uploading large files? Spring framework default <em>multipart</em> implementation depends on <em>Apache Commons Fileupload</em> library and after looking at the user guide there is a way of attaching progress listener so it is possible to achieve progress bar functionality by modifying the default implementation. The second question is how to get the progress bar interaction on your website. In most examples online there seems to be another AJAX request polling the server for an update which means the upload progress needs to be stored somewhere &#8211; probably in the HTTP session or as  a thread attached variable?</p>
<p>To start with this implementation is not tested in production therefore I do not take any blame whatsoever. Also I do not claim that this is a good solution I wrote this while playing with spring approximately for two hours. Apart from the progress bar it would be nice to implement resume functionality if the upload fails. Again I am not sure if this is a good practice, maybe it should be left to the protocols dedicated for doing it.</p>
<p>So to get the thing running we need to modify the default implementation, store the upload progress in session, write some <em>JavaScript</em> code to query the progress and implement a filter that would return the upload progress if a specific URL is requested.</p>
<p>Let&#8217;s start with modifying the default implementation of <em>multipart </em>resolver. In this case we only need to override a single method to attach the progress listener. To be more precise I am talking about the <em>parseRequest </em>method.</p>
<p><pre class="brush: java;">
package com.wordpress.geekdev101.fileupload;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

public class ProgressCapableMultipartResolver
	extends CommonsMultipartResolver 
{
	public final static String PROGRESS_PREFIX = &quot;ProgressCapableMultipartResolver:&quot;;

	private void initProgressProcessor(HttpServletRequest request) {
		request.getSession().setAttribute(
				PROGRESS_PREFIX + request.getRequestURI(), 
				new ProgressDescriptor());
	}
	
	private void clearProgressProcessor(HttpServletRequest request) { 
		request.getSession().removeAttribute(PROGRESS_PREFIX 
				+ request.getRequestURI());
	}
	
	private void processProgress(long bytesRead, long bytesTotal, 
			HttpServletRequest request) {
		
		request.getSession().setAttribute(
				PROGRESS_PREFIX + request.getRequestURI(), 
				new ProgressDescriptor(bytesRead, bytesTotal));
	}
	
	protected MultipartParsingResult parseRequest(
			final HttpServletRequest request) 
		throws MultipartException 
	{
		String encoding = determineEncoding(request);
		ServletFileUpload fileUpload = (ServletFileUpload)prepareFileUpload(encoding);

		initProgressProcessor(request); 
		fileUpload.setProgressListener(new ProgressListener() {
			@Override
			public void update(
					long pBytesRead, long pContentLength, int pItems) {
				processProgress(pBytesRead, pContentLength, request);
			}
		});
		
		MultipartParsingResult result = null; 
		try {
			result = parseFileItems( 
						fileUpload.parseRequest(request), 
						encoding);
			
		} catch (FileUploadBase.SizeLimitExceededException ex) {
			throw new MaxUploadSizeExceededException(
					fileUpload.getSizeMax(), ex);
			
		} catch (FileUploadException ex) {
			throw new MultipartException(&quot;Could not parse &quot; +
					&quot;multipart servlet request&quot;, ex);
		}
		
		clearProgressProcessor(request); 
		return result; 
	}
}
</pre></p>
<p><pre class="brush: java;">
package com.wordpress.geekdev101.fileupload;

public class ProgressDescriptor 
{
	public long bytesRead; 
	public long bytesTotal;
	
	public ProgressDescriptor() {
		bytesRead = bytesTotal = 0; 
	}
	
	public ProgressDescriptor(long bytesRead,
			long bytesTotal) 
	{ 
		this.bytesRead = bytesRead; 
		this.bytesTotal = bytesTotal; 
	}
	
	public long getBytesRead() { 
		return bytesRead; 
	}
	
	public long getBytesTotal() { 
		return bytesTotal;
	}
	
	@Override
	public String toString() {
		return bytesRead + &quot;/&quot; + bytesTotal; 
	}
}
</pre></p>
<p>Let say the request for progress will always be a URI with &#8220;.progress&#8221; appended to the request which makes it easy to define our filter in <em>web.xml </em> i.e. by specifying the filter mapping as <em>&#8220;*.progress&#8221;</em>. Once the filter gets the request we modify the URI to the original state i.e. without the progress tail and look it up in the session.</p>
<p><pre class="brush: java;">
package com.wordpress.geekdev101.fileupload;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

public class UploadProgressFilter 
	extends OncePerRequestFilter
{
	private final String PROGRESS_TAIL = &quot;.progress&quot;; 
	
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		String requestUri = request.getRequestURI();
		
		String originalUrl = requestUri.substring(0, 
				requestUri.length() - PROGRESS_TAIL.length());
		
		String attributeName = ProgressCapableMultipartResolver.PROGRESS_PREFIX 
			+ originalUrl;
		
		Object progress = request.getSession().getAttribute(attributeName);
		if (progress != null) {
			ProgressDescriptor descriptor = (ProgressDescriptor)progress; 
			response.getOutputStream().write( 
					descriptor.toString().getBytes() );
		}
	}
}
</pre></p>
<p>To avoid writing a lot of <em>JavaScript</em> I am reusing the existing components available on-line. In this case <em>jQuery</em> and <em>jQuery Forms</em> making my code  very simple. Before submit we start polling the server for information. Note there are two options how the progress can be queried either polling or http binding. It is probably better to use binding in this case, but you could argue. For example, assuming that there always be a progress in a second interval. I am using polling because it is easier to implement and is this is just a silly example.</p>
<p><pre class="brush: jscript;">
var time;        
var elem = $('#progress'); 
var form = $('form').ajaxForm({ 
	beforeSubmit: function() {
		var url = form[0].action;

		var pos = url.indexOf(&quot;;&quot;); 
		if (pos != -1) url = url.substring(0, pos); 
		
		time = setInterval(function() {
			 $.get(url + &quot;.progress&quot;, function(data) {
				 if (!data) return;
				 data = data.split(&quot;/&quot;);
				 
				 elem.width( Math.round(data[0] / data[1] * 100) * 2 ); 
			 }); 
		}, 500); 
	}, 
	success: function() {
		clearInterval(time);  
		elem.width(100 * 2); 
	}
});
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/geekdev101.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/geekdev101.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/geekdev101.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/geekdev101.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/geekdev101.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/geekdev101.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/geekdev101.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/geekdev101.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/geekdev101.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/geekdev101.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/geekdev101.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/geekdev101.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/geekdev101.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/geekdev101.wordpress.com/61/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=geekdev101.wordpress.com&amp;blog=8926028&amp;post=61&amp;subd=geekdev101&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://geekdev101.wordpress.com/2009/10/25/java-file-uploads-with-progress-bar-in-spring-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9472154ca3949bd43fab2ae41803458f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">geekdev101</media:title>
		</media:content>
	</item>
		<item>
		<title>[WTL] Implementing multiple SDI views</title>
		<link>http://geekdev101.wordpress.com/2009/08/10/wtl-implementing-multiple-sdi-views/</link>
		<comments>http://geekdev101.wordpress.com/2009/08/10/wtl-implementing-multiple-sdi-views/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 18:54:47 +0000</pubDate>
		<dc:creator>geekdev101</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[SDI]]></category>
		<category><![CDATA[WTL]]></category>

		<guid isPermaLink="false">http://geekdev101.wordpress.com/?p=9</guid>
		<description><![CDATA[I came across a thread on WTL news group about multiple views in SDI application and also the answer which suggested to create all views on application start as hidden and change by using show window function. The solution definetely works, but from the implementation point of view seems a bit clumsy. I didn&#8217;t like [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=geekdev101.wordpress.com&amp;blog=8926028&amp;post=9&amp;subd=geekdev101&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I came across a thread on WTL news group about multiple views in SDI application and also the answer which suggested to create all views on application start as hidden and change by using show window function. The solution definetely works, but from the implementation point of view seems a bit clumsy. I didn&#8217;t like that you had to show or hide the view list whenever  a new view is added to your application (or maybe it was just for the sake of simplicity?).</p>
<p>Anyhow I would much better prefer some sort implementation as below:</p>
<p><pre class="brush: cpp;">
BEGIN_VIEW_MAP()
VIEW_ENTRY(1, CMultiviewSDIView )
VIEW_ENTRY(2, CHTMLView )
END_VIEW_MAP()
DEFINE_DEFAULT_VIEW(1);
</pre></p>
<p>In order to achieve the implementation it is assumed that each view would implement a common interface with two methods which are <em>HWND Init(HWND hContainer)</em> and <em>PreTranslateMessage</em> (just in case).</p>
<p>Using the approach each view can be implemented in its own class and avoids putting code inside MainFrame implementation. See a stub code for a view implementation: </p>
<p><pre class="brush: cpp;">
class CMultiviewSDIView :
   public CWindowImpl&lt;CMultiviewSDIView, CRichEditCtrl&gt;,
   public IManagerViewClient
{
   /* implementation here */ 
}
</pre></p>
<p>The MainFrame class would extend <em>CViewManager</em> and call <em>InitViewManager(HWND hContainer)</em> on create event. The container is assumed to be the client area of the document handle. To change a view simply call <em>ChangeView(UINT nID)</em> method also provided by the view manager implementation. The <em>nID</em> is the ID specified inside the view map (defined at the start of this post). That&#8217;s it&#8230; </p>
<p>Here is the complete implementation of the view manager and the client interface: </p>
<p><pre class="brush: cpp;">
#pragma once 
#include &quot;stdafx.h&quot;

#include &lt;atlcoll.h&gt;

interface IManagerViewClient
{
 public:
    virtual HWND Init(HWND hContainer) = 0; 
    virtual BOOL PreTranslateMessage(MSG* pMsg) = 0; 
};

struct _ViewMgrEntry {
   UINT nID;
   IManagerViewClient* _client;
};

struct _ViewMgrData { 
   HWND hClient; 
   const _ViewMgrEntry *_e;
};

#define BEGIN_VIEW_MAP() \
   static const _ViewMgrEntry* GetViewMap() \
   { \
      static const _ViewMgrEntry theMap[] = \
      {

#define VIEW_ENTRY(uid, cl) \
    { uid, new cl },

#define END_VIEW_MAP() \
       { (WORD) - 1, NULL },  \
   }; \

  return theMap; \
};

#define DEFINE_DEFAULT_VIEW(uid) \
static const UINT GetDefaultView() \
{ \
   return uid; \
}; \

template &lt;class T&gt;
class CViewManager {
private:
    _ViewMgrData m_pCurView; 
    ATL::CAtlMap&lt;UINT, _ViewMgrData&gt; m_viewMap; 

public: 
    IManagerViewClient* GetCurrentView() { 
    ATLASSERT(m_pCurView._e != NULL); 
    return m_pCurView._e-&gt;_client;
}

void ChangeView(UINT nView) 
{
   ATLASSERT(m_pCurView._e != NULL); 
   ATLASSERT(m_pCurView._e-&gt;nID != nView); 
 
   POSITION pos = m_viewMap.GetStartPosition(); 
   while (pos != NULL) {
      UINT nID = m_viewMap.GetKeyAt(pos);
      _ViewMgrData pDat = m_viewMap.GetNextValue(pos); 

      if (nID != nView)  {
        ::ShowWindow(pDat.hClient, SW_HIDE); 
      } else { 
        ::ShowWindow(pDat.hClient, SW_SHOW); 
         T* p = static_cast&lt;T*&gt;(this); 
         p-&gt;m_hWndClient = pDat.hClient; 
        m_pCurView = pDat; 
        p-&gt;UpdateLayout(); 
     }
  }
}

void InitViewManager(HWND hContainer) {
   T* p = static_cast&lt;T*&gt;(this); 
   const _ViewMgrEntry* theMap = p-&gt;GetViewMap(); 

   int nCount; 
   for (nCount = 0; theMap-&gt;nID != (WORD)-1; nCount++) {
      HWND _c = theMap-&gt;_client-&gt;Init(hContainer); 
      const _ViewMgrData pDat = { _c, theMap }; 
      
      if (theMap-&gt;nID == p-&gt;GetDefaultView()) 
      {
         m_pCurView = pDat;
         p-&gt;m_hWndClient = _c; 
      }
   
      m_viewMap.SetAt(theMap-&gt;nID, pDat); 
      theMap++; 
    } 
  }
};
</pre></p>
<p><em>P.S. I am not saying that this is a good solution or bad one, it is just an idea. </em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/geekdev101.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/geekdev101.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/geekdev101.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/geekdev101.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/geekdev101.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/geekdev101.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/geekdev101.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/geekdev101.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/geekdev101.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/geekdev101.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/geekdev101.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/geekdev101.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/geekdev101.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/geekdev101.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=geekdev101.wordpress.com&amp;blog=8926028&amp;post=9&amp;subd=geekdev101&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://geekdev101.wordpress.com/2009/08/10/wtl-implementing-multiple-sdi-views/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9472154ca3949bd43fab2ae41803458f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">geekdev101</media:title>
		</media:content>
	</item>
	</channel>
</rss>
