/*
* RealmFilter.java
*
* Created on August 6, 2007, 3:17 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.atomojo.auth.service.app;
import java.sql.SQLException;
import java.util.UUID;
import java.util.logging.Level;
import org.atomojo.auth.service.db.AuthDB;
import org.atomojo.auth.service.db.Realm;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.routing.Filter;
/**
*
* @author alex
*/
public class RealmFilter extends Filter
{
AuthDB db;
/** Creates a new instance of RealmFilter */
public RealmFilter(Context context,AuthDB db)
{
super(context);
this.db = db;
}
protected int beforeHandle(Request request,Response response)
{
request.getAttributes().put(AuthApplication.REALM_REQUIRED_ATTR,Boolean.TRUE);
getContext().getLogger().fine("Finding realm...");
boolean found = false;
String realmName = AuthApplication.getStringAttribute(request,"realm-name",null);
if (realmName!=null) {
try {
Realm realm = db.getRealm(realmName);
if (realm!=null) {
if (getContext().getLogger().isLoggable(Level.FINE)) {
getContext().getLogger().fine("Found realm by name "+realmName);
}
found = true;
request.getAttributes().put(AuthApplication.REALM_ATTR,realm);
}
} catch (SQLException ex) {
getContext().getLogger().log(Level.SEVERE,"Cannot retrieve realm.",ex);
}
}
String realmId = AuthApplication.getStringAttribute(request,"realm-id",null);
if (realmId!=null && !found) {
try {
Realm realm = db.getRealm(UUID.fromString(realmId));
if (realm!=null) {
if (getContext().getLogger().isLoggable(Level.FINE)) {
getContext().getLogger().fine("Found realm by id "+realmId);
}
found = true;
request.getAttributes().put(AuthApplication.REALM_ATTR,realm);
}
} catch (SQLException ex) {
getContext().getLogger().log(Level.SEVERE,"Cannot retrieve realm.",ex);
} catch (IllegalArgumentException ex) {
getContext().getLogger().log(Level.SEVERE,"Bad UUID specified for realm.");
}
}
if (!found) {
getContext().getLogger().fine("Cannot find realm.");
}
return Filter.CONTINUE;
}
}