001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.directory.ldap.client.template;
021
022import org.apache.directory.api.ldap.model.entry.Attribute;
023import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
024import org.apache.directory.api.ldap.model.entry.DefaultEntry;
025import org.apache.directory.api.ldap.model.entry.Entry;
026import org.apache.directory.api.ldap.model.entry.Value;
027import org.apache.directory.api.ldap.model.exception.LdapException;
028import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
029import org.apache.directory.api.ldap.model.message.AddRequest;
030import org.apache.directory.api.ldap.model.message.AddRequestImpl;
031import org.apache.directory.api.ldap.model.message.DeleteRequest;
032import org.apache.directory.api.ldap.model.message.DeleteRequestImpl;
033import org.apache.directory.api.ldap.model.message.ModifyRequest;
034import org.apache.directory.api.ldap.model.message.ModifyRequestImpl;
035import org.apache.directory.api.ldap.model.message.SearchRequest;
036import org.apache.directory.api.ldap.model.message.SearchRequestImpl;
037import org.apache.directory.api.ldap.model.message.SearchScope;
038import org.apache.directory.api.ldap.model.name.Dn;
039import org.apache.directory.ldap.client.template.exception.LdapRuntimeException;
040
041
042/**
043 * The default implementation of {@link ModelFactory}.
044 *
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047public class ModelFactoryImpl implements ModelFactory
048{
049    @Override
050    public AddRequest newAddRequest( Entry entry )
051    {
052        return new AddRequestImpl().setEntry( entry );
053    }
054
055
056    @Override
057    public Attribute newAttribute( String name, byte[]... values )
058    {
059        return new DefaultAttribute( name, values );
060    }
061
062    
063    @Override
064    public Attribute newAttribute( String name, String... values )
065    {
066        return new DefaultAttribute( name, values );
067    }
068    
069
070    @Override
071    public Attribute newAttribute( String name, Value<?>... values )
072    {
073        return new DefaultAttribute( name, values );
074    }
075
076
077    @Override
078    public DeleteRequest newDeleteRequest( Dn dn )
079    {
080        return new DeleteRequestImpl()
081            .setName( dn );
082    }
083
084
085    @Override
086    public Dn newDn( String dn )
087    {
088        try
089        {
090            return new Dn( dn );
091        }
092        catch ( LdapInvalidDnException e )
093        {
094            throw new LdapRuntimeException( e );
095        }
096    }
097
098
099    @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}