1 /*------------------------------------------------------------------------------
  2 Name:      SecurityQos.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   The qos for the security (a subelement of connect qos)
  6 ------------------------------------------------------------------------------*/
  7 #include <authentication/SecurityQosFactory.h>
  8 #include <string>
  9 #include <util/StringTrim.h>
 10 
 11 namespace org { namespace xmlBlaster { namespace authentication {
 12 
 13 using namespace std;
 14 using namespace org::xmlBlaster::util;
 15 using namespace org::xmlBlaster::util::parser;
 16 
 17 SecurityQosFactory::SecurityQosFactory(Global& global)
 18    : XmlHandlerBase(global), ME("SecurityQosFactory-simple"), securityQos_(global)
 19 {
 20    log_.call(ME, "first constructor");
 21    inSecurityService_ = false;
 22    inUser_            = false;
 23    inPasswd_          = false;
 24 }
 25 
 26 SecurityQos SecurityQosFactory::parse(const string& xmlQoS_literal)
 27 {
 28    // Strip CDATA tags that we are able to parse it:
 29    string ret = xmlQoS_literal;
 30 
 31    securityQos_ = SecurityQos(global_);
 32 
 33    // xmlQoS_literal = StringHelper.replaceAll(xmlQoS_literal, "<![CDATA[", "");
 34    string::size_type pos = 0;
 35    while (pos != ret.npos) {
 36       pos = ret.find("<![CDATA[");
 37       if (pos == ret.npos) break;
 38       ret = ret.erase(pos, 9);
 39    }
 40 
 41    // xmlQoS_literal = StringHelper.replaceAll(xmlQoS_literal, "]]>", "");
 42    pos = 0;
 43    while (pos != ret.npos) {
 44       pos = ret.find("]]>");
 45       if (pos == ret.npos) break;
 46       ret = ret.erase(pos, 3);
 47    }
 48    init(ret);
 49    return securityQos_;
 50 }
 51 
 52 /**
 53  * Start element, event from SAX parser.
 54  * <p />
 55  * @param name Tag name
 56  * @param attrs the attributes of the tag
 57  */
 58 void SecurityQosFactory::startElement(const string &name, const AttributeMap& attrs)
 59 {
 60    // if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs));
 61    if (name.compare("securityService") == 0) {
 62       inSecurityService_ = true;
 63       AttributeMap::const_iterator iter = attrs.begin();
 64       while (iter != attrs.end()) {
 65          string tmpName = (*iter).first;
 66          if ( tmpName.compare("type") == 0) securityQos_.type_ = (*iter).second;
 67          else if (tmpName.compare("version") == 0) {
 68             securityQos_.version_ = (*iter).second;
 69          }
 70          iter++;
 71       }
 72       character_.erase();
 73       return;
 74    }
 75 
 76    if (name.compare("user") == 0) {
 77       inUser_ = true;
 78       character_.erase();
 79       return;
 80    }
 81 
 82    if (name.compare("passwd") == 0) {
 83       inPasswd_ = true;
 84       character_.erase();
 85       return;
 86    }
 87 }
 88 
 89 /**
 90  * End element, event from SAX parser.
 91  * <p />
 92  * @param name Tag name
 93  */
 94  void SecurityQosFactory::endElement(const string &name)
 95 {
 96    // log_.call(ME, "endElement");
 97    if (name.compare("user") == 0) {
 98       inUser_ = false;
 99       StringTrim::trim(character_);
100       securityQos_.setUserId(character_);
101       character_.erase();
102       return;
103    }
104 
105    if (name.compare("passwd") == 0) {
106       inPasswd_ = false;
107       StringTrim::trim(character_);
108       securityQos_.setCredential(character_);
109       character_.erase();
110       return;
111    }
112 
113    if (name.compare("securityService") == 0) {
114       inSecurityService_ = false;
115       character_.erase();
116       return;
117    }
118 }
119 
120 }}} // namespaces
121 
122 
123 #ifdef _XMLBLASTER_CLASSTEST
124 using namespace std;
125 using namespace org::xmlBlaster::authentication;
126 
127 /** For testing: java org.xmlBlaster.authentication.plugins.simple.SecurityQosFactory */
128 int main(int args, char* argv[])
129 {
130 
131    string xml =
132       string("<securityService type=\"  htpasswd \" version=\"1.0\">\n") +
133       string("   <![CDATA[\n") +
134       string("   <passwd>theUsersPwd</passwd>\n") +
135       string("   <user>aUser</user>\n") +
136       string("   ]]>\n") +
137       string("</securityService>");
138 
139    Global& glob = Global::getInstance();
140    glob.initialize(args, argv);
141    cout << "Original:\n" << xml << endl;
142    org::xmlBlaster::authentication::SecurityQosFactory factory(glob);
143    SecurityQos qos = factory.parse(xml);
144    cout << "Result:\n" << qos.toXml() << endl;
145    qos.setUserId("AnotherUser");
146    qos.setCredential("AnotherPassword");
147    cout << qos.toXml() << endl;
148    return 0;
149 }
150 
151 #endif


syntax highlighted by Code2HTML, v. 0.9.1