otsdaq  v2_05_02_indev
SOAPUtilities.cc
1 #include "otsdaq/SOAPUtilities/SOAPUtilities.h"
2 
3 #include "otsdaq/Macros/CoutMacros.h"
4 #include "otsdaq/MessageFacility/MessageFacility.h"
5 
6 #include <xdaq/NamespaceURI.h>
7 #include <xoap/MessageReference.h>
8 #include <xoap/Method.h>
9 
10 #pragma GCC diagnostic push
11 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
12 #include <xoap/MessageFactory.h>
13 #pragma GCC diagnostic pop
14 
15 #include <xoap/AttachmentPart.h>
16 #include <xoap/SOAPBody.h>
17 #include <xoap/SOAPEnvelope.h>
18 #include <xoap/SOAPPart.h>
19 #include <xoap/domutils.h>
20 
21 using namespace ots;
22 
23 //==============================================================================
24 SOAPUtilities::SOAPUtilities(void) {}
25 
26 //==============================================================================
27 SOAPUtilities::~SOAPUtilities(void) {}
28 
29 //==============================================================================
30 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(SOAPCommand soapCommand)
31 {
32  if(soapCommand.hasParameters())
33  return makeSOAPMessageReference(soapCommand.getCommand(), soapCommand.getParameters());
34  else
35  return makeSOAPMessageReference(soapCommand.getCommand());
36 }
37 
38 //==============================================================================
39 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command)
40 {
41  xoap::MessageReference message = xoap::createMessage();
42  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
43  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
44  xoap::SOAPBody body = envelope.getBody();
45  body.addBodyElement(name);
46  return message;
47 }
48 
49 //==============================================================================
50 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command, SOAPParameters parameters)
51 {
52  //__COUT__ << "Command: " << command << " par size: " << parameters.size() <<
53  // std::endl;
54  if(parameters.size() == 0)
55  return makeSOAPMessageReference(command);
56  xoap::MessageReference message = xoap::createMessage();
57  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
58  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
59  xoap::SOAPBody body = envelope.getBody();
60  xoap::SOAPElement bodyCommand = body.addBodyElement(name);
61  xoap::SOAPName parameterName = envelope.createName("Null");
62  for(SOAPParameters::iterator it = parameters.begin(); it != parameters.end(); it++)
63  {
64  parameterName = envelope.createName(it->first);
65  bodyCommand.addAttribute(parameterName, it->second);
66  }
67 
68  return message;
69 }
70 
71 //==============================================================================
72 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command, std::string fileName)
73 {
74  __COUT__ << "SOAP XML file path : " << fileName << std::endl;
75  xoap::MessageReference message = xoap::createMessage();
76  xoap::SOAPPart soap = message->getSOAPPart();
77  xoap::SOAPEnvelope envelope = soap.getEnvelope();
78  xoap::AttachmentPart* attachment;
79  attachment = message->createAttachmentPart();
80  attachment->setContent(fileName);
81  attachment->setContentId("SOAPTEST1");
82  attachment->addMimeHeader("Content-Description", "This is a SOAP message with attachments");
83  message->addAttachmentPart(attachment);
84  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
85  xoap::SOAPBody body = envelope.getBody();
86  body.addBodyElement(name);
87  return message;
88 }
89 
90 //==============================================================================
91 void SOAPUtilities::addParameters(xoap::MessageReference& message, SOAPParameters parameters)
92 {
93  //__COUT__ << "adding parameters!!!!!!" << std::endl;
94  if(parameters.size() == 0)
95  return;
96  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
97  xoap::SOAPBody body = envelope.getBody();
98  xoap::SOAPName name(translate(message).getCommand(), "xdaq", XDAQ_NS_URI);
99 
100  std::vector<xoap::SOAPElement> bodyList = body.getChildElements();
101  for(std::vector<xoap::SOAPElement>::iterator it = bodyList.begin(); it != bodyList.end(); it++)
102  {
103  if((*it).getElementName() == name)
104  {
105  for(SOAPParameters::iterator itPar = parameters.begin(); itPar != parameters.end(); itPar++)
106  {
107  xoap::SOAPName parameterName = envelope.createName(itPar->first);
108  (*it).addAttribute(parameterName, itPar->second);
109  }
110  }
111  }
112 }
113 
114 //==============================================================================
115 SOAPCommand SOAPUtilities::translate(const xoap::MessageReference& message)
116 {
117  SOAPCommand soapCommand;
118  const std::vector<xoap::SOAPElement>& bodyList = message->getSOAPPart().getEnvelope().getBody().getChildElements();
119  for(std::vector<xoap::SOAPElement>::const_iterator it = bodyList.begin(); it != bodyList.end(); it++)
120  {
121  xoap::SOAPElement element = *it;
122  soapCommand.setCommand(element.getElementName().getLocalName());
123  DOMNamedNodeMap* parameters = element.getDOM()->getAttributes();
124  for(unsigned int i = 0; i < parameters->getLength(); i++)
125  soapCommand.setParameter(xoap::XMLCh2String(parameters->item(i)->getNodeName()), xoap::XMLCh2String(parameters->item(i)->getNodeValue()));
126  }
127  return soapCommand;
128 }
129 
130 //==============================================================================
131 std::string SOAPUtilities::receive(const xoap::MessageReference& message, SOAPCommand& soapCommand) { return receive(message, soapCommand.getParametersRef()); }
132 
133 //==============================================================================
134 std::string SOAPUtilities::receive(const xoap::MessageReference& message)
135 {
136  // NOTE it is assumed that there is only 1 command for each message (that's why we use
137  // begin)
138  return (message->getSOAPPart().getEnvelope().getBody().getChildElements()).begin()->getElementName().getLocalName();
139 }
140 
141 //==============================================================================
142 std::string SOAPUtilities::receive(const xoap::MessageReference& message, SOAPParameters& parameters)
143 {
144  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
145  std::vector<xoap::SOAPElement> bodyList = envelope.getBody().getChildElements();
146  xoap::SOAPElement command = bodyList[0];
147  std::string commandName = command.getElementName().getLocalName();
148  xoap::SOAPName name = envelope.createName("Key");
149 
150  for(SOAPParameters::iterator it = parameters.begin(); it != parameters.end(); it++)
151  {
152  name = envelope.createName(it->first);
153 
154  try
155  {
156  it->second = command.getAttributeValue(name);
157  // if( parameters.getParameter(it->first).isEmpty() )
158  //{
159  // __COUT__ << "Complaint from " <<
160  // (theApplication_->getApplicationDescriptor()->getClassName()) <<
161  // std::endl;
162  // __COUT__ << " : Parameter "<< it->first
163  // << " does not exist in the list of incoming parameters!" << std::endl;
164  // __COUT__ << "It could also be because you passed an empty std::string"
165  // << std::endl;
166  // //assert(0);
167  //};
168  }
169  catch(xoap::exception::Exception& e)
170  {
171  __COUT__ << "Parameter " << it->first << " does not exist in the list of incoming parameters!" << std::endl;
172  XCEPT_RETHROW(xoap::exception::Exception, "Looking for parameter that does not exist!", e);
173  }
174  }
175 
176  return commandName;
177 }