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.DsmlDecorator;
24 import org.apache.directory.api.ldap.codec.api.LdapApiService;
25 import org.apache.directory.api.ldap.model.message.AbstractResponse;
26 import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
27 import org.apache.directory.api.ldap.model.message.Response;
28 import org.dom4j.Element;
29 import org.dom4j.tree.DefaultElement;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class ErrorResponse extends AbstractResponse implements Response, DsmlDecorator<Response>
53 {
54 private static final String ERROR_RESPONSE_TAG = "errorResponse";
55
56
57
58
59
60
61 public enum ErrorResponseType
62 {
63
64 NOT_ATTEMPTED,
65
66 COULD_NOT_CONNECT,
67
68 CONNECTION_CLOSED,
69
70 MALFORMED_REQUEST,
71
72 GATEWAY_INTERNAL_ERROR,
73
74 AUTHENTICATION_FAILED,
75
76 UNRESOLVABLE_URI,
77
78 OTHER
79 }
80
81
82 private ErrorResponseType errorType;
83
84
85 private String message;
86
87
88 private int requestID;
89
90
91
92
93
94
95
96
97 public ErrorResponse( int id, MessageTypeEnum type )
98 {
99 super( id, type );
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113 public ErrorResponse( int requestID, ErrorResponseType type, String message )
114 {
115 super( requestID, null );
116 this.requestID = requestID;
117 this.errorType = type;
118 this.message = message;
119 }
120
121
122
123
124
125 public Element toDsml( Element root )
126 {
127 Element element = null;
128
129 if ( root != null )
130 {
131 element = root.addElement( ERROR_RESPONSE_TAG );
132 }
133 else
134 {
135 element = new DefaultElement( ERROR_RESPONSE_TAG );
136 }
137
138
139 if ( requestID != 0 )
140 {
141 element.addAttribute( "requestID", "" + requestID );
142 }
143
144
145 element.addAttribute( "type", getTypeDescr( errorType ) );
146
147
148
149 if ( ( message != null ) && ( !"".equals( message ) ) )
150 {
151 Element messageElement = element.addElement( "message" );
152 messageElement.addText( message );
153 }
154
155 return element;
156 }
157
158
159
160
161
162
163
164
165
166
167 public String getTypeDescr( ErrorResponseType type )
168 {
169 if ( type.equals( ErrorResponseType.NOT_ATTEMPTED ) )
170 {
171 return "notAttempted";
172 }
173 else if ( type.equals( ErrorResponseType.COULD_NOT_CONNECT ) )
174 {
175 return "couldNotConnect";
176 }
177 else if ( type.equals( ErrorResponseType.CONNECTION_CLOSED ) )
178 {
179 return "connectionClosed";
180 }
181 else if ( type.equals( ErrorResponseType.MALFORMED_REQUEST ) )
182 {
183 return "malformedRequest";
184 }
185 else if ( type.equals( ErrorResponseType.GATEWAY_INTERNAL_ERROR ) )
186 {
187 return "gatewayInternalError";
188 }
189 else if ( type.equals( ErrorResponseType.AUTHENTICATION_FAILED ) )
190 {
191 return "authenticationFailed";
192 }
193 else if ( type.equals( ErrorResponseType.UNRESOLVABLE_URI ) )
194 {
195 return "unresolvableURI";
196 }
197 else if ( type.equals( ErrorResponseType.OTHER ) )
198 {
199 return "other";
200 }
201 else
202 {
203 return "unknown";
204 }
205 }
206
207
208
209
210
211
212
213
214 public String getMessage()
215 {
216 return message;
217 }
218
219
220
221
222
223
224
225
226 public void setMessage( String message )
227 {
228 this.message = message;
229 }
230
231
232
233
234
235
236
237
238 public int getRequestID()
239 {
240 return requestID;
241 }
242
243
244
245
246
247
248
249
250 public void setRequestID( int requestID )
251 {
252 this.requestID = requestID;
253 }
254
255
256
257
258
259
260
261 public ErrorResponseType getErrorType()
262 {
263 return errorType;
264 }
265
266
267
268
269
270
271
272 public void setErrorType( ErrorResponseType errorType )
273 {
274 this.errorType = errorType;
275 }
276
277
278 public LdapApiService getCodecService()
279 {
280 throw new IllegalArgumentException( "This should not be a decorator " +
281 "but seems it was made into one. We need to do something about" +
282 "this if this exception is being raise." );
283 }
284
285
286 public Response getDecorated()
287 {
288 return this;
289 }
290 }