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 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 public SearchResponseDsml( LdapApiService codec )
57 {
58 super( codec, new SearchResponse() );
59 }
60
61
62
63
64
65
66
67 public SearchResponseDsml( LdapApiService codec, Message response )
68 {
69 super( codec, ( Response ) response );
70 }
71
72
73
74
75
76
77
78
79
80
81 public boolean addResponse( DsmlDecorator<? extends Response> response )
82 {
83 if ( response instanceof SearchResultEntry )
84 {
85 ( ( SearchResponse ) getDecorated() ).addSearchResultEntry(
86 ( SearchResultEntryDsml ) response );
87 }
88 else if ( response instanceof SearchResultReference )
89 {
90 ( ( SearchResponse ) getDecorated() ).addSearchResultReference(
91 ( SearchResultReferenceDsml ) response );
92 }
93 else if ( response instanceof SearchResultDone )
94 {
95 ( ( SearchResponse ) getDecorated() ).setSearchResultDone(
96 ( SearchResultDoneDsml ) response );
97 }
98 else
99 {
100 throw new IllegalArgumentException( "Unidentified search resp type" );
101 }
102
103 return responses.add( response );
104 }
105
106
107
108
109
110
111
112
113
114
115 public boolean removeResponse( DsmlDecorator<? extends Response> response )
116 {
117 if ( response instanceof SearchResultEntry )
118 {
119 ( ( SearchResponse ) getDecorated() ).removeSearchResultEntry(
120 ( SearchResultEntryDsml ) response );
121 }
122 else if ( response instanceof SearchResultReference )
123 {
124 ( ( SearchResponse ) getDecorated() ).removeSearchResultReference(
125 ( SearchResultReferenceDsml ) response );
126 }
127 else if ( response instanceof SearchResultDone )
128 {
129 if ( response.equals( ( ( SearchResponse ) getDecorated() ).getSearchResultDone() ) )
130 {
131 ( ( SearchResponse ) getDecorated() ).setSearchResultDone( null );
132 }
133 }
134 else
135 {
136 throw new IllegalArgumentException( "Unidentified search resp type" );
137 }
138
139 return responses.remove( response );
140 }
141
142
143
144
145
146 public Element toDsml( Element root )
147 {
148 Element element = null;
149
150 if ( root != null )
151 {
152 element = root.addElement( SEARCH_RESPONSE_TAG );
153 }
154 else
155 {
156 element = new DefaultElement( SEARCH_RESPONSE_TAG );
157 }
158
159
160 if ( getDecorated() != null )
161 {
162 int requestID = getDecorated().getMessageId();
163 if ( requestID > 0 )
164 {
165 element.addAttribute( "requestID", "" + requestID );
166 }
167 }
168
169 for ( DsmlDecorator<? extends Response> response : responses )
170 {
171 response.toDsml( element );
172 }
173
174 return element;
175 }
176 }