All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.novell.ldap.SPMLConnection Maven / Gradle / Ivy

/* **************************************************************************
 *
 * Copyright (C) 2005 Marc Boorshtein All Rights Reserved.
 *
 * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
 * TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
 * TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
 * AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
 * IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
 * OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
 * PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
 * THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
 ******************************************************************************/
package com.novell.ldap;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.openspml.client.LighthouseClient;
import org.openspml.client.SpmlClient;
import org.openspml.message.AddRequest;
import org.openspml.message.AddResponse;
import org.openspml.message.Attribute;
import org.openspml.message.Filter;
import org.openspml.message.FilterTerm;
import org.openspml.message.Identifier;
import org.openspml.message.Modification;
import org.openspml.message.ModifyRequest;
import org.openspml.message.ModifyResponse;
import org.openspml.message.SearchRequest;
import org.openspml.message.SearchResponse;
import org.openspml.message.SearchResult;
import org.openspml.message.SpmlResponse;
import org.openspml.util.SpmlException;

import java.io.*;
import java.lang.reflect.Field;
import java.net.MalformedURLException;

import com.novell.ldap.Connection;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPConstraints;
import com.novell.ldap.LDAPControl;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPExtendedOperation;
import com.novell.ldap.LDAPExtendedResponse;
import com.novell.ldap.LDAPMessage;
import com.novell.ldap.LDAPMessageQueue;
import com.novell.ldap.LDAPModification;
import com.novell.ldap.LDAPResponseQueue;
import com.novell.ldap.LDAPSchema;
import com.novell.ldap.LDAPSearchConstraints;
import com.novell.ldap.LDAPSearchQueue;
import com.novell.ldap.LDAPSearchResults;
import com.novell.ldap.LDAPSocketFactory;
import com.novell.ldap.LDAPUnsolicitedNotificationListener;
import com.novell.ldap.rfc2251.RfcFilter;
import com.novell.ldap.spml.SPMLImpl;
import com.novell.ldap.util.*;

/**
 * @author Marc Boorshtein
 *
 * This class is meant to be a drop-in replacement for an LDAPConnection 
 * when working sith synchronous LDAP calls
 */
public class SPMLConnection extends LDAPConnection {

	/** Default spml implementation */
	public static final String DEF_IMPL = "com.novell.ldap.spml.SunIdm";
	
	/** The Connection To The DSMLv2 Server */
	SpmlClient con;
	
	/** The vendor specific version of the SPML implementation */
	SPMLImpl vendorImpl;
	
	/** The URL of the server */
	String serverString;
	
	/** The User's Name */
	String binddn;
	
	/** The User's Password */
	String pass;
	
	/** Determine if the connection is bound */
	boolean isBound;
	
	/** determine if we are "connected" */
	boolean isConnected;
	
	

	/** The host extracted from the url */
	private String host;
	
	/** Allow for the adjustment of the HTTP Post */
	HttpRequestCallback callback;
	
	
	/**
	 * Short hand for executing a modification operation (add/modify/delete/rename)
	 * @param message The message
	 * @return The response 
	 * @throws LDAPException
	 */
	private LDAPMessage sendMessage(LDAPMessage message) throws LDAPException {
		return null;//return (LDAPMessage) sendMessage(message,false);
	}
	
	/**
	 * Short hand for retrieving results from a query
	 * @param message
	 * @return The search results
	 * @throws LDAPException
	 */
	private DSMLSearchResults execQuery(LDAPMessage message) throws LDAPException {
		return null;//return (DSMLSearchResults) sendMessage(message,true);
	}
	
	
	
	
	
	/**
	 * Default Contructor, initilizes the http client
	 *
	 */
	public SPMLConnection(){
		this.loadImpl(SPMLConnection.DEF_IMPL,null);
		
		
	}
	
	public SPMLConnection(String className){
		this.loadImpl(className,null);
		
		
	}
	
	public SPMLConnection(String className,ClassLoader loader){
		this.loadImpl(className,loader);
		
		
	}
	
	/**
	 * Creates an instace of DsmlConnection ignoring the socket factory
	 * @param factory
	 */
	public SPMLConnection(LDAPSocketFactory factory) {
		this();
	}
	

	private void loadImpl(String className,ClassLoader loader) {
		SPMLImpl impl = null;
		
		if (loader == null) {
			try {
				impl = (SPMLImpl) Class.forName(className).newInstance();
			} catch (Exception e) {
				return;
			}
		} else {
			try {
				impl = (SPMLImpl) loader.loadClass(className).newInstance();
			} catch (Exception e) {
				return;
			}
				
		}
		
		this.vendorImpl = impl;
		this.con = impl.getSpmlClient();
		
	}
	
	
	/**
	 * Sets the host as the serverUrl, port is ignored
	 * @param serverUrl The Server location and context
	 * @param port The port (ignored)
	 */
	public void connect(String serverUrl, int port) throws LDAPException {
		this.serverString = serverUrl;
		host = serverUrl.substring(serverUrl.indexOf("//") + 2,serverUrl.indexOf("/",serverUrl.indexOf("//") + 2));
		try {
			this.con.setUrl(serverUrl);
		} catch (MalformedURLException e) {
			throw new LDAPLocalException(e.toString(), 53, e);
		}
		this.isConnected = true;
		
	}
	
	

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(int, java.lang.String, byte[], com.novell.ldap.LDAPConstraints)
	 */
	public void bind(int arg0, String binddn, byte[] pass, LDAPConstraints arg3)
		throws LDAPException {
		this.bind(binddn,new String(pass));
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(int, java.lang.String, byte[], com.novell.ldap.LDAPResponseQueue, com.novell.ldap.LDAPConstraints)
	 */
	public LDAPResponseQueue bind(
		int arg0,
		String arg1,
		byte[] arg2,
		LDAPResponseQueue arg3,
		LDAPConstraints arg4)
		throws LDAPException {
		
		return null;
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(int, java.lang.String, byte[], com.novell.ldap.LDAPResponseQueue)
	 */
	public LDAPResponseQueue bind(
		int arg0,
		String arg1,
		byte[] arg2,
		LDAPResponseQueue arg3)
		throws LDAPException {
		
		return null;
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(int, java.lang.String, byte[])
	 */
	public void bind(int arg0, String binddn, byte[] pass) throws LDAPException {
		this.bind(binddn,new String(pass));
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(int, java.lang.String, java.lang.String, com.novell.ldap.LDAPConstraints)
	 */
	public void bind(int arg0, String binddn, String pass, LDAPConstraints arg3)
		throws LDAPException {
		this.bind(binddn,new String(pass));
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(int, java.lang.String, java.lang.String)
	 */
	public void bind(int arg0, String binddn, String pass) throws LDAPException {
		this.bind(binddn,new String(pass));
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(java.lang.String, java.lang.String, com.novell.ldap.LDAPConstraints)
	 */
	public void bind(String binddn, String pass, LDAPConstraints arg2)
		throws LDAPException {
		this.bind(binddn,new String(pass));
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(java.lang.String, java.lang.String, java.util.Map, java.lang.Object, com.novell.ldap.LDAPConstraints)
	 */
	public void bind(
		String binddn,
		String pass,
		Map arg2,
		Object arg3,
		LDAPConstraints arg4)
		throws LDAPException {
		this.bind(binddn,new String(pass));
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(java.lang.String, java.lang.String, java.util.Map, java.lang.Object)
	 */
	public void bind(String binddn, String pass, Map arg2, Object arg3)
		throws LDAPException {
		this.bind(binddn,new String(pass));
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(java.lang.String, java.lang.String, java.lang.String[], java.util.Map, java.lang.Object, com.novell.ldap.LDAPConstraints)
	 */
	public void bind(
		String binddn,
		String pass,
		String[] arg2,
		Map arg3,
		Object arg4,
		LDAPConstraints arg5)
		throws LDAPException {
		this.bind(binddn,new String(pass));
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(java.lang.String, java.lang.String, java.lang.String[], java.util.Map, java.lang.Object)
	 */
	public void bind(
		String binddn,
		String arg1,
		String[] arg2,
		Map arg3,
		Object arg4)
		throws LDAPException {
		this.bind(binddn,new String(pass));
	}

	/* (non-Javadoc)
	 * @see com.novell.ldap.LDAPConnection#bind(java.lang.String, java.lang.String)
	 */
	public void bind(String binddn, String password) throws LDAPException {
		if (isBound) {
			
				this.vendorImpl.logout();
			
		}
		//set the credentials globaly
		this.isBound = false;
		
		
		this.vendorImpl.login(binddn,password);
		
		this.isBound = true;
	}

	
	
	public void add(LDAPEntry entry, LDAPConstraints cont)
		throws LDAPException {
		
		AddRequest add = new AddRequest();
		
		DN dn = new DN(entry.getDN());
		
		RDN rdn = (RDN) dn.getRDNs().get(0);
		Identifier id = new Identifier();
		
		try {
			id.setType(this.getIdentifierType(rdn.getType()));
		} catch (IllegalArgumentException e1) {
			throw new LDAPException("Could not determine type",53, e1.toString(), e1);
		} catch (IllegalAccessException e1) {
			throw new LDAPException("Could not determine type",53, e1.toString(), e1);
		}
		
		id.setId(rdn.getValue());
		
		String objectClass = entry.getAttribute("objectClass").getStringValue();
		add.setObjectClass(objectClass);
		
		Iterator it = entry.getAttributeSet().iterator();
		
		HashMap map = new HashMap();
		
		while (it.hasNext()) {
			LDAPAttribute attrib = (LDAPAttribute) it.next();
			if (attrib.getName().toLowerCase().equals("objectclass")) {
				continue;
			}
			
			String[] vals = attrib.getStringValueArray();
			ArrayList list = new ArrayList();
			for (int i=0,m=vals.length;i=0) {
                    //one character hex string
                    binary.append("\\0");
                    binary.append(Integer.toHexString(value[i]));
                } else {
                    //negative (eight character) hex string
                    binary.append("\\"+
                            Integer.toHexString(value[i]).substring(6));
                }
            }
            toReturn = binary.toString();
        }
        return toReturn;
    }
	
	private void stringFilter(Iterator itr, FilterTerm filter) {
        int op=-1;
        //filter.append('(');
        String comp = null;
        byte[] value;
        boolean doAdd = false;
        boolean isFirst = true;
        FilterTerm part;
        while (itr.hasNext()){
            Object filterpart = itr.next();
            if (filterpart instanceof Integer){
                op = ((Integer)filterpart).intValue();
                switch (op){
                    case LDAPSearchRequest.AND:
                        if (filter.getOperation() != null) {
	                        FilterTerm andFilter = new FilterTerm();
	                        andFilter.setOperation(FilterTerm.OP_AND);
	                        filter.addOperand(andFilter);
	                        filter = andFilter;
                        } else {
                        	filter.setOperation(FilterTerm.OP_AND);
                        }
                    	break;
                    case LDAPSearchRequest.OR:
                    	if (filter.getOperation() != null) {
	                    	FilterTerm orFilter = new FilterTerm();
		                    orFilter.setOperation(FilterTerm.OP_OR);
		                    filter.addOperand(orFilter);
		                    filter = orFilter;
                    	} else {
                    		if (filter.getOperation() == null) {
                    			filter.setOperation(FilterTerm.OP_OR);
                    		}
                    	}
                        break;
                    case LDAPSearchRequest.NOT:
                    	if (filter.getOperation() != null) {
	                    	FilterTerm notFilter = new FilterTerm();
	                    	notFilter.setOperation(FilterTerm.OP_NOT);
	                    	filter.addOperand(notFilter);
	                    	filter = notFilter;
                    	} else {
                    		filter.setOperation(FilterTerm.OP_NOT);
                    	}
                        break;
                    case LDAPSearchRequest.EQUALITY_MATCH:{
                    	if (filter.getOperation() == null) {
                    		part = filter;
                    	} else {
                    		part = new FilterTerm();
                    		doAdd = true;
                    	}
                        part.setName((String)itr.next());
                        part.setOperation(FilterTerm.OP_EQUAL);
                        value = (byte[])itr.next();
                        part.setValue(byteString(value));
                        
                        if (doAdd) {
                        	filter.addOperand(part);
                        }
                        
                        break;
                    }
                    case LDAPSearchRequest.GREATER_OR_EQUAL:{
                    	if (filter.getOperation() == null) {
                    		part = filter;
                    	} else {
                    		part = new FilterTerm();
                    		doAdd = true;
                    	}
                        part.setName((String)itr.next());
                        part.setOperation(FilterTerm.OP_GTE);
                        value = (byte[])itr.next();
                        part.setValue(byteString(value));
                        if (doAdd) {
                        	filter.addOperand(part);
                        }
                        break;
                    }
                    case LDAPSearchRequest.LESS_OR_EQUAL:{
                    	if (filter.getOperation() == null) {
                    		part = filter;
                    	} else {
                    		part = new FilterTerm();
                    		doAdd = true;
                    	}
                        part.setName((String)itr.next());
                        part.setOperation(FilterTerm.OP_LTE);
                        value = (byte[])itr.next();
                        part.setValue(byteString(value));
                        if (doAdd) {
                        	filter.addOperand(part);
                        }
                        break;
                    }
                    case LDAPSearchRequest.PRESENT:
                    	if (filter.getOperation() == null) {
                    		part = filter;
                    	} else {
                    		part = new FilterTerm();
                    		doAdd = true;
                    	}
	                    part.setName((String)itr.next());
	                    part.setOperation(FilterTerm.OP_PRESENT);
	                    if (doAdd) {
                        	filter.addOperand(part);
                        }
                        
                        break;
                    case LDAPSearchRequest.APPROX_MATCH:
                    	if (filter.getOperation() == null) {
                    		part = filter;
                    	} else {
                    		part = new FilterTerm();
                    		doAdd = true;
                    	}
	                    part.setName((String)itr.next());
	                    part.setOperation(FilterTerm.OP_APPROX);
	                    value = (byte[])itr.next();
	                    part.setValue(byteString(value));
	                    if (doAdd) {
                        	filter.addOperand(part);
                        }
                        break;
                    case LDAPSearchRequest.EXTENSIBLE_MATCH:
                        String oid = (String)itr.next();

	                    if (filter.getOperation() == null) {
	                		part = filter;
	                	} else {
	                		part = new FilterTerm();
	                		doAdd = true;
	                	}
	                    part.setName((String)itr.next() + ":" + oid);
	                    part.setOperation(FilterTerm.OP_EXTENSIBLE_MATCH);
	                    value = (byte[])itr.next();
	                    part.setValue(byteString(value));
	                    if (doAdd) {
                        	filter.addOperand(part);
                        }
                        
                        
                        break;
                    case LDAPSearchRequest.SUBSTRINGS:{
                    	if (filter.getOperation() == null) {
                    		part = filter;
                    	} else {
                    		part = new FilterTerm();
                    		doAdd = true;
                    	}
	                    part.setName((String)itr.next());
	                    part.setOperation(FilterTerm.OP_SUBSTRINGS);
                        boolean noStarLast = false;
                        while (itr.hasNext()){
                            op = ((Integer)itr.next()).intValue();
                            switch(op){
                                case LDAPSearchRequest.INITIAL:
                                    filter.addSubstring((String)itr.next());
                                    
                                    noStarLast = false;
                                    break;
                                case LDAPSearchRequest.ANY:
                                    
                                   filter.addSubstring((String)itr.next());
                                    
                                    noStarLast = false;
                                    break;
                                case LDAPSearchRequest.FINAL:
                                    
                                    
                                	filter.addSubstring((String)itr.next());
                                    break;
                            }
                            
                            
                            
                            
                        }
                        
                        if (doAdd) {
                        	filter.addOperand(part);
                        }
                        
                        break;
                    }
                }
            } else if (filterpart instanceof Iterator){
                stringFilter((Iterator)filterpart, filter);
            }
            
            
        }
        
        
        
        
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy