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 java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.directory.api.dsmlv2.DsmlDecorator;
27 import org.apache.directory.api.ldap.codec.api.LdapApiService;
28 import org.apache.directory.api.ldap.model.message.Message;
29 import org.apache.directory.api.ldap.model.message.Response;
30 import org.apache.directory.api.ldap.model.message.SearchResultDone;
31 import org.apache.directory.api.ldap.model.message.SearchResultEntry;
32 import org.apache.directory.api.ldap.model.message.SearchResultReference;
33 import org.dom4j.Element;
34 import org.dom4j.tree.DefaultElement;
35
36
37
38
39
40
41
42
43
44 public class SearchResponseDsml extends AbstractResponseDsml<Response>
45 {
46 private static final String SEARCH_RESPONSE_TAG = "searchResponse";
47
48
49 private List<DsmlDecorator<? extends Response>> responses =
50 new ArrayList<DsmlDecorator<? extends Response>>();
51
52
53
54
55
56
57
58 public SearchResponseDsml( LdapApiService codec )
59 {
60 super( codec, new SearchResponse() );
61 }
62
63
64
65
66
67
68
69
70 public SearchResponseDsml( LdapApiService codec, Message response )
71 {
72 super( codec, ( Response ) response );
73 }
74
75
76
77
78
79
80
81
82
83
84 public boolean addResponse( DsmlDecorator<? extends Response> response )
85 {
86 if ( response instanceof SearchResultEntry )
87 {
88 ( ( SearchResponse ) getDecorated() ).addSearchResultEntry(
89 ( SearchResultEntryDsml ) response );
90 }
91 else if ( response instanceof SearchResultReference )
92 {
93 ( ( SearchResponse ) getDecorated() ).addSearchResultReference(
94 ( SearchResultReferenceDsml ) response );
95 }
96 else if ( response instanceof SearchResultDone )
97 {
98 ( ( SearchResponse ) getDecorated() ).setSearchResultDone(
99 ( SearchResultDoneDsml ) response );
100 }
101 else
102 {
103 throw new IllegalArgumentException( "Unidentified search resp type" );
104 }
105
106 return responses.add( response );
107 }
108
109
110
111
112
113
114
115
116
117
118 public boolean removeResponse( DsmlDecorator<? extends Response> response )
119 {
120 if ( response instanceof SearchResultEntry )
121 {
122 ( ( SearchResponse ) getDecorated() ).removeSearchResultEntry(
123 ( SearchResultEntryDsml ) response );
124 }
125 else if ( response instanceof SearchResultReference )
126 {
127 ( ( SearchResponse ) getDecorated() ).removeSearchResultReference(
128 ( SearchResultReferenceDsml ) response );
129 }
130 else if ( response instanceof SearchResultDone )
131 {
132 if ( response.equals( ( ( SearchResponse ) getDecorated() ).getSearchResultDone() ) )
133 {
134 ( ( SearchResponse ) getDecorated() ).setSearchResultDone( null );
135 }
136 }
137 else
138 {
139 throw new IllegalArgumentException( "Unidentified search resp type" );
140 }
141
142 return responses.remove( response );
143 }
144
145
146
147
148
149 public Element toDsml( Element root )
150 {
151 Element element = null;
152
153 if ( root != null )
154 {
155 element = root.addElement( SEARCH_RESPONSE_TAG );
156 }
157 else
158 {
159 element = new DefaultElement( SEARCH_RESPONSE_TAG );
160 }
161
162
163 if ( getDecorated() != null )
164 {
165 int requestID = getDecorated().getMessageId();
166 if ( requestID > 0 )
167 {
168 element.addAttribute( "requestID", Integer.toString( requestID ) );
169 }
170 }
171
172 for ( DsmlDecorator<? extends Response> response : responses )
173 {
174 response.toDsml( element );
175 }
176
177 return element;
178 }
179 }