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.util.Hashtable;
021:
022: import javax.naming.Context;
023: import javax.naming.NamingException;
024: import javax.naming.ldap.Control;
025: import javax.naming.ldap.LdapReferralException;
026:
027: import org.apache.harmony.jndi.provider.ldap.parser.LdapUrlParser;
028:
029: public class ReferralExceptionImpl extends LdapReferralException {
030:
031: private static final long serialVersionUID = -485662331078312979L;
032:
033: private String targetDN;
034:
035: private String[] referrals;
036:
037: private Hashtable<Object, Object> env;
038:
039: private int index = 0;
040:
041: public ReferralExceptionImpl(String targetDN, String[] referrals,
042: Hashtable<Object, Object> env) {
043: this .targetDN = targetDN;
044: this .referrals = referrals;
045: if (env == null) {
046: this .env = new Hashtable<Object, Object>();
047: } else {
048: this .env = (Hashtable<Object, Object>) env.clone();
049: }
050: }
051:
052: @Override
053: public Context getReferralContext() throws NamingException {
054: if (index >= referrals.length) {
055: return null;
056: }
057:
058: LdapUrlParser parser = LdapUtils.parserURL(referrals[index],
059: false);
060:
061: String host = parser.getHost();
062: int port = parser.getPort();
063:
064: LdapClient client = LdapClient.newInstance(host, port, env);
065:
066: LdapContextImpl context = new LdapContextImpl(client,
067: (Hashtable<Object, Object>) env, targetDN);
068: return context;
069:
070: }
071:
072: @Override
073: public Context getReferralContext(Hashtable<?, ?> h)
074: throws NamingException {
075: if (index >= referrals.length) {
076: return null;
077: }
078:
079: if (h == null) {
080: return getReferralContext();
081: }
082:
083: Hashtable<Object, Object> myEnv = (Hashtable<Object, Object>) h
084: .clone();
085: LdapUrlParser parser = LdapUtils.parserURL(referrals[index],
086: true);
087:
088: String host = parser.getHost();
089: int port = parser.getPort();
090:
091: LdapClient client = LdapClient.newInstance(host, port, myEnv);
092:
093: LdapContextImpl context = new LdapContextImpl(client, myEnv,
094: targetDN);
095:
096: return context;
097: }
098:
099: @Override
100: public Object getReferralInfo() {
101: if (index >= referrals.length) {
102: return null;
103: }
104: return referrals[index];
105: }
106:
107: @Override
108: public void retryReferral() {
109: // TODO what should we do?
110: // do nothing
111: }
112:
113: @Override
114: public boolean skipReferral() {
115: index++;
116: return index < referrals.length;
117: }
118:
119: @Override
120: public Context getReferralContext(Hashtable<?, ?> h, Control[] cs)
121: throws NamingException {
122: Hashtable<Object, Object> myEnv = (Hashtable<Object, Object>) h;
123: myEnv.put("java.naming.ldap.control.connect", cs); //$NON-NLS-1$
124: return getReferralContext(myEnv);
125: }
126:
127: }
|