* @throws UnableToBuildCapabilityMapException
* @see org.apache.jetspeed.services.capability.CapabilityService#getCapabilityMap(java.lang.String)
*/
public CapabilityMap getCapabilityMap(String userAgent) throws UnableToBuildCapabilityMapException
{
CapabilityMap map = null;
boolean bClientFound = false;
String defaultAgent = null;
if (userAgent == null)
{
userAgent = DEFAULT_AGENT;
}
// Check the cache if we have already a capability map for
// the given Agent
synchronized (capabilityMapCache)
{
map = (CapabilityMap) capabilityMapCache.get(userAgent);
if (map != null)
{
capabilityMapCacheKeyList.remove(userAgent);
capabilityMapCacheKeyList.addFirst(userAgent);
}
}
if (map != null)
{
// Entry found
return map;
}
while (!bClientFound)
{
Client entry = findClient(userAgent);
if (entry == null)
{
if (userAgent.equals(DEFAULT_AGENT))
{
log.error(
"CapabilityMap: Default agent not found in Client Registry !");
// Stop searching -- event the default userAgent can't be found
bClientFound = true;
} else
{
// Retry with the default Agent
if (log.isDebugEnabled())
{
log.debug(
"CapabilityMap: useragent "
+ userAgent
+ "unknown, falling back to default");
}
// Use default Client
defaultAgent = userAgent;
userAgent = DEFAULT_AGENT;
}
} else
{
bClientFound = true;
try
{
// Found Client entry start populating the capability map.
map = new CapabilityMapImpl();
// Add client to CapabilityMap
map.setClient(entry);
// Add capabilities
Iterator capabilities = entry.getCapabilities().iterator();
while (capabilities.hasNext())
{
map.addCapability((Capability) capabilities.next());
}
// Add Mimetypes to map
Iterator mimetypes = entry.getMimetypes().iterator();
while (mimetypes.hasNext())
{
map.addMimetype((MimeType) mimetypes.next());
}
// Add Mediatypes for Mimetype to map
Collection mediatypes =
getMediaTypesForMimeTypes(entry.getMimetypes().iterator());
Iterator media = mediatypes.iterator();
while (media.hasNext())
{
map.addMediaType((MediaType) media.next());
}
// Validate preferred Mimetype
MimeType mimeTypeEntry = map.getPreferredType();
if (mimeTypeEntry == null)
{
throw new RuntimeException("Unable to get preferred Mimetype for client: "+entry.getName());
}
// Set preferred Mediatype for Mimetype
MediaType mediaTypeEntry = getMediaTypeForMimeType(mimeTypeEntry.getName());
if (mediaTypeEntry == null)
{
throw new RuntimeException("Unable to find preferred Mediatype for Mimetype/client: "+mimeTypeEntry.getName()+"/"+entry.getName());
}
map.setPreferredMediaType(mediaTypeEntry);
// Add map to cache
synchronized (capabilityMapCache)
{
if (capabilityMapCache.put(userAgent, map) != null)