001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.jndi.provider.ldap;
019:
020: import java.lang.reflect.Constructor;
021: import java.util.HashMap;
022:
023: import javax.naming.AuthenticationException;
024: import javax.naming.AuthenticationNotSupportedException;
025: import javax.naming.CommunicationException;
026: import javax.naming.ContextNotEmptyException;
027: import javax.naming.InvalidNameException;
028: import javax.naming.LimitExceededException;
029: import javax.naming.NameAlreadyBoundException;
030: import javax.naming.NameNotFoundException;
031: import javax.naming.NamingException;
032: import javax.naming.NoPermissionException;
033: import javax.naming.OperationNotSupportedException;
034: import javax.naming.PartialResultException;
035: import javax.naming.ServiceUnavailableException;
036: import javax.naming.SizeLimitExceededException;
037: import javax.naming.TimeLimitExceededException;
038: import javax.naming.directory.AttributeInUseException;
039: import javax.naming.directory.InvalidAttributeIdentifierException;
040: import javax.naming.directory.InvalidAttributeValueException;
041: import javax.naming.directory.InvalidSearchFilterException;
042: import javax.naming.directory.NoSuchAttributeException;
043: import javax.naming.directory.SchemaViolationException;
044:
045: import org.apache.harmony.jndi.internal.nls.Messages;
046: import org.apache.harmony.jndi.provider.ldap.parser.FilterParser;
047: import org.apache.harmony.jndi.provider.ldap.parser.LdapUrlParser;
048: import org.apache.harmony.jndi.provider.ldap.parser.ParseException;
049:
050: @SuppressWarnings("boxing")
051: public class LdapUtils {
052: private static HashMap<Integer, Class<?>> errorCodesMap = new HashMap<Integer, Class<?>>();
053:
054: static {
055: errorCodesMap.put(1, NamingException.class);
056: errorCodesMap.put(2, CommunicationException.class);
057: errorCodesMap.put(3, TimeLimitExceededException.class);
058: errorCodesMap.put(4, SizeLimitExceededException.class);
059: errorCodesMap.put(7, AuthenticationNotSupportedException.class);
060: errorCodesMap.put(8, AuthenticationNotSupportedException.class);
061: errorCodesMap.put(9, PartialResultException.class);
062: errorCodesMap.put(11, LimitExceededException.class);
063: errorCodesMap.put(12, OperationNotSupportedException.class);
064: errorCodesMap
065: .put(13, AuthenticationNotSupportedException.class);
066: errorCodesMap.put(16, NoSuchAttributeException.class);
067: errorCodesMap
068: .put(17, InvalidAttributeIdentifierException.class);
069: errorCodesMap.put(18, InvalidSearchFilterException.class);
070: errorCodesMap.put(19, InvalidAttributeValueException.class);
071: errorCodesMap.put(20, AttributeInUseException.class);
072: errorCodesMap.put(21, InvalidAttributeValueException.class);
073: errorCodesMap.put(32, NameNotFoundException.class);
074: errorCodesMap.put(33, NamingException.class);
075: errorCodesMap.put(34, InvalidNameException.class);
076: errorCodesMap.put(36, NamingException.class);
077: errorCodesMap
078: .put(48, AuthenticationNotSupportedException.class);
079: errorCodesMap.put(49, AuthenticationException.class);
080: errorCodesMap.put(50, NoPermissionException.class);
081: errorCodesMap.put(51, ServiceUnavailableException.class);
082: errorCodesMap.put(52, ServiceUnavailableException.class);
083: errorCodesMap.put(53, OperationNotSupportedException.class);
084: errorCodesMap.put(54, NamingException.class);
085: errorCodesMap.put(64, InvalidNameException.class);
086: errorCodesMap.put(65, SchemaViolationException.class);
087: errorCodesMap.put(66, ContextNotEmptyException.class);
088: errorCodesMap.put(67, SchemaViolationException.class);
089: errorCodesMap.put(68, NameAlreadyBoundException.class);
090: errorCodesMap.put(69, SchemaViolationException.class);
091: errorCodesMap.put(71, NamingException.class);
092: errorCodesMap.put(80, NamingException.class);
093: }
094:
095: public static Filter parseFilter(String filter, Object[] args)
096: throws InvalidSearchFilterException {
097: if (filter == null) {
098: // ldap.28=Parameter of filter should not be null
099: throw new NullPointerException(Messages
100: .getString("ldap.28")); //$NON-NLS-1$
101: }
102:
103: FilterParser parser = new FilterParser(filter);
104:
105: if (args == null) {
106: args = new Object[0];
107: }
108:
109: parser.setArgs(args);
110:
111: try {
112: return parser.parse();
113: } catch (ParseException e) {
114: // ldap.29=Invalid search filter
115: InvalidSearchFilterException ex = new InvalidSearchFilterException(
116: Messages.getString("ldap.29")); //$NON-NLS-1$
117: ex.setRootCause(e);
118: throw ex;
119: }
120: }
121:
122: public static LdapUrlParser parserURL(String url,
123: boolean isAllowedQuery) throws InvalidNameException {
124: if (url == null) {
125: // ldap.2B=LDAP URL should not be null
126: throw new NullPointerException(Messages
127: .getString("ldap.2B")); //$NON-NLS-1$
128: }
129:
130: LdapUrlParser parser = new LdapUrlParser(url);
131: try {
132: parser.parseURL();
133: } catch (ParseException e) {
134: // ldap.2C=Invalid LDAP URL
135: IllegalArgumentException ex = new IllegalArgumentException(
136: Messages.getString("ldap.2C")); //$NON-NLS-1$
137: ex.initCause(e);
138: throw ex;
139: }
140:
141: if (!isAllowedQuery
142: && (parser.getFilter() != null || parser.getControls() != null)) {
143: // ldap.2D=LDAP URL may only contain host, port and dn components
144: throw new InvalidNameException(Messages
145: .getString("ldap.2D")); //$NON-NLS-1$
146: }
147:
148: return parser;
149: }
150:
151: public static NamingException getExceptionFromResult(
152: LdapResult result) {
153: int errorCode = result.getResultCode();
154: // 0 means successful
155: if (errorCode == 0) {
156: return null;
157: }
158:
159: Class<?> exceptionClass = errorCodesMap.get(errorCode);
160: // not in map, using NamingException
161: if (exceptionClass == null) {
162: exceptionClass = NamingException.class;
163: }
164:
165: try {
166: Constructor<?> constructor = exceptionClass
167: .getConstructor(new Class[] { String.class });
168: String message = null;
169:
170: if (result.getErrorMessage() != null
171: && !result.getErrorMessage().equals("")) { //$NON-NLS-1$
172: // ldap.34=[LDAP: error code {0} - {1}]
173: message = Messages.getString("ldap.34", new Object[] { //$NON-NLS-1$
174: errorCode, result.getErrorMessage() });
175: } else {
176: // ldap.35=[LDAP: error code {0}]
177: message = Messages.getString("ldap.35", //$NON-NLS-1$
178: new Object[] { errorCode });
179: }
180:
181: return (NamingException) constructor
182: .newInstance(new Object[] { message });
183: } catch (Exception e) {
184: // ldap.35=[LDAP: error code {0}]
185: return new NamingException(Messages.getString("ldap.35", //$NON-NLS-1$
186: new Object[] { errorCode }));
187: }
188: }
189: }
|