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 * https://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.response; 021 022 023import org.apache.directory.api.dsmlv2.DsmlDecorator; 024import org.apache.directory.api.i18n.I18n; 025import org.apache.directory.api.ldap.codec.api.LdapApiService; 026import org.apache.directory.api.ldap.model.message.AbstractResponse; 027import org.apache.directory.api.ldap.model.message.MessageTypeEnum; 028import org.apache.directory.api.ldap.model.message.Response; 029import org.dom4j.Element; 030import org.dom4j.tree.DefaultElement; 031 032 033/** 034 * Class representing Error Response. 035 * <br> 036 * An Error Response has a requestID, a message, and a type which can be : 037 * <ul> 038 * <li>NOT_ATTEMPTED,</li> 039 * <li>COULD_NOT_CONNECT,</li> 040 * <li>CONNECTION_CLOSED,</li> 041 * <li>MALFORMED_REQUEST,</li> 042 * <li>GATEWAY_INTERNAL_ERROR,</li> 043 * <li>AUTHENTICATION_FAILED,</li> 044 * <li>UNRESOLVABLE_URI,</li> 045 * <li>OTHER</li> 046 * </ul> 047 * 048 * TODO review this class - maybe it should not be decorated and if it is 049 * it should extend AbstractResultResponseDsml - by Alex 050 * 051 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 052 */ 053public class ErrorResponse extends AbstractResponse implements Response, DsmlDecorator<Response> 054{ 055 private static final String ERROR_RESPONSE_TAG = "errorResponse"; 056 057 /** 058 * This enum represents the different types of error response 059 * 060 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 061 */ 062 public enum ErrorResponseType 063 { 064 /** Not attempted error response type. */ 065 NOT_ATTEMPTED, 066 /** Could not connect error response type. */ 067 COULD_NOT_CONNECT, 068 /** error response type. */ 069 CONNECTION_CLOSED, 070 /** Malformed request error response type. */ 071 MALFORMED_REQUEST, 072 /** Gateway internal error error response type. */ 073 GATEWAY_INTERNAL_ERROR, 074 /** Authentication failed error response type. */ 075 AUTHENTICATION_FAILED, 076 /** Unresolveable URI error response type. */ 077 UNRESOLVABLE_URI, 078 /** Other error response type. */ 079 OTHER 080 } 081 082 /** The type of error response */ 083 private ErrorResponseType errorType; 084 085 /** The associated message */ 086 private String message; 087 088 /** The request ID */ 089 private int requestID; 090 091 092 /** 093 * Creates a new instance of ErrorResponse. 094 * 095 * @param id the response eliciting this Request 096 * @param type the message type of the response 097 */ 098 public ErrorResponse( int id, MessageTypeEnum type ) 099 { 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( ERROR_RESPONSE_TAG ); 134 } 135 else 136 { 137 element = new DefaultElement( ERROR_RESPONSE_TAG ); 138 } 139 140 // RequestID 141 if ( requestID != 0 ) 142 { 143 element.addAttribute( "requestID", Integer.toString( requestID ) ); 144 } 145 146 // Type 147 element.addAttribute( "type", getTypeDescr( errorType ) ); 148 149 // TODO Add Detail 150 151 if ( ( message != null ) && ( !"".equals( message ) ) ) 152 { 153 Element messageElement = element.addElement( "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}