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   *     https://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.template;
21  
22  import org.apache.directory.api.ldap.model.entry.Attribute;
23  import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
24  import org.apache.directory.api.ldap.model.entry.DefaultEntry;
25  import org.apache.directory.api.ldap.model.entry.Entry;
26  import org.apache.directory.api.ldap.model.entry.Value;
27  import org.apache.directory.api.ldap.model.exception.LdapException;
28  import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
29  import org.apache.directory.api.ldap.model.message.AddRequest;
30  import org.apache.directory.api.ldap.model.message.AddRequestImpl;
31  import org.apache.directory.api.ldap.model.message.DeleteRequest;
32  import org.apache.directory.api.ldap.model.message.DeleteRequestImpl;
33  import org.apache.directory.api.ldap.model.message.ModifyRequest;
34  import org.apache.directory.api.ldap.model.message.ModifyRequestImpl;
35  import org.apache.directory.api.ldap.model.message.SearchRequest;
36  import org.apache.directory.api.ldap.model.message.SearchRequestImpl;
37  import org.apache.directory.api.ldap.model.message.SearchScope;
38  import org.apache.directory.api.ldap.model.name.Dn;
39  import org.apache.directory.ldap.client.api.search.FilterBuilder;
40  import org.apache.directory.ldap.client.template.exception.LdapRuntimeException;
41  
42  
43  /**
44   * The default implementation of {@link ModelFactory}.
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  class ModelFactoryImpl implements ModelFactory
49  {
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public AddRequest newAddRequest( Entry entry )
55      {
56          return new AddRequestImpl().setEntry( entry );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public Attribute newAttribute( String name )
65      {
66          return new DefaultAttribute( name );
67      }
68  
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public Attribute newAttribute( String name, byte[]... values )
75      {
76          return new DefaultAttribute( name, values );
77      }
78  
79      
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public Attribute newAttribute( String name, String... values )
85      {
86          return new DefaultAttribute( name, values );
87      }
88      
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public Attribute newAttribute( String name, Value... values )
95      {
96          return new DefaultAttribute( name, values );
97      }
98  
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public DeleteRequest newDeleteRequest( Dn dn )
105     {
106         return new DeleteRequestImpl()
107             .setName( dn );
108     }
109 
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public Dn newDn( String dn )
116     {
117         try
118         {
119             return new Dn( dn );
120         }
121         catch ( LdapInvalidDnException e )
122         {
123             throw new LdapRuntimeException( e );
124         }
125     }
126 
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public Entry newEntry( String dn )
133     {
134         return newEntry( newDn( dn ) );
135     }
136 
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public Entry newEntry( Dn dn )
143     {
144         return new DefaultEntry( dn );
145     }
146 
147 
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     public ModifyRequest newModifyRequest( String dn )
153     {
154         return newModifyRequest( newDn( dn ) );
155     }
156 
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public ModifyRequest newModifyRequest( Dn dn )
163     {
164         return new ModifyRequestImpl().setName( dn );
165     }
166 
167 
168     /**
169      * {@inheritDoc}
170      */
171     @Override
172     public SearchRequest newSearchRequest( String baseDn, FilterBuilder filter,
173         SearchScope scope )
174     {
175         return newSearchRequest( newDn( baseDn ), filter.toString(), scope );
176     }
177 
178 
179     /**
180      * {@inheritDoc}
181      */
182     @Override
183     public SearchRequest newSearchRequest( String baseDn, String filter,
184         SearchScope scope )
185     {
186         return newSearchRequest( newDn( baseDn ), filter, scope );
187     }
188 
189 
190     /**
191      * {@inheritDoc}
192      */
193     @Override
194     public SearchRequest newSearchRequest( Dn baseDn, FilterBuilder filter,
195         SearchScope scope )
196     {
197         return newSearchRequest( baseDn, filter.toString(), scope, ( String[] ) null );
198     }
199 
200 
201     /**
202      * {@inheritDoc}
203      */
204     @Override
205     public SearchRequest newSearchRequest( Dn baseDn, String filter,
206         SearchScope scope )
207     {
208         return newSearchRequest( baseDn, filter, scope, ( String[] ) null );
209     }
210 
211 
212     /**
213      * {@inheritDoc}
214      */
215     @Override
216     public SearchRequest newSearchRequest( String baseDn, FilterBuilder filter,
217         SearchScope scope, String... attributes )
218     {
219         return newSearchRequest( newDn( baseDn ), filter.toString(), scope, attributes );
220     }
221 
222 
223     /**
224      * {@inheritDoc}
225      */
226     @Override
227     public SearchRequest newSearchRequest( String baseDn, String filter,
228         SearchScope scope, String... attributes )
229     {
230         return newSearchRequest( newDn( baseDn ), filter, scope, attributes );
231     }
232 
233 
234     /**
235      * {@inheritDoc}
236      */
237     @Override
238     public SearchRequest newSearchRequest( Dn baseDn, FilterBuilder filter,
239         SearchScope scope, String... attributes )
240     {
241         return newSearchRequest( baseDn, filter.toString(), scope, attributes );
242     }
243 
244 
245     /**
246      * {@inheritDoc}
247      */
248     @Override
249     public SearchRequest newSearchRequest( Dn baseDn, String filter,
250         SearchScope scope, String... attributes )
251     {
252         SearchRequest searchRequest = null;
253         try
254         {
255             searchRequest = new SearchRequestImpl()
256                 .setBase( baseDn )
257                 .setFilter( filter )
258                 .setScope( scope == null ? SearchScope.OBJECT : scope );
259             if ( attributes != null && attributes.length > 0 )
260             {
261                 searchRequest.addAttributes( attributes );
262             }
263         }
264         catch ( LdapException e )
265         {
266             throw new LdapRuntimeException( e );
267         }
268         return searchRequest;
269     }
270 }