KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > reponse > ErrorResponse


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
21 package org.apache.directory.ldapstudio.dsmlv2.reponse;
22
23
24 import org.apache.directory.ldapstudio.dsmlv2.DsmlDecorator;
25 import org.apache.directory.shared.ldap.codec.LdapResponse;
26 import org.dom4j.Element;
27
28
29 /**
30  * Class representing Error Response. <br>
31  * <br>
32  * An Error Response has a requestID, a message, and a type which can be :
33  * <ul>
34  * <li>NOT_ATTEMPTED,</li>
35  * <li>COULD_NOT_CONNECT,</li>
36  * <li>CONNECTION_CLOSED,</li>
37  * <li>MALFORMED_REQUEST,</li>
38  * <li>GATEWAY_INTERNAL_ERROR,</li>
39  * <li>AUTHENTICATION_FAILED,</li>
40  * <li>UNRESOLVABLE_URI,</li>
41  * <li>OTHER</li>
42  * </ul>
43  *
44  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
45  * @version $Rev$, $Date$
46  */

47 public class ErrorResponse extends LdapResponse implements DsmlDecorator
48 {
49     /**
50      * This enum represents the different types of error response
51      *
52      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
53      * @version $Rev$, $Date$
54      */

55     public enum ErrorResponseType
56     {
57         NOT_ATTEMPTED, COULD_NOT_CONNECT, CONNECTION_CLOSED, MALFORMED_REQUEST, GATEWAY_INTERNAL_ERROR, AUTHENTICATION_FAILED, UNRESOLVABLE_URI, OTHER
58     };
59
60     /** The type of error response */
61     private ErrorResponseType type;
62
63     /** The associated message */
64     private String JavaDoc message;
65
66     /** The request ID */
67     private int requestID;
68
69
70     /**
71      * Creates a new instance of ErrorResponse.
72      */

73     public ErrorResponse()
74     {
75     }
76
77
78     /**
79      * Creates a new instance of ErrorResponse.
80      *
81      * @param requestID
82      * the requestID of the response
83      * @param type
84      * the type of the response
85      * @param message
86      * the associated message
87      */

88     public ErrorResponse( int requestID, ErrorResponseType type, String JavaDoc message )
89     {
90         this.requestID = requestID;
91         this.type = type;
92         this.message = message;
93     }
94
95
96     /* (non-Javadoc)
97      * @see org.apache.directory.ldapstudio.dsmlv2.reponse.DsmlDecorator#toDsml(org.dom4j.Element)
98      */

99     public Element toDsml( Element root )
100     {
101         Element element = root.addElement( "errorResponse" );
102
103         // RequestID
104
if ( requestID != 0 )
105         {
106             element.addAttribute( "requestID", "" + requestID );
107         }
108
109         // Type
110
element.addAttribute( "type", getTypeDescr( type ) );
111
112         // TODO Add Detail
113

114         if ( ( message != null ) && ( !"".equals( message ) ) )
115         {
116             Element messageElement = element.addElement( "message" );
117             messageElement.addText( message );
118         }
119
120         return element;
121     }
122
123
124     /**
125      * Returns the String associated to the error response type
126      *
127      * @param type
128      * the error response type
129      * @return
130      * the corresponding String
131      */

132     public String JavaDoc getTypeDescr( ErrorResponseType type )
133     {
134         if ( type.equals( ErrorResponseType.NOT_ATTEMPTED ) )
135         {
136             return "notAttempted";
137         }
138         else if ( type.equals( ErrorResponseType.COULD_NOT_CONNECT ) )
139         {
140             return "couldNotConnect";
141         }
142         else if ( type.equals( ErrorResponseType.CONNECTION_CLOSED ) )
143         {
144             return "connectionClosed";
145         }
146         else if ( type.equals( ErrorResponseType.MALFORMED_REQUEST ) )
147         {
148             return "malformedRequest";
149         }
150         else if ( type.equals( ErrorResponseType.GATEWAY_INTERNAL_ERROR ) )
151         {
152             return "gatewayInternalError";
153         }
154         else if ( type.equals( ErrorResponseType.AUTHENTICATION_FAILED ) )
155         {
156             return "authenticationFailed";
157         }
158         else if ( type.equals( ErrorResponseType.UNRESOLVABLE_URI ) )
159         {
160             return "unresolvableURI";
161         }
162         else if ( type.equals( ErrorResponseType.OTHER ) )
163         {
164             return "other";
165         }
166         else
167         {
168             return "unknown";
169         }
170     }
171
172
173     /**
174      * Gets the message
175      *
176      * @return
177      * the message
178      */

179     public String JavaDoc getMessage()
180     {
181         return message;
182     }
183
184
185     /**
186      * Sets the message
187      *
188      * @param message
189      * the message to set
190      */

191     public void setMessage( String JavaDoc message )
192     {
193         this.message = message;
194     }
195
196
197     /**
198      * Gets the request ID
199      *
200      * @return
201      * the request ID
202      */

203     public int getRequestID()
204     {
205         return requestID;
206     }
207
208
209     /**
210      * Sets the request ID
211      *
212      * @param requestID
213      * the request ID to set
214      */

215     public void setRequestID( int requestID )
216     {
217         this.requestID = requestID;
218     }
219
220
221     /**
222      * Gets the type of error response
223      *
224      * @return
225      * the type of error response
226      */

227     public ErrorResponseType getType()
228     {
229         return type;
230     }
231
232
233     /**
234      * Sets the type of error response
235      *
236      * @param type
237      * the type of error response to set
238      */

239     public void setType( ErrorResponseType type )
240     {
241         this.type = type;
242     }
243 }
244
Popular Tags