View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.dsmlv2.response;
21  
22  
23  import org.apache.directory.api.dsmlv2.ParserUtils;
24  import org.apache.directory.api.ldap.codec.api.LdapApiService;
25  import org.apache.directory.api.ldap.model.entry.Attribute;
26  import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
27  import org.apache.directory.api.ldap.model.entry.Entry;
28  import org.apache.directory.api.ldap.model.entry.Value;
29  import org.apache.directory.api.ldap.model.exception.LdapException;
30  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
31  import org.apache.directory.api.ldap.model.message.SearchResultEntry;
32  import org.apache.directory.api.ldap.model.message.SearchResultEntryImpl;
33  import org.apache.directory.api.ldap.model.name.Dn;
34  import org.dom4j.Document;
35  import org.dom4j.Element;
36  import org.dom4j.Namespace;
37  import org.dom4j.QName;
38  import org.dom4j.tree.DefaultElement;
39  
40  
41  /**
42   * DSML Decorator for SearchResultEntry
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class SearchResultEntryDsml
47      extends AbstractResponseDsml<SearchResultEntry>
48      implements SearchResultEntry
49  {
50      private static final String SEARCH_RESULT_ENTRY_TAG = "searchResultEntry";
51  
52      /** The current attribute being processed */
53      private Attribute currentAttribute;
54  
55  
56      /**
57       * Creates a new getDecoratedMessage() of SearchResultEntryDsml.
58       * 
59       * @param codec The LDAP Service to use
60       */
61      public SearchResultEntryDsml( LdapApiService codec )
62      {
63          super( codec, new SearchResultEntryImpl() );
64      }
65  
66  
67      /**
68       * Creates a new getDecoratedMessage() of SearchResultEntryDsml.
69       *
70       * @param codec The LDAP Service to use
71       * @param ldapMessage the message to decorate
72       */
73      public SearchResultEntryDsml( LdapApiService codec, SearchResultEntry ldapMessage )
74      {
75          super( codec, ldapMessage );
76      }
77  
78  
79      /**
80       * @return The current ATtributeType
81       */
82      public Attribute getCurrentAttribute()
83      {
84          return currentAttribute;
85      }
86  
87  
88      /**
89       * Create a new attribute
90       * 
91       * @param type The attribute's type
92       * @throws LdapException If we can't add the new attributeType
93       */
94      public void addAttribute( String type ) throws LdapException
95      {
96          currentAttribute = new DefaultAttribute( type );
97  
98          getDecorated().getEntry().put( currentAttribute );
99      }
100 
101 
102     /**
103      * Add a new value to the current attribute
104      * 
105      * @param value The added value
106      * @throws LdapException If we can't add the new attributeType
107      */
108     public void addAttributeValue( Object value ) throws LdapException
109     {
110         if ( value instanceof String )
111         {
112             currentAttribute.add( ( String ) value );
113         }
114         else
115         {
116             currentAttribute.add( ( byte[] ) value );
117         }
118     }
119 
120 
121     /**
122      * {@inheritDoc}
123      */
124     public MessageTypeEnum getType()
125     {
126         return getDecorated().getType();
127     }
128 
129 
130     /**
131      * {@inheritDoc}
132      */
133     public Element toDsml( Element root )
134     {
135         Element element = null;
136 
137         if ( root != null )
138         {
139             element = root.addElement( SEARCH_RESULT_ENTRY_TAG );
140         }
141         else
142         {
143             element = new DefaultElement( SEARCH_RESULT_ENTRY_TAG );
144         }
145 
146         SearchResultEntry searchResultEntry = ( SearchResultEntry ) getDecorated();
147         element.addAttribute( "dn", searchResultEntry.getObjectName().getName() );
148 
149         Entry entry = searchResultEntry.getEntry();
150         for ( Attribute attribute : entry )
151         {
152 
153             Element attributeElement = element.addElement( "attr" );
154             attributeElement.addAttribute( "name", attribute.getUpId() );
155 
156             for ( Value<?> value : attribute )
157             {
158                 if ( ParserUtils.needsBase64Encoding( value.getValue() ) )
159                 {
160                     Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
161                     Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
162                     Document doc = attributeElement.getDocument();
163 
164                     if ( doc != null )
165                     {
166                         Element docRoot = doc.getRootElement();
167                         docRoot.add( xsdNamespace );
168                         docRoot.add( xsiNamespace );
169                     }
170 
171                     Element valueElement = attributeElement.addElement( "value" ).addText(
172                         ParserUtils.base64Encode( value.getValue() ) );
173                     valueElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":"
174                         + ParserUtils.BASE64BINARY );
175                 }
176                 else
177                 {
178                     attributeElement.addElement( "value" ).addText( value.getString() );
179                 }
180             }
181         }
182 
183         return element;
184     }
185 
186 
187     /**
188      * Get the entry Dn
189      * 
190      * @return Returns the objectName.
191      */
192     public Dn getObjectName()
193     {
194         return getDecorated().getObjectName();
195     }
196 
197 
198     /**
199      * Set the entry Dn
200      * 
201      * @param objectName The objectName to set.
202      */
203     public void setObjectName( Dn objectName )
204     {
205         getDecorated().setObjectName( objectName );
206     }
207 
208 
209     /**
210      * Get the entry.
211      * 
212      * @return Returns the entry.
213      */
214     public Entry getEntry()
215     {
216         return getDecorated().getEntry();
217     }
218 
219 
220     /**
221      * Initialize the entry.
222      * 
223      * @param entry the entry
224      */
225     public void setEntry( Entry entry )
226     {
227         getDecorated().setEntry( entry );
228     }
229 }