001package com.nimbusds.common.ldap;
002
003
004import com.unboundid.ldap.sdk.LDAPException;
005import com.unboundid.ldap.sdk.LDAPResult;
006import com.unboundid.ldap.sdk.ResultCode;
007
008import com.thetransactioncompany.jsonrpc2.JSONRPC2Error;
009
010
011/**
012 * LDAP exception utilities.
013 */
014public class LDAPExceptionUtils {
015
016
017        /**
018         * Turns an LDAP exception into a JSON-RPC 2.0 error with corresponding 
019         * code and message. The LDAP error codes are mapped directly (1:1) to 
020         * JSON-RPC 2.0 error codes, e.g. LDAP error 34 (Invalid DN syntax) 
021         * becomes JSON-RPC 2.0 error 34.
022         *
023         * @param ldapException A valid LDAP exception instance. Must not be 
024         *                      {@code null}.
025         *
026         * @return A JSON-RPC 2.0 error representing the original LDAP 
027         *         exception, ready for insertion into a JSON-RPC 2.0 response.
028         */
029        public static JSONRPC2Error toJSONRPC2Error(final LDAPException ldapException) {
030        
031                final ResultCode ldapCode = ldapException.getResultCode();
032                
033                // The LDAP code is mapped 1:1 to a Json2Ldap error code;
034                // it has a range from 0 to infinity
035                final int code = ldapCode.intValue();
036                
037                String message = ldapCode.getName();
038                
039                if (message != null && message.length() > 1) {
040                        // Capitalise the first letter of the LDAP message text
041                        message = "LDAP error: " + 
042                                  message.substring(0,1).toUpperCase() +
043                                  message.substring(1);
044                }
045                else {
046                        // No LDAP message was found, just state a generic LDAP error
047                        message = "LDAP error";
048                }
049                
050                return new JSONRPC2Error(code, message);
051        }
052        
053        
054        /**
055         * Throws a JSON-RPC 2.0 error with corresponding code and message if 
056         * the specified LDAP result indicates failure.
057         *
058         * @param result The result from an LDAP request. Must not be 
059         *               {@code null}.
060         *
061         * @throws JSONRPC2Error On an LDAP result indicating a failure.
062         */
063        public static void ensureSuccess(final LDAPResult result)
064                throws JSONRPC2Error {
065        
066                final ResultCode ldapCode = result.getResultCode();
067                
068                if (ldapCode == ResultCode.SUCCESS)
069                        return;
070                        
071                final int code = ldapCode.intValue();
072                String message = result.getDiagnosticMessage();
073                
074                if (message == null)
075                        message = ldapCode.getName(); // try alternative
076        
077                if (message != null && message.length() > 1) {
078                        // Capitalise the first letter of the LDAP message text
079                        message = "LDAP error: " + 
080                                  message.substring(0,1).toUpperCase() +
081                                  message.substring(1);
082                }
083                else {
084                        // No LDAP message was found, just state a generic LDAP error
085                        message = "LDAP error";
086                }
087                
088                throw new JSONRPC2Error(code, message);
089        }
090        
091
092        /**
093         * Prevents instantiation.
094         */
095        private LDAPExceptionUtils() {
096        
097                // Nothing to do
098        }
099}