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.server.core.api.entry;
021
022
023import java.io.IOException;
024import java.io.ObjectInput;
025import java.io.ObjectOutput;
026import java.util.Collection;
027import java.util.Iterator;
028import java.util.List;
029
030import org.apache.directory.api.ldap.model.entry.Attribute;
031import org.apache.directory.api.ldap.model.entry.DefaultEntry;
032import org.apache.directory.api.ldap.model.entry.Entry;
033import org.apache.directory.api.ldap.model.entry.Value;
034import org.apache.directory.api.ldap.model.exception.LdapException;
035import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
036import org.apache.directory.api.ldap.model.name.Dn;
037import org.apache.directory.api.ldap.model.schema.AttributeType;
038import org.apache.directory.server.i18n.I18n;
039
040
041/**
042 * A ServerEntry refers to the original entry before being modified by
043 * EntryFilters or operations.
044 *
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047public class ClonedServerEntry implements Entry
048{
049    /** The original entry as returned by the backend */
050    protected Entry originalEntry;
051
052    /** The copied entry */
053    protected Entry clonedEntry;
054
055
056    /**
057     * Creates a new instance of ClonedServerEntry.
058     */
059    public ClonedServerEntry()
060    {
061    }
062
063
064    /**
065     * Creates a new instance of ClonedServerEntry.
066     *
067     * The original entry is cloned in order to protect its content.
068     *
069     * @param originalEntry The original entry
070     */
071    public ClonedServerEntry( Entry originalEntry )
072    {
073        this.originalEntry = originalEntry;
074        this.clonedEntry = originalEntry.clone();
075    }
076
077
078    /**
079     * @return the originalEntry
080     */
081    public Entry getOriginalEntry()
082    {
083        return originalEntry;
084    }
085
086
087    /**
088     * @return the cloned Entry
089     */
090    public Entry getClonedEntry()
091    {
092        return clonedEntry;
093    }
094
095
096    @Override
097    public Entry add( AttributeType attributeType, byte[]... values ) throws LdapException
098    {
099        return clonedEntry.add( attributeType, values );
100    }
101
102
103    @Override
104    public Entry add( AttributeType attributeType, String... values ) throws LdapException
105    {
106        return clonedEntry.add( attributeType, values );
107    }
108
109
110    @Override
111    public Entry add( AttributeType attributeType, Value... values ) throws LdapException
112    {
113        return clonedEntry.add( attributeType, values );
114    }
115
116
117    @Override
118    public Entry add( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
119    {
120        return clonedEntry.add( attributeType, values );
121    }
122
123
124    @Override
125    public Entry add( String upId, AttributeType attributeType, String... values ) throws LdapException
126    {
127        return clonedEntry.add( attributeType, values );
128    }
129
130
131    @Override
132    public Entry add( String upId, AttributeType attributeType, Value... values ) throws LdapException
133    {
134        return clonedEntry.add( attributeType, values );
135    }
136
137
138    @Override
139    public boolean contains( AttributeType attributeType, byte[]... values )
140    {
141        return clonedEntry.contains( attributeType, values );
142    }
143
144
145    @Override
146    public boolean contains( AttributeType attributeType, String... values )
147    {
148        return clonedEntry.contains( attributeType, values );
149    }
150
151
152    @Override
153    public boolean contains( AttributeType attributeType, Value... values )
154    {
155        return clonedEntry.contains( attributeType, values );
156    }
157
158
159    @Override
160    public boolean containsAttribute( AttributeType attributeType )
161    {
162        return clonedEntry.containsAttribute( attributeType );
163    }
164
165
166    @Override
167    public Attribute get( AttributeType attributeType )
168    {
169        return clonedEntry.get( attributeType );
170    }
171
172
173    /**
174     * {@inheritDoc}
175     */
176    @Override
177    public Collection<Attribute> getAttributes()
178    {
179        return clonedEntry.getAttributes();
180    }
181
182
183    @Override
184    public boolean hasObjectClass( Attribute... objectClasses )
185    {
186        return clonedEntry.hasObjectClass( objectClasses );
187    }
188
189
190    @Override
191    public Attribute put( AttributeType attributeType, byte[]... values ) throws LdapException
192    {
193        return clonedEntry.put( attributeType, values );
194    }
195
196
197    @Override
198    public Attribute put( AttributeType attributeType, String... values ) throws LdapException
199    {
200        return clonedEntry.put( attributeType, values );
201    }
202
203
204    @Override
205    public Attribute put( AttributeType attributeType, Value... values ) throws LdapException
206    {
207        return clonedEntry.put( attributeType, values );
208    }
209
210
211    @Override
212    public Attribute put( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
213    {
214        return clonedEntry.put( attributeType, values );
215    }
216
217
218    @Override
219    public Attribute put( String upId, AttributeType attributeType, String... values ) throws LdapException
220    {
221        return clonedEntry.put( upId, attributeType, values );
222    }
223
224
225    @Override
226    public Attribute put( String upId, AttributeType attributeType, Value... values ) throws LdapException
227    {
228        return clonedEntry.put( upId, attributeType, values );
229    }
230
231
232    @Override
233    public boolean remove( AttributeType attributeType, byte[]... values ) throws LdapException
234    {
235        return clonedEntry.remove( attributeType, values );
236    }
237
238
239    @Override
240    public boolean remove( AttributeType attributeType, String... values ) throws LdapException
241    {
242        return clonedEntry.remove( attributeType, values );
243    }
244
245
246    @Override
247    public boolean remove( AttributeType attributeType, Value... values ) throws LdapException
248    {
249        return clonedEntry.remove( attributeType, values );
250    }
251
252
253    @Override
254    public List<Attribute> remove( Attribute... attributes ) throws LdapException
255    {
256        return clonedEntry.remove( attributes );
257    }
258
259
260    @Override
261    public void removeAttributes( AttributeType... attributes )
262    {
263        clonedEntry.removeAttributes( attributes );
264    }
265
266
267    @Override
268    public Entry add( Attribute... attributes ) throws LdapException
269    {
270        return clonedEntry.add( attributes );
271    }
272
273
274    @Override
275    public Entry add( String upId, String... values ) throws LdapException
276    {
277        return clonedEntry.add( upId, values );
278    }
279
280
281    @Override
282    public Entry add( String upId, byte[]... values ) throws LdapException
283    {
284        return clonedEntry.add( upId, values );
285    }
286
287
288    @Override
289    public Entry add( String upId, Value... values ) throws LdapException
290    {
291        return clonedEntry.add( upId, values );
292    }
293
294
295    @Override
296    public void clear()
297    {
298        clonedEntry.clear();
299    }
300
301
302    @Override
303    public boolean contains( Attribute... attributes )
304    {
305        return clonedEntry.contains( attributes );
306    }
307
308
309    @Override
310    public boolean contains( String upId, byte[]... values )
311    {
312        return clonedEntry.contains( upId, values );
313    }
314
315
316    @Override
317    public boolean contains( String upId, String... values )
318    {
319        return clonedEntry.contains( upId, values );
320    }
321
322
323    @Override
324    public boolean contains( String upId, Value... values )
325    {
326        return clonedEntry.contains( upId, values );
327    }
328
329
330    @Override
331    public boolean containsAttribute( String... attributes )
332    {
333        return clonedEntry.containsAttribute( attributes );
334    }
335
336
337    @Override
338    public Attribute get( String alias )
339    {
340        return clonedEntry.get( alias );
341    }
342
343
344    @Override
345    public Dn getDn()
346    {
347        return clonedEntry.getDn();
348    }
349
350
351    @Override
352    public boolean hasObjectClass( String... objectClasses )
353    {
354        return clonedEntry.hasObjectClass( objectClasses );
355    }
356
357
358    /**
359     * {@inheritDoc}
360     */
361    @Override
362    public boolean isSchemaAware()
363    {
364        return clonedEntry.isSchemaAware();
365    }
366
367
368    @Override
369    public Iterator<Attribute> iterator()
370    {
371        return clonedEntry.iterator();
372    }
373
374
375    @Override
376    public List<Attribute> put( Attribute... attributes ) throws LdapException
377    {
378        return clonedEntry.put( attributes );
379    }
380
381
382    @Override
383    public Attribute put( String upId, byte[]... values )
384    {
385        return clonedEntry.put( upId, values );
386    }
387
388
389    @Override
390    public Attribute put( String upId, String... values )
391    {
392        return clonedEntry.put( upId, values );
393    }
394
395
396    @Override
397    public Attribute put( String upId, Value... values )
398    {
399        return clonedEntry.put( upId, values );
400    }
401
402
403    @Override
404    public boolean remove( String upId, byte[]... values ) throws LdapException
405    {
406        return clonedEntry.remove( upId, values );
407    }
408
409
410    @Override
411    public boolean remove( String upId, String... values ) throws LdapException
412    {
413        return clonedEntry.remove( upId, values );
414    }
415
416
417    @Override
418    public boolean remove( String upId, Value... values ) throws LdapException
419    {
420        return clonedEntry.remove( upId, values );
421    }
422
423
424    @Override
425    public void removeAttributes( String... attributes )
426    {
427        clonedEntry.removeAttributes( attributes );
428    }
429
430
431    @Override
432    public void setDn( Dn dn )
433    {
434        clonedEntry.setDn( dn );
435    }
436
437
438    @Override
439    public void setDn( String dn ) throws LdapInvalidDnException
440    {
441        clonedEntry.setDn( dn );
442    }
443
444
445    @Override
446    public int size()
447    {
448        return clonedEntry.size();
449    }
450
451
452    public Entry toClientEntry() throws LdapException
453    {
454        // Copy the Dn
455        Entry clientEntry = new DefaultEntry( clonedEntry.getDn() );
456
457        // Convert each attribute
458        for ( Attribute clonedEntry : this )
459        {
460            Attribute clientAttribute = clonedEntry.clone();
461            clientEntry.add( clientAttribute );
462        }
463
464        return clientEntry;
465    }
466
467
468    /**
469     * @see java.io.Externalizable#readExternal(ObjectInput)
470     *
471     * We can't use this method for a ServerEntry
472     */
473    @Override
474    public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
475    {
476        throw new IllegalStateException( I18n.err( I18n.ERR_455 ) );
477    }
478
479
480    /**
481     * @see java.io.Externalizable#writeExternal(ObjectOutput)
482     *
483     * We can't use this method for a ServerEntry
484     */
485    @Override
486    public void writeExternal( ObjectOutput out ) throws IOException
487    {
488        throw new IllegalStateException( I18n.err( I18n.ERR_456 ) );
489    }
490
491
492    @Override
493    public Entry clone()
494    {
495        return clonedEntry.clone();
496    }
497
498
499    @Override
500    public Entry shallowClone()
501    {
502        return clonedEntry.shallowClone();
503    }
504
505
506    /**
507     * {@inheritDoc}
508     */
509    @Override
510    public int hashCode()
511    {
512        return 703;
513    }
514
515
516    /**
517     * {@inheritDoc}
518     */
519    @Override
520    public boolean equals( Object obj )
521    {
522        // Short circuit
523        if ( this == obj )
524        {
525            return true;
526        }
527
528        Entry other;
529
530        if ( obj instanceof ClonedServerEntry )
531        {
532            other = ( ( ClonedServerEntry ) obj ).getClonedEntry();
533        }
534        else if ( obj instanceof Entry )
535        {
536            other = ( Entry ) obj;
537        }
538        else
539        {
540            return false;
541        }
542        if ( clonedEntry == null )
543        {
544            return other == null;
545        }
546        else
547        {
548            return clonedEntry.equals( other );
549        }
550    }
551
552
553    /**
554     * @see Object#toString()
555     */
556    @Override
557    public String toString()
558    {
559        return toString( "" );
560    }
561
562
563    /**
564     * {@inheritDoc}
565     */
566    @Override
567    public String toString( String tabs )
568    {
569        return clonedEntry.toString( tabs );
570    }
571}