package org.jboss.metadata;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeSet;
public class WebSecurityMetaData
{
public static final Set ALL_HTTP_METHODS;
public static final String[] ALL_HTTP_METHOD_NAMES;
static
{
TreeSet tmp = new TreeSet();
tmp.add("GET");
tmp.add("POST");
tmp.add("PUT");
tmp.add("DELETE");
tmp.add("HEAD");
tmp.add("OPTIONS");
tmp.add("TRACE");
ALL_HTTP_METHODS = Collections.unmodifiableSortedSet(tmp);
ALL_HTTP_METHOD_NAMES = new String[ALL_HTTP_METHODS.size()];
ALL_HTTP_METHODS.toArray(ALL_HTTP_METHOD_NAMES);
}
private HashMap webResources = new HashMap();
private Set roles = new HashSet();
private String transportGuarantee;
private boolean unchecked = false;
private boolean excluded = false;
public static String[] getMissingHttpMethods(HashSet httpMethods)
{
String[] methods = {};
if( httpMethods.size() > 0 && httpMethods.containsAll(ALL_HTTP_METHODS) == false )
{
HashSet missingMethods = new HashSet(ALL_HTTP_METHODS);
missingMethods.removeAll(httpMethods);
methods = new String[missingMethods.size()];
missingMethods.toArray(methods);
}
return methods;
}
public WebResourceCollection addWebResource(String name)
{
WebResourceCollection webrc = new WebResourceCollection(name);
if( webResources.containsKey(name) == true )
{
name = name + '@' + System.identityHashCode(webrc);
}
webResources.put(name, webrc);
return webrc;
}
public HashMap getWebResources()
{
return webResources;
}
public void addRole(String name)
{
roles.add(name);
}
public Set getRoles()
{
return roles;
}
public String getTransportGuarantee()
{
return transportGuarantee;
}
public void setTransportGuarantee(String transportGuarantee)
{
this.transportGuarantee = transportGuarantee;
}
public boolean isUnchecked()
{
return unchecked;
}
public void setUnchecked(boolean flag)
{
this.unchecked = flag;
}
public boolean isExcluded()
{
return excluded;
}
public void setExcluded(boolean flag)
{
this.excluded = flag;
}
public static class WebResourceCollection
{
private String name;
private HashSet urlPatterns = new HashSet();
private ArrayList httpMethods = new ArrayList();
public WebResourceCollection(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void addPattern(String pattern)
{
urlPatterns.add(pattern);
}
public String[] getUrlPatterns()
{
String[] patterns = {};
patterns = new String[urlPatterns.size()];
urlPatterns.toArray(patterns);
return patterns;
}
public void addHttpMethod(String method)
{
httpMethods.add(method);
}
public String[] getHttpMethods()
{
String[] methods = {};
if( httpMethods.containsAll(ALL_HTTP_METHODS) == false )
{
methods = new String[httpMethods.size()];
httpMethods.toArray(methods);
}
return methods;
}
public String[] getMissingHttpMethods()
{
String[] methods = {};
if( httpMethods.size() > 0 && httpMethods.containsAll(ALL_HTTP_METHODS) == false )
{
HashSet missingMethods = new HashSet(ALL_HTTP_METHODS);
missingMethods.removeAll(httpMethods);
methods = new String[missingMethods.size()];
missingMethods.toArray(methods);
}
return methods;
}
}
}