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.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
43
44
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
54 private Attribute currentAttribute;
55
56
57
58
59
60 public SearchResultEntryDsml( LdapApiService codec )
61 {
62 super( codec, new SearchResultEntryImpl() );
63 }
64
65
66
67
68
69
70
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
86
87
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
99
100
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
117
118 public MessageTypeEnum getType()
119 {
120 return getDecorated().getType();
121 }
122
123
124
125
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
183
184
185
186 public Dn getObjectName()
187 {
188 return getDecorated().getObjectName();
189 }
190
191
192
193
194
195
196
197 public void setObjectName( Dn objectName )
198 {
199 getDecorated().setObjectName( objectName );
200 }
201
202
203
204
205
206
207
208 public Entry getEntry()
209 {
210 return getDecorated().getEntry();
211 }
212
213
214
215
216
217
218
219 public void setEntry( Entry entry )
220 {
221 getDecorated().setEntry( entry );
222 }
223 }