<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dominikdorn.com &#187; JSF</title>
	<atom:link href="http://dominikdorn.com/tag/jsf/feed/" rel="self" type="application/rss+xml" />
	<link>http://dominikdorn.com</link>
	<description>shit happens ;)</description>
	<lastBuildDate>Mon, 06 Sep 2010 16:37:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Maven + JUnit + ClassFormatError: Absent Code attribute in method that is not native or abstract in class file</title>
		<link>http://dominikdorn.com/2010/05/maven-junit-classformaterror-absent-code-attribute/</link>
		<comments>http://dominikdorn.com/2010/05/maven-junit-classformaterror-absent-code-attribute/#comments</comments>
		<pubDate>Tue, 11 May 2010 19:39:29 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=294</guid>
		<description><![CDATA[Solutions to the Absent Code attribute problem often encountered when trying to do unit testing with Maven&#038;/JUnit.]]></description>
			<content:encoded><![CDATA[<p>Once in a while one stumbles upon an error message like this</p>
<pre>
java.lang.<strong>ClassFormatError</strong>: <strong>Absent Code attribute</strong> in method that is not native or abstract in class file javax/mail/Session
</pre>
<p>This happens if your code compiles against incomplete classes, like the JavaEE6 Api and your Unit tests try to access code thats not there. JUnit will simply fail and mark the test as error, printing something like this</p>
<pre>
Tests in error:
  initializationError(com.dominikdorn.dc.passwordReset.SimplePasswordResetServiceTest)
</pre>
<p>and the corresponding surefire text file starts like this:</p>
<pre>
-------------------------------------------------------------------------------
Test set: com.dominikdorn.dc.passwordReset.SimplePasswordResetServiceTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.022 sec <<< FAILURE!
initializationError(com.dominikdorn.dc.passwordReset.SimplePasswordResetServiceTest)  Time elapsed: 0.005 sec  <<< ERROR!
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/Session
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
....
</pre>
<p><strong>The Solution</strong><br />
You have to compile against real-implementations of the classes. You do that by adding those dependencies before the most generic dependency in your pom.xml</p>
<p>As example, we add javax.mail BEFORE javaee6-api like this</p>
<pre lang='xml' line='1'>
<project ...>
...
    <dependencies>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
</project>
</pre>
<p>If you are deploying your App on an Appserver like Glassfish or JBoss AS, leave the scope of javax.mail:mail as provided to prevent the inclusion of the jar in the final webapp. </p>
<p>I now list required dependencies for various Absent Code Errors I've encountered and still encountering. This post will be updated every time I solve another of these problems.</p>
<p><strong>JavaMail</strong>: <strong>javax/mail/Session</strong></p>
<pre lang='xml' line='1'>
<project ...>
...
    <dependencies>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
</pre>
<p><strong>Servlet 3.0</strong></p>
<pre lang='xml' line='1'>
<project ...>
...
    <dependencies>
               <dependency>
                       <groupId>org.glassfish</groupId>
                       <artifactId>javax.servlet</artifactId>
                       <version>3.0</version>
                       <scope>provided</scope>
               </dependency>
...
    </dependencies>
....
    <repositories>
               <!-- Required until the Servlet 3.0 API can be resolved in Central -->
               <repository>
                       <id>Glassfish</id>
                       <name>Glassfish Maven2 Repository</name>
                       <url>http://download.java.net/maven/glassfish/</url>
               </repository>
    </repositories>
 </project>
</pre>
<p><strong>JPA2</strong>: <strong>javax/persistence/PersistenceException</strong></p>
<pre lang="xml" line="1">
<dependencies>...
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
            <scope>provided</scope>
        </dependency>
...</dependencies>
..
<repositories>
...
    <repositories>
...
        <repository>
            <id>eclipse</id>
            <name>Eclipse Maven Repository</name>
            <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
        </repository>
    </repositories>
</pre>
<p><strong>JAX-RS</strong>: <strong>javax/ws/rs/core/UriBuilder</strong></p>
<pre lang='xml' line='1'>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.1.5</version>
            <scope>provided</scope>
        </dependency>
<!-- for json support -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.1.5</version>
            <scope>provided</scope>
        </dependency>
<!-- for testing -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-test-framework</artifactId>
            <version>1.1.5.1</version>
            <scope>test</scope>
        </dependency>
</pre>
<p>more to come soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/05/maven-junit-classformaterror-absent-code-attribute/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>XHTML strict &amp; OL start=&#8221;xx&#8221;</title>
		<link>http://dominikdorn.com/2010/04/xhtml-strict-ol-start/</link>
		<comments>http://dominikdorn.com/2010/04/xhtml-strict-ol-start/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 11:53:19 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[markup]]></category>
		<category><![CDATA[strict]]></category>
		<category><![CDATA[valid]]></category>
		<category><![CDATA[Validator]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=287</guid>
		<description><![CDATA[This little post shows how to replace the start attribute of the ol tag in XHTML STRICT pages to validate again!]]></description>
			<content:encoded><![CDATA[<p>In XHTML strict, the ol-tag does not have the attribute start. </p>
<p>To get this working again, simply create a css-class</p>
<pre lang='css' line='1'>
.counterList li {display: block }
.counterList li:before {content: counter(start) ". "; counter-increment: start}
</pre>
<p>and surround your list with a tag like this</p>
<pre lang='html' line='1'>
<div class='counterList>
<ol style="counter-reset: start 4">
<li>this will be item 5</li>
<li>this will be item 6</li>

...
</ol>
</div>
</pre>
<p>I got this idea from http://www.timrivera.com/tests/ol-start.html </p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/04/xhtml-strict-ol-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CDI/Weld manual lookup</title>
		<link>http://dominikdorn.com/2010/04/cdi-weld-manual-bean-lookup/</link>
		<comments>http://dominikdorn.com/2010/04/cdi-weld-manual-bean-lookup/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 18:04:28 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[CDI]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JSF2]]></category>
		<category><![CDATA[Weld]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=279</guid>
		<description><![CDATA[This post describes how to manually lookup beans in a CDI / Weld managed environment when you are somewhere where a simple @Inject does not work because the object itself is not managed by CDI / Weld. ]]></description>
			<content:encoded><![CDATA[<p>So, you&#8217;re ended up in a situation, where you are somewhere (e.g. a javax.faces.Converter) where you are <strong>unable to simple @Inject SomeClass</strong> ? </p>
<p>I had the problem, that I had a FacesConverter like this:</p>
<pre lang="java" line='1'>
import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;

@FacesConverter(forClass = AvailableCountry.class)
@ManagedBean // does not help <img src='http://dominikdorn.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />
@RequestScoped // does not help <img src='http://dominikdorn.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />
public class AvailableCountryConverter implements Converter
{
	@Inject
	AvailableCountryDao dao;

	@PostConstruct
	public void postConstruct()
	{
		System.out.println("calling postConstruct");
	}

	public Object getAsObject(FacesContext facesContext, UIComponent
			component, String value) {
		if (value == null || value.length() == 0) {
			return null;
		}
		return dao.find(getKey(value));
	}

	Long getKey(String value) {
		Long key;
		key = Long.valueOf(value);
		return key;
	}

	String getStringKey(long value) {
		StringBuffer sb = new StringBuffer();
		sb.append(value);
		return sb.toString();
	}

	public String getAsString(FacesContext facesContext, UIComponent
			component, Object object) {
		if (object == null) {
			return null;
		}
		if (object instanceof AvailableCountry) {
			AvailableCountry o = (AvailableCountry) object;
			return getStringKey(o.getCountry().getId());
		} else {
			throw new IllegalArgumentException("object " + object + "
					is of type " + object.getClass().getName() + "; expected type: " +
					AvailableCountry.class.getName());
		}
	}
}
</pre>
<p>But my <strong>DAO was <u>not</u> injected</strong>, nor was the postConstruct method triggered by CDI.</p>
<p>Why?<br />
<strong>Because the bean is not managed by CDI</strong>, not even when annotating it with @ManagedBean because it gets created by the JSF-Lifecycle and not by CDI. </p>
<p>Well.. but <strong>how to manually lookup a Bean with CDI / Weld? </strong></p>
<p>First, you need to get the <strong>BeanManager</strong>. When you have a <strong>FacesContext</strong> (like in the converter above), you can get it like this:</p>
<pre lang="java" line="1">
    public BeanManager getBeanManager()
    {
        return (BeanManager)
              ((ServletContext) facesContext.getExternalContext().getContext())
                   .getAttribute("javax.enterprise.inject.spi.BeanManager");
    }
</pre>
<p>If you don&#8217;t have access to a FacesContext, ServletContext or similar, you can<strong> lookup the BeanManager through JNDI</strong></p>
<pre lang="java" line='1'>
    public BeanManager getBeanManager()
    {
        try{
            InitialContext initialContext = new InitialContext();
            return (BeanManager) initialContext.lookup("java:comp/BeanManager");
        catch (NamingException e) {
            log.error("Couldn't get BeanManager through JNDI");
            return null;
        }
    }
</pre>
<p>After you&#8217;ve got your <strong>BeanManager</strong>, simply <strong>lookup</strong> your Bean like this:<br />
(In my case, I wanted to lookup a bean with the type AvailableCountryDao)<br />
<strong>Type-based CDI manual lookup</strong></p>
<pre lang='java' line='1'>
    public AvailableCountryDao getFacade()
    {
        BeanManager bm = getBeanManager();
        Bean<AvailableCountryDao> bean = (Bean<AvailableCountryDao>) bm.getBeans(AvailableCountryDao.class).iterator().next();
        CreationalContext<AvailableCountryDao> ctx = bm.createCreationalContext(bean);
        AvailableCountryDao dao = (AvailableCountryDao) bm.getReference(bean, AvailableCountryDao.class, ctx); // this could be inlined, but intentionally left this way
        return dao;
    }
</pre>
<p>Thanks to my friend <a href="http://ocpsoft.com/">Lincoln Baxter, III</a> for the snipped. </p>
<p><strong>Name-based CDI manual lookup</strong></p>
<pre lang='java' line='1'>
    public Object getBeanByName(String name) // eg. name=availableCountryDao
    {
        BeanManager bm = getBeanManager();
        Bean bean = bm.getBeans(name).iterator().next();
        CreationalContext ctx = bm.createCreationalContext(bean); // could be inlined below
        Object o = bm.getReference(bean, bean.getClass(), ctx); // could be inlined with return
        return o;
    }
</pre>
<p>So, now you&#8217;re able to manually lookup beans with CDI.<br />
In case you have the same problem (with Converters/Validators) like I had above, checkout <a href="http://seamframework.org/Seam3/FacesModule">Seam Faces</a>, where this <strong>problem already is fixed</strong>,<br />
meaning your <strong>@ManagedBean annotated Converter/Validator is working as expected with @Inject, @PostConstruct &#038; @PreDestroy</strong> <img src='http://dominikdorn.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/04/cdi-weld-manual-bean-lookup/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using complex urls &amp; regular expressions with PrettyFaces</title>
		<link>http://dominikdorn.com/2010/03/complex-urls-regular-expressions-validators-prettyfaces/</link>
		<comments>http://dominikdorn.com/2010/03/complex-urls-regular-expressions-validators-prettyfaces/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 20:15:24 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[Facelets]]></category>
		<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JSF2]]></category>
		<category><![CDATA[PrettyFaces]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[Validator]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=249</guid>
		<description><![CDATA[In this post you will learn how to match complex urls with regular expressions and PrettyFaces in JSF2 Web Applications.]]></description>
			<content:encoded><![CDATA[<p>If you like to construct complex &#038; pretty urls with JSF2 &#038; PrettyFaces, you might be interested in the following few lines of code.</p>
<p>In our example, we want to match a URL like this one</p>
<pre lang='xml' line='1'>

http://www.studyguru.eu/at/tuwien/184.153--Entwurfsmethoden-fuer-verteilte-Systeme
</pre>
<p>Previously I tried to match it with a PrettyFaces Pattern/Regex like this:</p>
<pre lang="xml">
<pattern value="/([a-z]{2})/([a-z0-9\-_]*)/([a-z0-9\-_\.]*)\-\-.*"/>
</pre>
<p>But thankfully, PrettyFaces >2.0.4 supports directly populating the RequestParams! </p>
<p>Configure your pretty-config like this:</p>
<pre lang='xml' line='1'>
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/2.0.4"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://ocpsoft.com/prettyfaces/2.0.4
                http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-2.0.4.xsd">
....

    <url-mapping id="coursePage">
<pattern value="/#{countryCode}/#{uniShortName}/#{courseId}--.*"/>
        <view-id>/path/to/coursePage.xhtml</view-id>
    </url-mapping>
....
</pre>
<p>and use JSF2&#8242;s viewParams like this:</p>
<pre lang='xml' line='1'>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:pretty="http://ocpsoft.com/prettyfaces"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
        >
<body>
<f:metadata>
    <f:viewParam id="countryCodeId"
                 required="true"
                 requiredMessage="Kein Land spezifiziert"
                 name="countryCode" value="#{coursePageBean.countryCode}"
            >
        <f:validateRegex pattern="([a-z]{2})"/>
    </f:viewParam>

    <f:viewParam id="universityCodeId"
                 required="true"
                 requiredMessage="Keine Hochschule spezifiziert"
                 name="uniShortName" value="#{coursePageBean.universityCode}"
            >
        <f:validateRegex pattern="([a-z0-9\-_]*)"/>
    </f:viewParam>
    <f:viewParam id="courseIdId"
                 required="true"
                 requiredMessage="Keine KursId spezifiziert"
                 name="courseId" value="#{coursePageBean.courseId}"
            >
        <f:validateRegex pattern="([a-z0-9\-_\.]*)"/>
    </f:viewParam>

    <f:event type="preRenderView" listener="#{coursePageBean.populate}"/>
</f:metadata>
<h1>coursePage</h1>

countryCode #{ coursePageBean.countryCode} <br/>
courseId #{coursePageBean.courseId}<br/>
universityCode #{coursePageBean.universityCode}<br/>
</body>
</html>
</pre>
<p>Voila! You now can match complex urls with PrettyFaces and apply all your custom validators<br />
to your Pretty URL <img src='http://dominikdorn.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/03/complex-urls-regular-expressions-validators-prettyfaces/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Authentication in JSF2 with JAAS: Part 2 &#8211; Authenticating a client/user</title>
		<link>http://dominikdorn.com/2010/02/authentication-jsf2-jaas-authorization-user-roles-realms/</link>
		<comments>http://dominikdorn.com/2010/02/authentication-jsf2-jaas-authorization-user-roles-realms/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 11:28:04 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[JAAS]]></category>
		<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JSF2]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=213</guid>
		<description><![CDATA[The second part of the series covers the basics needed to understand authorization in a JavaEE application based on the principles provided by JAAS and how they apply in modern, JavaEE based web applications. The user is thought, how to configure his web-application to use role-based authorization and authenticate against a specific realm.]]></description>
			<content:encoded><![CDATA[<p>In the second part of this series, we are going to understand some more terminology of JAAS and start creating our own authentication system.</p>
<p>A <strong>LoginContext</strong> Object is used by applications to authenticate <strong>user</strong>s independently of the underlying authentication technology. <strong>Authentication technology</strong> in this context means stuff like &#8220;file based authentication&#8221;, &#8220;Database based authentication&#8221;, &#8220;LDAP authentication&#8221;, &#8220;CERTIFICATE authentication&#8221; and so on.</p>
<p>The <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/login/LoginContext.html">LoginContext</a> class is part of the javax.security.auth.login package and describes methods used to authenticate <strong>Subjects/user</strong> in the meaning of <a href="http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/">Part 1 of this series</a>). The <a href="http://java.sun.com/developer/technicalArticles/Security/jaasv2/index.html">documentation</a> states:</p>
<blockquote><p>A subject is an identity in a system that you want to authenticate and assign access rights to. A subject can be a human user, a process, or a machine..</p></blockquote>
<p>which is basically what I said before. Its still a little bit unclear to me, why they use &#8220;user&#8221; in one documentation and &#8220;subject&#8221; in the other, but as its the same thing, we know what to do.</p>
<p>The documentation further states, that a <strong>subject</strong> may interact with different <strong>authorities</strong> (applications) and may have different passwords (<strong>credentials</strong>) for each of them. To represent the <strong>subject/user</strong> in these applications, the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/security/Principal.html"><strong>java.security.Principal</strong></a> class is used.</p>
<p>Our own <strong>LoginContext</strong> now invokes various <strong>LoginModules</strong> ( these handle the different ways of authentication, like form based, HTTP-Basic, HTTP-Digest, etc.). The <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/spi/LoginModule.html"><strong>LoginModule</strong></a> interface is part of<br />
the <strong>javax.security.auth.spi</strong> package. If one ever wants to integrate Facebook connect or something similar, this is where to look at!</p>
<p>But how does the <strong>LoginContext</strong> know, which <strong>LoginModules</strong> to invoke? This is done by a <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/login/Configuration.html"><strong>Configuration</strong></a> Object.</p>
<p>So, to sum this up:</p>
<ol>
<li>We have 1-n <strong>LoginModules</strong>, handling different form of authentication.. file, db, ldap and so on</li>
<li>We have a <strong>LoginContext</strong>, taking users credentials and supplying them to the <strong>LoginModules</strong></li>
<li>We have <strong>configuration</strong>s, specifying which <strong>LoginModules</strong> should be used by the <strong>LoginContext</strong></li>
<li>A <strong>client supplies</strong> its <strong>credentials</strong> to the <strong>LoginContext</strong>. The LoginContext <strong>looks up</strong>, <strong>which LoginModules to use</strong> in its <strong>Configuration</strong> and <strong>authenticates the client</strong> with the <strong>provided credentials</strong> against the <strong>LoginModules</strong>.</li>
</ol>
<p><strong>But why bother with all this stuff of LoginContext, LoginModules, Configuration and so on? Shouldn&#8217;t it be easy to use authentication, especially custom authentication in a web-app? </strong></p>
<p>Because we&#8217;ve already got it in our AppServer!</p>
<p>Configuration:<br />
We&#8217;re actually creating our Configuration object manually in our web.xml. </p>
<p>Here we define, how we gain the authentication data from the user. In this example, I used auth-method=FORM because no-one wants to use HTTP-Basic Authentication anymore today! </p>
<pre lang="xml" lines="1">
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>nameOfTheRealm</realm-name>
<form-login-config>
<form-login-page>/login.xhtml</form-login-page>
<form-error-page>/loginError.xhtml</form-error-page>
        </form-login-config>
    </login-config>
</pre>
<p>With this configuration, you can already create your own login form with your preferred style, like this:<br />
login.xhtml</p>
<pre lang="html" line="1">
<form method="post" action="/j_security_check">
<label for="username">Userid</label>
<input type="text" id="username" name="j_username">
<label for="password">Password</label>
<input type="password" id="password" name="j_password">
<input type="submit" value="Login">
</form>
</pre>
<p>In the next part of the series, we&#8217;ll see how you can do it with an jsf/facelets page.</p>
<p>Ok, we now got our form for login. To <strong>Logout</strong> simply make a link to <strong>/j_security_logout </strong></p>
<p>We&#8217;ve now got login + logout, but they are not working yet. What we still have to do, is specify, what we&#8217;re authenticating against. In this post, we called it LoginModule, in Glassfishv3 its called Realm and we have already<br />
configured it with our Login-Config above.</p>
<p>A post which explains <a href="http://www.developinjava.com/features/47-enterprise-java/105-securing-a-web-application-on-glassfish-using-jaas.html">how to configure the simply, build in Glassfish &#8220;File-Realm&#8221;</a> is available at <a href="http://www.developinjava.com">DevelopInJava.com</a>. </p>
<p>In the next blog entry of this series, we&#8217;ll see how we can create our own realm, that really suits our needs.</p>
<p>We now got everything needed for a basic authentication, but we don&#8217;t yet have a group to role mapping, which we need for stuff like #{request.isUserInRole(&#8216;ADMIN&#8217;)} or annotating our beans (see later for all of that).<br />
So what we&#8217;re going to do is, map the given &#8220;groups&#8221; to roles. Before we do that, we should tell our application, which roles we have. Do it like that in your web.xml</p>
<pre lang="xml" line="1">
    <security-role>
        <description>all the users with the role Admin</description>
        <role-name>ADMIN</role-name>
    </security-role>

    <security-role>
        <description>all the users that are authenticated</description>
        <role-name>LOGGEDIN_USER</role-name>
    </security-role>

    <security-role>
        <description>all the users that are moderators/extended rights</description>
        <role-name>MODERATOR</role-name>
    </security-role>
</pre>
<p>Now we can map the groups we get from the realm to our roles. We have to put this in<br />
<strong>/WEB-INF/sun-web.xml </strong> (if you&#8217;re using Glassfish v3). My file looks like this:</p>
<pre lang="xml" line="1">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC '-//Sun Microsystems, Inc.//DTD
Application Server 9.0 Servlet 2.5//EN'
    'http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd'>
<sun-web-app error-url="">
    <context-root>/</context-root>
    <security-role-mapping>
        <role-name>LOGGEDIN_USER</role-name>
        <group-name>default</group-name>
    </security-role-mapping>
    <security-role-mapping>
        <role-name>MODERATOR</role-name>
        <group-name>moderatoren</group-name>
    </security-role-mapping>
    <security-role-mapping>
        <role-name>ADMIN</role-name>
        <group-name>root</group-name>
    </security-role-mapping>
    <class-loader delegate="true"/>
    <jsp-config>
<property name="keepgenerated" value="true">
            <description>Keep a copy of the generated
                servlet class' java code.</description>
        </property>
    </jsp-config>
</sun-web-app>
</pre>
<p>Click here to <a href="http://docs.sun.com/app/docs/doc/819-3660/beaql?a=view">view the general structure of the sun-web.xml file</a>.</p>
<p>Our User -> Principal, Group -> Role mapping now works. We can start protecting resources in our app! <strong>Horray</strong>!</p>
<p>We&#8217;re doing this by specifying &#8220;security-constraints&#8221; like this in our web.xml</p>
<pre lang="xml" line="1">
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>admin-area</web-resource-name>
            <url-pattern>/admin/</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description>admins should be allowed to access this resources</description>
            <role-name>ADMIN</role-name>
        </auth-constraint>
    </security-constraint>
</pre>
<p>This should be quite self-explanatory, for a detailed explanation take a look at<br />
<a href="http://java.sun.com/developer/technicalArticles/J2EE/security_annotation/">Security Annotations and Authorization in GlassFish and the Java EE 5 SDK</a>, which also explains how to secure beans with annotations<br />
and has a nice matrix whats possible with annotations and whats not.</p>
<p>If you want to lock some stuff for everyone, simply leave the auth-constraint element empty, like this:</p>
<pre lang="xml" line="1">
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>admin-area</web-resource-name>
            <url-pattern>/admin/</url-pattern>
        </web-resource-collection>
        <auth-constraint>
        </auth-constraint>
    </security-constraint>
</pre>
<p>If you&#8217;ve followed these instructions and the one on the blog-post <a href="http://www.developinjava.com/features/47-enterprise-java/105-securing-a-web-application-on-glassfish-using-jaas.html">Securing a Web Application on Glassfish using JAAS</a>, you should now be able to authenticate different users with different roles in your app using the File-Realm. </p>
<p>In the next articles of the series, we will look how to create an own realm, how to replace the form-based login with a JSF-Based one and how to work with roles in your JSF-Pages.</p>
<p>References:</p>
<ul>
<li><a href="http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/">Authentication in JSF2 with JAAS: Part 1 – Understand the terminology</a></li>
<li><a href="http://java.sun.com/developer/technicalArticles/Security/jaasv2/index.html">Java Authentication and Authorization Service (JAAS)in Java 2, Standard Edition (J2SE) 1.4</a></li>
<li><a href="http://www.developinjava.com/features/47-enterprise-java/105-securing-a-web-application-on-glassfish-using-jaas.html">Securing a Web Application on Glassfish using JAAS</a></li>
<li><a href="http://docs.sun.com/app/docs/doc/819-3660/beaql?a=view">Structure of sun-web.xml file</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/02/authentication-jsf2-jaas-authorization-user-roles-realms/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Using German Umlauts / Umlaute in JSF/JSF2</title>
		<link>http://dominikdorn.com/2010/02/using-german-umlauts-umlaute-in-jsf-jsf2/</link>
		<comments>http://dominikdorn.com/2010/02/using-german-umlauts-umlaute-in-jsf-jsf2/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 17:28:16 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[JSF2]]></category>
		<category><![CDATA[l10n]]></category>
		<category><![CDATA[umlauts]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=198</guid>
		<description><![CDATA[If you happen to localize your JSF Web-Application to German, you&#8217;ll probably have problems displaying German Umlauts ( &#228;&#196;, &#246; &#214;, &#252; &#220;, &#223;) in your page. How to resolve this? Encode the umlauts with their Unicode code in your message-bundle like this &#228; = \u00e4 &#196; = \u00c4 &#246; = \u00f6 &#214; =\u00d6 &#252; [...]]]></description>
			<content:encoded><![CDATA[<p>If you happen to localize your JSF Web-Application to German, you&#8217;ll probably have problems displaying German Umlauts ( &auml;&Auml;, &ouml; &Ouml;, &uuml; &Uuml;, &szlig;) in your page.</p>
<p>How to resolve this? Encode the umlauts with their Unicode code in your message-bundle like this</p>
<ol>
<li>&auml; = \u00e4 </li>
<li>&Auml; = \u00c4</li>
<li>&ouml; = \u00f6</li>
<li>&Ouml; =\u00d6</li>
<li>&uuml; = \u00fc </li>
<li>&Uuml; = \u00dc</li>
<li>&szlig; = \u00df</li>
</ol>
<p>So, e.g. just specify it like this in your bundle.properties</p>
<pre lang="text" line="1">
homework=Haus\u00fcbung
</pre>
<p>Specify the bundle itself in your faces-config.xml</p>
<pre lang="xml" line="1">
<?xml version='1.0' encoding='UTF-8'?>

<faces-config version="2.0"
    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-facesconfig_2_0.xsd">

    <application>
        <resource-bundle>
            <base-name>/bundle</base-name>
            <var>bundle</var>
        </resource-bundle>
    </application>
</faces-config>
</pre>
<p>and use it in your facelets page example.xhtml like this:</p>
<pre lang="html" line="1">
#{bundle.homework}
</pre>
<p>or like this</p>
<pre lang="xml" line="1">
<h:outputText value="#{bundle.homework}" />
</pre>
<p>If you don&#8217;t want to convert all the Umlauts in your bundle by hand, simply use javas</p>
<pre lang="bash" line="1">
native2ascii
</pre>
<p>command to automatically encode the Umlauts in your bundle.properties file.</p>
<p>Update:<br />
I found this <a href="http://inamidst.com/stuff/unidata/">unicode chart</a>, which I think is quite useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/02/using-german-umlauts-umlaute-in-jsf-jsf2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Spring Security Facelets/JSF 2.0 Taglib released in v0.3</title>
		<link>http://dominikdorn.com/2010/01/spring-security-faceletsjsf-2-0-taglib-released-in-v0-3/</link>
		<comments>http://dominikdorn.com/2010/01/spring-security-faceletsjsf-2-0-taglib-released-in-v0-3/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 15:42:10 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Facelets]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring-Security]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=173</guid>
		<description><![CDATA[The Spring Security Facelets/JSF 2.0 Taglib got released in version 0.3. CHANGES: Replaced roles.isEmpty() with roles.equals(&#8220;&#8221;) to allow usage with Java 5 Adopted taglib for usage with Spring 3/Spring Security 3 final removed faces-config.xml in taglibs for jsf 1.2 as it prevented deploying in some scenarios Fetch it from the Project Homepage]]></description>
			<content:encoded><![CDATA[<p>The Spring Security Facelets/JSF 2.0 Taglib got released in version 0.3.</p>
<p>CHANGES:</p>
<ul>
<li>Replaced roles.isEmpty() with roles.equals(&#8220;&#8221;) to allow usage with Java 5</li>
<li>Adopted taglib for usage with Spring 3/Spring Security 3 final</li>
<li>removed faces-config.xml in taglibs for jsf 1.2 as it prevented deploying in some scenarios</li>
</ul>
<p>Fetch it from the <a href="http://code.google.com/p/spring-security-facelets-taglib/" target="_blank">Project Homepage</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/01/spring-security-faceletsjsf-2-0-taglib-released-in-v0-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FileUpload with JSF 2 and Servlet 3.0</title>
		<link>http://dominikdorn.com/2009/12/fileupload-with-jsf-2-and-servlet-3-0/</link>
		<comments>http://dominikdorn.com/2009/12/fileupload-with-jsf-2-and-servlet-3-0/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 23:11:28 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Facelets]]></category>
		<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JSF2]]></category>
		<category><![CDATA[Servlet 3.0]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=169</guid>
		<description><![CDATA[Facelets Taglib for JSF2 and Servlet 3.0]]></description>
			<content:encoded><![CDATA[<p>As I had serious problems with FileUpload and the existing &#8220;solutions&#8221;, mainly Tomahawk, MyFaces, RichFaces, PrimeFaces, etc. which are all not 100% ready for JSF 2.</p>
<p>I created a taglib based on the code of <a href="http://balusc.blogspot.com">BalusC</a>.<br />
You can find the <a href="http://github.com/domdorn/fileUploadServlet3JSF2">Taglib on Github</a></p>
<p>The source code is based on these two posts:</p>
<ol>
<li><a href="http://balusc.blogspot.com/2009/12/uploading-files-in-servlet-30.html">Uploading files in Servlet 3.0</a></li>
<li><a href="http://balusc.blogspot.com/2009/12/uploading-files-with-jsf-20-and-servlet.html" target="_blank">Uploading files with JSF 2.0 and Servlet 3.0</a></li>
</ol>
<p>Simply check out the code with git</p>
<pre lang="bash" line="1">
git clone http://github.com/domdorn/fileUploadServlet3JSF2.git
</pre>
<p>then install the taglib with</p>
<pre lang="bash" line="1">
mvn clean compile install
</pre>
<p>and import it into your maven project like this</p>
<pre>
net.balusc
fileUploadServlet3JSF2
1.0-SNAPSHOT
</pre>
<p>You can then use the taglib in your Facelets files like this:</p>
<p>Upload.xhtml</p>
<pre lang="XML" line="1">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:hh="http://balusc.net/jsf/html"
>
<h:head>

</h:head>
<body>

<h:form prependId="false">

	<h:messages globalOnly="false" id="messages"/>

	<h:panelGrid columns="3">

			<label for="someText">
				SomeText:
			</label>

			<h:inputText id="someText" value="#{uploadBean.someText}" required="true">
				<f:ajax event="blur" render="someText someTextMessage" execute="@this"/>
			</h:inputText> <h:message for="someText" id="someTextMessage"/>

			<label for="filenameText">
			Filename:
			</label>

			<h:inputText id="filenameText" value="#{uploadBean.filename}" required="true">
				<f:ajax event="blur" render="filenameText filenameTextMessage" execute="@this" />
			</h:inputText>
			<h:message for="filenameText" id="filenameTextMessage"/>
	</h:panelGrid>
	</h:form>

	<h:form enctype="multipart/form-data" prependId="false">
	<h:panelGrid columns="3">
		<h:outputLabel for="uploadedFile" rendered="#{empty uploadBean.file}">
				Input File:
		</h:outputLabel>
		<hh:inputFile id="uploadedFile" value="#{uploadBean.file}" rendered="#{empty uploadBean.file}">
			<f:validator validatorId="fileValidator"/>
		</hh:inputFile>
		<h:message for="uploadedFile"/>
	</h:panelGrid>
	<h:commandButton value="submit" action="#{uploadBean.submit}" />
</h:form>
</body>
</html>
</pre>
<p>The bean:</p>
<pre lang="java" line="1">
package com.dominikdorn.simpleFileUpload.beans;</code>

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import java.io.File;

@ViewScoped
@ManagedBean
public class UploadBean {

private File file;
private String filename;

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
System.out.println("binding filename");
this.filename = filename;
}

public String submit()
{
System.out.println("calling submit");

if(file != null)
{
this.filename = file.getName();
}

System.out.println("processed");
// do what you want with the file
return "yeah";
}

public UploadBean() {
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

private String someText;

public String getSomeText() {
return someText;
}

public void setSomeText(String someText) {
System.out.println("binding someText");
this.someText = someText;
}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2009/12/fileupload-with-jsf-2-and-servlet-3-0/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JavaEE 6 + JSF 2 + Context &amp; Dependency Injection</title>
		<link>http://dominikdorn.com/2009/12/javaee-6-jsf-2-context-dependency-injection/</link>
		<comments>http://dominikdorn.com/2009/12/javaee-6-jsf-2-context-dependency-injection/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 10:28:36 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[CDI]]></category>
		<category><![CDATA[JavaEE]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=162</guid>
		<description><![CDATA[Andy Gibson has blogged about how to use Context &#38; Dependency Injection ( CDI, the @Inject annotation ) with JSF 2. Also take a look at the comments, there is some useful info there too!]]></description>
			<content:encoded><![CDATA[<p>Andy Gibson has blogged about <a href="http://www.andygibson.net/blog/index.php/2009/12/16/getting-started-with-jsf-2-0-and-cdi-in-jee-6-part-1/" target="_blank">how to use Context &amp; Dependency Injection ( CDI, the @Inject annotation ) with JSF 2</a>.</p>
<p>Also take a look at the comments, there is some useful info there too!</p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2009/12/javaee-6-jsf-2-context-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Glassfish v3 final &amp; available</title>
		<link>http://dominikdorn.com/2009/12/glassfish-v3-final-available/</link>
		<comments>http://dominikdorn.com/2009/12/glassfish-v3-final-available/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 18:30:18 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Glassfish]]></category>
		<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JEE6]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=144</guid>
		<description><![CDATA[Paul Sterk just announced the availability of the final version of Glassfish v3! I just tried it out and it really rocks! Download, extract, start domain, running! Even PHP Applications are easily possible: I will look further into this this weekend!]]></description>
			<content:encoded><![CDATA[<p><a href="http://blogs.sun.com/psterk/">Paul Sterk</a> just <a href="https://glassfish.dev.java.net/servlets/ReadMsg?list=users&#038;msgNo=40184">announced</a> the availability of the final version of <a href="http://glassfish.org/downloads/v3-final.html">Glassfish v3</a>!</p>
<p>I just tried it out and it really rocks! Download, extract, start domain, running!<br />
Even <a href="http://docs.sun.com/app/docs/doc/820-7697/giraf?a=view">PHP Applications</a> are easily possible: I will look further into this this weekend!</p>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2009/12/glassfish-v3-final-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
