Tuesday, April 24, 2012

CSV Excel - Double Click Issue

So here I am again, with a fix for another issue which I tried to resolve with my colleague the other day.

Problem Statement:
We used CSVWriter to write CSV file's and on double clicking it; excel was not showing the special characters, to be precise korean characters. However if we were opening the same file in Notepad characters were rendering properly.

Issue:
The CSV file was getting generated without BOM(Byte Order Mark). BOM is nothing but the Unicode character used to signal the byte order of the text file or stream. Its code point is U+FEFF. BOM use is optional, and, if used, should appear at the start of the text stream

I detected this issue when I opened the file in Notepad++ and found "UTF8 without BOM" option was selected under "Format" menu. I then changed the format to "UTF8" and saved a copy of the file. Double click the file and Excel rendered all the korean characters.

Click here to read more on Byte Order Mark.

Fix:
We added below mentioned three statements to have BOM character at the start of the file. That's it.

OutputStream fos = new FileOutputStream("c:/test.csv");
//Write BOM Characters
fos.write(239); //0xEF
fos.write(187); //0xBB
fos.write(191); //0xBF

PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, "UTF-8"));
//Write all the required contents.

writer.close();
fos.close();

Thanks !!!

Thursday, March 15, 2012

Internet Explorer file downloads issue over SSL

Problem
File download fails in IE over SSL(https) protocol. I am posting another issue which I faced couple of days back. And it consumed approximately 4 hours of my time.

Reason
It happens when server response header contains "Pragma: no-cache" and/or "Cache-Control: no-store" or "Cache-Control: no-cache".

This is a known issue in IE and mentioned by Microsoft at http://support.microsoft.com/kb/323308.

Solution
While sending the response set "Pragma: cache" and/or "Cache-Control: Private" depending on the header you are receiving.

To track the response headers in IE you can use Fiddler tool. Or you can switch to Firefox.

I have been working on Struts-2 based web application. I end up implementing interceptor to modify response headers. I placed this interceptor just before workflow interceptor in my default interceptor stack.

Tip
While going through many forums I read it somewhere that above header setting should be used for only for IE and SSL.

Hope it helps.

Enjoy !!! :)

Tuesday, January 24, 2012

Broken Pipe Issue in Hibernate

Hey guys, here is one of the many key issues which I was facing in one the web based java application on which I am currently working on. Here is the development environment.
  • Java 1.6
  • Struts 2
  • Hibernate with C3p0
  • Postgres
  • Tomcat 6
Assuming database server & tomcat are both up an running and are on different machines. At any point if there is any network issues or for some reason database server goes down for few seconds, the c3p0/hibernate connection pool goes into bad state. And application keeps on throwing "Socket Exception" / "Broken Pipe" issue. And even if the database server comes up, it is not able to re-create the connection pool. The only solution to this issue is to re-start the tomcat server. There are other users who faced this issue with MySQL database.

There is key entry which was missing from my "hibernate.cfg.xml". Adding this entry resolves this issue.
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

Along with the above I do have following properties in my application
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.timeout">1800</property>

I tested this setting and it works fine.
I just fixed my broken pipe !!! :)