1 /*------------------------------------------------------------------------------
2 Name: SessionName.cpp
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment:
6 ------------------------------------------------------------------------------*/
7 #include <util/SessionName.h>
8 #include <stdlib.h>
9 #include <util/lexical_cast.h>
10 #include <util/StringStripper.h>
11 #include <util/StringTrim.h>
12 #include <util/Global.h>
13
14 namespace org { namespace xmlBlaster { namespace util {
15
16 using namespace org::xmlBlaster::util;
17 using namespace org::xmlBlaster::util::parser;
18 using namespace std;
19
20
21 /*---------------------------- SessionName --------------------------------*/
22
23 SessionName::SessionName(Global& global, const string& defaultUserName, long publicSessionId)
24 : ReferenceCounterBase(), ME("SessionName"), global_(global)
25 {
26 initialize("", defaultUserName, publicSessionId);
27 }
28
29 SessionName::SessionName(Global& global, const string& absoluteName)
30 : ReferenceCounterBase(), ME("SessionName"), global_(global)
31 {
32 initialize(absoluteName, "", 0);
33 }
34
35 SessionName::~SessionName()
36 {
37 }
38
39 void SessionName::copy(const SessionName& data)
40 {
41 clusterNodeId_ = data.clusterNodeId_;
42 subjectId_ = data.subjectId_;
43 pubSessionId_ = data.pubSessionId_;
44 useSessionMarker_ = data.useSessionMarker_;
45 }
46
47
48 void SessionName::initialize(const string& absoluteName, const string& defaultUserName, long publicSessionId)
49 {
50 pubSessionId_ = publicSessionId;
51
52 // Change default to true on version 2.0!
53 useSessionMarker_ = global_.getProperty().getBoolProperty("xmlBlaster/useSessionMarker", false);
54
55 if (!absoluteName.empty()) {
56 setAbsoluteName(absoluteName);
57 return;
58 }
59
60 string name = global_.getProperty().getStringProperty("session.name", "");
61 if (!name.empty()) {
62 setAbsoluteName(name);
63 return;
64 }
65
66 clusterNodeId_ = global_.getProperty().getStringProperty("session.clusterNodeId", "");
67
68 if (!defaultUserName.empty()) {
69 subjectId_ = defaultUserName;
70 return;
71 }
72
73 subjectId_ = global_.getProperty().getStringProperty("user.name", "guest");
74 }
75
76
77 SessionName::SessionName(const SessionName& data) : ReferenceCounterBase(), ME(data.ME), global_(data.global_)
78 {
79 copy(data);
80 }
81
82 SessionName& SessionName::operator =(const SessionName& data)
83 {
84 copy(data);
85 return *this;
86 }
87
88
89 void SessionName::setAbsoluteName(const string& name)
90 {
91 pubSessionId_ = 0; // resets the value if previously set
92 string relative = "";
93 if (name.empty())
94 throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "the absolute name is empty");
95
96 if (name[0] == '/') { // then it is an absolute name
97 StringStripper stripper("/");
98 vector<std::string> help = stripper.strip(name);
99 help.erase(help.begin()); // since it is empty for sure.
100 if (help.size() < 4)
101 throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "the absolute name '" + name + "' is not allowed");
102 if (help[0] == "node") clusterNodeId_ = help[1];
103 else throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "the absolute name '" + name + "' is not allowed. It should start with '/node'");
104 if (help[2] != "client")
105 throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "the absolute name '" + name + "' is not allowed. '/client' is missing");
106
107 for (size_t i=3; i < help.size(); i++) {
108 relative += help[i];
109 if ( i < help.size()-1) relative += "/";
110 }
111 }
112 else relative = name;
113
114 StringStripper relStripper("/");
115 vector<std::string> relHelp = relStripper.strip(relative);
116 if (relHelp.empty()) {
117 throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "there is no relative name information: '" + name + "' is not allowed");
118 }
119
120 size_t ii = 0;
121 if ( relHelp.size() > ii ) {
122 string tmp = relHelp[ii++];
123 if ( tmp == "client" ) {
124 if ( relHelp.size() > ii ) {
125 subjectId_ = relHelp[ii++];
126 }
127 else {
128 throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "there is no relative name information: '" + name + "' is not allowed");
129 }
130 }
131 else subjectId_ = tmp;
132 }
133 if ( relHelp.size() > ii ) {
134 string tmp = relHelp[ii++];
135 if ( tmp == "session" ) {
136 if ( relHelp.size() > ii ) {
137 pubSessionId_ = lexical_cast<long>(relHelp[ii++]);
138 }
139 else {
140 throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "there is no relative session number given: '" + name + "' is not allowed");
141 }
142 }
143 else pubSessionId_ = lexical_cast<long>(tmp);
144 }
145 }
146
147 string SessionName::getRelativeName() const
148 {
149 return getRelativeName(false);
150 }
151
152 string SessionName::getRelativeName(bool forceSessionMarker) const
153 {
154 string ret = string("client/") + subjectId_;
155 if (pubSessionId_ != 0) {
156 if (useSessionMarker_ || forceSessionMarker)
157 ret += string("/session/") + lexical_cast<std::string>(pubSessionId_);
158 else
159 ret += string("/") + lexical_cast<std::string>(pubSessionId_);
160 }
161 return ret;
162 }
163
164 string SessionName::getAbsoluteName() const
165 {
166 if (clusterNodeId_.empty()) return getRelativeName();
167 return string("/node/") + clusterNodeId_ + string("/") + getRelativeName();
168 }
169
170 string SessionName::getClusterNodeId() const
171 {
172 return clusterNodeId_;
173 }
174
175 void SessionName::setClusterNodeId(const string& clusterNodeId)
176 {
177 clusterNodeId_ = clusterNodeId;
178 }
179
180 string SessionName::getSubjectId() const
181 {
182 return subjectId_;
183 }
184
185 void SessionName::setSubjectId(const string& subjectId)
186 {
187 subjectId_ = subjectId;
188 }
189
190 long SessionName::getPubSessionId() const
191 {
192 return pubSessionId_;
193 }
194
195 void SessionName::setPubSessionId(const long pubSessionId)
196 {
197 pubSessionId_ = pubSessionId;
198 }
199
200 string SessionName::toXml(const string& extraOffset) const
201 {
202 string offset = Constants::OFFSET + extraOffset;
203 string ret;
204
205 ret += offset + string("<session");
206 ret += string(" name='") + getAbsoluteName() + string("'");
207 ret += string("/>\n");
208 return ret;
209 }
210
211 }}}
syntax highlighted by Code2HTML, v. 0.9.1