* @param session the current session
* @return a sort response control
*/
private SortResponseControl canSort( SortRequestControl sortControl, LdapResult ldapResult, SchemaManager schemaManager )
{
SortResponseControl resp = new SortResponseControlImpl();
List<SortKey> keys = sortControl.getSortKeys();
// only ONE key is supported by the server for now
if( keys.size() > 1 )
{
ldapResult.setDiagnosticMessage( "Cannot sort results based on more than one attribute" );
resp.setSortResult( SortResultCode.UNWILLINGTOPERFORM );
return resp;
}
SortKey sk = keys.get( 0 );
AttributeType at = schemaManager.getAttributeType( sk.getAttributeTypeDesc() );
if ( at == null )
{
ldapResult.setDiagnosticMessage( "No attribute with the name " + sk.getAttributeTypeDesc() + " exists in the server's schema" );
resp.setSortResult( SortResultCode.NOSUCHATTRIBUTE );
resp.setAttributeName( sk.getAttributeTypeDesc() );
return resp;
}
String mrOid = sk.getMatchingRuleId();
if( mrOid != null )
{
MatchingRule mr = at.getOrdering();
if( mr != null )
{
if( !mrOid.equals( mr.getOid() ) )
{
ldapResult.setDiagnosticMessage( "Given matchingrule " + mrOid + " is not applicable for the attribute " + sk.getAttributeTypeDesc() );
resp.setSortResult( SortResultCode.INAPPROPRIATEMATCHING );
resp.setAttributeName( sk.getAttributeTypeDesc() );
return resp;
}
}
try
{
schemaManager.lookupComparatorRegistry( mrOid );
}
catch ( LdapException e )
{
ldapResult.setDiagnosticMessage( "Given matchingrule " + mrOid + " is not supported" );
resp.setSortResult( SortResultCode.INAPPROPRIATEMATCHING );
resp.setAttributeName( sk.getAttributeTypeDesc() );
return resp;
}
}
else
{
MatchingRule mr = at.getOrdering();
if( mr == null )
{
mr = at.getEquality();
}
ldapResult.setDiagnosticMessage( "Matchingrule is required for sorting by the attribute " + sk.getAttributeTypeDesc() );
resp.setSortResult( SortResultCode.INAPPROPRIATEMATCHING );
resp.setAttributeName( sk.getAttributeTypeDesc() );
if( mr == null )
{
return resp;
}
try
{
schemaManager.lookupComparatorRegistry( mr.getOid() );
}
catch ( LdapException e )
{
return resp;
}
}
resp.setSortResult( SortResultCode.SUCCESS );
return resp;
}