]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPResult.cpp
initial support for SASL
[openldap] / contrib / ldapc++ / src / LDAPResult.cpp
1 /*
2  * Copyright 2000-2007, OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6
7 #include "debug.h"
8 #include"LDAPResult.h"
9 #include"LDAPAsynConnection.h"
10 #include "LDAPRequest.h"
11 #include "LDAPException.h"
12
13 #include <cstdlib>
14
15 using namespace std;
16
17 LDAPResult::LDAPResult(const LDAPRequest *req, LDAPMessage *msg) : 
18         LDAPMsg(msg){
19     if(msg != 0){
20         DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPResult::LDAPResult()" << endl);
21         const LDAPAsynConnection *con=req->getConnection();
22         char **refs=0;
23         LDAPControl** srvctrls=0;
24         char* matchedDN=0;
25         char* errMsg=0;
26         int err=ldap_parse_result(con->getSessionHandle(),msg,&m_resCode,
27                 &matchedDN, &errMsg,&refs,&srvctrls,0);
28         if(err != LDAP_SUCCESS){
29             ber_memvfree((void**) refs);
30             ldap_controls_free(srvctrls);
31             throw LDAPException(err);
32         }else{
33             if (refs){
34                 m_referrals=LDAPUrlList(refs);
35                 ber_memvfree((void**) refs);
36             }
37             if (srvctrls){
38                 m_srvControls = LDAPControlSet(srvctrls);
39                 m_hasControls = true;
40                 ldap_controls_free(srvctrls);
41             }else{
42                 m_hasControls = false;
43             }
44             if(matchedDN != 0){
45                 m_matchedDN=string(matchedDN);
46                 free(matchedDN);
47             }
48             if(errMsg != 0){
49                 m_errMsg=string(errMsg);
50                 free(errMsg);
51             }
52         }
53     }
54 }
55
56 LDAPResult::LDAPResult(int type, int resultCode, const std::string &msg) : 
57         LDAPMsg(type,0), m_resCode(resultCode), m_errMsg(msg)
58 {}
59
60
61 LDAPResult::~LDAPResult(){
62     DEBUG(LDAP_DEBUG_DESTROY,"LDAPResult::~LDAPResult()" << endl);
63 }
64
65 int LDAPResult::getResultCode() const{
66     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getResultCode()" << endl);
67     return m_resCode;
68 }
69
70 string LDAPResult::resToString() const{
71     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::resToString()" << endl);
72     return string(ldap_err2string(m_resCode));
73 }
74
75 const string& LDAPResult::getErrMsg() const{
76     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getErrMsg()" << endl);
77     return m_errMsg;
78 }
79
80 const string& LDAPResult::getMatchedDN() const{
81     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getMatchedDN()" << endl);
82     return m_matchedDN;
83 }
84
85 const LDAPUrlList& LDAPResult::getReferralUrls() const{
86     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getReferralUrl()" << endl);
87     return m_referrals;
88 }
89
90 ostream& operator<<(ostream &s,LDAPResult &l){
91     return s << "Result: " << l.m_resCode << ": "  
92         << ldap_err2string(l.m_resCode) << endl 
93         << "Matched: " << l.m_matchedDN << endl << "ErrMsg: " << l.m_errMsg;
94 }
95