ffead.server.doc
Message.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * Message.cpp
18  *
19  * Created on: Sep 21, 2009
20  * Author: sumeet
21  */
22 
23 #include "Message.h"
24 
25 Message::Message()
26 {
27 
28 }
29 
30 Message::Message(string xml)
31 {
32  try
33  {
34  XmlParser parser("Parser");
35  Document doc = parser.getDocument(xml);
36  Element message = doc.getRootElement();
37  if(message.getTagName()!="message")
38  {
39  throw new InvalidMessageException("No message Tag\n");
40  }
41  else if(message.getChildElements().size()!=2)
42  {
43  throw new InvalidMessageException("Every message should have a headers and body tag\n");
44  }
45  Element headers = message.getChildElements().at(0);
46  Element body = message.getChildElements().at(1);
47  if(headers.getTagName()!="headers")
48  {
49  throw new InvalidMessageException("No headers Tag\n");
50  }
51  else if(body.getTagName()!="body")
52  {
53  throw new InvalidMessageException("No body Tag\n");
54  }
55  Element destination = headers.getElementByName("destination");
56  Element encoding = headers.getElementByName("encoding");
57  Element timestamp = headers.getElementByName("timestamp");
58  Element priority = headers.getElementByName("priority");
59  Element type = headers.getElementByName("type");
60  Element userid = headers.getElementByName("userid");
61  if(destination.getTagName()!="destination")
62  {
63  throw new InvalidMessageException("Destination Header is mandatory\n");
64  }
65  else if (destination.getAttributes().size()!=2)
66  {
67  throw new InvalidMessageException("Type and Name Attributes should be speciifes for a Destination\n");
68  }
69  else if (destination.getAttribute("name")=="" || destination.getAttribute("type")=="")
70  {
71  throw new InvalidMessageException("Type and Name Attributes cannot be blank for a Destination\n");
72  }
73  else if(type.getTagName()!="type")
74  {
75  throw new InvalidMessageException("Type Header is mandatory\n");
76  }
77  Destination des;
78  des.setName(destination.getAttribute("name"));
79  des.setType(destination.getAttribute("type"));
80  this->destination = des;
81  this->body = body.getText();
82  this->timestamp = timestamp.getText();
83  this->type = type.getText();
84  this->priority = priority.getText();
85  this->userId = userid.getText();
86  this->encoding = encoding.getText();
87  }
88  catch(Exception *e)
89  {
90  throw e;
91  }
92 }
93 
94 Message::Message(Document doc)
95 {
96  Element message = doc.getRootElement();
97  if(message.getTagName()!="message")
98  {
99  throw new InvalidMessageException("No message Tag\n");
100  }
101  else if(message.getChildElements().size()!=2)
102  {
103  throw new InvalidMessageException("Every message should have a headers and body tag\n");
104  }
105  Element headers = message.getChildElements().at(0);
106  Element body = message.getChildElements().at(1);
107  if(headers.getTagName()!="headers")
108  {
109  throw new InvalidMessageException("No headers Tag\n");
110  }
111  else if(body.getTagName()!="body")
112  {
113  throw new InvalidMessageException("No body Tag\n");
114  }
115  Element destination = headers.getElementByName("destination");
116  Element encoding = headers.getElementByName("encoding");
117  Element timestamp = headers.getElementByName("timestamp");
118  Element priority = headers.getElementByName("priority");
119  Element type = headers.getElementByName("type");
120  Element userid = headers.getElementByName("userid");
121  if(destination.getTagName()!="destination")
122  {
123  throw new InvalidMessageException("Destination Header is mandatory\n");
124  }
125  else if (destination.getAttributes().size()!=2)
126  {
127  throw new InvalidMessageException("Type and Name Attributes should be speciifes for a Destination\n");
128  }
129  else if (destination.getAttribute("name")=="" || destination.getAttribute("type")=="")
130  {
131  throw new InvalidMessageException("Type and Name Attributes cannot be blank for a Destination\n");
132  }
133  else if(type.getTagName()!="type")
134  {
135  throw new InvalidMessageException("Type Header is mandatory\n");
136  }
137  Destination des;
138  des.setName(destination.getAttribute("name"));
139  des.setType(destination.getAttribute("type"));
140  this->destination = des;
141  this->body = body.getText();
142  this->timestamp = timestamp.getText();
143  this->type = type.getText();
144  this->priority = priority.getText();
145  this->userId = userid.getText();
146  this->encoding = encoding.getText();
147 }
148 
149 string Message::toXml()
150 {
151  string xml;
152  xml = "<message>\n<headers>\n<destination ";
153  xml += ("name=\""+this->getDestination().getName());
154  xml += ("\" type=\""+this->getDestination().getType());
155  xml += ("\"></destination>\n");
156  xml += ("<timestamp>\n");
157  xml += (this->getTimestamp());
158  xml += ("</timestamp>\n");
159  xml += ("<priority>\n");
160  xml += (this->getPriority());
161  xml += ("</priority>\n");
162  xml += ("<type>\n");
163  xml += (this->getType());
164  xml += ("</type>\n");
165  xml += ("<userId>\n");
166  xml += (this->getUserId());
167  xml += ("</userId>\n");
168  xml += ("<encoding>\n");
169  xml += (this->getEncoding());
170  xml += ("</encoding>\n");
171  xml += ("</headers>\n");
172  xml += ("<body>\n");
173  xml += (this->getBody());
174  xml += ("</body>\n");
175  xml += ("</message>\n");
176  return xml;
177 }
178 
179 Message::~Message() {
180  // TODO Auto-generated destructor stub
181 }
182 
183 Destination Message::getDestination() const
184 {
185  return this->destination;
186 }
187 
188 void Message::setDestination(Destination destination)
189 {
190  this->destination = destination;
191 }
192 
193 string Message::getTimestamp() const
194 {
195  return this->timestamp;
196 }
197 
198 void Message::setTimestamp(string timestamp)
199 {
200  this->timestamp = timestamp;
201 }
202 
203 string Message::getType() const
204 {
205  return this->type;
206 }
207 
208 void Message::setType(string type)
209 {
210  this->type = type;
211 }
212 
213 string Message::getPriority() const
214 {
215  return this->priority;
216 }
217 
218 void Message::setPriority(string priority)
219 {
220  this->priority = priority;
221 }
222 
223 string Message::getUserId() const
224 {
225  return this->userId;
226 }
227 
228 void Message::setUserId(string userId)
229 {
230  this->userId = userId;
231 }
232 
233 string Message::getBody() const
234 {
235  return this->body;
236 }
237 
238 void Message::setBody(string body)
239 {
240  this->body = body;
241 }
242 
243 string Message::getEncoding() const
244 {
245  return this->encoding;
246 }
247 
248 void Message::setEncoding(string encoding)
249 {
250  this->encoding = encoding;
251 }