tdaq-develop-2025-02-12
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(),
34  soapCommand.getParameters());
35  else
36  return makeSOAPMessageReference(soapCommand.getCommand());
37 }
38 
39 //==============================================================================
40 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command)
41 {
42  xoap::MessageReference message = xoap::createMessage();
43  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
44  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
45  xoap::SOAPBody body = envelope.getBody();
46  body.addBodyElement(name);
47  return message;
48 }
49 
50 //==============================================================================
51 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command,
52  SOAPParameters parameters)
53 {
54  //__COUT__ << "Command: " << command << " par size: " << parameters.size() <<
55  // std::endl;
56  if(parameters.size() == 0)
57  return makeSOAPMessageReference(command);
58  xoap::MessageReference message = xoap::createMessage();
59  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
60  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
61  xoap::SOAPBody body = envelope.getBody();
62  xoap::SOAPElement bodyCommand = body.addBodyElement(name);
63  xoap::SOAPName parameterName = envelope.createName("Null");
64  for(SOAPParameters::iterator it = parameters.begin(); it != parameters.end(); it++)
65  {
66  parameterName = envelope.createName(it->first);
67  bodyCommand.addAttribute(parameterName, it->second);
68  }
69 
70  return message;
71 }
72 
73 //==============================================================================
74 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command,
75  std::string fileName)
76 {
77  __COUT__ << "SOAP XML file path : " << fileName << std::endl;
78  xoap::MessageReference message = xoap::createMessage();
79  xoap::SOAPPart soap = message->getSOAPPart();
80  xoap::SOAPEnvelope envelope = soap.getEnvelope();
81  xoap::AttachmentPart* attachment;
82  attachment = message->createAttachmentPart();
83  attachment->setContent(fileName);
84  attachment->setContentId("SOAPTEST1");
85  attachment->addMimeHeader("Content-Description",
86  "This is a SOAP message with attachments");
87  message->addAttachmentPart(attachment);
88  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
89  xoap::SOAPBody body = envelope.getBody();
90  body.addBodyElement(name);
91  return message;
92 }
93 
94 //==============================================================================
95 void SOAPUtilities::addParameters(xoap::MessageReference& message,
96  SOAPParameters parameters)
97 {
98  //__COUT__ << "adding parameters!!!!!!" << std::endl;
99  if(parameters.size() == 0)
100  return;
101  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
102  xoap::SOAPBody body = envelope.getBody();
103  xoap::SOAPName name(translate(message).getCommand(), "xdaq", XDAQ_NS_URI);
104 
105  std::vector<xoap::SOAPElement> bodyList = body.getChildElements();
106  for(std::vector<xoap::SOAPElement>::iterator it = bodyList.begin();
107  it != bodyList.end();
108  it++)
109  {
110  if((*it).getElementName() == name)
111  {
112  for(SOAPParameters::iterator itPar = parameters.begin();
113  itPar != parameters.end();
114  itPar++)
115  {
116  xoap::SOAPName parameterName = envelope.createName(itPar->first);
117  (*it).addAttribute(parameterName, itPar->second);
118  }
119  }
120  }
121 }
122 
123 //==============================================================================
124 SOAPCommand SOAPUtilities::translate(const xoap::MessageReference& message)
125 {
126  SOAPCommand soapCommand;
127  const std::vector<xoap::SOAPElement>& bodyList =
128  message->getSOAPPart().getEnvelope().getBody().getChildElements();
129  for(std::vector<xoap::SOAPElement>::const_iterator it = bodyList.begin();
130  it != bodyList.end();
131  it++)
132  {
133  xoap::SOAPElement element = *it;
134  soapCommand.setCommand(element.getElementName().getLocalName());
135  DOMNamedNodeMap* parameters = element.getDOM()->getAttributes();
136  for(unsigned int i = 0; i < parameters->getLength(); i++)
137  soapCommand.setParameter(
138  xoap::XMLCh2String(parameters->item(i)->getNodeName()),
139  xoap::XMLCh2String(parameters->item(i)->getNodeValue()));
140  }
141  return soapCommand;
142 }
143 
144 //==============================================================================
145 std::string SOAPUtilities::receive(const xoap::MessageReference& message,
146  SOAPCommand& soapCommand)
147 {
148  return receive(message, soapCommand.getParametersRef());
149 }
150 
151 //==============================================================================
152 std::string SOAPUtilities::receive(const xoap::MessageReference& message)
153 {
154  // NOTE it is assumed that there is only 1 command for each message (that's why we use
155  // begin)
156  return (message->getSOAPPart().getEnvelope().getBody().getChildElements())
157  .begin()
158  ->getElementName()
159  .getLocalName();
160 }
161 
162 //==============================================================================
163 std::string SOAPUtilities::receive(const xoap::MessageReference& message,
164  SOAPParameters& parameters)
165 {
166  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
167  std::vector<xoap::SOAPElement> bodyList = envelope.getBody().getChildElements();
168  xoap::SOAPElement command = bodyList[0];
169  std::string commandName = command.getElementName().getLocalName();
170  xoap::SOAPName name = envelope.createName("Key");
171 
172  for(SOAPParameters::iterator it = parameters.begin(); it != parameters.end(); it++)
173  {
174  name = envelope.createName(it->first);
175 
176  try
177  {
178  it->second = command.getAttributeValue(name);
179  // if( parameters.getParameter(it->first).isEmpty() )
180  //{
181  // __COUT__ << "Complaint from " <<
182  // (theApplication_->getApplicationDescriptor()->getClassName()) <<
183  // std::endl;
184  // __COUT__ << " : Parameter "<< it->first
185  // << " does not exist in the list of incoming parameters!" << std::endl;
186  // __COUT__ << "It could also be because you passed an empty std::string"
187  // << std::endl;
188  // //assert(0);
189  //};
190  }
191  catch(xoap::exception::Exception& e)
192  {
193  __COUT__ << "Parameter " << it->first
194  << " does not exist in the list of incoming parameters!"
195  << std::endl;
196  XCEPT_RETHROW(xoap::exception::Exception,
197  "Looking for parameter that does not exist!",
198  e);
199  }
200  }
201 
202  return commandName;
203 }
unsigned int size(void) const
Methods.
Definition: DataStructs.h:99
iterator begin(void)
Iterators.
Definition: DataStructs.h:91
const std::string & getCommand(void) const
Getters.
Definition: SOAPCommand.cc:44