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.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
43
44
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
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 public MessageTypeEnum getType()
125 {
126 return getDecorated().getType();
127 }
128
129
130
131
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
189
190
191
192 public Dn getObjectName()
193 {
194 return getDecorated().getObjectName();
195 }
196
197
198
199
200
201
202
203 public void setObjectName( Dn objectName )
204 {
205 getDecorated().setObjectName( objectName );
206 }
207
208
209
210
211
212
213
214 public Entry getEntry()
215 {
216 return getDecorated().getEntry();
217 }
218
219
220
221
222
223
224
225 public void setEntry( Entry entry )
226 {
227 getDecorated().setEntry( entry );
228 }
229 }