001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.dsmlv2.reponse; 021 022 023import org.apache.directory.api.dsmlv2.DsmlDecorator; 024import org.apache.directory.api.ldap.codec.api.LdapApiService; 025import org.apache.directory.api.ldap.model.message.AbstractResponse; 026import org.apache.directory.api.ldap.model.message.MessageTypeEnum; 027import org.apache.directory.api.ldap.model.message.Response; 028import org.dom4j.Element; 029import org.dom4j.tree.DefaultElement; 030 031 032/** 033 * Class representing Error Response. 034 * <br/> 035 * An Error Response has a requestID, a message, and a type which can be : 036 * <ul> 037 * <li>NOT_ATTEMPTED,</li> 038 * <li>COULD_NOT_CONNECT,</li> 039 * <li>CONNECTION_CLOSED,</li> 040 * <li>MALFORMED_REQUEST,</li> 041 * <li>GATEWAY_INTERNAL_ERROR,</li> 042 * <li>AUTHENTICATION_FAILED,</li> 043 * <li>UNRESOLVABLE_URI,</li> 044 * <li>OTHER</li> 045 * </ul> 046 * 047 * @TODO review this class - maybe it should not be decorated and if it is 048 * it should extend AbstractResultResponseDsml - by Alex 049 * 050 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 051 */ 052public class ErrorResponse extends AbstractResponse implements Response, DsmlDecorator<Response> 053{ 054 private static final String ERROR_RESPONSE_TAG = "errorResponse"; 055 056 /** 057 * This enum represents the different types of error response 058 * 059 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 060 */ 061 public enum ErrorResponseType 062 { 063 /** Not attempted error response type. */ 064 NOT_ATTEMPTED, 065 /** Could not connect error response type. */ 066 COULD_NOT_CONNECT, 067 /** error response type. */ 068 CONNECTION_CLOSED, 069 /** Malformed request error response type. */ 070 MALFORMED_REQUEST, 071 /** Gateway internal error error response type. */ 072 GATEWAY_INTERNAL_ERROR, 073 /** Authentication failed error response type. */ 074 AUTHENTICATION_FAILED, 075 /** Unresolveable URI error response type. */ 076 UNRESOLVABLE_URI, 077 /** Other error response type. */ 078 OTHER 079 } 080 081 /** The type of error response */ 082 private ErrorResponseType errorType; 083 084 /** The associated message */ 085 private String message; 086 087 /** The request ID */ 088 private int requestID; 089 090 091 /** 092 * Creates a new instance of ErrorResponse. 093 * 094 * @param id the response eliciting this Request 095 * @param type the message type of the response 096 */ 097 public ErrorResponse( int id, MessageTypeEnum type ) 098 { 099 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", "" + 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 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}