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   *    https://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.commons.text.StringEscapeUtils;
24  import org.apache.directory.api.dsmlv2.DsmlLiterals;
25  import org.apache.directory.api.dsmlv2.ParserUtils;
26  import org.apache.directory.api.ldap.codec.api.LdapApiService;
27  import org.apache.directory.api.ldap.model.entry.Attribute;
28  import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
29  import org.apache.directory.api.ldap.model.entry.Entry;
30  import org.apache.directory.api.ldap.model.entry.Value;
31  import org.apache.directory.api.ldap.model.exception.LdapException;
32  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
33  import org.apache.directory.api.ldap.model.message.SearchResultEntry;
34  import org.apache.directory.api.ldap.model.message.SearchResultEntryImpl;
35  import org.apache.directory.api.ldap.model.name.Dn;
36  import org.dom4j.Document;
37  import org.dom4j.Element;
38  import org.dom4j.Namespace;
39  import org.dom4j.QName;
40  import org.dom4j.tree.DefaultElement;
41  
42  
43  /**
44   * DSML Decorator for SearchResultEntry
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  public class SearchResultEntryDsml
49      extends AbstractResponseDsml<SearchResultEntry>
50      implements 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     @Override
125     public MessageTypeEnum getType()
126     {
127         return getDecorated().getType();
128     }
129 
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public Element toDsml( Element root )
136     {
137         Element element;
138 
139         if ( root != null )
140         {
141             element = root.addElement( DsmlLiterals.SEARCH_RESULT_ENTRY );
142         }
143         else
144         {
145             element = new DefaultElement( DsmlLiterals.SEARCH_RESULT_ENTRY );
146         }
147 
148         SearchResultEntry searchResultEntry = getDecorated();
149         element.addAttribute( DsmlLiterals.DN, searchResultEntry.getObjectName().getName() );
150 
151         Entry entry = searchResultEntry.getEntry();
152         for ( Attribute attribute : entry )
153         {
154 
155             Element attributeElement = element.addElement( DsmlLiterals.ATTR );
156             attributeElement.addAttribute( DsmlLiterals.NAME, attribute.getUpId() );
157 
158             for ( Value value : attribute )
159             {
160                 if ( value.isHumanReadable() )
161                 {
162                     attributeElement.addElement( DsmlLiterals.VALUE ).addText( StringEscapeUtils.escapeXml11( value.getString() ) );
163                 }
164                 else
165                 {
166                     Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
167                     Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
168                     Document doc = attributeElement.getDocument();
169 
170                     if ( doc != null )
171                     {
172                         Element docRoot = doc.getRootElement();
173                         docRoot.add( xsdNamespace );
174                         docRoot.add( xsiNamespace );
175                     }
176 
177                     Element valueElement = attributeElement.addElement( DsmlLiterals.VALUE ).addText(
178                         ParserUtils.base64Encode( value.getBytes() ) );
179                     valueElement.addAttribute( new QName( DsmlLiterals.TYPE, xsiNamespace ), ParserUtils.XSD_COLON
180                         + ParserUtils.BASE64BINARY );
181                 }
182             }
183         }
184 
185         return element;
186     }
187 
188 
189     /**
190      * Get the entry Dn
191      * 
192      * @return Returns the objectName.
193      */
194     @Override
195     public Dn getObjectName()
196     {
197         return getDecorated().getObjectName();
198     }
199 
200 
201     /**
202      * Set the entry Dn
203      * 
204      * @param objectName The objectName to set.
205      */
206     @Override
207     public void setObjectName( Dn objectName )
208     {
209         getDecorated().setObjectName( objectName );
210     }
211 
212 
213     /**
214      * Get the entry.
215      * 
216      * @return Returns the entry.
217      */
218     @Override
219     public Entry getEntry()
220     {
221         return getDecorated().getEntry();
222     }
223 
224 
225     /**
226      * Initialize the entry.
227      * 
228      * @param entry the entry
229      */
230     @Override
231     public void setEntry( Entry entry )
232     {
233         getDecorated().setEntry( entry );
234     }
235 }