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:
019: package org.apache.jmeter.protocol.ldap.sampler;
020:
021: import java.util.Hashtable;
022:
023: import javax.naming.Context;
024: import javax.naming.NamingException;
025: import javax.naming.NamingEnumeration;
026: import javax.naming.directory.Attributes;
027: import javax.naming.directory.DirContext;
028: import javax.naming.directory.InitialDirContext;
029: import javax.naming.directory.ModificationItem;
030: import javax.naming.directory.SearchControls;
031:
032: import org.apache.jorphan.logging.LoggingManager;
033: import org.apache.log.Logger;
034:
035: /*******************************************************************************
036: *
037: * author Dolf Smits(Dolf.Smits@Siemens.com) created Aug 09 2003 11:00 AM
038: * company Siemens Netherlands N.V..
039: *
040: * Based on the work of: author T.Elanjchezhiyan(chezhiyan@siptech.co.in)
041: * created Apr 29 2003 11:00 AM company Sip Technologies and Exports Ltd.
042: *
043: ******************************************************************************/
044:
045: /*******************************************************************************
046: * Ldap Client class is main class to create ,modify, search and delete all the
047: * LDAP functionality available
048: ******************************************************************************/
049: public class LdapExtClient {
050: private static final Logger log = LoggingManager
051: .getLoggerForClass();
052:
053: private static final String CONTEXT_IS_NULL = "Context is null"; //TODO should perhaps be a resource
054:
055: /**
056: * Constructor for the LdapClient object
057: */
058: public LdapExtClient() {
059: }
060:
061: /**
062: * connect to server
063: *
064: * @param host
065: * Description of Parameter
066: * @param username
067: * Description of Parameter
068: * @param password
069: * Description of Parameter
070: * @exception NamingException
071: * Description of Exception
072: */
073: public DirContext connect(String host, String port, String rootdn,
074: String username, String password, String connTimeOut,
075: boolean secure) throws NamingException {
076: DirContext dirContext;
077: Hashtable env = new Hashtable();
078: env.put(Context.INITIAL_CONTEXT_FACTORY,
079: "com.sun.jndi.ldap.LdapCtxFactory"); // $NON-NLS-1$
080: StringBuffer sb = new StringBuffer(80);
081: if (secure) {
082: sb.append("ldaps://"); // $NON-NLS-1$
083: } else {
084: sb.append("ldap://"); // $NON-NLS-1$
085: }
086: sb.append(host);
087: if (port.length() > 0) {
088: sb.append(":"); // $NON-NLS-1$
089: sb.append(port);
090: }
091: sb.append("/"); // $NON-NLS-1$
092: sb.append(rootdn);
093: env.put(Context.PROVIDER_URL, sb.toString());
094: log.info("prov_url= " + env.get(Context.PROVIDER_URL)); // $NON-NLS-1$
095: if (connTimeOut.length() > 0) {
096: env.put("com.sun.jndi.ldap.connect.timeout", connTimeOut); // $NON-NLS-1$
097: }
098: env.put(Context.REFERRAL, "throw"); // $NON-NLS-1$
099: env.put("java.naming.batchsize", "0"); // $NON-NLS-1$ // $NON-NLS-2$
100: env.put(Context.SECURITY_CREDENTIALS, password);
101: env.put(Context.SECURITY_PRINCIPAL, username);
102: dirContext = new InitialDirContext(env);
103: return dirContext;
104: }
105:
106: /**
107: * disconnect from the server
108: */
109: public void disconnect(DirContext dirContext) {
110: if (dirContext == null) {
111: log.info("Cannot disconnect null context");
112: return;
113: }
114:
115: try {
116: dirContext.close();
117: } catch (NamingException e) {
118: log.warn("Ldap client disconnect - ", e);
119: }
120: }
121:
122: /***************************************************************************
123: * Filter the data in the ldap directory for the given search base
124: *
125: * @param searchBase
126: * base where the search should start
127: * @param searchFilter
128: * filter filter this value from the base
129: **************************************************************************/
130: public NamingEnumeration searchTest(DirContext dirContext,
131: String searchBase, String searchFilter, int scope,
132: long countlim, int timelim, String[] attrs, boolean retobj,
133: boolean deref) throws NamingException {
134: if (dirContext == null) {
135: throw new NamingException(CONTEXT_IS_NULL);
136: }
137: SearchControls searchcontrols = null;
138: searchcontrols = new SearchControls(scope, countlim, timelim,
139: attrs, retobj, deref);
140: log.debug("scope, countlim, timelim, attrs, retobj, deref= "
141: + searchFilter + scope + countlim + timelim + attrs
142: + retobj + deref);
143: return dirContext.search(searchBase, searchFilter,
144: searchcontrols);
145: }
146:
147: /***************************************************************************
148: * Filter the data in the ldap directory
149: *
150: * @param filter
151: * filter this value from the base
152: **************************************************************************/
153: public NamingEnumeration compare(DirContext dirContext,
154: String filter, String entrydn) throws NamingException {
155: if (dirContext == null) {
156: throw new NamingException(CONTEXT_IS_NULL);
157: }
158: SearchControls searchcontrols = new SearchControls(0, 1, 0,
159: new String[0], false, false);
160: return dirContext.search(entrydn, filter, searchcontrols);
161: }
162:
163: /***************************************************************************
164: * ModDN the data in the ldap directory for the given search base
165: *
166: **************************************************************************/
167: public void moddnOp(DirContext dirContext, String ddn, String newdn)
168: throws NamingException {
169: log.debug("ddn and newDn= " + ddn + "@@@@" + newdn);
170: if (dirContext == null) {
171: throw new NamingException(CONTEXT_IS_NULL);
172: }
173: dirContext.rename(ddn, newdn);
174: }
175:
176: /***************************************************************************
177: * Modify the attribute in the ldap directory for the given string
178: *
179: * @param mods
180: * add all the entry in to the ModificationItem
181: * @param string
182: * The string (dn) value
183: **************************************************************************/
184: public void modifyTest(DirContext dirContext,
185: ModificationItem[] mods, String string)
186: throws NamingException {
187: if (dirContext == null) {
188: throw new NamingException(CONTEXT_IS_NULL);
189: }
190: dirContext.modifyAttributes(string, mods);
191:
192: }
193:
194: /***************************************************************************
195: * Create the entry in the ldap directory for the given string
196: *
197: * @param attributes
198: * add all the attributes and values from the attributes object
199: * @param string
200: * The string (dn) value
201: **************************************************************************/
202: public DirContext createTest(DirContext dirContext,
203: Attributes attributes, String string)
204: throws NamingException {
205: if (dirContext == null) {
206: throw new NamingException(CONTEXT_IS_NULL);
207: }
208: return dirContext.createSubcontext(string, attributes);
209: }
210:
211: /***************************************************************************
212: * Delete the attribute from the ldap directory
213: *
214: * @param string
215: * The string (dn) value
216: **************************************************************************/
217: public void deleteTest(DirContext dirContext, String string)
218: throws NamingException {
219: if (dirContext == null) {
220: throw new NamingException(CONTEXT_IS_NULL);
221: }
222: dirContext.destroySubcontext(string);
223: }
224: }
|