Large File Downloads with JBoss Seam 2 and Servlets

Today I worked an an issue which presented users of our application a 403 error page when downloading large files which were generated on the fly. The files were delivered by a servlet and the files were directly copied to the ServletOutputStream, so the files were streamed instead of holding them in memory.

After some debugging it turned out, that the files were not streamed since the Seam Ajax4JSF Filter doesn’t allow to stream files and this resulted in OutOfMemoryExceptions. There are a few ways how to disable or extend the Ajax4JSF Filter (see this Seam Forum Thread).

Since all our download resources are mapped to servlets which resides in the „/media/*“ path, it was pretty easy to just enable the Ajax4JSF Filter for all paths except the „/media/*“ paths. To achieve this, just add to your components.xml:

<web:ajax4jsf-filter regex-url-pattern="^(?:[^/]|/(?!media/))*$"/>

Set Context Root for a JavaEE 6 Application with JBoss 7.1

Per default, a Web Application which is packaged as a WAR file is mapped to „http://jbossurl:port/war_file_name“ when deployed to JBoss AS 7.1. We wanted the application to be mapped to the root context instead of the file name. It turns out, that this is pretty easy to achieve:

  1. Edit standalone.xml in the JBoss configuration directory. Edit the relevant part of the file like this:
    <subsystem xmlns="urn:jboss:domain:web:1.0" default-virtual-server="default-host">
        <connector name="http" protocol="HTTP/1.1" socket-binding="http" scheme="http"/>
        <virtual-server name="default-host" enable-welcome-root="false">

    The important part is the enable-welcome-root=“false“.

  2. Create a file „jboss-web.xml“ in the WEB-INF folder of your application.
  3. Add the following to the file:

    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-web>
        <context-root>/</context-root>
    </jboss-web>

Now your Application will be mapped to the context-root of the Application Server and should be accessible with „http://jbossurl:port“.

Programatically Login User with Seam 2 Security Framework

This is a quick one:

When you want to login a user programmatically without checking if the credentials are right or the user is in the IdentityStore with Seam 2, that is pretty easy.

 
final Principal principal = new SimplePrincipal("username");
identity.acceptExternallyAuthenticatedPrincipal(principal);
identity.authenticate();

This will override the Seam 2 Security Framework authentication mechanism and a user with the given username will be logged in, even if the user is not in the IdentityStore of the Application.

I use this regularly when I have some sort of administration interface where I cannot use the default IdentityStore of the application. In this case I provide an own implementation of the required authentication mechanism and just log in the the user with the approach described above. When doing this I add a role to the logged in user (via identity.addRole("myRole")), so I can use all the Seam goodies like the Authorization Annotations (@Restrict) or checks like Identity.loggedIn.

In-Memory Database for Seam TestNG Tests

Today I struggled with getting my TestNG Integration Tests working in my Seam 2 project. It’s not a very smart idea to use the production/development database for database tests and here’s why:

1. It is damn slow
2. We want to separate „real“/fake data from unit test data.
3. Under normal circumstances we want a clean database or a database with predefined data (see )
4. We want the unit tests to run everywhere. It should not matter if we have a Windows machine, a Linux Buildserver or a MacOSX. It should just work. Oh and I forgot. We even want it to work without Internet Connection.
5. …

So I decided to use HSQLDB for my Unit tests.

The first barrier was getting TestNG working. Weiterlesen

Upgrade Seam Project froom 2.2.1.CR1 to 2.2.1CR2

We had a Problem with JBoss Seam 2.2.1.CR1 Logger. We were not able to log with parameters. (This is a known bug: https://jira.jboss.org/browse/JBSEAM-4606 )

I was looking for a tutorial which covers upgrading Seam, but I did not find any. So this is my solution how to upgrade from Seam 2.2.1.CR1 to Seam 2.2.1.CR2:

It is kind of a trivial job, because in normal case you just have to replace libraries. Here is a step to step tutorial:

Weiterlesen

Add JBoss Repository to Nexus

JBoss changed its Repository URL, so our Nexus Repository Manager was not able to download the newest Hibernate Version (3.5.1-Final). Although the old JBoss Repository is readable and searchable we had problems downloading the .jar files. We changed the URL to the new JBoss Repository URL and everything worked fine again.

This is the new URL:

https://repository.jboss.org/nexus/content/groups/public