1 /*------------------------------------------------------------------------------
  2 Name:      ConnectQosFactory.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Factory for ConnectQosData (for ConnectReturnQos and ConnectQos)
  6 ------------------------------------------------------------------------------*/
  7 
  8 #include <util/qos/ConnectQosFactory.h>
  9 #include <util/Global.h>
 10 // #include <util/XmlBlasterException>
 11 #include <util/lexical_cast.h>
 12 
 13 namespace org { namespace xmlBlaster { namespace util { namespace qos {
 14 
 15 using namespace std;
 16 using namespace org::xmlBlaster::authentication;
 17 using namespace org::xmlBlaster::util;
 18 using namespace org::xmlBlaster::util::parser;
 19 using namespace org::xmlBlaster::util::qos::storage;
 20 
 21 ConnectQosFactory::ConnectQosFactory(Global& global)
 22    : XmlHandlerBase(global),
 23      ME("ConnectQosFactory"),
 24      sessionQosFactory_(global),
 25      securityQosFactory_(global),
 26      queuePropertyFactory_(global),
 27      addressFactory_(global),
 28 //     securityQos_(global),
 29 //     serverRef_(Global::getDefaultProtocol()), //"SOCKET"),
 30      clientProperty_(0),
 31      connectQos_((ConnectQos*)0)
 32 {
 33    log_.call(ME, "constructor");
 34    prep();
 35 }
 36 
 37 void ConnectQosFactory::prep()
 38 {
 39    inSecurityService_ = false;
 40    inServerRef_       = false;
 41    inSession_         = false;
 42    inRefreshSession_  = false;
 43    inInstanceId_      = false;
 44    inReconnected_     = false;
 45    inIsPersistent_    = false;
 46    serverRefType_     = "";
 47    subFactory_        = NULL;
 48    inClientProperty_  = false;
 49    inQos_             = false;
 50 }
 51 
 52 ConnectQosFactory::~ConnectQosFactory()
 53 {
 54    log_.call(ME, "destructor");
 55    if (clientProperty_ != 0) {
 56       delete(clientProperty_);
 57    }
 58    //if (securityQos_ != NULL) delete securityQos_;
 59    //if (serverRef_ != NULL) delete serverRef_;
 60 }
 61 
 62 void ConnectQosFactory::characters(const string &ch)
 63 {
 64    if (subFactory_) {
 65       subFactory_->characters(ch);
 66       return;
 67    }
 68 
 69    if (inSession_) {
 70       sessionQosFactory_.characters(ch);
 71       return;
 72    }
 73 
 74    character_ += StringTrim::trim(ch);
 75    if (log_.trace()) log_.trace(ME, string("characters, character:'") + character_ + string("'"));
 76 }
 77 
 78 void ConnectQosFactory::startElement(const string& name, const AttributeMap& attrs) {
 79    //if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs));
 80 
 81    if (name.compare("qos") == 0) {
 82      inQos_ = true;
 83      connectQos_ = new ConnectQos(global_); // kind of reset
 84      return;
 85    }
 86 
 87    if (name.compare("queue") == 0) {
 88       if (!inQos_) return;
 89       subFactory_ = &queuePropertyFactory_;
 90    }
 91 
 92    if (subFactory_) {
 93       subFactory_->startElement(name, attrs);
 94       return;
 95    }
 96 
 97    if (name.compare("securityService") == 0) {
 98       if (!inQos_) return;
 99       inSecurityService_ = true;
100       character_ = getStartElementAsString(name, attrs);
101       return;
102    }
103 
104    if (name.compare("session") == 0) {
105       if (!inQos_) return;
106       inSession_ = true;
107       sessionQosFactory_.reset();
108    }
109    if (inSession_) {
110       sessionQosFactory_.startElement(name, attrs);
111       return;
112    }
113 
114    if (name.compare("ptp") == 0) {
115       if (!inQos_) return;
116       connectQos_->setPtp(true);
117       character_.erase();
118       return;
119    }
120 
121    if (name.compare("clusterNode") == 0) {
122       if (!inQos_) return;
123       connectQos_->setClusterNode(true);
124       character_.erase();
125       return;
126    }
127 
128    if (name.compare("refreshSession") == 0) {
129       if (!inQos_) return;
130       inRefreshSession_ = true;
131       connectQos_->setRefreshSession(true);
132       character_.erase();
133       return;
134    }
135 
136    if (name.compare("duplicateUpdates") == 0) {
137       if (!inQos_) return;
138       connectQos_->setDuplicateUpdates(true);
139       character_.erase();
140       return;
141    }
142 
143    if (name.compare("reconnected") == 0) {
144       if (!inQos_) return;
145       inReconnected_ = true;
146       connectQos_->setReconnected(true);
147       character_.erase();
148       return;
149    }
150 
151    if (name.compare("instanceId") == 0) {
152       if (!inQos_) return;
153       inInstanceId_ = true;
154       character_.erase();
155       return;
156    }
157 
158    if (name.compare("persistent") == 0) {
159       if (!inQos_) return;
160       inIsPersistent_ = true;
161       connectQos_->setPersistent(true);
162       character_.erase();
163       return;
164    }
165 
166    if (name.compare("serverRef") == 0) {
167       if (!inQos_) return;
168       character_.erase();
169       inServerRef_ = true;
170       AttributeMap::const_iterator iter = attrs.begin();
171       while (iter != attrs.end()) {
172          if (  ((*iter).first).compare("type") == 0) {
173             serverRefType_ = (*iter).second;
174          }
175          iter++;
176       }
177    }
178    
179    if (name.compare("clientProperty") == 0) {
180       if (!inQos_) return;
181       inClientProperty_ = true;
182       character_.erase();
183       string nameAttr;
184       AttributeMap::const_iterator iter = attrs.find("name");
185       if (iter != attrs.end()) nameAttr = (*iter).second;
186       string encoding;
187       iter = attrs.find("encoding");
188       if (iter != attrs.end()) encoding = (*iter).second;
189       string type;
190       iter = attrs.find("type");
191       if (iter != attrs.end()) type = (*iter).second;
192       string charset;
193       iter = attrs.find("charset");
194       if (iter != attrs.end()) charset = (*iter).second;
195       clientProperty_ = new ClientProperty(true, nameAttr, type, encoding, charset);
196    }
197 }
198 
199 void ConnectQosFactory::endElement(const string &name) {
200    //if (log_.call()) log_.call(ME, "endElement");
201    //if (log_.trace()) log_.trace(ME, string("endElement. name:'") + name + string("' character: '") + character_ + string("'"));   
202 
203    if (inQos_ && subFactory_) {
204       subFactory_->endElement(name);
205       if (name.compare("queue") == 0) {
206          // determine wether it is a callback or a client queue ...
207          QueuePropertyBase help = queuePropertyFactory_.getQueueProperty();
208          if (help.getRelating() == Constants::RELATING_CLIENT) {
209             ClientQueueProperty prop = help;
210             connectQos_->addClientQueueProperty(prop);
211          }
212          else {
213              CbQueueProperty prop = help;
214              connectQos_->setSessionCbQueueProperty(prop);
215          }
216          subFactory_ = NULL;
217       }
218       return;
219    }
220 
221    if (name.compare("qos") == 0) {
222      inQos_ = false;
223      character_.erase();
224      return;
225    }
226 
227    if (!inQos_) {
228       return;
229    }
230 
231    if (name.compare("securityService") == 0) {
232       inSecurityService_ = false;
233       character_ += string("\n</securityService>\n");
234 //      securityQos_ = securityQosFactory_.parse(character_);
235       connectQos_->setSecurityQos(securityQosFactory_.parse(character_));
236       character_.erase();
237       return;
238    }
239 
240    if (name.compare("session") == 0) {
241       sessionQosFactory_.endElement(name);
242       inSession_ = false;
243       return;
244    }
245 
246    if (name.compare("ptp") == 0) {
247       connectQos_->setPtp(StringTrim::isTrueTrim(character_));
248       character_.erase();
249       return;
250    }
251 
252    if (name.compare("clusterNode") == 0) {
253       connectQos_->setClusterNode(StringTrim::isTrueTrim(character_));
254       character_.erase();
255       return;
256    }
257 
258    if(name.compare("refreshSession") == 0) {
259       inRefreshSession_ = false;
260       connectQos_->setRefreshSession(StringTrim::isTrueTrim(character_));
261       character_.erase();
262       return;
263    }
264 
265    if (name.compare("duplicateUpdates") == 0) {
266       connectQos_->setDuplicateUpdates(StringTrim::isTrueTrim(character_));
267       character_.erase();
268       return;
269    }
270 
271    if(name.compare("instanceId") == 0) {
272       inInstanceId_ = false;
273       StringTrim::trim(character_);
274       connectQos_->setInstanceId(character_);
275       character_.erase();
276       return;
277    }
278 
279    if(name.compare("reconnected") == 0) {
280       inReconnected_ = false;
281       connectQos_->setReconnected(StringTrim::isTrueTrim(character_));
282       character_.erase();
283       return;
284    }
285 
286    if(name.compare("persistent") == 0) {
287       inIsPersistent_ = false;
288       connectQos_->setPersistent(StringTrim::isTrueTrim(character_));
289       character_.erase();
290       return;
291    }
292 
293    if (inSession_) {
294       sessionQosFactory_.endElement(name);
295       return;
296    }
297 
298    if (name.compare("serverRef") == 0) {
299       inServerRef_ = false;
300       string address = character_;
301 //      serverRef_ = ServerRef(serverRefType_, address);
302       connectQos_->addServerRef(ServerRef(serverRefType_, address));
303       character_.erase();
304    }
305 
306    if (name.compare("clientProperty") == 0) {
307       inClientProperty_ = false;
308       clientProperty_->setValueRaw(character_);
309       connectQos_->addClientProperty(*clientProperty_);
310       delete clientProperty_;
311       clientProperty_ = 0;
312       character_.erase();
313    }
314 
315    return;
316 }
317 
318 
319 ConnectQosDataRef ConnectQosFactory::readObject(const string& qos)
320 {
321    // this should be synchronized here ....
322 //   userId_ = "";
323    prep();
324    delete clientProperty_;
325    clientProperty_ = 0;
326    init(qos);
327 //   ConnectQosData data(global_);
328 //   connectQos_->setSecurityQos(securityQos_);
329 //   connectQos_->addServerRef(serverRef_);
330    connectQos_->setSessionQos(sessionQosFactory_.getData());
331 //   connectQos_->setPtp(isPtp_);
332 
333    return connectQos_;
334 }
335 
336 }}}} // namespaces
337 
338 
339 #ifdef _XMLBLASTER_CLASSTEST
340 
341 using namespace std;
342 using namespace org::xmlBlaster::util::qos;
343 
344 /** For testing: java org.xmlBlaster.authentication.plugins.simple.SecurityQos */
345 int main(int args, char* argv[])
346 {
347    string qos =
348    string("<qos>\n") +
349    string("   <securityService type='htpasswd' version='1.0'>\n") +
350    string("     <![CDATA[\n") +
351    string("     <user>joe</user>\n") +
352    string("     <passwd>secret</passwd>\n") +
353    string("     ]]>\n") +
354    string("   </securityService>\n") +
355    string("   <session name='/node/heron/client/joe/-9' timeout='3600000' maxSessions='10' clearSessions='false' sessionId='4e56890ghdFzj0'/>\n") +
356    string("   <ptp>true</ptp>\n") +
357    string("   <!-- The client side queue: -->\n") +
358    string("   <queue relating='client' type='CACHE' version='1.0' maxEntries='1000' maxBytes='4000' onOverflow='exception'>\n") +
359    string("      <address type='IOR' sessionId='4e56890ghdFzj0'>\n") +
360    string("         IOR:10000010033200000099000010....\n") +
361    string("      </address>\n") +
362    string("   </queue>\n") +
363    string("   <!-- The server side callback queue: -->\n") +
364    string("   <queue relating='callback' type='CACHE' version='1.0' maxEntries='1000' maxBytes='4000' onOverflow='deadMessage'>\n") +
365    string("      <callback type='IOR' sessionId='4e56890ghdFzj0'>\n") +
366    string("         IOR:10000010033200000099000010....\n") +
367    string("         <burstMode collectTime='400' />\n") +
368    string("         <attribute name='aKey'>aValue</attribute>\n") +
369    string("      </callback>\n") +
370    string("   </queue>\n") +
371    string("   <serverRef type='IOR'>IOR:100000100332...</serverRef>\n") +
372    string("</qos>\n");
373 
374    Global& glob = Global::getInstance();
375    glob.initialize(args, argv);
376    ConnectQosFactory factory(glob);
377    ConnectQosDataRef data = factory.readObject(qos);
378    cout << "sessionId    : " << data->getSecretSessionId() << endl;
379    cout << "userId       : " << data->getUserId() << endl;
380    cout << " type: " << data->getCallbackType() << endl;
381    cout << "is ptp       : " << data->getBoolAsString(data->getPtp()) << endl;
382    cout << "securityQos  : " << data->getSecurityQos().toXml() << endl;
383    cout << "sessionQos   : " << data->getSessionQos().toXml() << endl;
384 
385    ServerRef ref = data->getServerRef();
386    cout << "server reference:  " << ref.toXml() << endl;
387 
388    return 0;
389 }
390 
391 #endif


syntax highlighted by Code2HTML, v. 0.9.1