14 package cz.vutbr.fit.knot.annotations.comet;
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
35 import org.xml.sax.InputSource;
36 import org.xml.sax.SAXException;
66 String msg =
"Bad XML from client: " + XMLString;
67 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
72 Element docEl = doc.getDocumentElement();
73 requestInfo.setRequestString(XMLString);
74 requestInfo.setRequestDocument(doc);
78 requestInfo.setFlier(flier);
81 NodeList sessNodes = docEl.getElementsByTagName(
"session");
82 ArrayList<Element> sessElements =
new ArrayList<Element>();
83 for (
int i = 0; i < sessNodes.getLength(); i++) {
84 Element sessEl = (Element) sessNodes.item(i);
85 sessElements.add(sessEl);
88 String idString = docEl.getAttribute(
"sessionID");
90 if ((sessElements.isEmpty() && idString.isEmpty())
91 || (sessElements.isEmpty() && idString.equals(
"undefined")) ) {
93 NodeList connNodes = docEl.getElementsByTagName(
"connect");
94 Element connEl = (Element) connNodes.item(0);
97 String msg =
"Unknown session (not sent).";
98 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
104 String protVersion = connEl.getAttribute(
"protocolVersion");
105 if (protVersion.length() == 0) {
107 String msg =
"Bad protocol version.";
108 Logger.getLogger(MessageProcessor.class.getName()).log(Level.SEVERE, msg);
113 String attachCometToStr = connEl.getAttribute(
"attachCometTo");
115 if (attachCometToStr != null && !attachCometToStr.isEmpty()) {
117 Long attachCometTo = Long.decode(attachCometToStr);
118 if (attachCometTo != null) {
119 attachCometSession = AppBean.getSession(attachCometTo);
121 }
catch (NumberFormatException ex) {
123 String msg =
"Can not attach comet to another session.";
124 Logger.getLogger(MessageProcessor.class.getName()).log(Level.SEVERE, msg);
131 requestInfo.setSession(session);
132 AppBean.addSession(session);
133 flier.setCreatedInSessId(session.getSessionId());
134 flier.setCreatedInSession(session);
136 if(!protVersion.startsWith(
"1.") && !protVersion.startsWith(
"2.") && !protVersion.equals(
"1") && !protVersion.equals(
"2")){
138 String msg =
"Unsupported protocol version. Client is trying to connect with version = " + protVersion;
139 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
145 requestInfo.appendToReply(
"<connected protocolVersion=\"2.0\" sessionID=\""
146 + session.getSessionId() +
"\"/>");
148 requestInfo.appendToReply(
"<connected protocolVersion=\"1.1\" sessionID=\""
149 + session.getSessionId() +
"\"/>");
151 requestInfo.appendToReply(
"<connected protocolVersion=\"1.0\" sessionID=\""
152 + session.getSessionId() +
"\"/>");
155 if (attachCometSession != null) {
156 session.setCometContext(attachCometSession.getCometContext());
157 AnnotHandler handler = attachCometSession.getCometHandler();
158 if (handler != null) {
159 session.setCometHandler(handler);
160 handler.addSessionInSessionList(session);
165 requestInfo.setConnectWPV(protVersion);
166 }
else if(!sessElements.isEmpty() && sessElements.size() < 2) {
167 Element sessEl = sessElements.get(0);
169 String sessIdStr = sessEl.getAttribute(
"id");
170 if (sessIdStr.isEmpty()) {
172 String msg =
"Unknown session (id not sent).";
173 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
182 sessId = Long.decode(sessIdStr);
183 }
catch(Exception ex){
185 String msg =
"Can't decode session ID.";
186 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
194 session = AppBean.getSession(sessId);
195 if (session == null) {
199 String msg =
"Unknown session (not found).";
200 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
205 requestInfo.setSession(session);
206 flier.setCreatedInSessId(sessId);
207 flier.setCreatedInSession(session);
210 if (sessEl.getNextSibling() == null && sessEl.getPreviousSibling() == null) {
212 requestInfo.setSessionOnly(
true);
213 requestInfo.setCanNotify(
false);
218 Node auxNode = sessEl;
219 auxNode = auxNode.getNextSibling();
220 if (auxNode != null) {
221 while (auxNode.getNextSibling() != null && auxNode.getNodeType() != Node.ELEMENT_NODE) {
222 auxNode = auxNode.getNextSibling();
225 if (auxNode == null || auxNode.getNodeType() != Node.ELEMENT_NODE) {
227 auxNode = auxNode.getPreviousSibling();
228 if (auxNode != null) {
229 while (auxNode.getPreviousSibling() != null && auxNode.getNodeType() != Node.ELEMENT_NODE) {
230 auxNode = auxNode.getPreviousSibling();
233 if (auxNode == null || auxNode.getNodeType() != Node.ELEMENT_NODE) {
235 requestInfo.setSessionOnly(
true);
236 requestInfo.setCanNotify(
false);
240 }
else if (!sessElements.isEmpty()) {
243 NodeList nl = docEl.getChildNodes();
244 for(
int i = 0; i < nl.getLength(); i++){
247 if(n.getNodeType() == Node.TEXT_NODE && n.getNodeValue().trim().length() == 0){
251 if((!n.getNodeName().equals(
"session")) && (!n.getNodeName().equals(
"comet"))){
253 String msg =
"Illegal elements in request.";
254 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
262 ArrayList<EditorSession> sessAL =
new ArrayList<EditorSession>();
263 Iterator<Element> sessElIt = sessElements.iterator();
266 while(sessElIt.hasNext()){
267 Element e = sessElIt.next();
269 String sessIdStr = e.getAttribute(
"id");
271 if (sessIdStr.isEmpty()) {
273 String msg =
"Unknown session (id not sent).";
274 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
282 sessId = Long.decode(sessIdStr);
283 }
catch (NumberFormatException numberFormatException) {
286 String msg =
"Bad session id: ";
287 if (sessIdStr != null) {
290 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
296 tmpSess = AppBean.getSession(sessId);
298 if (tmpSess == null) {
302 String msg =
"Unknown session (not found).";
303 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
311 requestInfo.setCometSessionList(sessAL);
312 session = sessAL.get(0);
314 requestInfo.setSession(session);
315 requestInfo.setSessionOnly(
true);
316 requestInfo.setCanNotify(
false);
320 if (idString.isEmpty()) {
322 String msg =
"Unknown session (id not sent).";
323 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
331 sessId = Long.decode(idString);
332 }
catch (NumberFormatException numberFormatException) {
337 if (sessId != null) {
338 session = AppBean.getSession(sessId);
340 if (session == null) {
344 String msg =
"Unknown session (not found).";
345 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg);
350 requestInfo.setSession(session);
351 flier.setCreatedInSessId(sessId);
352 flier.setCreatedInSession(session);
356 switch(session.getProtocolLOD()){
357 case Constants.PROTOCOL_LOD_V2 :{
360 proc.processXMLDocument(docEl,requestInfo);
363 case Constants.PROTOCOL_LOD_V1_1 :
364 case Constants.PROTOCOL_LOD_V1 :{
367 return proc.processXML(docEl, requestInfo);
380 private static Document
parseXml(String XMLString) {
382 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
383 dbf.setNamespaceAware(
true);
386 DocumentBuilder db = dbf.newDocumentBuilder();
387 InputSource is =
new InputSource();
388 is.setCharacterStream(
new StringReader(XMLString));
390 }
catch (ParserConfigurationException pce) {
392 String msg =
"ParserConfigurationException while parsing messages from the client.";
393 Logger.getLogger(MessageProcessor.class.getName()).log(Level.SEVERE, msg, pce);
396 }
catch (SAXException se) {
398 String msg =
"SAXException while parsing messages from the client.";
399 Logger.getLogger(MessageProcessor.class.getName()).log(Level.ALL, msg, se);
402 }
catch (IOException ioe) {
404 String msg =
"IOException while parsing messages from the client.";
405 Logger.getLogger(MessageProcessor.class.getName()).log(Level.SEVERE, msg, ioe);
423 String content = null;
424 if (contentEl != null) {
426 NodeList contentCSNL = contentEl.getChildNodes();
427 if (contentCSNL.getLength() == 3) {
429 Node contentCSEl = contentCSNL.item(1);
430 if (contentCSEl != null) {
431 content = contentCSEl.getNodeValue();
433 }
else if (contentCSNL.getLength() == 2) {
435 Node contentCSEl = contentCSNL.item(0);
436 if (contentCSEl != null) {
437 content = contentCSEl.getNodeValue();
439 if ((content.trim()).isEmpty()) {
441 contentCSEl = contentCSNL.item(1);
442 if (contentCSEl != null) {
443 content = contentCSEl.getNodeValue();
446 }
else if (contentCSNL.getLength() == 1) {
447 Node contentCSEl = contentCSNL.item(0);
448 if (contentCSEl != null) {
449 content = contentCSEl.getNodeValue();
452 content = contentEl.getNodeValue();
466 String retStr = aString.replace(
" -> ",
"/");
467 retStr = retStr.replace(
" ->",
"/");
468 retStr = retStr.replace(
"-> ",
"/");
469 retStr = retStr.replace(
"->",
"/");
480 if (escaped != null) {
499 for (
int i = 0; i < b.length; i++) {
500 result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
512 if (!gStr.startsWith(
"g")) {
516 gStr = MessageProcessor.replaceArrows(gStr);
518 int index = gStr.indexOf(
"/");
523 String gIdStr = gStr.substring(1, index);
527 gId = Integer.parseInt(gIdStr);
528 }
catch (NumberFormatException nfe) {
532 Object[] params =
new Object[2];
535 List gList = AppBean.getPersistenceManager().queryDB(
"UserGroup.findById", params);
536 if (gList != null && !gList.isEmpty()) {
static boolean isGroupIncluded(String gStr)
static final int ERROR_0_PROTO_VERSION
static final int LOG_LEVEL_ALL_ERRORS
static String unescapeXMLAmp
static String replaceArrows(String aString)
static final int LOG_LEVEL_SERVER_INTERNAL_ERRORS
static String unescapeXMLQuotes
Singleton for storing global variables.
Class which process XML with messages in 4A protocol v 1.x.
static String unescapeXMLGT
static final int ERROR_31_SESSION_EXPIRED
static final int PROTOCOL_LOD_V1
static String unescapeXML(String escaped)
Static class which parses and process XML with messages.
static String getHexString(byte[] b)
static final int LOG_LEVEL
Flier with informations for comet handlers.
static final int LOG_LEVEL_ALL
static Document parseXml(String XMLString)
static final int LANGUAGE_ENG
static final int LOG_LEVEL_SERVER_ERRORS
static final int PROTOCOL_LOD_V1_1
static final int PROTOCOL_LOD_V2
static String getElementContent(Element contentEl)
Processed informations about client request.
static final int ERROR_32_BAD_REQUEST
Class which parses and process XML with messages for protocol version 2.
Class responsible for localised strings.
Informations about client session.
static RequestInfo processXML(String XMLString)
static String unescapeXMLApos
static String unescapeXMLLT