1 #include "otsdaq-core/SOAPUtilities/SOAPMessenger.h"
2 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
3 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
4 #include "otsdaq-core/MessageFacility/MessageFacility.h"
5 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
8 #include <xoap/Method.h>
9 #include <xdaq/NamespaceURI.h>
10 #include <xoap/MessageReference.h>
11 #include <xoap/MessageFactory.h>
12 #include <xoap/SOAPPart.h>
13 #include <xoap/SOAPEnvelope.h>
14 #include <xoap/SOAPBody.h>
15 #include <xoap/domutils.h>
16 #include <xoap/AttachmentPart.h>
28 SOAPMessenger::SOAPMessenger(xdaq::Application* application) :
29 theApplication_(application)
34 SOAPMessenger::SOAPMessenger(
const SOAPMessenger& aSOAPMessenger) :
35 theApplication_(aSOAPMessenger.theApplication_)
40 std::string SOAPMessenger::receive(
const xoap::MessageReference& message,
SOAPCommand& soapCommand)
42 return receive(message, soapCommand.getParametersRef());
46 std::string SOAPMessenger::receive(
const xoap::MessageReference& message)
49 return (message->getSOAPPart().getEnvelope().getBody().getChildElements()).begin()->getElementName().getLocalName();
53 std::string SOAPMessenger::receive(
const xoap::MessageReference& message,
SOAPParameters& parameters)
55 xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
56 std::vector<xoap::SOAPElement> bodyList = envelope.getBody().getChildElements();
57 xoap::SOAPElement command = bodyList[0];
58 std::string commandName = command.getElementName().getLocalName();
59 xoap::SOAPName name = envelope.createName(
"Key");
61 for (SOAPParameters::iterator it=parameters.begin(); it!=parameters.end(); it++)
63 name = envelope.createName(it->first);
67 it->second = command.getAttributeValue(name);
77 catch (xoap::exception::Exception& e)
79 std::cout << __COUT_HDR_FL__ <<
"Parameter " << it->first <<
" does not exist in the list of incoming parameters!" << std::endl;
80 XCEPT_RETHROW(xoap::exception::Exception,
"Looking for parameter that does not exist!",e);
93 std::string SOAPMessenger::send(
const xdaq::ApplicationDescriptor* ind,
94 xoap::MessageReference message)
95 throw (xdaq::exception::Exception)
100 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
101 const_cast<xdaq::ApplicationDescriptor*
>(ind);
104 message->getMimeHeaders()->setHeader(
"Content-Location", d->getURN());
105 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message,
106 *(theApplication_->getApplicationDescriptor()),
108 std::string replyString = receive(reply);
109 std::cout << __COUT_HDR_FL__ <<
"replyString " << replyString << std::endl;
112 catch (xdaq::exception::Exception& e)
114 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
115 << d->getClassName() <<
" instance " << d->getInstance()
116 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e);
117 std::string mystring;
118 message->writeTo(mystring);
119 std::cout << __COUT_HDR_FL__<< mystring << std::endl;
120 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
125 std::string SOAPMessenger::send(
const xdaq::ApplicationDescriptor* d,
SOAPCommand soapCommand)
126 throw (xdaq::exception::Exception)
128 if(soapCommand.hasParameters())
129 return send(d, soapCommand.getCommand(), soapCommand.getParameters());
131 return send(d, soapCommand.getCommand());
135 std::string SOAPMessenger::send(
const xdaq::ApplicationDescriptor* d, std::string command)
136 throw (xdaq::exception::Exception)
138 xoap::MessageReference message;
139 message = SOAPUtilities::makeSOAPMessageReference(command);
140 return send(d, message);
144 std::string SOAPMessenger::send(
const xdaq::ApplicationDescriptor* ind, std::string cmd,
146 throw (xdaq::exception::Exception)
151 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
152 const_cast<xdaq::ApplicationDescriptor*
>(ind);
154 xoap::MessageReference message;
157 message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
158 message->getMimeHeaders()->setHeader(
"Content-Location", d->getURN());
159 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
160 std::string replyString = receive(reply);
163 catch (xdaq::exception::Exception& e)
165 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
166 << d->getClassName() <<
" instance " << d->getInstance()
167 <<
" with command = " << cmd
168 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
170 std::string mystring;
171 message->writeTo(mystring);
172 mf::LogError(__FILE__) << mystring;
173 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
179 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
const xdaq::ApplicationDescriptor* ind,
181 throw (xdaq::exception::Exception)
186 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
187 const_cast<xdaq::ApplicationDescriptor*
>(ind);
190 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd);
191 message->getMimeHeaders()->setHeader(
"Content-Location", d->getURN());
192 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
195 catch (xdaq::exception::Exception& e)
197 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
198 << d->getClassName() <<
" instance " << d->getInstance()
199 <<
" with command = " << cmd
200 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
201 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
207 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
const xdaq::ApplicationDescriptor *ind,
208 xoap::MessageReference message)
209 throw (xdaq::exception::Exception)
214 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
215 const_cast<xdaq::ApplicationDescriptor*
>(ind);
218 message->getMimeHeaders()->setHeader(
"Content-Location", d->getURN());
219 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
222 catch (xdaq::exception::Exception& e)
224 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
225 << d->getClassName() <<
" instance " << d->getInstance()
226 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e);
227 std::string mystring;
228 message->writeTo(mystring);
229 mf::LogError(__FILE__)<< mystring << std::endl;
230 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
235 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
const xdaq::ApplicationDescriptor* ind,
237 throw (xdaq::exception::Exception)
242 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
243 const_cast<xdaq::ApplicationDescriptor*
>(ind);
246 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
247 message->getMimeHeaders()->setHeader(
"Content-Location", d->getURN());
248 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
251 catch (xdaq::exception::Exception& e)
253 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
254 << d->getClassName() <<
" instance " << d->getInstance()
255 <<
" with command = " << cmd
256 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e);
257 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
299 std::string SOAPMessenger::sendStatus(
const xdaq::ApplicationDescriptor* ind,
301 throw (xdaq::exception::Exception)
306 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
307 const_cast<xdaq::ApplicationDescriptor*
>(ind);
309 std::string cmd =
"StatusNotification";
313 gettimeofday(&tv,NULL);
315 std::stringstream ss;
317 parameters.addParameter(
"Description",message);
318 ss.str(
""); ss << tv.tv_sec;
319 parameters.addParameter(
"Time",ss.str());
320 ss.str(
""); ss << tv.tv_usec;
321 parameters.addParameter(
"usec",ss.str());
322 return send(d, cmd, parameters);
324 catch (xdaq::exception::Exception& e)
326 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP error message to "
327 << d->getClassName() <<
" instance " << d->getInstance()
328 <<
" with command = " << cmd
329 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
330 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);