View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.ldap.client.api.search;
21  
22  
23  import org.apache.directory.api.ldap.model.filter.FilterEncoder;
24  
25  
26  /**
27   * A class to represent the extensible matching filter.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  /* No qualifier*/class MatchingRuleAssertionFilter extends AbstractFilter
32  {
33      /** The associated attribute */
34      private String attribute;
35      
36      /** The rule to use */
37      private String matchingRule;
38  
39      /** The Filter operator */
40      private FilterOperator operator;
41      
42      /** Whether or not to include dn attributes in the matching */
43      private boolean useDnAttributes = false;
44  
45      /** The filter value */
46      private String value;
47  
48  
49      /**
50       * Creates a new instance of MatchingRuleAssertionFilter.
51       * 
52       * @param attribute The attribute to test
53       * @param value The value to test for
54       * @param operator The FilterOperator
55       */
56      MatchingRuleAssertionFilter( String attribute, String value, 
57          FilterOperator operator )
58      {
59          this.attribute = attribute;
60          this.value = value;
61          this.operator = operator;
62      }
63  
64  
65      /**
66       * Creates a new instance of MatchingRuleAssertionFilter without an attribute.
67       * 
68       * @param value The value to test for
69       * @return A new MatchingRuleAssertionFilter
70       */
71      public static MatchingRuleAssertionFilter extensible( String value )
72      {
73          return new MatchingRuleAssertionFilter( null, value, 
74              FilterOperator.EXTENSIBLE_EQUAL );
75      }
76  
77  
78      /**
79       * Creates an extensible filter
80       *
81       * @param attribute The attribute to test
82       * @param value The value to test for
83       * @return A new MatchingRuleAssertionFilter
84       */
85      public static MatchingRuleAssertionFilter extensible( String attribute, String value )
86      {
87          return new MatchingRuleAssertionFilter( attribute, value, 
88              FilterOperator.EXTENSIBLE_EQUAL );
89      }
90  
91  
92      /**
93       * Sets the matching rule to use.  Can be either a name or an OID string.
94       *
95       * @param matchingRule The matching rule to use
96       * @return This filter
97       */
98      public MatchingRuleAssertionFilter setMatchingRule( String matchingRule )
99      {
100         this.matchingRule = matchingRule;
101         return this;
102     }
103 
104     
105     /**
106      * If set, the dn attributes will be included in the matching.
107      *
108      * @return This filter
109      */
110     public MatchingRuleAssertionFilter useDnAttributes()
111     {
112         this.useDnAttributes = true;
113         return this;
114     }
115     
116     
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public StringBuilder build( StringBuilder builder )
122     {
123         builder.append( "(" );
124         if ( attribute != null )
125         {
126             builder.append( attribute );
127         }
128         if ( useDnAttributes )
129         {
130             builder.append( ":dn" );
131         }
132         if ( matchingRule != null && !matchingRule.isEmpty() )
133         {
134             builder.append( ":" ).append( matchingRule );
135         }
136         return builder.append( operator.operator() )
137             .append( FilterEncoder.encodeFilterValue( value ) ).append( ")" );
138     }
139 }