otsdaq  v2_00_00
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 
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/SOAPPart.h>
16 #include <xoap/SOAPEnvelope.h>
17 #include <xoap/SOAPBody.h>
18 #include <xoap/domutils.h>
19 #include <xoap/AttachmentPart.h>
20 
21 using namespace ots;
22 
23 
24 //========================================================================================================================
25 SOAPUtilities::SOAPUtilities(void)
26 {}
27 
28 //========================================================================================================================
29 SOAPUtilities::~SOAPUtilities(void)
30 {}
31 
32 //========================================================================================================================
33 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(SOAPCommand soapCommand)
34 {
35  if(soapCommand.hasParameters())
36  return makeSOAPMessageReference(soapCommand.getCommand(), soapCommand.getParameters());
37  else
38  return makeSOAPMessageReference(soapCommand.getCommand());
39 }
40 
41 //========================================================================================================================
42 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command)
43 {
44  xoap::MessageReference message = xoap::createMessage();
45  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
46  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
47  xoap::SOAPBody body = envelope.getBody();
48  body.addBodyElement(name);
49  return message;
50 }
51 
52 //========================================================================================================================
53 xoap::MessageReference SOAPUtilities::makeSOAPMessageReference(std::string command, SOAPParameters parameters)
54 {
55  //std::cout << __COUT_HDR_FL__ << "Command: " << command << " par size: " << parameters.size() << 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, std::string fileName)
75 {
76  std::cout << __COUT_HDR_FL__ << "SOAP XML file path : " << fileName << std::endl;
77  xoap::MessageReference message = xoap::createMessage();
78  xoap::SOAPPart soap = message->getSOAPPart();
79  xoap::SOAPEnvelope envelope = soap.getEnvelope();
80  xoap::AttachmentPart* attachment;
81  attachment = message->createAttachmentPart();
82  attachment->setContent(fileName);
83  attachment->setContentId("SOAPTEST1");
84  attachment->addMimeHeader("Content-Description", "This is a SOAP message with attachments");
85  message->addAttachmentPart(attachment);
86  xoap::SOAPName name = envelope.createName(command, "xdaq", XDAQ_NS_URI);
87  xoap::SOAPBody body= envelope.getBody();
88  body.addBodyElement(name);
89  return message;
90 }
91 
92 //========================================================================================================================
93 void SOAPUtilities::addParameters(xoap::MessageReference& message, SOAPParameters parameters)
94 {
95  std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "adding parameters!!!!!!" << std::endl;
96  if(parameters.size() == 0) return;
97  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
98  xoap::SOAPBody body = envelope.getBody();
99  xoap::SOAPName name(translate(message).getCommand(), "xdaq", XDAQ_NS_URI);
100 
101  std::vector<xoap::SOAPElement> bodyList = body.getChildElements();
102  for(std::vector<xoap::SOAPElement>::iterator it = bodyList.begin(); it != bodyList.end(); it++)
103  {
104  if((*it).getElementName() == name)
105  {
106  for (SOAPParameters::iterator itPar=parameters.begin(); itPar!=parameters.end(); itPar++)
107  {
108  xoap::SOAPName parameterName = envelope.createName(itPar->first);
109  (*it).addAttribute(parameterName,itPar->second);
110  }
111  }
112  }
113 }
114 
115 //========================================================================================================================
116 SOAPCommand SOAPUtilities::translate(const xoap::MessageReference& message)
117 {
118  SOAPCommand soapCommand;
119  const std::vector<xoap::SOAPElement>& bodyList = message->getSOAPPart().getEnvelope().getBody().getChildElements();
120  for(std::vector<xoap::SOAPElement>::const_iterator it = bodyList.begin(); it != bodyList.end(); it++)
121  {
122  xoap::SOAPElement element = *it;
123  soapCommand.setCommand(element.getElementName().getLocalName());
124  DOMNamedNodeMap* parameters = element.getDOM()->getAttributes();
125  for(unsigned int i=0; i<parameters->getLength(); i++)
126  soapCommand.setParameter(xoap::XMLCh2String(parameters->item(i)->getNodeName()), xoap::XMLCh2String(parameters->item(i)->getNodeValue()));
127  }
128  return soapCommand;
129 }