Remove warnings
[anna.git] / source / ldap / Search.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <time.h>
10 #include <ldap.h>
11
12 #include <anna/core/functions.hpp>
13 #include <anna/core/tracing/Logger.hpp>
14
15 #include <anna/xml/Node.hpp>
16
17 #include <anna/ldap/Search.hpp>
18 #include <anna/ldap/Session.hpp>
19
20 using namespace std;
21 using namespace anna;
22 using namespace anna::ldap;
23
24 Search::attribute_pool Search::st_attributes;
25
26 void Search::clear()
27 throw() {
28   a_base.clear();
29   a_scope = Scope::Base;
30   clearFilter();
31   a_onlyType = false,
32   clearSizeLimit();
33   clearAttributes();
34 }
35
36 void Search::clearAttributes()
37 throw() {
38   for(attribute_iterator ii = attribute_begin(), maxii = attribute_end(); ii != maxii; ii ++)
39     st_attributes.release(&attribute(ii));
40
41   a_attributes.clear();
42 }
43
44 IdMessage Search::send(Session& session) const
45 throw() {
46   IdMessage result(-1);
47   LDAP* ldap = (LDAP*) session.getLDAP();
48   int scope = -1;
49
50   switch(a_scope) {
51   case Scope::Base: scope = LDAP_SCOPE_BASE; break;
52   case Scope::OneLevel: scope = LDAP_SCOPE_ONELEVEL; break;
53   case Scope::SubTree: scope = LDAP_SCOPE_SUBTREE; break;
54   case Scope::None: return result; break;
55   }
56
57   int maxi = attribute_size() + 1;
58   char** attributes = NULL;
59
60   if(maxi > 1) {
61     attributes = new char* [maxi];
62     int i = 0;
63
64     for(const_attribute_iterator ii = attribute_begin(), maxii = attribute_end(); ii != maxii; ii ++)
65       attributes [i ++] = const_cast <char*>(attribute(ii).c_str());
66
67     attributes [i] = NULL;
68   }
69
70   result = ldap_search(ldap, a_base.c_str(), scope, asCString(a_filter), attributes, a_onlyType);
71   LOGDEBUG(
72     string msg("ldap::Search::send | Scope: ");
73     msg += functions::asString(scope);
74     msg += " | ";
75     msg += asString();
76     msg += " | Attributes: ";
77
78   if(attributes != NULL) {
79   for(int ii = 0; attributes [ii] != NULL; ii ++) {
80       msg += attributes [ii];
81       msg += ' ';
82     }
83   } else
84     msg += "<null>";
85     Logger::debug(msg, ANNA_FILE_LOCATION);
86     );
87
88   delete [] attributes;
89   return result;
90 }
91
92 string Search::asString() const
93 throw() {
94   string result("ldap::Search { ");
95   result += Request::asString();
96   result += " | Base: ";
97   result += a_base;
98   result += " | Scope: ";
99   result += Scope::asText(a_scope);
100   result += " | Filter: ";
101   result += asText(a_filter);
102   result += functions::asText(" | OnlyType: ", a_onlyType);
103   result += functions::asText(" | SizeLimit: ", a_sizeLimit);
104   return result += " }";
105 }
106
107 xml::Node* Search::asXML(xml::Node* parent) const
108 throw() {
109   parent = Request::asXML(parent);
110   parent->createAttribute("Base", a_base);
111   parent->createAttribute("Scope", Scope::asText(a_scope));
112   parent->createAttribute("Filter", asText(a_filter));
113   parent->createAttribute("OnlyType", functions::asString(a_onlyType));
114   parent->createAttribute("SizeLimit", a_sizeLimit);
115   return parent;
116 }
117