<?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; JSF2</title>
	<atom:link href="http://dominikdorn.com/tag/jsf2/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>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>Authentication in JSF2 with JAAS: Part 1 &#8211; Understand the terminology</title>
		<link>http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/</link>
		<comments>http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 23:02:12 +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>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=206</guid>
		<description><![CDATA[Authenticating users in a JSF2 application with the help of JAAS. Part one of a multiple article series, covering the terminology used by JAAS and how to understand it with simple examples.]]></description>
			<content:encoded><![CDATA[<p>Ok, this will be a multi-part blog entry series.<br />
What I want to do with this blog entries, is to document, how to make your JSF2 application use JAAS ( Java Authentication and Authorization Service ) to manage your/my users, authenticate them through a form with<br />
the help of the server and use that security information in our JSF2 pages and our Java Beans.</p>
<p>In this first part of the series, I&#8217;ll try to cover the terminology used in easy to understand words. </p>
<p>These are the terms, you&#8217;ll need to know:</p>
<ul>
<li><strong>realm</strong>: A realm is basically a &quot;user database&quot;, being it a flat file with user/pass + group info,  database tables or even an ldap directory or something else you can imagine, like facebook connect or google authentication system. It may be used by n applications.</li>
<li><strong>user</strong>: A user is a person or program wishing to authenticate against our server/app. If you only make your website for real persons, these are your users. If you also offer a webservice, other programs accessing that service are also users. A user belongs to a realm, so may be valid in n applications (see principal below)</li>
<li><strong>role</strong>: Roles are assigned to users and/or groups in an application. E.g. GUEST for a not authenticated visitor, LOGGEDIN_USER for an authenticated user, MODERATOR or ADMIN for special people. </li>
<li><strong>group</strong>: Groups are like roles, but they are used over multiple applications and mapped to specific ROLES on an per-application-basis</li>
<li><strong>principal</strong>: A Principal is an authenticated user in the scope of an application. The same user may have different principals in different applications. A principal is identified by its <strong>name</strong> and authenticated using <strong>authentication data (credentials)</strong></li>
<li><strong>security policy domain</strong>: Also called <strong>security domain</strong> or <strong>realm</strong>. Basically, the database where you lookup users. But in this meaning, its where the realms are used, being it application1, application2, applicationN</li>
<li><strong>Security attributes</strong>: are attributes associated with every principal, like &quot; is allowed to access the admin area&quot; or stuff like that.</li>
<li><strong>credential</strong>: contains or references security attributes; are used to authenticate a Principal for a Java EE product service (your webapp)</li>
<p>If you want to get the original documentation, take a look at the <a href="http://docs.sun.com/app/docs/doc/820-7627/gijrp?a=view">Security chapter</a> in the <a href="http://docs.sun.com/app/docs/doc/820-7627">Java EE 6 Tutorial Volume I</a></p>
<p>Further references, which I&#8217;ll probably be using in the next posts of these series:</p>
<ul>
<li><a href="http://jcp.org/en/jsr/detail?id=115">JSR 115: JavaTM Authorization Contract for Containers</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://java.sun.com/javase/6/docs/technotes/guides/security/jaas/JAASRefGuide.html">JavaTM  Authentication and Authorization Service (JAAS) Reference Guide</a></li>
<li><a href="http://java.sun.com/javase/6/docs/technotes/guides/security/jaas/JAASLMDevGuide.html">JavaTM  Authentication and Authorization Service (JAAS) LoginModule Developer&#8217;s Guide</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/feed/</wfw:commentRss>
		<slash:comments>2</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>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>
	</channel>
</rss>
