00001 /*------------------------------------------------------------------------------ 00002 Name: SecurityQos.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: The qos for the security (a subelement of connect qos) 00006 ------------------------------------------------------------------------------*/ 00007 #include <authentication/SecurityQosFactory.h> 00008 #include <string> 00009 #include <util/StringTrim.h> 00010 00011 namespace org { namespace xmlBlaster { namespace authentication { 00012 00013 using namespace std; 00014 using namespace org::xmlBlaster::util; 00015 using namespace org::xmlBlaster::util::parser; 00016 00017 SecurityQosFactory::SecurityQosFactory(Global& global) 00018 : XmlHandlerBase(global), ME("SecurityQosFactory-simple"), securityQos_(global) 00019 { 00020 log_.call(ME, "first constructor"); 00021 inSecurityService_ = false; 00022 inUser_ = false; 00023 inPasswd_ = false; 00024 } 00025 00026 SecurityQos SecurityQosFactory::parse(const string& xmlQoS_literal) 00027 { 00028 // Strip CDATA tags that we are able to parse it: 00029 string ret = xmlQoS_literal; 00030 00031 securityQos_ = SecurityQos(global_); 00032 00033 // xmlQoS_literal = StringHelper.replaceAll(xmlQoS_literal, "<![CDATA[", ""); 00034 string::size_type pos = 0; 00035 while (pos != ret.npos) { 00036 pos = ret.find("<![CDATA["); 00037 if (pos == ret.npos) break; 00038 ret = ret.erase(pos, 9); 00039 } 00040 00041 // xmlQoS_literal = StringHelper.replaceAll(xmlQoS_literal, "]]>", ""); 00042 pos = 0; 00043 while (pos != ret.npos) { 00044 pos = ret.find("]]>"); 00045 if (pos == ret.npos) break; 00046 ret = ret.erase(pos, 3); 00047 } 00048 init(ret); 00049 return securityQos_; 00050 } 00051 00058 void SecurityQosFactory::startElement(const string &name, const AttributeMap& attrs) 00059 { 00060 // if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs)); 00061 if (name.compare("securityService") == 0) { 00062 inSecurityService_ = true; 00063 AttributeMap::const_iterator iter = attrs.begin(); 00064 while (iter != attrs.end()) { 00065 string tmpName = (*iter).first; 00066 if ( tmpName.compare("type") == 0) securityQos_.type_ = (*iter).second; 00067 else if (tmpName.compare("version") == 0) { 00068 securityQos_.version_ = (*iter).second; 00069 } 00070 iter++; 00071 } 00072 character_.erase(); 00073 return; 00074 } 00075 00076 if (name.compare("user") == 0) { 00077 inUser_ = true; 00078 character_.erase(); 00079 return; 00080 } 00081 00082 if (name.compare("passwd") == 0) { 00083 inPasswd_ = true; 00084 character_.erase(); 00085 return; 00086 } 00087 } 00088 00094 void SecurityQosFactory::endElement(const string &name) 00095 { 00096 // log_.call(ME, "endElement"); 00097 if (name.compare("user") == 0) { 00098 inUser_ = false; 00099 StringTrim::trim(character_); 00100 securityQos_.setUserId(character_); 00101 character_.erase(); 00102 return; 00103 } 00104 00105 if (name.compare("passwd") == 0) { 00106 inPasswd_ = false; 00107 StringTrim::trim(character_); 00108 securityQos_.setCredential(character_); 00109 character_.erase(); 00110 return; 00111 } 00112 00113 if (name.compare("securityService") == 0) { 00114 inSecurityService_ = false; 00115 character_.erase(); 00116 return; 00117 } 00118 } 00119 00120 }}} // namespaces 00121 00122 00123 #ifdef _XMLBLASTER_CLASSTEST 00124 using namespace std; 00125 using namespace org::xmlBlaster::authentication; 00126 00128 int main(int args, char* argv[]) 00129 { 00130 00131 string xml = 00132 string("<securityService type=\" htpasswd \" version=\"1.0\">\n") + 00133 string(" <![CDATA[\n") + 00134 string(" <passwd>theUsersPwd</passwd>\n") + 00135 string(" <user>aUser</user>\n") + 00136 string(" ]]>\n") + 00137 string("</securityService>"); 00138 00139 Global& glob = Global::getInstance(); 00140 glob.initialize(args, argv); 00141 cout << "Original:\n" << xml << endl; 00142 org::xmlBlaster::authentication::SecurityQosFactory factory(glob); 00143 SecurityQos qos = factory.parse(xml); 00144 cout << "Result:\n" << qos.toXml() << endl; 00145 qos.setUserId("AnotherUser"); 00146 qos.setCredential("AnotherPassword"); 00147 cout << qos.toXml() << endl; 00148 return 0; 00149 } 00150 00151 #endif