1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
45
46
47
48 public class SearchResultEntryDsml
49 extends AbstractResponseDsml<SearchResultEntry>
50 implements SearchResultEntry
51 {
52
53 private Attribute currentAttribute;
54
55
56
57
58
59
60
61 public SearchResultEntryDsml( LdapApiService codec )
62 {
63 super( codec, new SearchResultEntryImpl() );
64 }
65
66
67
68
69
70
71
72
73 public SearchResultEntryDsml( LdapApiService codec, SearchResultEntry ldapMessage )
74 {
75 super( codec, ldapMessage );
76 }
77
78
79
80
81
82 public Attribute getCurrentAttribute()
83 {
84 return currentAttribute;
85 }
86
87
88
89
90
91
92
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
104
105
106
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
123
124 @Override
125 public MessageTypeEnum getType()
126 {
127 return getDecorated().getType();
128 }
129
130
131
132
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
191
192
193
194 @Override
195 public Dn getObjectName()
196 {
197 return getDecorated().getObjectName();
198 }
199
200
201
202
203
204
205
206 @Override
207 public void setObjectName( Dn objectName )
208 {
209 getDecorated().setObjectName( objectName );
210 }
211
212
213
214
215
216
217
218 @Override
219 public Entry getEntry()
220 {
221 return getDecorated().getEntry();
222 }
223
224
225
226
227
228
229
230 @Override
231 public void setEntry( Entry entry )
232 {
233 getDecorated().setEntry( entry );
234 }
235 }