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