Archive for the ‘Java’ Category

Java Stateless Session Filter released on sourceforge

January 24th, 2010

I just finished publishing on sourceforge a simple filter named Java Stateless Session Filter which let you change the way your application use HTTP session without refactoring the code, or share user session between webapps while using the standard session API.

Several session stores are available out of the box :

  • Cookie, with variants : plain (development only), Encrypted, JSON based
  • Memcache
  • Native session (when some attributes doesn’t need sharing)

The inital code is released under the Apache License v2.0 by Capgemini.
Capgemini Logo

Status:
This code was written in a few days as a Proof Of Concept and was tested with Struts2, Wicket and JSF.

Feel free to test this filter and report success or issues. See the support page for project forums and trackers.

How it works:
How it works

Usage:
The project exposes a maven2 repository. To add this filter on your application, just add repository and dependencies to your project pom :

<repository
<id>statelessfilter-repository</id>
<name>Stateless Filter repository</name>
<url>http://statelessfilter.sourceforge.net/maven2/repository</url>
</repository>


<dependency>
<groupId>net.sourceforge.statelessfilter</groupId>
<artifactId>stateless-core</artifactId>
<version>0.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>


<dependency>
<groupId>net.sourceforge.statelessfilter</groupId>
<artifactId>stateless-cookie-plain</artifactId>
<version>0.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

Then add the filter in WEB-INF/web.xml :

<filter>
<filter-name>session</filter-name>
<filter-class>net.sourceforge.statelessfilter.filter.StatelessFilter</filter-class>
</filter>

(...)

<filter-mapping>
<filter-name>session</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

For additional details, see the complete documentation on sourceforge.

Displaytag + Struts 2 localization

August 12th, 2009

Displaytag 1.2 (current latest release) provides several LocaleResolver, but none works with the latest version of Struts 2.1.X. There was a small change in Xwork API breaking the I18nWebworkadapter. ActionContext#getValueStack() now returns a ValueStack instead of OgnlValueStack.

Here is the updated LocaleResolver for Struts 2.1.6 :
/**
* Licensed under the Artistic License; you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://displaytag.sourceforge.net/license.html
*
* THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
package org.displaytag.localization;

import java.util.Iterator;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.views.jsp.TagUtils;
import org.displaytag.Messages;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.util.ValueStack;

/**
* Struts2 implementation of a resource provider and locale resolver.
* @author Richard HALLIER
* @author Fabrizio Giustina
* @author Nicolas Richeton - Fix for Struts 2.1.6
* @version $Revision: 1081 $ ($Author: fgiust $)
*/
public class I18nStruts2Adapter implements LocaleResolver, I18nResourceProvider
{

/**
* prefix/suffix for missing entries.
*/
public static final String UNDEFINED_KEY = "???"; //$NON-NLS-1$

/**
* logger.
*/
private static Log log = LogFactory.getLog(I18nStruts2Adapter.class);

/**
* @see LocaleResolver#resolveLocale(HttpServletRequest)
*/
public Locale resolveLocale(HttpServletRequest request)
{

Locale result = null;
ValueStack stack = ActionContext.getContext().getValueStack();

Iterator iterator = stack.getRoot().iterator();
while (iterator.hasNext())
{
Object o = iterator.next();

if (o instanceof LocaleProvider)
{
LocaleProvider lp = (LocaleProvider) o;
result = lp.getLocale();

break;
}
}

if (result == null)
{
log.debug("Missing LocalProvider actions, init locale to default");
result = Locale.getDefault();
}

return result;
}

/**
* @see I18nResourceProvider#getResource(String, String, Tag, PageContext)
*/
public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext)
{

// if resourceKey isn't defined either, use defaultValue
String key = (resourceKey != null) ? resourceKey : defaultValue;

String message = null;
ValueStack stack = TagUtils.getStack(pageContext);
Iterator iterator = stack.getRoot().iterator();

while (iterator.hasNext())
{
Object o = iterator.next();

if (o instanceof TextProvider)
{
TextProvider tp = (TextProvider) o;
message = tp.getText(key);

break;
}
}

// if user explicitely added a titleKey we guess this is an error
if (message == null && resourceKey != null)
{
log.debug(Messages.getString("Localization.missingkey", resourceKey)); //$NON-NLS-1$
message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY;
}

return message;
}

}

Fixing Struts2 initialization error on Jetty restart

August 12th, 2009

[ERROR] Error reconfiguring/restarting webapp after change in watched files
Caught exception while loading file struts-default.xml – [unknown location]

Caused by: java.lang.ClassCastException: org.apache.xerces.parsers.XIncludeAwareParserConfiguration cannot be cast to org.apache.xerces.xni.parser.XMLParserConfiguration

This issue seems to only occur with Java 6 and is related to the Xalan version included in this JDK.

Add xalan to your pom or in an endorsed/shared directory and the problem goes away.

<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.1</version>
</dependency>

More details there : JIRA item.

Struts 2 tips

August 11th, 2009

Filters
org.apache.struts2.dispatcher.FilterDispatcher and org.apache.struts2.dispatcher.ActionContextCleanUp filters are now deprecated (Struts 2.1.x), but most wiki resources and maven archetypes are not updated yet.

According to WW-2167, these 2 filters are broken and can even create memory leaks.

The new filters are : org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter and org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter

Security
Don’t forget to look at security bulletins. There are some pretty bad vulnerabilities issues on versions < 2.0.12, including a “remote code exploit” and a “Directory traversal vulnerability“.

Doctypes
Doc types headers are necessary to have Struts pick up your xml configuration files. Even with the right name and location, it will not work without the right doctype. Beware of copying/pasting without it.

Accessing page context data in struts tags
Use #attr . This is usefull when you want to get values created by jstl or display tags. See this post. The Struts2 -> Displaytag -> Struts2 round tip can be done this way :

<s:set name="user" value="#session.user" scope="request" />
<display:table uid="profile" name="user.profiles" />
<s:property value="#attr.profile.label"/>
</display:table>

Paris JUG : Soirée RIA (FR)

July 4th, 2009

Le Paris JUG organise une soirée RIA (Flex, JavaFX), le mardi 7 juillet.

Note : L’inscription est obligatoire.

Pour ma part, j’y vais. Rendez-vous là-bas, cela peut aussi être une occasion de discuter Eclipse et RCP :)