otsdaq  v1_01_03
 All Classes Namespaces Functions
SOAPUtilities.cc
1 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
2 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
4 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
5 
6 #include "xoap/Method.h"
7 #include "xdaq/NamespaceURI.h"
8 #include "xoap/MessageReference.h"
9 #include "xoap/MessageFactory.h"
10 #include "xoap/SOAPPart.h"
11 #include "xoap/SOAPEnvelope.h"
12 #include "xoap/SOAPBody.h"
13 #include "xoap/domutils.h"
14 #include "xoap/AttachmentPart.h"
15 
16 using namespace ots;
17 
18 
19 //========================================================================================================================
20 SOAPUtilities::SOAPUtilities(void)
21 {}
22 
23 //========================================================================================================================
24 SOAPUtilities::~SOAPUtilities(void)
25 {}
26 
27 //========================================================================================================================
28 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(SOAPCommand soapCommand)
29 {
30  if(soapCommand.hasParameters())
31  return makeSOAPMessageReference(soapCommand.getCommand(), soapCommand.getParameters());
32  else
33  return makeSOAPMessageReference(soapCommand.getCommand());
34 }
35 
36 //========================================================================================================================
37 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command)
38 {
39  xoap::MessageReference message = xoap::createMessage();
40  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
41  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
42  xoap::SOAPBody body = envelope.getBody();
43  body.addBodyElement(name);
44  return message;
45 }
46 
47 //========================================================================================================================
48 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command, SOAPParameters parameters)
49 {
50  //std::cout << __COUT_HDR_FL__ << "Command: " << command << " par size: " << parameters.size() << std::endl;
51  if(parameters.size() == 0)
52  return makeSOAPMessageReference(command);
53  xoap::MessageReference message = xoap::createMessage();
54  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
55  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
56  xoap::SOAPBody body = envelope.getBody();
57  xoap::SOAPElement bodyCommand = body.addBodyElement(name);
58  xoap::SOAPName parameterName = envelope.createName("Null");
59  for (SOAPParameters::iterator it=parameters.begin(); it!=parameters.end(); it++)
60  {
61  parameterName = envelope.createName(it->first);
62  bodyCommand.addAttribute(parameterName,it->second);
63  }
64 
65  return message;
66 }
67 
68 //========================================================================================================================
69 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command, std::string fileName)
70 {
71  std::cout << __COUT_HDR_FL__ << "SOAP XML file path : " << fileName << std::endl;
72  xoap::MessageReference message = xoap::createMessage();
73  xoap::SOAPPart soap = message->getSOAPPart();
74  xoap::SOAPEnvelope envelope = soap.getEnvelope();
75  xoap::AttachmentPart* attachment;
76  attachment = message->createAttachmentPart();
77  attachment->setContent(fileName);
78  attachment->setContentId("SOAPTEST1");
79  attachment->addMimeHeader("Content-Description", "This is a SOAP message with attachments");
80  message->addAttachmentPart(attachment);
81  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
82  xoap::SOAPBody body= envelope.getBody();
83  body.addBodyElement(name);
84  return message;
85 }
86 
87 //========================================================================================================================
88 void SOAPUtilities::addParameters(xoap::MessageReference& message, SOAPParameters parameters)
89 {
90  std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "adding parameters!!!!!!" << std::endl;
91  if(parameters.size() == 0) return;
92  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
93  xoap::SOAPBody body = envelope.getBody();
94  xoap::SOAPName name(translate(message).getCommand(), "xdaq", XDAQ_NS_URI);
95 
96  std::vector<xoap::SOAPElement> bodyList = body.getChildElements();
97  for(std::vector<xoap::SOAPElement>::iterator it = bodyList.begin(); it != bodyList.end(); it++)
98  {
99  if((*it).getElementName() == name)
100  {
101  for (SOAPParameters::iterator itPar=parameters.begin(); itPar!=parameters.end(); itPar++)
102  {
103  xoap::SOAPName parameterName = envelope.createName(itPar->first);
104  (*it).addAttribute(parameterName,itPar->second);
105  }
106  }
107  }
108 }
109 
110 //========================================================================================================================
111 SOAPCommand SOAPUtilities::translate(const xoap::MessageReference& message)
112 {
113  SOAPCommand soapCommand;
114  const std::vector<xoap::SOAPElement>& bodyList = message->getSOAPPart().getEnvelope().getBody().getChildElements();
115  for(std::vector<xoap::SOAPElement>::const_iterator it = bodyList.begin(); it != bodyList.end(); it++)
116  {
117  xoap::SOAPElement element = *it;
118  soapCommand.setCommand(element.getElementName().getLocalName());
119  DOMNamedNodeMap* parameters = element.getDOM()->getAttributes();
120  for(unsigned int i=0; i<parameters->getLength(); i++)
121  soapCommand.setParameter(xoap::XMLCh2String(parameters->item(i)->getNodeName()), xoap::XMLCh2String(parameters->item(i)->getNodeValue()));
122  }
123  return soapCommand;
124 }