org.jboss.resteasy.spi.touri.ObjectToURI Maven / Gradle / Ivy
package org.jboss.resteasy.spi.touri;
import java.util.ArrayList;
import java.util.List;
/**
*
* This class encapsulates how an object can be converted to a URI string. There
* are three basic ways to perform the transformation:
*
*
* - adding a @URITemplate("your-uri-template") to an object
*
- having an object extend URIable and have the object perform custom logic
*
- registering a custom URIResolver
*
*
* @author Solomon Duskis
* @version $Revision: 1 $
*/
public class ObjectToURI
{
private static ObjectToURI instance = null;
static
{
instance = new ObjectToURI();
instance.defaultResolvers.add(new URIableURIResolver());
instance.defaultResolvers.add(new URITemplateAnnotationResolver());
instance.defaultResolvers.add(new MappedByAnnotationResolver());
}
public static ObjectToURI getInstance()
{
return instance;
}
private List resolvers = new ArrayList();
private List defaultResolvers = new ArrayList();
public void registerURIResolver(URIResolver uriResolver)
{
this.resolvers.add(uriResolver);
}
public String resolveURI(Object object)
{
String result = getResolver(object, resolvers);
if (result == null)
{
result = getResolver(object, defaultResolvers);
}
return result;
}
private String getResolver(Object object, List resolvers)
{
Class extends Object> type = object.getClass();
for (URIResolver resolver : resolvers)
{
if (resolver.handles(type))
{
return resolver.resolveURI(object);
}
}
return null;
}
}