我遇到的情况是应用程序开发人员和框架提供者不是人。作为一个框架提供者,我希望能够将开发人员看作单个Filter,但实际上是一系列标准过滤器(例如身份验证,设置调用上下文,度量标准,++)。
我似乎没有在标准库中找到此功能,但可能有一个扩展名。
答案 0 :(得分:0)
我没有等待答案,而是继续我自己的实施并在这里分享,如果有人需要这个。
/**
* Composes an array of Restlet Filters into a single Filter.
*/
public class ComposingFilter extends Filter
{
private final Filter first;
private final Filter last;
public ComposingFilter( Filter... composedOf )
{
Objects.requireNonNull( composedOf );
if( composedOf.length == 0 )
{
throw new IllegalArgumentException( "Filter chain can't be empty." );
}
first = composedOf[ 0 ];
Filter prev = first;
for( int i = 1; i < composedOf.length; i++ )
{
Filter next = composedOf[ i ];
prev.setNext( next );
prev = next;
}
last = composedOf[ composedOf.length - 1 ];
}
@Override
protected int doHandle( Request request, Response response )
{
if( first != null )
{
first.handle( request, response );
Response.setCurrent( response );
if( getContext() != null )
{
Context.setCurrent( getContext() );
}
}
else
{
response.setStatus( Status.SERVER_ERROR_INTERNAL );
getLogger().warning( "The filter " + getName() + " was executed without a next Restlet attached to it." );
}
return CONTINUE;
}
@Override
public synchronized void start()
throws Exception
{
if( isStopped() )
{
first.start();
super.start();
}
}
@Override
public synchronized void stop()
throws Exception
{
if( isStarted() )
{
super.stop();
first.stop();
}
}
@Override
public Restlet getNext()
{
return last.getNext();
}
@Override
public void setNext( Class<? extends ServerResource> targetClass )
{
last.setNext( targetClass );
}
@Override
public void setNext( Restlet next )
{
last.setNext( next );
}
}
注意:尚未测试。