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 */ 020 021 package org.apache.directory.shared.dsmlv2.reponse; 022 023 024 import java.nio.ByteBuffer; 025 026 import org.apache.directory.shared.asn1.codec.EncoderException; 027 import org.apache.directory.shared.dsmlv2.DsmlDecorator; 028 import org.apache.directory.shared.ldap.codec.LdapResponseCodec; 029 import org.apache.directory.shared.ldap.codec.MessageTypeEnum; 030 import org.dom4j.Element; 031 032 033 /** 034 * Class representing Error Response. <br> 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 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 049 * @version $Rev$, $Date$ 050 */ 051 public class ErrorResponse extends LdapResponseCodec implements DsmlDecorator 052 { 053 /** 054 * This enum represents the different types of error response 055 * 056 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 057 * @version $Rev$, $Date$ 058 */ 059 public enum ErrorResponseType 060 { 061 NOT_ATTEMPTED, COULD_NOT_CONNECT, CONNECTION_CLOSED, MALFORMED_REQUEST, GATEWAY_INTERNAL_ERROR, AUTHENTICATION_FAILED, UNRESOLVABLE_URI, OTHER 062 }; 063 064 /** The type of error response */ 065 private ErrorResponseType type; 066 067 /** The associated message */ 068 private String message; 069 070 /** The request ID */ 071 private int requestID; 072 073 074 /** 075 * Creates a new instance of ErrorResponse. 076 */ 077 public ErrorResponse() 078 { 079 } 080 081 082 /** 083 * Creates a new instance of ErrorResponse. 084 * 085 * @param requestID 086 * the requestID of the response 087 * @param type 088 * the type of the response 089 * @param message 090 * the associated message 091 */ 092 public ErrorResponse( int requestID, ErrorResponseType type, String message ) 093 { 094 this.requestID = requestID; 095 this.type = type; 096 this.message = message; 097 } 098 099 100 /* (non-Javadoc) 101 * @see org.apache.directory.shared.dsmlv2.reponse.DsmlDecorator#toDsml(org.dom4j.Element) 102 */ 103 public Element toDsml( Element root ) 104 { 105 Element element = root.addElement( "errorResponse" ); 106 107 // RequestID 108 if ( requestID != 0 ) 109 { 110 element.addAttribute( "requestID", "" + requestID ); 111 } 112 113 // Type 114 element.addAttribute( "type", getTypeDescr( type ) ); 115 116 // TODO Add Detail 117 118 if ( ( message != null ) && ( !"".equals( message ) ) ) 119 { 120 Element messageElement = element.addElement( "message" ); 121 messageElement.addText( message ); 122 } 123 124 return element; 125 } 126 127 128 /** 129 * Returns the String associated to the error response type 130 * 131 * @param type 132 * the error response type 133 * @return 134 * the corresponding String 135 */ 136 public String getTypeDescr( ErrorResponseType type ) 137 { 138 if ( type.equals( ErrorResponseType.NOT_ATTEMPTED ) ) 139 { 140 return "notAttempted"; 141 } 142 else if ( type.equals( ErrorResponseType.COULD_NOT_CONNECT ) ) 143 { 144 return "couldNotConnect"; 145 } 146 else if ( type.equals( ErrorResponseType.CONNECTION_CLOSED ) ) 147 { 148 return "connectionClosed"; 149 } 150 else if ( type.equals( ErrorResponseType.MALFORMED_REQUEST ) ) 151 { 152 return "malformedRequest"; 153 } 154 else if ( type.equals( ErrorResponseType.GATEWAY_INTERNAL_ERROR ) ) 155 { 156 return "gatewayInternalError"; 157 } 158 else if ( type.equals( ErrorResponseType.AUTHENTICATION_FAILED ) ) 159 { 160 return "authenticationFailed"; 161 } 162 else if ( type.equals( ErrorResponseType.UNRESOLVABLE_URI ) ) 163 { 164 return "unresolvableURI"; 165 } 166 else if ( type.equals( ErrorResponseType.OTHER ) ) 167 { 168 return "other"; 169 } 170 else 171 { 172 return "unknown"; 173 } 174 } 175 176 177 /** 178 * Gets the message 179 * 180 * @return 181 * the message 182 */ 183 public String getMessage() 184 { 185 return message; 186 } 187 188 189 /** 190 * Sets the message 191 * 192 * @param message 193 * the message to set 194 */ 195 public void setMessage( String message ) 196 { 197 this.message = message; 198 } 199 200 201 /** 202 * Gets the request ID 203 * 204 * @return 205 * the request ID 206 */ 207 public int getRequestID() 208 { 209 return requestID; 210 } 211 212 213 /** 214 * Sets the request ID 215 * 216 * @param requestID 217 * the request ID to set 218 */ 219 public void setRequestID( int requestID ) 220 { 221 this.requestID = requestID; 222 } 223 224 225 /** 226 * Gets the type of error response 227 * 228 * @return 229 * the type of error response 230 */ 231 public ErrorResponseType getType() 232 { 233 return type; 234 } 235 236 237 /** 238 * Sets the type of error response 239 * 240 * @param type 241 * the type of error response to set 242 */ 243 public void setType( ErrorResponseType type ) 244 { 245 this.type = type; 246 } 247 248 249 @Override 250 protected int computeLengthProtocolOp() 251 { 252 return 0; 253 } 254 255 256 @Override 257 protected void encodeProtocolOp( ByteBuffer buffer ) throws EncoderException 258 { 259 } 260 261 262 @Override 263 public MessageTypeEnum getMessageType() 264 { 265 return null; 266 } 267 268 269 @Override 270 public String getMessageTypeName() 271 { 272 return null; 273 } 274 }