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   *    http://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.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   * Class representing Error Response.
34   * <br>
35   * An Error Response has a requestID, a message, and a type which can be :
36   * <ul>
37   *     <li>NOT_ATTEMPTED,</li>
38   *     <li>COULD_NOT_CONNECT,</li>
39   *     <li>CONNECTION_CLOSED,</li>
40   *     <li>MALFORMED_REQUEST,</li>
41   *     <li>GATEWAY_INTERNAL_ERROR,</li>
42   *     <li>AUTHENTICATION_FAILED,</li>
43   *     <li>UNRESOLVABLE_URI,</li>
44   *     <li>OTHER</li>
45   * </ul>
46   * 
47   * TODO review this class - maybe it should not be decorated and if it is
48   * it should extend AbstractResultResponseDsml - by Alex
49   * 
50   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
51   */
52  public class ErrorResponse extends AbstractResponse implements Response, DsmlDecorator<Response>
53  {
54      private static final String ERROR_RESPONSE_TAG = "errorResponse";
55  
56      /**
57       * This enum represents the different types of error response
58       *
59       * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
60       */
61      public enum ErrorResponseType
62      {
63          /** Not attempted error response type. */
64          NOT_ATTEMPTED,
65          /** Could not connect error response type. */
66          COULD_NOT_CONNECT,
67          /**  error response type. */
68          CONNECTION_CLOSED,
69          /** Malformed request error response type. */
70          MALFORMED_REQUEST,
71          /** Gateway internal error error response type. */
72          GATEWAY_INTERNAL_ERROR,
73          /** Authentication failed error response type. */
74          AUTHENTICATION_FAILED,
75          /** Unresolveable URI error response type. */
76          UNRESOLVABLE_URI,
77          /** Other error response type. */
78          OTHER
79      }
80  
81      /** The type of error response */
82      private ErrorResponseType errorType;
83  
84      /** The associated message */
85      private String message;
86  
87      /** The request ID */
88      private int requestID;
89  
90  
91      /**
92       * Creates a new instance of ErrorResponse.
93       *
94       * @param id the response eliciting this Request
95       * @param type the message type of the response
96       */
97      public ErrorResponse( int id, MessageTypeEnum type )
98      {
99          super( id, type );
100     }
101 
102 
103     /**
104      * Creates a new instance of ErrorResponse.
105      *
106      * @param requestID
107      *      the requestID of the response
108      * @param type
109      *      the type of the response
110      * @param message
111      *      the associated message
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      * {@inheritDoc}
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         // RequestID
139         if ( requestID != 0 )
140         {
141             element.addAttribute( "requestID", Integer.toString( requestID ) );
142         }
143 
144         // Type
145         element.addAttribute( "type", getTypeDescr( errorType ) );
146 
147         // TODO Add Detail
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      * Returns the String associated to the error response type
161      * 
162      * @param type
163      *      the error response type
164      * @return
165      *      the corresponding String
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      * Gets the message
210      *
211      * @return
212      *      the message
213      */
214     public String getMessage()
215     {
216         return message;
217     }
218 
219 
220     /**
221      * Sets the message
222      *
223      * @param message
224      *      the message to set
225      */
226     public void setMessage( String message )
227     {
228         this.message = message;
229     }
230 
231 
232     /**
233      * Gets the request ID
234      *
235      * @return
236      *      the request ID
237      */
238     public int getRequestID()
239     {
240         return requestID;
241     }
242 
243 
244     /**
245      * Sets the request ID
246      *
247      * @param requestID
248      *      the request ID to set
249      */
250     public void setRequestID( int requestID )
251     {
252         this.requestID = requestID;
253     }
254 
255 
256     /**
257      * Gets the type of error response
258      *
259      * @return the type of error response
260      */
261     public ErrorResponseType getErrorType()
262     {
263         return errorType;
264     }
265 
266 
267     /**
268      * Sets the type of error response
269      *
270      * @param errorType the type of error response to set
271      */
272     public void setErrorType( ErrorResponseType errorType )
273     {
274         this.errorType = errorType;
275     }
276 
277 
278     /**
279      * @return The LdapApiService instance
280      */
281     public LdapApiService getCodecService()
282     {
283         throw new IllegalArgumentException( "This should not be a decorator "
284             + "but seems it was made into one. We need to do something about"
285             + "this if this exception is being raise." );
286     }
287 
288 
289     public Response getDecorated()
290     {
291         return this;
292     }
293 }