WSO2 Registry is a content repository which provides set of abstractions over a raw relational db. Registry provides a rich set of features for storing, managing and finding resources/content. However, sometimes, one have to go beyond existing default functionality to implement custom behavior to suite specific requirements. Handlers and Filters are such an extension point of registry which allows users to implement customizations by write small about of code.
A handler is written by extending the Handler class and overriding its necessary methods. Custom handler is registered with the registry by putting an entry in the registry.xml
public void addAssociation(RequestContext requestContext) throws RegistryException {}
public void removeAssociation(RequestContext requestContext) throws RegistryException{}
}
<handler class="org.wso2.carbon.regisry.samples.handler.CustomHandler">
<filter class="org.wso2.carbon.registry.samples.handler.CustomMediaTypeMatcher">
<property name="mediaType">mytype</property>
</filter>
</handler>
In order to invoke our CustomHandler, we have defined a custom media type and a CustomMediaTypeMatcher class. CustomMediaTypeMatcher class should extend the MediaTypeMatcher class.
When a resource with media type mytype is added to the registry, associated registry filter classes will be called to perform an evaluation and based on its result, associated handlers will be invoked.
In our scenario, we want to invoke the methods addAssociation, and removeAssocation when a resource is associated with a resource which has the media type “mytype”. The code sample for the CustomMediaTypeMatcher class to handle it is shown below.
public class CustomMediaTypeMatcher extends MediaTypeMatcher{
public boolean handleAddAssociation(RequestContext requestContext)
throws RegistryException {
Resource resource = requestContext.getRepository().get(requestContext.getSourcePath());
if (resource != null) {
String mType = resource.getMediaType();
return mType != null && (invert != mType.equals(getMediaType()));
}
return false;
}
public boolean handleRemoveAssociation(RequestContext requestContext)
throws RegistryException {
Resource resource = requestContext.getRepository().get(requestContext.getSourcePath());
if (resource != null) {
String mType = resource.getMediaType();
return mType != null && (invert != mType.equals(getMediaType()));
} return false;
}
}