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

From String to any primitive type in 4 lines of code

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

 

Problem: i have some data in a text file, which contains 2 java.lang.Strings: the first one contains my data value, and the second one is the data type, i.e. “java.lang.Long”, “java.lang.Double”, etc.

How do i construct an object of the appropriate type containing the value in my text file?

Crappy solution: a loooong chain of if-else-if scanning through every supported type for choosing the appropriate parse* (Integer.parseInt, Double.parseDouble, etc.) method.

Why is this so crappy? Simply, because adding a supported type requires extending the if-else-if chain which may easily become too long and unreadable.

Funky solution: use the mighty powers of Java reflection!

Class objClass = Class.forName(type);
Class[] prototype = {String.class};
Constructor ctor = objClass.getConstructor(prototype);
return ctor.newInstance(new Object[]{value});

So what do these 4 lines do?
We get an instance of the Class object representing the class identified by the “type” String (the second string in our data file) and then we get the constructor which takes a single String as parameter, aka the constructor whose prototype is the one identified by our “prototype” variable; finally, we execute the constructor passing the value string.

Why is this so funky?

First of all, because it does in 4 lines of code what was done in a whoooole lot of lines in the crappy solution, but the very funky aspect of this solution is that it works not only with primitive types such as Integer, Long or Double, but also with any type which can be constructed with a String!

Veeeeeeeery funky!

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

5 Comments

Filtering an Access report for exporting

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

Ok, time for some un-funkiness, that goes by the name of MS Access: we love to have fun while coding, but as we still have to eat sometimes we may happen to come across such irritating technologies, and the funky way of dealing with them is sharing pieces of the arcane knowledge required.

So, let’s say you have a MS Access db and you want to export a distinct MS Word file for every record shown in a form, which may be, for example, a letter to send to every client displayed by your form.

MS Access, very kindly, provides the built-in macro command DoCmd.outputTo that allows to export anything in it, be it a form, a query, a table or a report in any format chosen among Xls, Rtf, Txt and Html: sounds exactly like what we need, doesn’t it?

We should just put somewhere in our form a button that does DoCmd.outputTo when clicked and everything should be ok, right?

Of course not, we’re still working with MS Access, an environment where “expected” is far from reality.

The button we just added doesn’t export the data shown in our form, it exports the whole table, which is definitely wrong; how do we solve this?

The solution is really tricky, unelegant and unmaintainable, but it works and that is enough: basically, we have to add a dummy, empty query and set it as our report’s data source, then we have to dynamically inject the appropriate SQL for our query:

Dim qd As DAO.QueryDef
Set qd = CurrentDb.QueryDefs("QueryTestReport")
qd.SQL = "Select * from people where id_person = " & idPerson

Where, of course, “QueryTestReport” is the dummy query we just added.

We’re halfway through our journey, then: we still need to produce a separate file for every record in our form’s RecordSet, but that’s quite easy, since MS Access forms expose their own recordset allowing us to loop through it:

Dim rs As Recordset
Set rs = Me.RecordsetClone
while not rs.EOF
   idPerson = rs("id_person")

Voila :)

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

Building a paged image gallery with jQuery

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

One of the funkiest features of jQuery is the enormous amount of plugins developed by the community: as every good developer knows, most problems have already been solved by someone else, so a good community of developers providing solutions to known problems is a key feature of any framework.

Today, we’ll see how to build a cool-looking paged image gallery using two jQuery plugins in combination: Galleria for image displaying and Pagination for (surprise!) pagination.

First of all, let’s build a single-page image gallery, we’ll add paging support later; Galleria is incredibly easy to use, you just have to put the images in an <ul> and then call  .galleria() on it:

<script type="text/javascript"></script>
<ul id="gallery">
	<li><img src="foo.jpg" alt="" /></li>
	<li><img src="bar.jpg" alt="" /></li>
</ul>

The result will look somehow like this (given some CSS tweaking):

galleria

Notice that the currently active image is displayed full-size and the rest of the images in the <ul> is displayed as a clickable thumbnail.

Of course Galleria provides some options to customize your image gallery rendering, and the most notable are:

  • The possibility to use your own CSS style for every element of the image gallery
  • The possibility to attach a callback to the event fired when a thumbnail is clicked and when a thumbnail is hovered
  • The choice weather to enable or not browser history, allowing the user to use the ‘back’ button in his/her browser

Let’s try adding some fancy animation effects on the thumbnails: for example, we want to have the hovered thumbnail highlighted and the selected image appear fading in, as in the demo:

function imageCallback(image, caption, thumb) {
	image.css('display','none').fadeIn();
	if(image.width() &gt; 500) {
		var ratio = image.width() / 500;
		image.css('width', '500px');
		image.css('height', image.height() / ratio);
	}	
 
	// fetch the thumbnail container
	var _li = thumb.parents('li');
		// fade out inactive thumbnail
	_li.siblings().children('img.selected').fadeTo(500,0.3);
		// fade in active thumbnail
	thumb.fadeTo('fast',1).addClass('selected');
}
 
function thumbCallback(thumb) {
	var _li = thumb.parents('li');
	var _fadeTo = _li.is('.active') ? '1' : '0.3';
	thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
	thumb.hover(
	function() { thumb.fadeTo('fast',1); },
	function() { _li.not('.active').children('img').fadeTo('fast',0.3); }
	)
}
 
$('ul.gallery').galleria({onImage: imageCallback, onThumb: thumbCallback});

And that’s it, fancy graphic effects on both images and thumbnails!

Now, on to pagination: let’s say you have a huge bunch of images in your gallery and having a single list with all the thumbnails is crappy…having just a dozen thumbnail tops for each page would be better…so how do we paginate our images?

With the pagination plugin :)

First of all, we need to store all our image paths and names in a javascript array:

    var images = [
        ['images/foo.jpg', 'foo'],
        ['images/bar.jpg', 'bar'],
        ['images/quux.jpg', 'quux']
    ];

The array will be handled by the pagination plugin which will internally deal with appropriate indices.

How do we do it? easy!

We add a div, with id=”Pagination”, that will contain page numbers, and then do

$("#Pagination").pagination(images.length, {callback: pageSelectCallback});

Is everything done, then?

No, but we just miss a small point: the pageSelectCallback.

function pageselectCallback(page_index, jq){
	var items_per_page = 2;
	var max_elem = Math.min((page_index+1) * items_per_page, images.length);
	var newcontent = '';
	for(var i=page_index*items_per_page;i
 
';
	}
	$('#imagelist').html(newcontent);
	$('#mainimage').html('');
	$('ul.gallery').galleria();
	return false;
}

Basically, we are calculating the array indices of the images in the current page, and then injecting the appropriate <li> items in the gallery <ul>, calling galleria() again to refresh the current page gallery.

You can see a very basic demo of this tutorial here…have fun!

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

13 Comments

is HTML the new PL/SQL?

Devviness rating ★★☆☆☆ 

Funkiness rating ★★★★☆ 

Back in the old days when dinosaurs were the rulers of the Earth and mankind was still far to come, PL/SQL was one of the funkiest languages a developer could have: it allowed you to perform any sort of complex operation on your data without leaving your DBMS (typically, the big creepy Moloch known as Oracle).

Back then, hardware performances were worse than crap (think about Moore’s law and apply it to ages ago), so any possible room for optimization had to be explored and performing operations on data without converting it to programmer-friendly format was more of a commitment than a choice.

After a while, the natural process of language evolution came in and many factors determined PL/SQL’s obsolescence, the most notable being the paradigm shift from procedural languages to object-oriented languages and the natural process that tends towards higher level abstractions as time goes by, along with the emerging need of appealing GUIs and multi-tiered systems which made it impossible to have entire, complex applications made out just of data-processing logic.

Nowadays, then, PL/SQL has been replaced by ORMs such as Hibernate (and its brother NHibernate) or Ibatis, even if some dinosaurs who can’t accept the change still exist: performance issues due to data conversion from DB to OO data types aren’t a concern thanks to the increase in hardware performances and ORMs allow the funky developers to intentionally ignore the underlying logic of the DBMS, which is topic for the DBAs, and focus on business objects, thus providing a higher level of abstraction.

As of today, then, the typical OO programmer isn’t required to write complex PL/SQL stored procedures that need thousands of LOC just to perform CRUD operations (ideally, he shouldn’t be required to write even the simplest of SQL queries thanks to the ORM layer sitting between him and the DBMS, but sometimes knowing SQL for a quick check on the DB can still help) and is now able to handle data in the most comfortable way, which is the object-oriented way.

(Note: the “Why (PL/)SQL sucks and you should use an ORM” topic is extremely wide and isn’t the main topic of this post, so we won’t go any further on this which is just an example)

Back in the early days of web development, developers were required to write massive amounts of monkey code: think of huge layouts made of tables, hundreds of thousands of lines of CSS to fix every possible browser quirk (don’t forget IE4!) and javascript code filled with document.getElementById and document.getElementsByTagName, all written by hand (don’t get me started on those deadly technologies that mix server-side business logic and presentation html, such as JSP and poorly-written PHP…you just need to know that those who practice these criminal activities should burn in hell).

Now think of those monsters you probably have seen in museums that look like DECLARE FOO (BAZ AS INTEGER, QUUX AS VARCHAR) AS PACKAGE, DECLARE PACKAGE BODY FOO, BEGIN, /*250 LOC to perform 2 queries, possibly with proprietary DB commands */ END…any similarity?

This is low-level, and low level is bad.

Why?

Low level == more lines of code == more space for bugs.

Low level == less space for abstraction == less time to focus on application logic and more time spent on trivial operations

In general, when you have to write low level, boilerplate code the best solution is to have someone else write it for you, and i’m not meaning offshoring.

The quick, dirty and oldschool solution is using a WYSIWYG editor that writes code according to what you specify visually, but it still produces a huge amout of code, which sometimes isn’t really the best you could get (it seems impossible, but there are websites made with FrontPage!) and still needs to be scanned through when debugging, so the best solution would be to have someone trusted to write code in a black box way such that you don’t need to see it…after all, who cares about how Hibernate performs his transformations from Criteria queries to SQL? It works, and it’s as much as i want to know.

HTML has been providing a higher level abstraction for quite a long time now: the DOM.

Just as Hibernate lets you handle DB data as objects, the DOM allows the funky developer to handle the HTML structure of a web page in an object-oriented way, as if it was a tree.

This is already quite funky, but the Javascript DOM API isn’t perfect (document.getElementById isn’t a perfect way to refer to a single element in a page, come on), so another level of abstraction was required.

A vanilla abstraction may be represented by all those funky Javascript frameworks that provide nice and clean APIs to perform complex tasks such as animation or drag&drop, but they still leave you unprotected from the evil beasts of CSS hacking: a good abstraction would let the developer abstract (exactly) from these implementation details allowing him/her to focus on what really matters in his/her application, i.e. business logic.

The question, now, is “are there any abstractions of this kind?” and the answer, luckily, is “yes”.

There are frameworks, such as GWT or Echo2, that allow the developer to write Java code as if he/she was writing a desktop application, but the output is valid and working XHTML+CSS+Javascript that autonomously deals with OS issues, browser quirks and any other display of Satan’s existence in the real world.

“But i don’t know Java, i only know HTML, CSS and Javascript!”

Ok, you can sit there, in the back row, next to the PL/SQL dinosaurs.

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

13 Comments

There is no silver bullet

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


As every good developer knows, there is no silver bullet in software development: no language, framework or toolkit can solve any problem, and a big deal of the effort required for any task depends on the choice of the right tool. 

This may seem evident in ordinary construction (or is there anybody who would use a screwdriver to cook pizza?), but when it comes to software it’s often easy to be biased by previous experience or personal, religious views.

Even worse, the wrong tool choice may be imposed by management: usually it’s a former-programmer-now-become-manager who forces the whole development team to use his favourite language/framework/toolkit/set of practices, ignoring that the word “former” in his role means his knowledge is, at best, outdated (this is the reason why young coders are still being forced to learn vintage technologies such as PL/SQL or JSP).

As common knowledge says, anyway, management and the funky developers speak different languages, so what we call “silver bullet” in management-ese sounds like “enterprise”: it doesn’t matter if your tool is right or wrong for your problem, if it will speed up your coding or will sit between you and a delivered project waiting to be dribbled, what usually matters to the pointy-haired bosses is if it’s “enterprise” (which probably means every pointy-haired boss in the world has a secret passion for Star trek).

Let’s pick a real world class of problems as an example: CMSs.

According to Wikipedia, a CMS is “A content management system (CMS) is a computer application used to create, edit, manage, search and publish various kinds of digital media and electronic text”, which seems pretty straightforward and not enterprisable; do we really need an enterprise technology to efficiently setup a CMS-based website?

Short answer: no.

Long answer: no, and let’s see why using the most famous Java-based CMS around, OpenCMS.

(Note: in case somebody didn’t know, “enterprise” and Java are almost synonyms)

OpenCMS is the typical, almost paradigmatic case of a tool that keeps on growing in size and complexity to be as enterprisey as possible until it becomes a giant Moloch that stands in the funky developer’s way instead of letting him run freely through his own path towards a working project.

Let’s think of the most common operation you might want to do on your customized CMS: developing your own module.

Typically, a CMS module is a small snippet of dynamically generated HTML dropped in your pages, whose content is usually generated by server-side scripting; usually, CMS modules aren’t very big in terms of lines of code and computational cost, because they constitute just a small part of the page which is (as the “C” in “CMS” may suggest) focused on its main content.

So, developing a custom CMS module shouldn’t be an enormous project and thus it shouldn’t require most of the infrastructure required by medium-to-large sized software projects: what is really needed to develop a custom CMS module is a good text editor (an IDE may even be too much) and a quick and easy way to test quick and easy modifications to the source code.

OpenCMS provides a built-in text editor and WebDav support to import and export modules via http, which works in combination with the Eclipse WebDav plugin to allow importing and exporting to and from your Java IDE.

Sounds cool, isn’t it?

Unfortunately, the built-in editor looks like this:

OpenCMS Screenshot

OpenCMS Screenshot

(click image to see it full-size. Warning! May damage eyesight)

which makes it completely unusable.

Using WebDAV on an OpenCMS website, instead, requires an afternoon of configuration tweaking to your application server to be set up, and another afternoon of tweaking your Eclipse environment to reach a wonderful speed of 1 shortcut + 5 clicks + TILDE20 seconds waiting for the server to answer to deploy every single change to any source file.

“No way, i’m using FTP to upload my modified files on the application server”

Are you kidding? FTP? FTP isn’t enterprise! Call an exorcist!

OpenCMS, which is *really* enterprise, doesn’t expose its file system to you funky bastards who want to be in control…OpenCMS HAS ITS OWN FILESYSTEM.

It’s called VFS, which stands for Virtual File System, and is probably a hybrid obtained breeding a real filesystem with a db (i don’t really want to know the details, i may be embarassed), and what’s really enterprisey with it is that it can’t be accessed in any way other than OpenCMS’s own file explorer or WebDAV.

I can only imagine the amazed look on your faces, now…but the really fun thing about this OpenCMS and VFS stuff is that…somebody actually uses it!

Obviously, the number of poor people frustrating themselves with such torture is waaaaay smaller than the one of people using a working, PHP-based CMS (and this alone should be a reason NOT to use OpenCMS…unless you don’t care about user knowledge base, documentation, plugins and similar stuff), but there really is someone using it, and it’s probably someone whose management thinks PHP is good only for garage-based fansites and Java is the only possible solution for enterprise projects.

So please, the next time you begin any kind of software project, please pay a little attention and spend some time choosing the right bullet for your target, as excluding some technologies due to religious issues (yes, ‘enterprise’ is a religion, since it makes its integralists completely blind) will most likely cause you utter pain.

Besides, once you discover that the best technology for your project isn’t in your skill set, the funkiest thing you can do is to learn something new instead of dumping it and sticking with your old-fashioned framework :)

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

,

4 Comments