/* * Copyright 2006-2008 Sxip Identity Corporation */ package org.openid4java.message.ax; import org.openid4java.message.MessageException; import org.openid4java.message.Parameter; import org.openid4java.message.ParameterList; import java.net.URL; import java.net.MalformedURLException; import java.util.Map; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Implements the extension for Attribute Exchange fetch requests. * * @see AxMessage Message * @author Marius Scurtescu, Johnny Bufu */ public class FetchRequest extends AxMessage { private static Log _log = LogFactory.getLog(FetchRequest.class); private static final boolean DEBUG = _log.isDebugEnabled(); private int _aliasCounter = 0; /** * Constructs a Fetch Request with an empty parameter list. */ protected FetchRequest() { _parameters.set(new Parameter("mode", "fetch_request")); if (DEBUG) _log.debug("Created empty fetch request."); } /** * Constructs a Fetch Request with an empty parameter list. */ public static FetchRequest createFetchRequest() { return new FetchRequest(); } /** * Constructs a FetchRequest from a parameter list. *
* The parameter list can be extracted from a received message with the
* getExtensionParams method of the Message class, and MUST NOT contain
* the "openid.
* The parameter list can be extracted from a received message with the
* getExtensionParams method of the Message class, and MUST NOT contain
* the "openid.
* Used when constructing a extension from a parameter list.
*
* @return True if the extension is valid, false otherwise.
*/
public boolean isValid()
{
if ( ! _parameters.hasParameter("required") &&
! _parameters.hasParameter("if_available") )
{
_log.warn("One of 'required' or 'if_available' parameters must be present.");
return false;
}
if ( ! _parameters.hasParameter("mode") ||
! "fetch_request".equals(_parameters.getParameterValue("mode")))
{
_log.warn("Invalid mode value in fetch_request: "
+ _parameters.getParameterValue("mode"));
return false;
}
if (_parameters.hasParameter("required"))
{
String[] aliases = _parameters.getParameterValue("required").split(",");
for (int i = 0; i < aliases.length; i++)
{
String alias = aliases[i];
if ( ! _parameters.hasParameter("type." + alias) )
{
_log.warn("Type missing for attribute alias: " + alias);
return false;
}
if (! checkCount(alias)) return false;
}
}
if ( _parameters.hasParameter("if_available"))
{
String[] aliases = _parameters.getParameterValue("if_available").split(",");
for (int i = 0; i < aliases.length; i++)
{
String alias = aliases[i];
if ( ! _parameters.hasParameter("type." + alias) )
{
_log.warn("Type missing for attribute alias: " + alias);
return false;
}
if (! checkCount(alias)) return false;
}
}
Iterator it = _parameters.getParameters().iterator();
while (it.hasNext())
{
String paramName = ((Parameter) it.next()).getKey();
if (! paramName.equals("mode") &&
! paramName.startsWith("type.") &&
! paramName.startsWith("count.") &&
! paramName.equals("required") &&
! paramName.equals("if_available") &&
! paramName.equals("update_url"))
{
_log.warn("Invalid parameter name in fetch request: " + paramName);
//return false;
}
}
return true;
}
private boolean checkCount(String alias)
{
int count = getCount(alias);
if ( count < 0 ||
( count == 0 &&
! "unlimited".equals(_parameters.getParameterValue("count." + alias))) )
{
_log.warn("Invalid value for count." + alias + ": " +
_parameters.getParameterValue("count." + alias));
return false;
}
return true;
}
private synchronized String generateAlias()
{
return "attr" + Integer.toString(++ _aliasCounter);
}
}