Thursday, May 21, 2009

Directly download a file by pressing a link using Java Servlet

There are times in which we would like to directly download a resource by pressing on a link on a page. The meaning of directly is:
  • The browser dialog will be open asking where to save the resource.
  • After saving the file the browser will stay on the same page without doing any refresh or opening a child window.

Usually this is useful if we would like to allow the user to download some real time dynamically generated file that is being created on the server side.
For the example we will use the ZipDir class from this post. We will see how we can allow the user download a dynamically created zip file by pressing on a link.

In general, in order to achieve the above behavior we have to instruct the response with 2 instructions:

  • Set the response content type to: “application/zip”. This is in the case of sending back to the client a Zip file. For any other file type we should set the correct content type. For example, if we would like to send back to the client a PDF file the following content type should be used: “application/pdf”.
  • Set response header: “Content-disposition” with the following value: “attachment; filename=<filename>”, where filename is the name of the file we want the user to see in the “save” dialog.

Here is an example of a Servlet that generates a Zip file on the fly and sends it back to the browser:

package com.bashan.blog.zip;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ZipServlet extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/zip");
    response.setHeader("Content-disposition", "attachment; filename=demo.zip");
    ServletOutputStream outputStream = response.getOutputStream();
    new ZipDir("/somedir", outputStream).zip();
  }
}

Note that our ZipDir class is smart enough to set the generated Zip file directly to the output stream of the response. That is a neat solution saving us from needing to generate temporary Zip files on the server that later will have to be deleted.

Nothing special on the web.xml file, just define a Servlet, with “zip” url pattern:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">
    <servlet>
      <servlet-name>Zip Servlet</servlet-name>
      <servlet-class>com.bashan.blog.zip.ZipServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>Zip Servlet</servlet-name>
      <url-pattern>/zip</url-pattern>
    </servlet-mapping>
      
</web-app>

And finally, a sample HTML page with a download link to the Zip resource:

<html>
 <head>
  <title> Zip Download Sample Page </title>
 </head>
 <body>
 <h3>Zip download sample page</h3>
 Press this link: <a href="zip">Download Zip resource</a>
 </body>
</html>
Note that we call “zip” Servlet directly as a relative path to the server context.

Monday, May 18, 2009

JSF input - empty value converted to Zero

When running a JSF application under Tomcat, you might encounter a weird case in which an empty value of a field bidden with numeric property (like Integer) is converted to zero instead of remaining "null".

Consider the following case, in which text field is used to input Integer value:

<h:inputText id="myInput" value="#{myBean.myInteger}" converter="IntegerConverter" />

And the bean holding the value:

package com.bashan.blog.jsf;
public class MyBean {
  private Integer myInteger;
  public Integer getMyInteger() {
    return myInteger;
  }
  public void setMyInteger(Integer myInteger) {
    this.myInteger = myInteger;
  }
}
Apparently, Tomcat is coercing “null” values to zeroes. This behavior can be disabled by setting the following JVM parameter to "false":
-Dorg.apache.el.parser.COERCE_TO_ZERO=false

By default the “true” value is used. By setting this value to “false” Tomcat will no longer try to coerce value expressions.

You can also set this parameter in Java code. This may be a better idea, if you don’t want to remember to set this parameter on each new environment you deploy your web application. Here is how it can be done:

System.getProperties().put("org.apache.el.parser.COERCE_TO_ZERO", "false");

You can run this code when your server first loads in some initialization Servlet.

If I recall correct, this behavior was changed sometime along the development of Tomcat. That means you can experience different behaviors on different environments of Tomcat. For example, Production environment behaves differently from Development environment, since there are 2 distinct versions of Tomcat. This may cause a bit confusion, when trying to identify the problem.

Sunday, May 17, 2009

Recursivly Zipping a directory in Java – Extending capabilities

In this post: Recursivly Zipping directory in Java we saw how we can use Java to easily zip all the contents of a directory including its subdirectories. In this post we will extend the ZipUtil class (talked on the mentioned post) to have more capabilities:

  • Accept FileFilter allowing to zip only desired files from a directory.
  • Get notifications on zip process:
    • Zip start.
    • Zip ended.
    • Before processing a directory.
    • Before processing a file.

These features allows us to gain better control over the Zip processes. The “Zip start” and “Zip end” notifications are not so necessary, since you are very well aware in the code when you start to zip a directory and when the zipping processes is ended. These methods are added as a refinement.

In order to implement these requirements the ZipUtil class will be converted to a ZipDir class, and all its static methods will be converted to a non static methods. This is required, since as much as static methods are a nice way of easily supplying utility functions, they do not give us the real benefit of object oriented programming: inheritance.

The logic behind the zip process remains pretty much the same as it was on the previous zip related post, but instead of one loop iterating directories and files, there will be 2 loops: one iterates directories and the second iterates files in a specific directory. This is needed, since we would like to get all files in a directory using a FileFilter. FileFilter in java is a simple interface with one method needed to be implemented: accept(File pathname). The accept method determines if a file should or shouldn’t be included in the result of calling the method listFiles of the File class.

In addition, 4 method are added to the ZipDir class:

  • beforeZip(): This method is called before the Zip process is started.
  • afterZip(): This method is called after the Zip process is ended.
  • beforeProcessDirectory(File dir): This method is called before processing every new directory. If this method returns false for a specific directory, this directory and all its sub directories will not be processed.
  • beforeProcessFile(File file): This method is called before zipping every new file. If this method returns false the specific file will not be zipped.

After these changes, the new ZipDir class looks like this:

package com.bashan.blog.zip;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipDir {
  private static final int BUFFER_SIZE = 1024 * 4;
  protected String dir;
  protected FileFilter fileFilter;
  protected ZipOutputStream zipOutputStream;
  int relativePos;
  public ZipDir(String dir, FileFilter fileFilter, OutputStream outputStream) {
    this.dir = dir;
    this.fileFilter = fileFilter;
    this.zipOutputStream = new ZipOutputStream(outputStream);
    relativePos = dir.length() + 1;
  }
  public ZipDir(String dir, OutputStream outputStream) {
    this(dir, null, outputStream);
  }
  public ZipDir(String dir, FileFilter fileFilter, String filenameZipOut) throws IOException {
    this(dir, fileFilter, new FileOutputStream(filenameZipOut));
  }
  public ZipDir(String dir, String filenameZipOut) throws IOException {
    this(dir, null, filenameZipOut);
  }
  public void zip() throws IOException {
    try {
      beforeZip();
      zip(new File(dir));
      afterZip();
    }
    finally {
      if (zipOutputStream != null) {
        zipOutputStream.close();
      }
    }
  }
  private void zip(File dir) throws IOException {
    if (dir.exists() && dir.isDirectory()) {
      File[] dirs = dir.listFiles(new FileFilter() {
        public boolean accept(File file) {
          return file.isDirectory();
        }
      });
      for (File currentDir : dirs) {
        if (beforeProcessDirectory(currentDir))
        {
          zip(currentDir);
        }
      }
      File[] files = dir.listFiles(fileFilter);
      for (File file : files) {
        if (file.isDirectory()) {
          continue;
        }
        if (beforeProcessFile(file))
        {
          InputStream in = null;
          try {
            String filename = file.getPath();
            zipOutputStream.putNextEntry(new ZipEntry(filename.substring(relativePos)));
            in = new FileInputStream(file);
            int len;
            byte[] buf = new byte[BUFFER_SIZE];
            while ((len = in.read(buf)) != -1) {
              zipOutputStream.write(buf, 0, len);
            }
            zipOutputStream.closeEntry();
          }
          finally {
            if (in != null) {
              in.close();
            }
          }
        }
      }
    }
  }
  protected boolean beforeProcessDirectory(File dir)
  {
    return true;
  }
  protected boolean beforeProcessFile(File file)
  {
    return true;
  }
  protected void beforeZip()
  {
  }
  protected void afterZip()
  {
  }
}
Here are some examples of using the ZipDir class:

Zip all files in a directory. No filtering used:

new ZipDir("/somedir", "/somezip.zip").zip();

Zip all icon files in a directory. Showing example of using a FileFilter:

    new ZipDir("/somedir",
        new FileFilter() {
          public boolean accept(File file) {
            return file.isFile() && file.getPath().endsWith("ico");
          }
        }, "/somezip.zip").zip();

Zip all files in a directory excluding directories ending with “def” string. Showing the usage of beforeProcessDirectory(File dir) method:

    new ZipDir("/somedir", "/somezip.zip")
    {
      @Override
      protected boolean beforeProcessDirectory(File dir)
      {
        return !dir.getPath().endsWith("def");
      }
    }.zip();

The above examples use java anonymous class to both implement a FileFilter and ZipDir class. We can also extend the ZipDir class in the traditional way, to give it more capabilities as we wish. For example, this is extension to the ZipDir class that collects small statistical information on the zipping process:

package com.bashan.blog.zip;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
public class StatZipDir extends ZipDir {
  protected int numDirs;
  protected int numFiles;
  protected Date zipStart;
  protected Date zipEnd;
  public StatZipDir(String dir, FileFilter fileFilter, OutputStream outputStream) {
    super(dir, fileFilter, outputStream);
  }
  public StatZipDir(String dir, OutputStream outputStream) {
    super(dir, outputStream);
  }
  public StatZipDir(String dir, FileFilter fileFilter, String filenameZipOut) throws IOException {
    super(dir, fileFilter, filenameZipOut);
  }
  public StatZipDir(String dir, String filenameZipOut) throws IOException {
    super(dir, filenameZipOut);
  }
  public int getNumDirs() {
    return numDirs;
  }
  public int getNumFiles() {
    return numFiles;
  }
  public Date getZipStart() {
    return zipStart;
  }
  public Date getZipEnd() {
    return zipEnd;
  }
  @Override
  protected boolean beforeProcessDirectory(File dir) {
    numDirs++;
    return super.beforeProcessDirectory(dir);
  }
  @Override
  protected boolean beforeProcessFile(File file) {
    numFiles++;
    return super.beforeProcessFile(file);
  }
  @Override
  protected void beforeZip() {
    zipStart = new Date();
  }
  @Override
  protected void afterZip() {
    zipEnd = new Date();
  }
  public static void main(String[] args) throws IOException {
    StatZipDir statZipDir = new StatZipDir("/someDir", "zomezip.zip");
    statZipDir.zip();
    System.out.println("Total zipped directories: " + statZipDir.getNumDirs());
    System.out.println("Total zipped files: " + statZipDir.getNumFiles());
    System.out.println("Total zip time: " + (statZipDir.getZipEnd().getTime() -
        statZipDir.getZipStart().getTime()) / 1000 + " seconds");
  }
}
At the bottom of this class there is a small test program outputting the number of directories and files that were zipped as well as the total time in seconds the zip processes took.

You can find the code for ZipDir class here and the code doe the StatZipDir class here.

Friday, May 15, 2009

Recursivly Zipping a directory in Java

There are times we need to package and compress group of files. Java has out-of-the-box support for Zip compression algorithm.
By taking advantage of two classes: ZipOutputStream, ZipEntry we can easily compress any set of files of any size.
The class ZipOutputStream is similar to any other OutputStream class. It allows writing chunk of information to the stream. The class ZipEntry defines a single entry in the created Zip file. An entry is actually a file we would like to archive and compress. Zip archiving method made it very easy, since we don't have to deal with directories in the Zip file at all. In the entry we have to specify the file we would like to add along with its path or relative path. For each file we add to the stream we need to open a new entry.
We would like to use Java ZipOutputStream and ZipEntry to construct a method that gets any directory and zip it to a single Zip file. We will pay attention to two things:

  • All files and directories under the given directory should be recursively zipped.
  • The path of all zipped files will be relative to the given directory.
  • It will be possible to Zip all file to a new Zip file or to a Stream.

Java made all the hard work for us, hiding the complexity of the Zip algorithm. All we has to do is:

  • Create a ZipOutputStream instance.
  • Scan all files and directories of the given directory.
  • For each file we find in the directory:
    • Find file relative path. This is the path starting from the directory we would like to Zip.
    • Add new file entry using putNextEntry method of ZipOutputStream instance and ZipEntry class. This looks something like: zipOutputStream.putNextEntry(new ZipEntry(fileRelativePath)).
    • Read file and write it to Zip stream.
    • Close read file.
  • For each directory we find in the directory:
    • Recursively call our Zip method with the new directory.
  • When all files and directory has been processed: Close Zip stream.

And, this is how it looks in Java code:

package com.bashan.blog.zip;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
  private static final int BUFFER_SIZE = 1024 * 4;
  public static void zipDir(String dir, OutputStream out) throws IOException {
    ZipOutputStream zipOutputStream = null;
    if (out == null) {
      throw new NullPointerException("No output stream supplied");
    } else {
      try {
        zipOutputStream = new ZipOutputStream(out);
        int relativePos = dir.length() + 1;
        zipDir(new File(dir), zipOutputStream, relativePos);
      }
      finally {
        if (zipOutputStream != null) {
          zipOutputStream.close();
        }
      }
    }
  }
  public static void zipDir(String dir, String zipFilename) throws IOException {
    FileOutputStream fileOutputStream = null;
    try {
      fileOutputStream = new FileOutputStream(zipFilename);
      zipDir(dir, fileOutputStream);
    }
    finally {
      if (fileOutputStream != null) {
        fileOutputStream.close();
      }
    }
  }
  private static void zipDir(File dir, ZipOutputStream zipOutputStream, int relativePos) throws IOException {
    if (dir.exists() && dir.isDirectory()) {
      File[] files = dir.listFiles();
      for (File file : files) {
        if (file.isDirectory()) {
          zipDir(file, zipOutputStream, relativePos);
        } else {
          InputStream in = null;
          try {
            String filename = file.getPath();
            zipOutputStream.putNextEntry(new ZipEntry(filename.substring(relativePos)));
            in = new FileInputStream(file);
            int len;
            byte[] buf = new byte[BUFFER_SIZE];
            while ((len = in.read(buf)) != -1) {
              zipOutputStream.write(buf, 0, len);
            }
            zipOutputStream.closeEntry();
          }
          finally {
            if (in != null) {
              in.close();
            }
          }
        }
      }
    }
  }
  public static void main(String[] args) throws Exception {
    zipDir("C:\\some\\dir", "C:\\somedir.zip");
  }
}

On the bottom of the class there is a small main program showing an example of Zipping a directory to a file.

You can download the class by pressing here.

Wednesday, May 13, 2009

Getting Class real path in Java

Getting web application real path is pretty straight forward:
getServletContext().getRealPath("/")
This code returns the physical root directory of a web application. Of course the method getServletContext() exists only in classes extending HttpServlet class. That mean, this method is available only for Servlets.
Sometimes it is useful to get the real path of a class. Something quite similar to the code shown above, but for a class and not for a Servlet. This is simple as well, but a bit less obvious:
AnyClass.class.getResource("/")
The function getResource with the parameter “/” returns a URL class containing the root directory in which the class resides.

Getting the full directory of the class can be done by simply passing empty string as a parameter:

AnyClass.class.getResource("")

For example, this code:

package com.bashan.blog.clazz;
public class AnyClass {
  public static void main(String[] args)
  {
    System.out.println(AnyClass.class.getResource("/"));
    System.out.println(AnyClass.class.getResource(""));
  }
}

Prints this output:

file:/C:/Users/bashan/IdeaProjects/out/production/general/
file:/C:/Users/bashan/IdeaProjects/out/production/general/com/bashan/blog/clazz/

So, what will happen if we try to use getResource() function on a class located in a jar file?

  • For the call getResource(“/”) we will still get the out class root directory.
  • For the call getResource(“”) we will get a URL containing our jar file and a full path to the location of the class in the jar. Note, that the protocol of the URL is “jar”.

For example for the following code:

package com.bashan.blog.clazz;
import org.json.simple.JSONArray;
import java.net.URL;
public class AnyClass {
  public static void main(String[] args)
  {
    System.out.println(JSONArray.class.getResource("/"));
    System.out.println(JSONArray.class.getResource(""));
    URL url = JSONArray.class.getResource("");
    System.out.println(url.getProtocol());
  }
}

We will get the following output:

file:/C:/Users/bashan/IdeaProjects/out/production/general/
jar:file:/C:/Users/bashan/IdeaProjects/general/lib/json_simple-1.1.jar!/org/json/simple/
jar

And finally, if we try the getResource() on Java standard classes like String:

package com.bashan.blog.clazz;
public class AnyClass {
  public static void main(String[] args)
  {
    System.out.println(String.class.getResource("/"));
    System.out.println(String.class.getResource(""));
  }
}

We will get:

file:/C:/Users/bashan/IdeaProjects/out/production/general/
null
Again, for the parameter “/” we get the class path of our code. And for the empty parameter we get “null”.