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.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.template.exception.LdapRuntimeException;
40  
41  
42  /**
43   * The default implementation of {@link ModelFactory}.
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   */
47  public class ModelFactoryImpl implements ModelFactory
48  {
49      @Override
50      public AddRequest newAddRequest( Entry entry )
51      {
52          return new AddRequestImpl().setEntry( entry );
53      }
54  
55  
56      @Override
57      public Attribute newAttribute( String name, byte[]... values )
58      {
59          return new DefaultAttribute( name, values );
60      }
61  
62      
63      @Override
64      public Attribute newAttribute( String name, String... values )
65      {
66          return new DefaultAttribute( name, values );
67      }
68      
69  
70      @Override
71      public Attribute newAttribute( String name, Value<?>... values )
72      {
73          return new DefaultAttribute( name, values );
74      }
75  
76  
77      @Override
78      public DeleteRequest newDeleteRequest( Dn dn )
79      {
80          return new DeleteRequestImpl()
81              .setName( dn );
82      }
83  
84  
85      @Override
86      public Dn newDn( String dn )
87      {
88          try
89          {
90              return new Dn( dn );
91          }
92          catch ( LdapInvalidDnException e )
93          {
94              throw new LdapRuntimeException( e );
95          }
96      }
97  
98  
99      @Override
100     public Entry newEntry( String dn )
101     {
102         return newEntry( newDn( dn ) );
103     }
104 
105 
106     @Override
107     public Entry newEntry( Dn dn )
108     {
109         return new DefaultEntry( dn );
110     }
111 
112 
113     @Override
114     public ModifyRequest newModifyRequest( String dn )
115     {
116         return newModifyRequest( newDn( dn ) );
117     }
118 
119 
120     @Override
121     public ModifyRequest newModifyRequest( Dn dn )
122     {
123         return new ModifyRequestImpl().setName( dn );
124     }
125 
126 
127     @Override
128     public SearchRequest newSearchRequest( String baseDn, String filter,
129         SearchScope scope )
130     {
131         return newSearchRequest( newDn( baseDn ), filter, scope );
132     }
133 
134 
135     @Override
136     public SearchRequest newSearchRequest( Dn baseDn, String filter,
137         SearchScope scope )
138     {
139         return newSearchRequest( baseDn, filter, scope, ( String[] ) null );
140     }
141 
142 
143     @Override
144     public SearchRequest newSearchRequest( String baseDn, String filter,
145         SearchScope scope, String... attributes )
146     {
147         return newSearchRequest( newDn( baseDn ), filter, scope, attributes );
148     }
149 
150 
151     @Override
152     public SearchRequest newSearchRequest( Dn baseDn, String filter,
153         SearchScope scope, String... attributes )
154     {
155         SearchRequest searchRequest = null;
156         try
157         {
158             searchRequest = new SearchRequestImpl()
159                 .setBase( baseDn )
160                 .setFilter( filter )
161                 .setScope( scope == null ? SearchScope.OBJECT : scope );
162             if ( attributes != null && attributes.length > 0 )
163             {
164                 searchRequest.addAttributes( attributes );
165             }
166         }
167         catch ( LdapException e )
168         {
169             throw new LdapRuntimeException( e );
170         }
171         return searchRequest;
172     }
173 }