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     */
020    package org.apache.directory.server.core.entry;
021    
022    
023    import java.io.IOException;
024    import java.io.ObjectInput;
025    import java.io.ObjectOutput;
026    import java.util.Collections;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Set;
030    
031    import org.apache.directory.server.i18n.I18n;
032    import org.apache.directory.shared.ldap.entry.Entry;
033    import org.apache.directory.shared.ldap.entry.EntryAttribute;
034    import org.apache.directory.shared.ldap.entry.ServerEntry;
035    import org.apache.directory.shared.ldap.entry.Value;
036    import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
037    import org.apache.directory.shared.ldap.exception.LdapException;
038    import org.apache.directory.shared.ldap.name.DN;
039    import org.apache.directory.shared.ldap.schema.AttributeType;
040    
041    
042    /**
043     * A ServerEntry refers to the original entry before being modified by 
044     * EntryFilters or operations.
045     *
046     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047     * @version $Rev$, $Date$
048     */
049    public class ClonedServerEntry implements ServerEntry
050    {
051        /** The original entry as returned by the backend */
052        private final ServerEntry originalEntry;
053        
054        /** The copied entry */
055        private final ServerEntry clonedEntry;
056    
057        
058        /**
059         * Creates a new instance of ClonedServerEntry.
060         * 
061         * The original entry is cloned in order to protect its content.
062         *
063         * @param originalEntry The original entry
064         */
065        public ClonedServerEntry( ServerEntry originalEntry )
066        {
067            this.originalEntry = ( ServerEntry )originalEntry.clone();
068            this.clonedEntry = ( ServerEntry ) originalEntry.clone();
069        }
070        
071        
072        /**
073         * @return the originalEntry
074         */
075        public ServerEntry getOriginalEntry()
076        {
077            return originalEntry;
078        }
079    
080    
081        /**
082         * @return the cloned Entry
083         */
084        public Entry getClonedEntry()
085        {
086            return clonedEntry;
087        }
088    
089    
090        public void add( AttributeType attributeType, byte[]... values ) throws LdapException
091        {
092            clonedEntry.add( attributeType, values );
093        }
094    
095    
096        public void add( AttributeType attributeType, String... values ) throws LdapException
097        {
098            clonedEntry.add( attributeType, values );
099        }
100    
101    
102        public void add( AttributeType attributeType, Value<?>... values ) throws LdapException
103        {
104            clonedEntry.add( attributeType, values );
105        }
106    
107    
108        public void add( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
109        {
110            clonedEntry.add( attributeType, values );
111        }
112    
113    
114        public void add( String upId, AttributeType attributeType, String... values ) throws LdapException
115        {
116            clonedEntry.add( attributeType, values );
117        }
118    
119    
120        public void add( String upId, AttributeType attributeType, Value<?>... values ) throws LdapException
121        {
122            clonedEntry.add( attributeType, values );
123        }
124    
125    
126        public boolean contains( AttributeType attributeType, byte[]... values )
127        {
128            return clonedEntry.contains( attributeType, values );
129        }
130    
131    
132        public boolean contains( AttributeType attributeType, String... values )
133        {
134            return clonedEntry.contains( attributeType, values );
135        }
136    
137    
138        public boolean contains( AttributeType attributeType, Value<?>... values )
139        {
140            return clonedEntry.contains( attributeType, values );
141        }
142    
143    
144        public boolean containsAttribute( AttributeType attributeType )
145        {
146            return clonedEntry.containsAttribute( attributeType );
147        }
148    
149    
150        public EntryAttribute get( AttributeType attributeType )
151        {
152            return clonedEntry.get( attributeType );
153        }
154    
155    
156        public Set<AttributeType> getAttributeTypes()
157        {
158            return clonedEntry.getAttributeTypes();
159        }
160    
161    
162        public boolean hasObjectClass( EntryAttribute objectClass )
163        {
164            return clonedEntry.hasObjectClass( objectClass );
165        }
166    
167    
168        public boolean isValid()
169        {
170            return clonedEntry.isValid();
171        }
172    
173    
174        public boolean isValid( String objectClass )
175        {
176            return clonedEntry.isValid( objectClass );
177        }
178    
179    
180        public boolean isValid( EntryAttribute objectClass )
181        {
182            return clonedEntry.isValid( objectClass );
183        }
184    
185    
186        public EntryAttribute put( AttributeType attributeType, byte[]... values ) throws LdapException
187        {
188            return clonedEntry.put( attributeType, values );
189        }
190    
191    
192        public EntryAttribute put( AttributeType attributeType, String... values ) throws LdapException
193        {
194            return clonedEntry.put( attributeType, values );
195        }
196    
197    
198        public EntryAttribute put( AttributeType attributeType, Value<?>... values ) throws LdapException
199        {
200            return clonedEntry.put( attributeType, values );
201        }
202    
203    
204        public EntryAttribute put( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
205        {
206            return clonedEntry.put( attributeType, values );
207        }
208    
209    
210        public EntryAttribute put( String upId, AttributeType attributeType, String... values ) throws LdapException
211        {
212            return clonedEntry.put( upId, attributeType, values );
213        }
214    
215    
216        public EntryAttribute put( String upId, AttributeType attributeType, Value<?>... values ) throws LdapException
217        {
218            return clonedEntry.put( upId, attributeType, values );
219        }
220    
221    
222        public boolean remove( AttributeType attributeType, byte[]... values ) throws LdapException
223        {
224            return clonedEntry.remove( attributeType, values );
225        }
226    
227    
228        public boolean remove( AttributeType attributeType, String... values ) throws LdapException
229        {
230            return clonedEntry.remove( attributeType, values );
231        }
232    
233    
234        public boolean remove( AttributeType attributeType, Value<?>... values ) throws LdapException
235        {
236            return clonedEntry.remove( attributeType, values );
237        }
238    
239    
240        public List<EntryAttribute> remove( EntryAttribute... attributes ) throws LdapException
241        {
242            return clonedEntry.remove( attributes );
243        }
244    
245    
246        public List<EntryAttribute> removeAttributes( AttributeType... attributes )
247        {
248            return clonedEntry.removeAttributes( attributes );
249        }
250    
251    
252        public List<EntryAttribute> set( AttributeType... attributeTypes )
253        {
254            return clonedEntry.set( attributeTypes );
255        }
256    
257    
258        public void add( EntryAttribute... attributes ) throws LdapException
259        {
260            clonedEntry.add( attributes );
261        }
262    
263    
264        public void add( String upId, String... values ) throws LdapException
265        {
266            clonedEntry.add( upId, values );
267        }
268    
269    
270        public void add( String upId, byte[]... values ) throws LdapException
271        {
272            clonedEntry.add( upId, values );
273        }
274    
275    
276        public void add( String upId, Value<?>... values ) throws LdapException
277        {
278            clonedEntry.add( upId, values );
279        }
280    
281    
282        public void clear()
283        {
284            clonedEntry.clear();
285        }
286    
287    
288        public boolean contains( EntryAttribute... attributes ) throws LdapException
289        {
290            return clonedEntry.contains( attributes );
291        }
292    
293    
294        public boolean contains( String upId, byte[]... values )
295        {
296            return clonedEntry.contains( upId, values );
297        }
298    
299    
300        public boolean contains( String upId, String... values )
301        {
302            return clonedEntry.contains( upId, values );
303        }
304    
305    
306        public boolean contains( String upId, Value<?>... values )
307        {
308            return clonedEntry.contains( upId, values );
309        }
310    
311    
312        public boolean containsAttribute( String... attributes )
313        {
314            return clonedEntry.containsAttribute( attributes );
315        }
316    
317    
318        public EntryAttribute get( String alias )
319        {
320            return clonedEntry.get( alias );
321        }
322    
323    
324        public DN getDn()
325        {
326            return clonedEntry.getDn();
327        }
328    
329    
330        public boolean hasObjectClass( String objectClass )
331        {
332            return clonedEntry.hasObjectClass( objectClass );
333        }
334    
335    
336        public Iterator<EntryAttribute> iterator()
337        {
338            return clonedEntry.iterator();
339        }
340    
341    
342        public List<EntryAttribute> put( EntryAttribute... attributes ) throws LdapException
343        {
344            return clonedEntry.put( attributes );
345        }
346    
347    
348        public EntryAttribute put( String upId, byte[]... values )
349        {
350            return clonedEntry.put( upId, values );
351        }
352    
353    
354        public EntryAttribute put( String upId, String... values )
355        {
356            return clonedEntry.put( upId, values );
357        }
358    
359    
360        public EntryAttribute put( String upId, Value<?>... values )
361        {
362            return clonedEntry.put( upId, values );
363        }
364    
365    
366        public boolean remove( String upId, byte[]... values ) throws LdapException
367        {
368            return clonedEntry.remove( upId, values );
369        }
370    
371    
372        public boolean remove( String upId, String... values ) throws LdapException
373        {
374            return clonedEntry.remove( upId, values );
375        }
376    
377    
378        public boolean remove( String upId, Value<?>... values ) throws LdapException
379        {
380            return clonedEntry.remove( upId, values );
381        }
382    
383    
384        public List<EntryAttribute> removeAttributes( String... attributes )
385        {
386            return clonedEntry.removeAttributes( attributes );
387        }
388    
389    
390        public List<EntryAttribute> set( String... upIds )
391        {
392            return clonedEntry.set( upIds );
393        }
394    
395    
396        public void setDn( DN dn )
397        {
398            clonedEntry.setDn( dn );
399        }
400    
401    
402        public int size()
403        {
404            return clonedEntry.size();
405        }
406    
407    
408        public Entry toClientEntry() throws LdapException
409        {
410            // Copy the DN
411            Entry clientEntry = new DefaultClientEntry( clonedEntry.getDn() );
412            
413            // Convert each attribute 
414            for ( EntryAttribute clonedEntry:this )
415            {
416                EntryAttribute clientAttribute = clonedEntry.toClientAttribute();
417                clientEntry.add( clientAttribute );
418            }
419            
420            return clientEntry;
421        }
422        
423        
424        /**
425         * @see java.io.Externalizable#readExternal(ObjectInput)
426         * 
427         * We can't use this method for a ServerEntry
428         */
429        public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
430        {
431            throw new IllegalStateException( I18n.err( I18n.ERR_455 ) );
432        }
433        
434        
435        /**
436         * @see java.io.Externalizable#writeExternal(ObjectOutput)
437         * 
438         * We can't use this method for a ServerEntry
439         */
440        public void writeExternal( ObjectOutput out ) throws IOException
441        {
442            throw new IllegalStateException( I18n.err( I18n.ERR_456 ) );
443        }
444        
445        
446        public ServerEntry clone()
447        {
448            return ( ServerEntry ) clonedEntry.clone();
449        }
450        
451        
452        /**
453         * @see Object#equals(Object);
454         */
455        public boolean equals( Object obj )
456        {
457            // Short circuit
458            if ( this == obj )
459            {
460                return true;
461            }
462            
463            Entry other;
464            
465            if ( obj instanceof ClonedServerEntry )
466            {
467                other = ((ClonedServerEntry)obj).getClonedEntry();
468            }
469            else if ( obj instanceof ServerEntry )
470            {
471                other = (ServerEntry)obj;
472            }
473            else 
474            {
475                return false;
476            }
477    
478            return clonedEntry.equals( other );
479        }
480        
481        
482        public String toString()
483        {
484            return clonedEntry.toString();
485        }
486        
487        
488        class EmptyEntry implements ServerEntry
489        {
490            DN dn;
491            
492            EmptyEntry( DN dn )
493            {
494                this.dn = dn;
495            }
496            
497            public void add( AttributeType attributeType, byte[]... values ) throws LdapException
498            {
499            }
500    
501            public void add( AttributeType attributeType, String... values ) throws LdapException
502            {
503            }
504    
505            public void add( AttributeType attributeType, Value<?>... values ) throws LdapException
506            {
507            }
508    
509            public void add( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
510            {
511            }
512    
513            public void add( String upId, AttributeType attributeType, String... values ) throws LdapException
514            {
515            }
516    
517            public void add( String upId, AttributeType attributeType, Value<?>... values ) throws LdapException
518            {
519            }
520    
521            public boolean contains( AttributeType attributeType, byte[]... values )
522            {
523                return false;
524            }
525    
526            public boolean contains( AttributeType attributeType, String... values )
527            {
528                return false;
529            }
530    
531            public boolean contains( AttributeType attributeType, Value<?>... values )
532            {
533                return false;
534            }
535    
536            public boolean containsAttribute( AttributeType attributeType )
537            {
538                return false;
539            }
540    
541            public EntryAttribute get( AttributeType attributeType )
542            {
543                return null;
544            }
545    
546            public Set<AttributeType> getAttributeTypes()
547            {
548                return null;
549            }
550    
551            public boolean hasObjectClass( EntryAttribute objectClass )
552            {
553                return false;
554            }
555    
556            public boolean isValid()
557            {
558                return false;
559            }
560    
561            public boolean isValid( String objectClass )
562            {
563                return false;
564            }
565    
566            public boolean isValid( EntryAttribute objectClass )
567            {
568                return false;
569            }
570    
571            public EntryAttribute put( AttributeType attributeType, byte[]... values ) throws LdapException
572            {
573                return null;
574            }
575    
576            public EntryAttribute put( AttributeType attributeType, String... values ) throws LdapException
577            {
578                return null;
579            }
580    
581            public EntryAttribute put( AttributeType attributeType, Value<?>... values ) throws LdapException
582            {
583                return null;
584            }
585    
586            public EntryAttribute put( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
587            {
588                return null;
589            }
590    
591            public EntryAttribute put( String upId, AttributeType attributeType, String... values ) throws LdapException
592            {
593                return null;
594            }
595    
596            public EntryAttribute put( String upId, AttributeType attributeType, Value<?>... values )
597                throws LdapException
598            {
599                return null;
600            }
601    
602            public boolean remove( AttributeType attributeType, byte[]... values ) throws LdapException
603            {
604                return false;
605            }
606    
607            public boolean remove( AttributeType attributeType, String... values ) throws LdapException
608            {
609                return false;
610            }
611    
612            public boolean remove( AttributeType attributeType, Value<?>... values ) throws LdapException
613            {
614                return false;
615            }
616    
617            public List<EntryAttribute> remove( EntryAttribute... attributes ) throws LdapException
618            {
619                return Collections.emptyList();
620            }
621    
622            public List<EntryAttribute> removeAttributes( AttributeType... attributes )
623            {
624                return Collections.emptyList();
625            }
626    
627            public List<EntryAttribute> set( AttributeType... attributeTypes )
628            {
629                return Collections.emptyList();
630            }
631    
632            public void add( EntryAttribute... attributes ) throws LdapException
633            {
634            }
635    
636            public void add( String upId, String... values ) throws LdapException
637            {
638            }
639    
640            public void add( String upId, byte[]... values ) throws LdapException
641            {
642            }
643    
644            public void add( String upId, Value<?>... values ) throws LdapException
645            {
646            }
647    
648            public void clear()
649            {
650            }
651    
652            public boolean contains( EntryAttribute... attributes ) throws LdapException
653            {
654                return false;
655            }
656    
657            public boolean contains( String upId, byte[]... values )
658            {
659                return false;
660            }
661    
662            public boolean contains( String upId, String... values )
663            {
664                return false;
665            }
666    
667            public boolean contains( String upId, Value<?>... values )
668            {
669                return false;
670            }
671    
672            public boolean containsAttribute( String... attributes )
673            {
674                return false;
675            }
676    
677            public EntryAttribute get( String alias )
678            {
679                return null;
680            }
681    
682            public DN getDn()
683            {
684                return null;
685            }
686    
687            public boolean hasObjectClass( String objectClass )
688            {
689                return false;
690            }
691    
692            @SuppressWarnings("unchecked")
693            public Iterator<EntryAttribute> iterator()
694            {
695                return ( ( List <EntryAttribute> ) Collections.EMPTY_LIST ).iterator();
696            }
697    
698            public List<EntryAttribute> put( EntryAttribute... attributes ) throws LdapException
699            {
700                return Collections.emptyList();
701            }
702    
703            public EntryAttribute put( String upId, byte[]... values )
704            {
705                return null;
706            }
707    
708            public EntryAttribute put( String upId, String... values )
709            {
710                return null;
711            }
712    
713            public EntryAttribute put( String upId, Value<?>... values )
714            {
715                return null;
716            }
717    
718            public boolean remove( String upId, byte[]... values ) throws LdapException
719            {
720                return false;
721            }
722    
723            public boolean remove( String upId, String... values ) throws LdapException
724            {
725                return false;
726            }
727    
728            public boolean remove( String upId, Value<?>... values ) throws LdapException
729            {
730                return false;
731            }
732    
733            public List<EntryAttribute> removeAttributes( String... attributes )
734            {
735                return Collections.emptyList();
736            }
737    
738            public List<EntryAttribute> set( String... upIds )
739            {
740                return Collections.emptyList();
741            }
742    
743            public void setDn( DN dn )
744            {
745                this.dn = dn;
746            }
747    
748            public int size()
749            {
750                return 0;
751            }
752        
753            
754            public ServerEntry clone()
755            {
756                return new EmptyEntry( dn );
757            }
758    
759            
760            public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
761            {
762            }
763            
764            
765            public void writeExternal( ObjectOutput out ) throws IOException
766            {
767            }
768            
769            
770            public Entry toClientEntry() throws LdapException
771            {
772                // Copy the DN
773                Entry clientEntry = new DefaultClientEntry( dn );
774                
775                // Convert each attribute 
776                for ( EntryAttribute serverAttribute:this )
777                {
778                    EntryAttribute clientAttribute = serverAttribute.toClientAttribute();
779                    clientEntry.add( clientAttribute );
780                }
781                
782                return clientEntry;
783            }
784        }
785    }