Back to index

openldap  2.4.31
LDAPResult.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 
00008 #include "debug.h"
00009 #include"LDAPResult.h"
00010 #include"LDAPAsynConnection.h"
00011 #include "LDAPRequest.h"
00012 #include "LDAPException.h"
00013 
00014 #include <cstdlib>
00015 
00016 using namespace std;
00017 
00018 LDAPResult::LDAPResult(const LDAPRequest *req, LDAPMessage *msg) : 
00019         LDAPMsg(msg){
00020     if(msg != 0){
00021         DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPResult::LDAPResult()" << endl);
00022         const LDAPAsynConnection *con=req->getConnection();
00023         char **refs=0;
00024         LDAPControl** srvctrls=0;
00025         char* matchedDN=0;
00026         char* errMsg=0;
00027         int err=ldap_parse_result(con->getSessionHandle(),msg,&m_resCode,
00028                 &matchedDN, &errMsg,&refs,&srvctrls,0);
00029         if(err != LDAP_SUCCESS){
00030             ber_memvfree((void**) refs);
00031             ldap_controls_free(srvctrls);
00032             throw LDAPException(err);
00033         }else{
00034             if (refs){
00035                 m_referrals=LDAPUrlList(refs);
00036                 ber_memvfree((void**) refs);
00037             }
00038             if (srvctrls){
00039                 m_srvControls = LDAPControlSet(srvctrls);
00040                 m_hasControls = true;
00041                 ldap_controls_free(srvctrls);
00042             }else{
00043                 m_hasControls = false;
00044             }
00045             if(matchedDN != 0){
00046                 m_matchedDN=string(matchedDN);
00047                 free(matchedDN);
00048             }
00049             if(errMsg != 0){
00050                 m_errMsg=string(errMsg);
00051                 free(errMsg);
00052             }
00053         }
00054     }
00055 }
00056 
00057 LDAPResult::LDAPResult(int type, int resultCode, const std::string &msg) : 
00058         LDAPMsg(type,0), m_resCode(resultCode), m_errMsg(msg)
00059 {}
00060 
00061 
00062 LDAPResult::~LDAPResult(){
00063     DEBUG(LDAP_DEBUG_DESTROY,"LDAPResult::~LDAPResult()" << endl);
00064 }
00065 
00066 int LDAPResult::getResultCode() const{
00067     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getResultCode()" << endl);
00068     return m_resCode;
00069 }
00070 
00071 string LDAPResult::resToString() const{
00072     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::resToString()" << endl);
00073     return string(ldap_err2string(m_resCode));
00074 }
00075 
00076 const string& LDAPResult::getErrMsg() const{
00077     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getErrMsg()" << endl);
00078     return m_errMsg;
00079 }
00080 
00081 const string& LDAPResult::getMatchedDN() const{
00082     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getMatchedDN()" << endl);
00083     return m_matchedDN;
00084 }
00085 
00086 const LDAPUrlList& LDAPResult::getReferralUrls() const{
00087     DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getReferralUrl()" << endl);
00088     return m_referrals;
00089 }
00090 
00091 ostream& operator<<(ostream &s,LDAPResult &l){
00092     return s << "Result: " << l.m_resCode << ": "  
00093         << ldap_err2string(l.m_resCode) << endl 
00094         << "Matched: " << l.m_matchedDN << endl << "ErrMsg: " << l.m_errMsg;
00095 }
00096