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.reponse;
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  
51      private static final String SEARCH_RESULT_ENTRY_TAG = "searchResultEntry";
52  
53      /** The current attribute being processed */
54      private Attribute currentAttribute;
55  
56  
57      /**
58       * Creates a new getDecoratedMessage() of SearchResultEntryDsml.
59       */
60      public SearchResultEntryDsml( LdapApiService codec )
61      {
62          super( codec, new SearchResultEntryImpl() );
63      }
64  
65  
66      /**
67       * Creates a new getDecoratedMessage() of SearchResultEntryDsml.
68       *
69       * @param ldapMessage
70       *      the message to decorate
71       */
72      public SearchResultEntryDsml( LdapApiService codec, SearchResultEntry ldapMessage )
73      {
74          super( codec, ldapMessage );
75      }
76  
77  
78      public Attribute getCurrentAttribute()
79      {
80          return currentAttribute;
81      }
82  
83  
84      /**
85       * Create a new attribute
86       * 
87       * @param type The attribute's type
88       */
89      public void addAttribute( String type ) throws LdapException
90      {
91          currentAttribute = new DefaultAttribute( type );
92  
93          getDecorated().getEntry().put( currentAttribute );
94      }
95  
96  
97      /**
98       * Add a new value to the current attribute
99       * 
100      * @param value The added value
101      */
102     public void addAttributeValue( Object value ) throws LdapException
103     {
104         if ( value instanceof String )
105         {
106             currentAttribute.add( ( String ) value );
107         }
108         else
109         {
110             currentAttribute.add( ( byte[] ) value );
111         }
112     }
113 
114 
115     /**
116      * {@inheritDoc}
117      */
118     public MessageTypeEnum getType()
119     {
120         return getDecorated().getType();
121     }
122 
123 
124     /**
125      * {@inheritDoc}
126      */
127     public Element toDsml( Element root )
128     {
129         Element element = null;
130 
131         if ( root != null )
132         {
133             element = root.addElement( SEARCH_RESULT_ENTRY_TAG );
134         }
135         else
136         {
137             element = new DefaultElement( SEARCH_RESULT_ENTRY_TAG );
138         }
139 
140         SearchResultEntry searchResultEntry = ( SearchResultEntry ) getDecorated();
141         element.addAttribute( "dn", searchResultEntry.getObjectName().getName() );
142 
143         Entry entry = searchResultEntry.getEntry();
144         for ( Attribute attribute : entry )
145         {
146 
147             Element attributeElement = element.addElement( "attr" );
148             attributeElement.addAttribute( "name", attribute.getUpId() );
149 
150             for ( Value<?> value : attribute )
151             {
152                 if ( ParserUtils.needsBase64Encoding( value.getValue() ) )
153                 {
154                     Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
155                     Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
156                     Document doc = attributeElement.getDocument();
157 
158                     if ( doc != null )
159                     {
160                         Element docRoot = doc.getRootElement();
161                         docRoot.add( xsdNamespace );
162                         docRoot.add( xsiNamespace );
163                     }
164 
165                     Element valueElement = attributeElement.addElement( "value" ).addText(
166                         ParserUtils.base64Encode( value.getValue() ) );
167                     valueElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":"
168                         + ParserUtils.BASE64BINARY );
169                 }
170                 else
171                 {
172                     attributeElement.addElement( "value" ).addText( value.getString() );
173                 }
174             }
175         }
176 
177         return element;
178     }
179 
180 
181     /**
182      * Get the entry Dn
183      * 
184      * @return Returns the objectName.
185      */
186     public Dn getObjectName()
187     {
188         return getDecorated().getObjectName();
189     }
190 
191 
192     /**
193      * Set the entry Dn
194      * 
195      * @param objectName The objectName to set.
196      */
197     public void setObjectName( Dn objectName )
198     {
199         getDecorated().setObjectName( objectName );
200     }
201 
202 
203     /**
204      * Get the entry.
205      * 
206      * @return Returns the entry.
207      */
208     public Entry getEntry()
209     {
210         return getDecorated().getEntry();
211     }
212 
213 
214     /**
215      * Initialize the entry.
216      * 
217      * @param entry the entry
218      */
219     public void setEntry( Entry entry )
220     {
221         getDecorated().setEntry( entry );
222     }
223 }