/*
* Created on Jun 25, 2005
*
* Copyright 2005 CafeSip.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cafesip.jiplet.cma;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.cafesip.jiplet.JipletLogger;
import org.cafesip.jiplet.Pair;
/**
* @author Amit Chatterjee
*
*/
public class Authorizations
{
private HashMap realms = new HashMap(); // key = realm name, value = another HashMap.
// the value HashMap has the key : CallId and value = Pair(AuthorizationInfo, expiry Time)
/**
* A constructor for this class.
*
*
*/
public Authorizations()
{
super();
}
protected void addEntry (AuthorizationInfo entry, Date ts)
{
HashMap map = null;
synchronized (realms)
{
map = (HashMap)realms.get(entry.getRealm());
if (map == null)
{
map = new HashMap();
realms.put(entry.getRealm(), map);
}
map.put(entry.getCallId(), new Pair(entry, ts));
}
}
protected String[] findEntry(String realm, String callId, String nonce, String response)
{
synchronized (realms)
{
HashMap map = (HashMap)realms.get(realm);
if (map == null)
{
return null;
}
Pair p = (Pair)map.get(callId);
if (p == null)
{
return null;
}
AuthorizationInfo info = (AuthorizationInfo)p.getFirst();
if (info.getNonce().equals(nonce) == false)
{
return null;
}
if (info.getResponse().equals(response) == false)
{
return null;
}
return info.getRoles();
}
}
protected void removeEntries (Date before)
{
synchronized (realms)
{
Iterator i = realms.entrySet().iterator();
while (i.hasNext() == true)
{
Map.Entry realm = (Map.Entry)i.next();
HashMap map = (HashMap)realm.getValue();
Iterator j = map.entrySet().iterator();
while (j.hasNext() == true)
{
Map.Entry entry = (Map.Entry)j.next();
Pair p = (Pair)entry.getValue();
AuthorizationInfo ainfo = (AuthorizationInfo)p.getFirst();
Date expires = (Date)p.getSecond();
if (expires.before(before) == true)
{
if (JipletLogger.isDebugEnabled() == true)
{
JipletLogger.debug("Remving cached authentication authorization for Realm="
+ ainfo.getRealm() + "call-id=" + ainfo.getCallId());
}
j.remove();
if (map.size() == 0)
{
i.remove();
}
}
}
}
}
}
}