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