Posts Tagged wtf

Downloading a file through a servlet on a cluster

Funkiness rating ★★★☆☆ 
Devviness rating ★★★☆☆ 

Let’s say you have a servlet that reads a file from the file system and streams it to the HTTP response that goes back to the client, like this:

BufferedInputStream bis = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(f));
		} catch (FileNotFoundException fnfe) {
			logger.error(fnfe.getMessage());
			return;
		}
 
		OutputStream os = null;
		try {
			os = response.getOutputStream();
			while((bytesRead = bis.read(buf)) != -1) {			
				os.write(buf, 0, bytesRead);				
			}
			os.flush();
			bis.close();
			os.close();
		} catch (IOException ioe) {
			logger.error(ioe.getMessage());
			return;			
		}

Let’s say you deploy the servlet on your local machine and test it extensively with every major browser, and it works, so you deploy it on the production clustered server, test it again and…bam!

Explorer doesn’t work, it keeps hanging before starting the download, in the “Requesting file information” phase.

To add even more WTF, if you connect directly to one of the clustered servers instead of going through the load balancer everything works perfectly

If you’re in this situation, panic is an accepted reaction.

Luckily (at least, for you), someone has already been in the same situation, and that would be me, so i can easily spare you a couple of afternoons of digging and investigation: fire up your Firebug - God bless it forever and ever and ever - and, using the “Net” tab, check that the HTTP response headers are the same when going through the load balancer and when directly connecting to a clustered server.

In my case - surprise! - they weren’t.

My load balancer was misteriously adding “Cache-Control: no-cache” to every HTTP response.

“So, what does ‘Cache-Control’ have to do with downloading files?”

Of course, nothing, but it seems IE has a problem downloading files when the response has “Cache-Control: no-cache”.

So, here are the lessons learned that i will kindly pass on to all my loving readers without having them experience the rage i had in the last few days and that sum up the content of this post:

  • IE sucks (didn’t you know?)
  • IE can’t download files if the HTTP response has “Cache-Control: no-cache”
  • Firebug is a great gift of the gods to us mortals
  • Deploying on a clustered server requires extensive HTTP response testing, as the load balancer may mess up your responses



All’s well, that ends well.

Add This! Blogmarks BlogMemes BlueDot BlogLines co.mments Connotea del.icio.us de.lirio.us Digg Diigo Facebook Google Google Reader icio.de IndianPad Leonaut LinkaGoGo Linkarena Linkter Magnolia MyShare Yahoo! MyWeb Netscape Newsgator Newsvine reddit Rojo Segnalo Shadows Simpy SlashDot Spurl Startaid StumbleUpon TailRank Technorati ThisNext

, , ,

No Comments