View Javadoc

1   package net.sf.provisioner.requests;
2   
3   import java.util.Map;
4   
5   import javax.naming.NamingEnumeration;
6   import javax.naming.NamingException;
7   import javax.naming.directory.Attribute;
8   import javax.naming.directory.Attributes;
9   import javax.naming.directory.DirContext;
10  import javax.naming.directory.SearchControls;
11  import javax.naming.directory.SearchResult;
12  
13  import net.sf.provisioner.config.ConfigRequest;
14  import net.sf.provisioner.config.LDAPNetworkElement;
15  import net.sf.provisioner.responses.Response;
16  
17  import org.jdom.Document;
18  
19  
20  /**
21   * @author g_pearson
22   *
23   */
24  public class LDAPSearchRequest extends LDAPRequest {
25  
26  	private int    searchScope = -1;
27  	private String filter      = null;
28  	
29  	
30  	public LDAPSearchRequest(ConfigRequest request, Document opParameters) {
31  		super(request,opParameters);
32  	}
33  
34  	protected void storeParameter(Map<String, Object> paramStore, String name, String value) {
35  		if (name.equalsIgnoreCase("searchScope")) {
36  			if (value.equalsIgnoreCase("object")) {
37  				searchScope = SearchControls.OBJECT_SCOPE;
38  			} else if (value.equalsIgnoreCase("onelevel")) {
39  				searchScope = SearchControls.ONELEVEL_SCOPE;
40  			} else if (value.equalsIgnoreCase("subtree")) {
41  				searchScope = SearchControls.SUBTREE_SCOPE;
42  			} else {
43  				throw new IllegalArgumentException("Invalid value for searchScope: '" +value + "'");
44  			}
45  		} else if (name.equalsIgnoreCase("filter")) {
46  			filter = value;
47  		} else {
48  			super.storeParameter(paramStore, name, value);
49  		}
50  	}
51  	
52  	@Override
53  	public Response sendRequest() throws Exception {
54  		DirContext context = ((LDAPNetworkElement) this.ne).findRootContext();
55  		
56  		try {
57  			if (logger.isTraceEnabled()) {
58  				logger.trace(
59  						String.format(
60  								"sending LDAP search request for dn: '%s', filter: '%s', searchScope: %d",
61  								getDistinguishedName(), filter, searchScope
62  						)
63  				);
64  			}
65  			SearchControls controls = new SearchControls();
66  	        controls.setSearchScope(searchScope);
67  	        NamingEnumeration results = context.search(
68  	        		getDistinguishedName(), 
69  	        		filter, 
70  	        		controls
71  	        );
72  
73  	        StringBuilder buffer = new StringBuilder();
74  	        while (results.hasMore()) {
75  	           SearchResult searchResult = (SearchResult) results.next();
76  	           Attributes attributes = searchResult.getAttributes();
77  	           NamingEnumeration ne2 = attributes.getAll();
78  	           while (ne2.hasMore()) {
79  	        	   Attribute attr = (Attribute) ne2.next();
80  	        	   buffer.append(attr.getID() + "=" + attributes.get(attr.getID()));
81  	           }
82  	           buffer.append("\n");
83  	        }
84  	        
85  			Response response    = new Response();
86  			response.result      = 
87  			response.errorStr    = "Deleted attribute " + getDistinguishedName();
88  			response.retry       = false;
89  			response.successfull = true;
90  			return response;
91  
92  		} catch (NamingException e) {
93  			Response response = new Response();
94  			response.result = "Failed to delete attribute.";
95  			
96  			Throwable t = e.getRootCause();
97  			if (t != null) {
98  				// Save the LDAP server's response, if it's there...
99  				response.errorStr = t.getMessage();
100 			} else {
101 				// ... or save Java's error message.
102 				response.errorStr = e.getExplanation();
103 			}
104 			// TODO: find out which errors should result in a retry. 
105 			response.retry       = false;
106 			response.successfull = false;
107 			return response;
108 		} finally {
109 			context.close();
110 		}
111 	}
112 }