1 /*------------------------------------------------------------------------------
2 Name: SessionQos.cpp
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Factory for SessionQosData (for ConnectReturnQos and ConnectQos)
6 ------------------------------------------------------------------------------*/
7
8 #include <util/qos/SessionQos.h>
9 #include <stdlib.h>
10 #include <util/lexical_cast.h>
11 #include <util/StringStripper.h>
12 #include <util/StringTrim.h>
13 #include <util/Global.h>
14
15 namespace org { namespace xmlBlaster { namespace util { namespace qos {
16
17 using namespace org::xmlBlaster::util;
18 using namespace org::xmlBlaster::util::parser;
19 using namespace std;
20
21
22 /*---------------------------- SessionQosData --------------------------------*/
23
24 SessionQosData::SessionQosData(Global& global, const string& defaultUserName, long publicSessionId)
25 : ReferenceCounterBase(), ME("SessionQosData"), sessionName_(0), global_(global)
26 {
27 initialize();
28 SessionName *p = new SessionName(global, defaultUserName, publicSessionId);
29 SessionNameRef r(p);
30 sessionName_ = r;
31 }
32
33 SessionQosData::SessionQosData(Global& global, const string& absoluteName)
34 : ReferenceCounterBase(), ME("SessionQosData"), sessionName_(0), global_(global)
35 {
36 initialize();
37 SessionName *p = new SessionName(global, absoluteName);
38 SessionNameRef r(p);
39 sessionName_ = r;
40 }
41
42 SessionQosData::~SessionQosData()
43 {
44 }
45
46 void SessionQosData::copy(const SessionQosData& data)
47 {
48 SessionName *p = new SessionName(global_, data.sessionName_->getAbsoluteName());
49 SessionNameRef r(p);
50 sessionName_ = r;
51
52 timeout_ = data.timeout_;
53 maxSessions_ = data.maxSessions_;
54 clearSessions_ = data.clearSessions_;
55 reconnectSameClientOnly_ = data.reconnectSameClientOnly_;
56 sessionId_ = data.sessionId_;
57 }
58
59
60 void SessionQosData::initialize()
61 {
62 timeout_ = global_.getProperty().getLongProperty("session.timeout", 86400000);
63 maxSessions_ = global_.getProperty().getIntProperty("session.maxSessions", 10);
64 clearSessions_ = global_.getProperty().getBoolProperty("session.clearSessions", false);
65 reconnectSameClientOnly_ = global_.getProperty().getBoolProperty("session.reconnectSameClientOnly", false);
66 sessionId_ = global_.getProperty().getStringProperty("session.secretSessionId", "");
67 }
68
69
70 SessionQosData::SessionQosData(const SessionQosData& data) :
71 ReferenceCounterBase(), ME(data.ME), sessionName_(0), global_(data.global_)
72 {
73 copy(data);
74 }
75
76 SessionQosData& SessionQosData::operator =(const SessionQosData& data)
77 {
78 copy(data);
79 return *this;
80 }
81
82
83 SessionNameRef SessionQosData::getSessionName()
84 {
85 return sessionName_;
86 }
87
88 void SessionQosData::setAbsoluteName(const string& name)
89 {
90 sessionName_->setAbsoluteName(name);
91 }
92
93 string SessionQosData::getRelativeName() const
94 {
95 return sessionName_->getRelativeName();
96 }
97
98 string SessionQosData::getAbsoluteName() const
99 {
100 return sessionName_->getAbsoluteName();
101 }
102
103 string SessionQosData::getClusterNodeId() const
104 {
105 return sessionName_->getClusterNodeId();
106 }
107
108 void SessionQosData::setClusterNodeId(const string& clusterNodeId)
109 {
110 return sessionName_->setClusterNodeId(clusterNodeId);
111 }
112
113 string SessionQosData::getSubjectId() const
114 {
115 return sessionName_->getSubjectId();
116 }
117
118 void SessionQosData::setSubjectId(const string& subjectId)
119 {
120 return sessionName_->setSubjectId(subjectId);
121 }
122
123 long SessionQosData::getPubSessionId() const
124 {
125 return sessionName_->getPubSessionId();
126 }
127
128 void SessionQosData::setPubSessionId(const long pubSessionId)
129 {
130 return sessionName_->setPubSessionId(pubSessionId);
131 }
132
133 long SessionQosData::getTimeout() const
134 {
135 return timeout_;
136 }
137
138 void SessionQosData::setTimeout(long timeout)
139 {
140 timeout_ = timeout;
141 }
142
143 int SessionQosData::getMaxSessions() const
144 {
145 return maxSessions_;
146 }
147
148 void SessionQosData::setMaxSessions(int maxSessions)
149 {
150 maxSessions_ = maxSessions;
151 }
152
153 bool SessionQosData::getClearSessions() const
154 {
155 return clearSessions_;
156 }
157
158 void SessionQosData::setClearSessions(bool clearSessions)
159 {
160 clearSessions_ = clearSessions;
161 }
162
163 bool SessionQosData::getReconnectSameClientOnly() const
164 {
165 return reconnectSameClientOnly_;
166 }
167
168 void SessionQosData::setReconnectSameClientOnly(bool reconnectSameClientOnly)
169 {
170 reconnectSameClientOnly_ = reconnectSameClientOnly;
171 }
172
173 string SessionQosData::getSecretSessionId() const
174 {
175 return sessionId_;
176 }
177
178 void SessionQosData::setSecretSessionId(const string& sessionId)
179 {
180 sessionId_ = sessionId;
181 }
182
183 string SessionQosData::toXml(const string& extraOffset) const
184 {
185 string offset = Constants::OFFSET + extraOffset;
186 string ret;
187
188 ret += offset + string("<session");
189 ret += string(" name='") + getAbsoluteName() + string("'");
190 ret += string(" timeout='") + lexical_cast<std::string>(getTimeout()) + string("'");
191 ret += string(" maxSessions='") + lexical_cast<std::string>(getMaxSessions()) + string("'");
192 ret += string(" clearSessions='") + Global::getBoolAsString(clearSessions_) + string("'");
193 ret += string(" reconnectSameClientOnly='") + Global::getBoolAsString(reconnectSameClientOnly_) + string("'");
194
195 if (!sessionId_.empty()) {
196 ret += string(" sessionId='") + sessionId_ + string("'");
197 }
198 ret += string("/>\n");
199 return ret;
200 }
201
202
203 /*-------------------------- SessionQosFactory -------------------------------*/
204
205 SessionQosFactory::SessionQosFactory(Global& global)
206 : XmlQoSBase(global), ME("SessionQosFactory")
207 {
208 sessionQos_ = NULL;
209 log_.call(ME, "constructor");
210 }
211
212
213 SessionQosFactory::~SessionQosFactory()
214 {
215 delete sessionQos_;
216 }
217
218 void SessionQosFactory::characters(const string &ch)
219 {
220 string trimmedCh = StringTrim::trim(ch);
221 character_ += trimmedCh;
222 if (log_.trace())
223 log_.trace(ME, string("characters, character:'") + ch + string("'"));
224 }
225
226 void SessionQosFactory::startElement(const string &name, const AttributeMap& attrs) {
227 if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs));
228
229 if (util::XmlQoSBase::startElementBase(name, attrs)) return;
230
231 if (name.compare("session") == 0) {
232 // get all attributes which are needed ...
233 AttributeMap::const_iterator iter = attrs.begin();
234 while (iter != attrs.end()) {
235 string tmpName = (*iter).first;
236 string tmpValue = (*iter).second;
237 if (tmpName.compare("name") == 0) {
238 sessionQos_->setAbsoluteName(tmpValue);
239 }
240 else if (tmpName.compare("timeout") == 0) {
241 sessionQos_->timeout_ = XmlHandlerBase::getLongValue(tmpValue);
242 }
243 else if (tmpName.compare("maxSessions") == 0) {
244 sessionQos_->maxSessions_ = XmlHandlerBase::getIntValue(tmpValue);
245 }
246 else if (tmpName.compare("clearSessions") == 0) {
247 sessionQos_->clearSessions_ = StringTrim::isTrueTrim(tmpValue);
248 }
249 else if (tmpName.compare("reconnectSameClientOnly") == 0) {
250 sessionQos_->reconnectSameClientOnly_ = StringTrim::isTrueTrim(tmpValue);
251 }
252 else if (tmpName.compare("sessionId") == 0) {
253 sessionQos_->sessionId_ = tmpValue;
254 }
255 iter++;
256 }
257 return;
258 }
259 }
260
261 void SessionQosFactory::endElement(const string &name) {
262 log_.trace(ME, "endElement");
263 if (util::XmlQoSBase::endElementBase(name)) return;
264
265 if (name.compare("session") == 0) {
266 return;
267 }
268 }
269
270 void SessionQosFactory::reset()
271 {
272 if (sessionQos_ != NULL) delete sessionQos_;
273 sessionQos_ = NULL;
274 sessionQos_ = new SessionQosData(global_);
275 }
276
277 const SessionQosData& SessionQosFactory::getData() const
278 {
279 return *sessionQos_;
280 }
281
282 SessionQosData SessionQosFactory::readObject(const string& qos)
283 {
284 // this should be synchronized here ....
285 reset();
286 init(qos);
287 return *sessionQos_;
288 }
289
290 /**
291 * Get a usage string for the connection parameters
292 */
293 string SessionQosData::usage()
294 {
295 string text;
296 text += string("Control my login session settings:\n");
297 text += string(" -session.name []\n");
298 text += string(" The name for login, e.g. 'joe' or with public session ID 'joe/2'.\n");
299 text += string(" -session.timeout ["+lexical_cast<std::string>((int)Constants::DAY_IN_MILLIS)+"], defaults to one day.\n");
300 text += string(" How long lasts our login session in milliseconds, 0 is forever.\n");
301 text += " -session.maxSessions [10]\n";
302 text += string(" Maximum number of simultanous logins per client.\n");
303 text += string(" -session.clearSessions [false]\n");
304 text += string(" Kill other sessions running under my login name.\n");
305 text += string(" -session.reconnectSameClientOnly [false]\n");
306 text += string(" Only creator client may reconnect to session.\n");
307 text += string(" -session.secretSessionId []\n");
308 text += string(" The secret sessionId.\n");
309 return text;
310 }
311 }}}} // namespaces
312
313
314 #ifdef _XMLBLASTER_CLASSTEST
315
316 using namespace std;
317 using namespace org::xmlBlaster::util::qos;
318
319 /** For testing: java org.xmlBlaster.authentication.plugins.simple.SecurityQos */
320 int main(int args, char* argv[])
321 {
322 {
323 string qos = "<session name='/node/http:/client/ticheta/-3' timeout='86400000' maxSessions='10' \n" +
324 string(" clearSessions='false' reconnectSameClientOnly='false' sessionId='IIOP:01110728321B0222011028'/>\n");
325
326 Global& glob = Global::getInstance();
327 glob.initialize(args, argv);
328 SessionQosFactory factory(glob);
329 SessionQosData data = factory.readObject(qos);
330
331 string ret = data.toXml();
332 cout << ret << endl;
333
334 cout << data.getPubSessionId() << endl;
335 cout << data.getSubjectId() << endl;
336 cout << data.getClusterNodeId() << endl << endl;
337
338 SessionQosData data2(glob);
339 cout << "second session qos: " << endl;
340 cout << data2.toXml() << endl;
341 cout << data2.getPubSessionId() << endl;
342 cout << data2.getSubjectId() << endl;
343 cout << data2.getClusterNodeId() << endl << endl;
344 }
345 return 0;
346 }
347
348 #endif
syntax highlighted by Code2HTML, v. 0.9.1