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 */
019package org.apache.directory.api.ldap.model.entry;
020
021
022import java.io.IOException;
023import java.io.ObjectInput;
024import java.io.ObjectOutput;
025import java.util.Collection;
026import java.util.Iterator;
027import java.util.List;
028
029import org.apache.directory.api.ldap.model.exception.LdapException;
030import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
031import org.apache.directory.api.ldap.model.name.Dn;
032import org.apache.directory.api.ldap.model.schema.AttributeType;
033import org.apache.directory.api.util.exception.NotImplementedException;
034
035
036/**
037 * A default implementation of a ServerEntry which should suite most
038 * use cases.
039 * 
040 * This class is final, it should not be extended.
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class ImmutableEntry implements Entry
045{
046    /** Used for serialization */
047    private static final long serialVersionUID = 2L;
048
049    /** The wrapped Entry for this entry */
050    private Entry entry;
051
052
053    //-------------------------------------------------------------------------
054    // Constructors
055    //-------------------------------------------------------------------------
056    /**
057     * Creates a new instance of DefaultEntry. 
058     * <p>
059     * This entry <b>must</b> be initialized before being used !
060     * </p>
061     * @param entry the Entry to store
062     */
063    public ImmutableEntry( Entry entry )
064    {
065        this.entry = entry;
066    }
067
068
069    //-------------------------------------------------------------------------
070    // Entry methods
071    //-------------------------------------------------------------------------
072    /**
073     * {@inheritDoc}
074     */
075    public Entry add( AttributeType attributeType, byte[]... values ) throws LdapException
076    {
077        new Exception().printStackTrace();
078        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
079    }
080
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public Entry add( AttributeType attributeType, String... values ) throws LdapException
087    {
088        new Exception().printStackTrace();
089        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
090    }
091
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public Entry add( AttributeType attributeType, Value<?>... values ) throws LdapException
098    {
099        new Exception().printStackTrace();
100        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
101    }
102
103
104    /**
105     * {@inheritDoc}
106     */
107    public Entry add( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
108    {
109        new Exception().printStackTrace();
110        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
111    }
112
113
114    /**
115     * {@inheritDoc}
116     */
117    @Override
118    public Entry add( String upId, AttributeType attributeType, Value<?>... values ) throws LdapException
119    {
120        new Exception().printStackTrace();
121        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
122    }
123
124
125    /**
126     * {@inheritDoc}
127     */
128    @Override
129    public Entry add( String upId, AttributeType attributeType, String... values ) throws LdapException
130    {
131        new Exception().printStackTrace();
132        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
133    }
134
135
136    /**
137     * {@inheritDoc}
138     */
139    @Override
140    public Entry add( Attribute... attributes ) throws LdapException
141    {
142        new Exception().printStackTrace();
143        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
144    }
145
146
147    /**
148     * {@inheritDoc}
149     */
150    public Entry add( String upId, byte[]... values ) throws LdapException
151    {
152        new Exception().printStackTrace();
153        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
154    }
155
156
157    /**
158     * {@inheritDoc}
159     */
160    @Override
161    public Entry add( String upId, String... values ) throws LdapException
162    {
163        new Exception().printStackTrace();
164        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
165    }
166
167
168    /**
169     * {@inheritDoc}
170     */
171    @Override
172    public Entry add( String upId, Value<?>... values ) throws LdapException
173    {
174        new Exception().printStackTrace();
175        throw new NotImplementedException( "Cannot add an attribute : the entry " + entry.getDn() + " is immutable." );
176    }
177
178
179    /**
180     * Clone an entry. All the element are duplicated, so a modification on
181     * the original object won't affect the cloned object, as a modification
182     * on the cloned object has no impact on the original object
183     */
184    @Override
185    public Entry clone()
186    {
187        return entry.clone();
188    }
189
190
191    /**
192     * {@inheritDoc}
193     */
194    @Override
195    public Entry shallowClone()
196    {
197        return entry.shallowClone();
198    }
199
200
201    /**
202     * {@inheritDoc}
203     */
204    @Override
205    public boolean contains( Attribute... attributes )
206    {
207        return entry.contains( attributes );
208    }
209
210
211    /**
212     * {@inheritDoc}
213     */
214    @Override
215    public boolean containsAttribute( String... attributes )
216    {
217        return entry.containsAttribute( attributes );
218    }
219
220
221    /**
222     * {@inheritDoc}
223     */
224    @Override
225    public boolean containsAttribute( AttributeType attributeType )
226    {
227        return entry.containsAttribute( attributeType );
228    }
229
230
231    /**
232     * {@inheritDoc}
233     */
234    public boolean contains( AttributeType attributeType, byte[]... values )
235    {
236        return entry.contains( attributeType, values );
237    }
238
239
240    /**
241     * {@inheritDoc}
242     */
243    @Override
244    public boolean contains( AttributeType attributeType, String... values )
245    {
246        return entry.contains( attributeType, values );
247    }
248
249
250    /**
251     * {@inheritDoc}
252     */
253    @Override
254    public boolean contains( AttributeType attributeType, Value<?>... values )
255    {
256        return entry.contains( attributeType, values );
257    }
258
259
260    /**
261     * {@inheritDoc}
262     */
263    public boolean contains( String upId, byte[]... values )
264    {
265        return entry.contains( upId, values );
266    }
267
268
269    /**
270     * {@inheritDoc}
271     */
272    @Override
273    public boolean contains( String upId, String... values )
274    {
275        return entry.contains( upId, values );
276    }
277
278
279    /**
280     * {@inheritDoc}
281     */
282    @Override
283    public boolean contains( String upId, Value<?>... values )
284    {
285        return entry.contains( upId, values );
286    }
287
288
289    /**
290     * {@inheritDoc}
291     */
292    @Override
293    public Attribute get( String alias )
294    {
295        return entry.get( alias );
296    }
297
298
299    /**
300     * {@inheritDoc}
301     */
302    @Override
303    public Attribute get( AttributeType attributeType )
304    {
305        return entry.get( attributeType );
306    }
307
308
309    /**
310     * {@inheritDoc}
311     */
312    @Override
313    public Collection<Attribute> getAttributes()
314    {
315        return entry.getAttributes();
316    }
317
318
319    /**
320     * {@inheritDoc}
321     */
322    public Attribute put( String upId, byte[]... values )
323    {
324        new Exception().printStackTrace();
325        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
326    }
327
328
329    /**
330     * {@inheritDoc}
331     */
332    @Override
333    public Attribute put( String upId, String... values )
334    {
335        new Exception().printStackTrace();
336        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
337    }
338
339
340    /**
341     * {@inheritDoc}
342     */
343    @Override
344    public Attribute put( String upId, Value<?>... values )
345    {
346        new Exception().printStackTrace();
347        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
348    }
349
350
351    /**
352     * {@inheritDoc}
353     */
354    @Override
355    public List<Attribute> put( Attribute... attributes ) throws LdapException
356    {
357        new Exception().printStackTrace();
358        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
359    }
360
361
362    /**
363     * {@inheritDoc}
364     */
365    public Attribute put( AttributeType attributeType, byte[]... values ) throws LdapException
366    {
367        new Exception().printStackTrace();
368        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
369    }
370
371
372    /**
373     * {@inheritDoc}
374     */
375    @Override
376    public Attribute put( AttributeType attributeType, String... values ) throws LdapException
377    {
378        new Exception().printStackTrace();
379        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
380    }
381
382
383    /**
384     * {@inheritDoc}
385     */
386    @Override
387    public Attribute put( AttributeType attributeType, Value<?>... values ) throws LdapException
388    {
389        new Exception().printStackTrace();
390        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
391    }
392
393
394    /**
395     * {@inheritDoc}
396     */
397    public Attribute put( String upId, AttributeType attributeType, byte[]... values ) throws LdapException
398    {
399        new Exception().printStackTrace();
400        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
401    }
402
403
404    /**
405     * {@inheritDoc}
406     */
407    @Override
408    public Attribute put( String upId, AttributeType attributeType, String... values ) throws LdapException
409    {
410        new Exception().printStackTrace();
411        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
412    }
413
414
415    /**
416     * {@inheritDoc}
417     */
418    @Override
419    public Attribute put( String upId, AttributeType attributeType, Value<?>... values ) throws LdapException
420    {
421        new Exception().printStackTrace();
422        throw new NotImplementedException( "Cannot put a value : the entry " + entry.getDn() + " is immutable." );
423    }
424
425
426    /**
427     * {@inheritDoc}
428     */
429    @Override
430    public List<Attribute> remove( Attribute... attributes ) throws LdapException
431    {
432        new Exception().printStackTrace();
433        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
434    }
435
436
437    /**
438     * {@inheritDoc}
439     */
440    public boolean remove( AttributeType attributeType, byte[]... values ) throws LdapException
441    {
442        new Exception().printStackTrace();
443        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
444    }
445
446
447    /**
448     * {@inheritDoc}
449     */
450    @Override
451    public boolean remove( AttributeType attributeType, String... values ) throws LdapException
452    {
453        new Exception().printStackTrace();
454        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
455    }
456
457
458    /**
459     * {@inheritDoc}
460     */
461    @Override
462    public boolean remove( AttributeType attributeType, Value<?>... values ) throws LdapException
463    {
464        new Exception().printStackTrace();
465        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
466    }
467
468
469    /**
470     * <p>
471     * Removes the attribute with the specified AttributeTypes. 
472     * </p>
473     * <p>
474     * The removed attribute are returned by this method. 
475     * </p>
476     * <p>
477     * If there is no attribute with the specified AttributeTypes,
478     * the return value is <code>null</code>.
479     * </p>
480     *
481     * @param attributes the AttributeTypes to be removed
482     */
483    @Override
484    public void removeAttributes( AttributeType... attributes )
485    {
486        new Exception().printStackTrace();
487        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
488    }
489
490
491    /**
492     * {@inheritDoc}
493     */
494    @Override
495    public void removeAttributes( String... attributes )
496    {
497        new Exception().printStackTrace();
498        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
499    }
500
501
502    /**
503     * <p>
504     * Removes the specified binary values from an attribute.
505     * </p>
506     * <p>
507     * If at least one value is removed, this method returns <code>true</code>.
508     * </p>
509     * <p>
510     * If there is no more value after having removed the values, the attribute
511     * will be removed too.
512     * </p>
513     * <p>
514     * If the attribute does not exist, nothing is done and the method returns 
515     * <code>false</code>
516     * </p> 
517     *
518     * @param upId The attribute ID  
519     * @param values the values to be removed
520     * @return <code>true</code> if at least a value is removed, <code>false</code>
521     * if not all the values have been removed or if the attribute does not exist. 
522     */
523    public boolean remove( String upId, byte[]... values ) throws LdapException
524    {
525        new Exception().printStackTrace();
526        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
527    }
528
529
530    /**
531     * <p>
532     * Removes the specified String values from an attribute.
533     * </p>
534     * <p>
535     * If at least one value is removed, this method returns <code>true</code>.
536     * </p>
537     * <p>
538     * If there is no more value after having removed the values, the attribute
539     * will be removed too.
540     * </p>
541     * <p>
542     * If the attribute does not exist, nothing is done and the method returns 
543     * <code>false</code>
544     * </p> 
545     *
546     * @param upId The attribute ID  
547     * @param values the attributes to be removed
548     * @return <code>true</code> if at least a value is removed, <code>false</code>
549     * if not all the values have been removed or if the attribute does not exist. 
550     */
551    @Override
552    public boolean remove( String upId, String... values ) throws LdapException
553    {
554        new Exception().printStackTrace();
555        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
556    }
557
558
559    /**
560     * <p>
561     * Removes the specified values from an attribute.
562     * </p>
563     * <p>
564     * If at least one value is removed, this method returns <code>true</code>.
565     * </p>
566     * <p>
567     * If there is no more value after having removed the values, the attribute
568     * will be removed too.
569     * </p>
570     * <p>
571     * If the attribute does not exist, nothing is done and the method returns 
572     * <code>false</code>
573     * </p> 
574     *
575     * @param upId The attribute ID  
576     * @param values the attributes to be removed
577     * @return <code>true</code> if at least a value is removed, <code>false</code>
578     * if not all the values have been removed or if the attribute does not exist. 
579     */
580    @Override
581    public boolean remove( String upId, Value<?>... values ) throws LdapException
582    {
583        new Exception().printStackTrace();
584        throw new NotImplementedException( "Cannot remove a value : the entry " + entry.getDn() + " is immutable." );
585    }
586
587
588    /**
589     * Get this entry's Dn.
590     *
591     * @return The entry's Dn
592     */
593    @Override
594    public Dn getDn()
595    {
596        return entry.getDn();
597    }
598
599
600    /**
601     * {@inheritDoc}
602     */
603    @Override
604    public void setDn( Dn dn )
605    {
606        new Exception().printStackTrace();
607        throw new NotImplementedException( "Cannot rename the entry " + entry.getDn() + " is immutable." );
608    }
609
610
611    /**
612     * {@inheritDoc}
613     */
614    @Override
615    public void setDn( String dn ) throws LdapInvalidDnException
616    {
617        new Exception().printStackTrace();
618        throw new NotImplementedException( "Cannot rename the entry " + entry.getDn() + " is immutable." );
619    }
620
621
622    /**
623     * Remove all the attributes for this entry. The Dn is not reset
624     */
625    @Override
626    public void clear()
627    {
628        new Exception().printStackTrace();
629        throw new NotImplementedException( "Cannot clear the entry " + entry.getDn() + " is immutable." );
630    }
631
632
633    /**
634     * Returns an enumeration containing the zero or more attributes in the
635     * collection. The behavior of the enumeration is not specified if the
636     * attribute collection is changed.
637     *
638     * @return an enumeration of all contained attributes
639     */
640    @Override
641    public Iterator<Attribute> iterator()
642    {
643        return entry.iterator();
644    }
645
646
647    /**
648     * Returns the number of attributes.
649     *
650     * @return the number of attributes
651     */
652    @Override
653    public int size()
654    {
655        return entry.size();
656    }
657
658
659    /**
660     * This is the place where we serialize entries, and all theirs
661     * elements.
662     * <br>
663     * The structure used to store the entry is the following :
664     * <ul>
665     *   <li>
666     *     <b>[Dn]</b> : If it's null, stores an empty Dn
667     *   </li>
668     *   <li>
669     *     <b>[attributes number]</b> : the number of attributes.
670     *   </li>
671     *   <li>
672     *     <b>[attribute]*</b> : each attribute, if we have some
673     *   </li>
674     * </ul>
675     * 
676     * {@inheritDoc} 
677     */
678    @Override
679    public void writeExternal( ObjectOutput out ) throws IOException
680    {
681        entry.writeExternal( out );
682    }
683
684
685    /**
686     * {@inheritDoc}
687     */
688    @Override
689    public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
690    {
691        new Exception().printStackTrace();
692        throw new NotImplementedException( "Cannot read the entry " + entry.getDn() + " is immutable." );
693    }
694
695
696    /**
697     * Serialize an Entry.
698     * 
699     * The structure is the following :
700     * <b>[a byte]</b> : if the Dn is empty 0 will be written else 1
701     * <b>[Rdn]</b> : The entry's Rdn.
702     * <b>[numberAttr]</b> : the bumber of attributes. Can be 0 
703     * <b>[attribute's oid]*</b> : The attribute's OID to get back 
704     * the attributeType on deserialization
705     * <b>[Attribute]*</b> The attribute
706     * 
707     * @param out the buffer in which the data will be serialized
708     * @throws IOException if the serialization failed
709     */
710    public void serialize( ObjectOutput out ) throws IOException
711    {
712        new Exception().printStackTrace();
713        throw new NotImplementedException( "Cannot serialize the entry " + entry.getDn() + " is immutable." );
714    }
715
716
717    /**
718     * Deserialize an entry. 
719     * 
720     * @param in The buffer containing the serialized serverEntry
721     * @throws IOException if there was a problem when deserializing
722     * @throws ClassNotFoundException if we can't deserialize an expected object
723     */
724    public void deserialize( ObjectInput in ) throws IOException, ClassNotFoundException
725    {
726        new Exception().printStackTrace();
727        throw new NotImplementedException( "Cannot deserialize the entry " + entry.getDn() + " is immutable." );
728    }
729
730
731    /**
732     * Get the hash code of this ClientEntry. The Attributes will be sorted
733     * before the comparison can be done.
734     *
735     * @see java.lang.Object#hashCode()
736     * @return the instance's hash code 
737     */
738    @Override
739    public int hashCode()
740    {
741        return entry.hashCode();
742    }
743
744
745    /**
746     * {@inheritDoc}
747     */
748    @Override
749    public boolean hasObjectClass( String... objectClasses )
750    {
751        return entry.hasObjectClass( objectClasses );
752    }
753
754
755    /**
756     * {@inheritDoc}
757     */
758    @Override
759    public boolean hasObjectClass( Attribute... objectClasses )
760    {
761        return entry.hasObjectClass( objectClasses );
762    }
763
764
765    /**
766     * {@inheritDoc}
767     */
768    @Override
769    public boolean isSchemaAware()
770    {
771        return entry.isSchemaAware();
772    }
773
774
775    /**
776     * @see Object#equals(Object)
777     */
778    @Override
779    public boolean equals( Object o )
780    {
781        return entry.equals( o );
782    }
783
784
785    /**
786     * @see Object#toString()
787     */
788    @Override
789    public String toString()
790    {
791        return entry.toString();
792    }
793
794
795    /**
796     * {@inheritDoc}
797     */
798    @Override
799    public String toString( String tabs )
800    {
801        return entry.toString( tabs );
802    }
803}