View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * Class representing Error Response.
37   * <br>
38   * An Error Response has a requestID, a message, and a type which can be :
39   * <ul>
40   *     <li>NOT_ATTEMPTED,</li>
41   *     <li>COULD_NOT_CONNECT,</li>
42   *     <li>CONNECTION_CLOSED,</li>
43   *     <li>MALFORMED_REQUEST,</li>
44   *     <li>GATEWAY_INTERNAL_ERROR,</li>
45   *     <li>AUTHENTICATION_FAILED,</li>
46   *     <li>UNRESOLVABLE_URI,</li>
47   *     <li>OTHER</li>
48   * </ul>
49   * 
50   * TODO review this class - maybe it should not be decorated and if it is
51   * it should extend AbstractResultResponseDsml - by Alex
52   * 
53   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
54   */
55  public class ErrorResponse extends AbstractResponse implements Response, DsmlDecorator<Response>
56  {
57      /**
58       * This enum represents the different types of error response
59       *
60       * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
61       */
62      public enum ErrorResponseType
63      {
64          /** Not attempted error response type. */
65          NOT_ATTEMPTED,
66          /** Could not connect error response type. */
67          COULD_NOT_CONNECT,
68          /**  error response type. */
69          CONNECTION_CLOSED,
70          /** Malformed request error response type. */
71          MALFORMED_REQUEST,
72          /** Gateway internal error error response type. */
73          GATEWAY_INTERNAL_ERROR,
74          /** Authentication failed error response type. */
75          AUTHENTICATION_FAILED,
76          /** Unresolveable URI error response type. */
77          UNRESOLVABLE_URI,
78          /** Other error response type. */
79          OTHER
80      }
81  
82      /** The type of error response */
83      private ErrorResponseType errorType;
84  
85      /** The associated message */
86      private String message;
87  
88      /** The request ID */
89      private int requestID;
90  
91  
92      /**
93       * Creates a new instance of ErrorResponse.
94       *
95       * @param id the response eliciting this Request
96       * @param type the message type of the response
97       */
98      public ErrorResponse( int id, MessageTypeEnum type )
99      {
100         super( id, type );
101     }
102 
103 
104     /**
105      * Creates a new instance of ErrorResponse.
106      *
107      * @param requestID
108      *      the requestID of the response
109      * @param type
110      *      the type of the response
111      * @param message
112      *      the associated message
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      * {@inheritDoc}
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         // RequestID
141         if ( requestID != 0 )
142         {
143             element.addAttribute( DsmlLiterals.REQUEST_ID, Integer.toString( requestID ) );
144         }
145 
146         // Type
147         element.addAttribute( DsmlLiterals.TYPE, getTypeDescr( errorType ) );
148 
149         // TODO Add Detail
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      * Returns the String associated to the error response type
163      * 
164      * @param type
165      *      the error response type
166      * @return
167      *      the corresponding String
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      * Gets the message
212      *
213      * @return
214      *      the message
215      */
216     public String getMessage()
217     {
218         return message;
219     }
220 
221 
222     /**
223      * Sets the message
224      *
225      * @param message
226      *      the message to set
227      */
228     public void setMessage( String message )
229     {
230         this.message = message;
231     }
232 
233 
234     /**
235      * Gets the request ID
236      *
237      * @return
238      *      the request ID
239      */
240     public int getRequestID()
241     {
242         return requestID;
243     }
244 
245 
246     /**
247      * Sets the request ID
248      *
249      * @param requestID
250      *      the request ID to set
251      */
252     public void setRequestID( int requestID )
253     {
254         this.requestID = requestID;
255     }
256 
257 
258     /**
259      * Gets the type of error response
260      *
261      * @return the type of error response
262      */
263     public ErrorResponseType getErrorType()
264     {
265         return errorType;
266     }
267 
268 
269     /**
270      * Sets the type of error response
271      *
272      * @param errorType the type of error response to set
273      */
274     public void setErrorType( ErrorResponseType errorType )
275     {
276         this.errorType = errorType;
277     }
278 
279 
280     /**
281      * @return The LdapApiService instance
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      * {@inheritDoc}
291      */
292     @Override
293     public Response getDecorated()
294     {
295         return this;
296     }
297 }