| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 2001 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap; |
| 39 | |
| 40 | //import org.ietf.ldap.client.*; |
| 41 | //import org.ietf.ldap.client.opers.*; |
| 42 | //import java.io.*; |
| 43 | import java.util.StringTokenizer; |
| 44 | import java.util.Vector; |
| 45 | |
| 46 | /** |
| 47 | * Represents the situation in which the LDAP server refers the client to |
| 48 | * another LDAP server. This exception constructs a list of referral URLs from |
| 49 | * the LDAP error message returned by the server. You can get this list by |
| 50 | * using the <CODE>getURLs</CODE> method. |
| 51 | * |
| 52 | * @version 1.0 |
| 53 | * @see org.ietf.ldap.LDAPException |
| 54 | */ |
| 55 | public class LDAPReferralException extends LDAPException { |
| 56 | |
| 57 | static final long serialVersionUID = 1771536577344289897L; |
| 58 | private String _referrals[] = null; |
| 59 | |
| 60 | /** |
| 61 | * Constructs a default exception with no specific error information. |
| 62 | */ |
| 63 | public LDAPReferralException() { |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Constructs a default exception with a specified string as |
| 68 | * additional information. This form is used for lower-level errors. |
| 69 | * @param message the additional error information |
| 70 | */ |
| 71 | public LDAPReferralException( String message ) { |
| 72 | super( message ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Constructs a default exception with a specified string as |
| 77 | * additional information. This form is used for higher-level LDAP |
| 78 | * operational errors. |
| 79 | * @param message the additional error information |
| 80 | * @param resultCode result code |
| 81 | * @param serverErrorMessage error message |
| 82 | */ |
| 83 | public LDAPReferralException( String message, |
| 84 | int resultCode, |
| 85 | String serverErrorMessage ) { |
| 86 | super(message, resultCode, serverErrorMessage); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Constructs an exception with a list of LDAP URLs to other LDAP servers. |
| 91 | * This list of referrals points the client to LDAP servers that may |
| 92 | * contain the requested entries. |
| 93 | * @param message the additional error information |
| 94 | * @param resultCode result code |
| 95 | * @param referrals array of LDAP URLs identifying other LDAP servers that |
| 96 | * may contain the requested entries |
| 97 | */ |
| 98 | public LDAPReferralException( String message, |
| 99 | int resultCode, |
| 100 | String referrals[] ) { |
| 101 | super(message, resultCode, (String)null); |
| 102 | _referrals = referrals; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Constructs an exception with a result code, a specified |
| 107 | * string of additional information, a string containing |
| 108 | * information passed back from the server, and a possible root |
| 109 | * exception. |
| 110 | * <P> |
| 111 | * |
| 112 | * After you construct the <CODE>LDAPException</CODE> object, |
| 113 | * the result code and messages will be accessible through the |
| 114 | * following ways: |
| 115 | * <P> |
| 116 | * <UL> |
| 117 | * <LI>This string of additional information appears if you |
| 118 | * call the <CODE>toString()</CODE> method. <P> |
| 119 | * <LI>The result code that you set is accessible through the |
| 120 | * <CODE>getLDAPResultCode()</CODE> method. <P> |
| 121 | * <LI>The string of server error information that you set |
| 122 | * is accessible through the <CODE>getLDAPErrorMessage</CODE> |
| 123 | * method. <P> |
| 124 | * </UL> |
| 125 | * <P> |
| 126 | * |
| 127 | * This form is used for higher-level LDAP operational errors. |
| 128 | * @param message the additional error information |
| 129 | * @param resultCode the result code returned |
| 130 | * @param serverErrorMessage error message specifying additional information |
| 131 | * returned from the server |
| 132 | * @param rootException An exception which caused the failure, if any |
| 133 | * @see org.ietf.ldap.LDAPException#toString() |
| 134 | * @see org.ietf.ldap.LDAPException#getResultCode() |
| 135 | * @see org.ietf.ldap.LDAPException#getLDAPErrorMessage() |
| 136 | * @see org.ietf.ldap.LDAPException#getMatchedDN() |
| 137 | */ |
| 138 | public LDAPReferralException( String message, |
| 139 | int resultCode, |
| 140 | String serverErrorMessage, |
| 141 | Throwable rootException ) { |
| 142 | super( message, resultCode, serverErrorMessage, rootException ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Gets the list of referrals (LDAP URLs to other servers) returned by the LDAP server. |
| 147 | * You can use this list to find the LDAP server that can fulfill your request. |
| 148 | * |
| 149 | * If you have set up your search constraints (or the <CODE>LDAPConnection</CODE> object) |
| 150 | * to follow referrals automatically, any operation that results in a referral will use |
| 151 | * this list to create new connections to the LDAP servers in this list. |
| 152 | * |
| 153 | * @return list of LDAP URLs to other LDAP servers. |
| 154 | */ |
| 155 | public String[] getReferrals() { |
| 156 | if ( getLDAPErrorMessage() == null ) { |
| 157 | return (_referrals != null) ? _referrals : new String[0]; |
| 158 | } else { |
| 159 | return extractReferrals( getLDAPErrorMessage() ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Gets the referral URL that could not be followed. If multiple URLs |
| 165 | * are in the list, and none could be followed, the method returns one |
| 166 | * of them. |
| 167 | * |
| 168 | * @return the referral URL that could not be followed |
| 169 | */ |
| 170 | public String getFailedReferral() { |
| 171 | String[] urls = getReferrals(); |
| 172 | return (urls.length > 0) ? urls[0] : ""; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Sets the referral URL that could not be followed |
| 177 | * |
| 178 | * @param referral the referral URL that could not be followed |
| 179 | */ |
| 180 | public void setFailedReferral( String referral ) { |
| 181 | _referrals = new String[] { referral }; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Extract referral string from the error message. The |
| 186 | * error string is based on "Referrals Within the |
| 187 | * LDAPConnection Protocol". |
| 188 | * @param error string |
| 189 | */ |
| 190 | private String[] extractReferrals(String error) { |
| 191 | if (error == null) |
| 192 | return null; |
| 193 | StringTokenizer st = new StringTokenizer(error, "\n"); |
| 194 | Vector<String> v = new Vector<>(); |
| 195 | boolean referrals = false; |
| 196 | while (st.hasMoreTokens()) { |
| 197 | String token = st.nextToken(); |
| 198 | if (referrals) { |
| 199 | v.addElement(token); |
| 200 | } else { |
| 201 | if (token.startsWith("Referral:")) |
| 202 | referrals = true; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (v.size() == 0) |
| 207 | return null; |
| 208 | String res[] = new String[v.size()]; |
| 209 | for (int i = 0; i < v.size(); i++) { |
| 210 | res[i] = v.elementAt(i); |
| 211 | } |
| 212 | return res; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Gets the string representation of the referral exception, |
| 217 | * which includes the result code, the message sent back |
| 218 | * from the LDAP server and the list of referrals. |
| 219 | * |
| 220 | * @return string representation of exception. |
| 221 | * @see org.ietf.ldap.LDAPException#resultCodeToString(int) |
| 222 | */ |
| 223 | public String toString() { |
| 224 | String str = super.toString(); |
| 225 | for (int i=0; i < _referrals.length; i++) { |
| 226 | str += "\n" + _referrals[i]; |
| 227 | } |
| 228 | return str; |
| 229 | } |
| 230 | } |