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);
111 catch (xdaq::exception::Exception& e)
113 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
114 << d->getClassName() <<
" instance " << d->getInstance()
115 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e);
116 std::string mystring;
117 message->writeTo(mystring);
118 std::cout << __COUT_HDR_FL__<< mystring << std::endl;
119 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
124 std::string SOAPMessenger::send(
const xdaq::ApplicationDescriptor* d,
SOAPCommand soapCommand)
125 throw (xdaq::exception::Exception)
127 if(soapCommand.hasParameters())
128 return send(d, soapCommand.getCommand(), soapCommand.getParameters());
130 return send(d, soapCommand.getCommand());
134 std::string SOAPMessenger::send(
const xdaq::ApplicationDescriptor* d, std::string command)
135 throw (xdaq::exception::Exception)
137 xoap::MessageReference message;
138 message = SOAPUtilities::makeSOAPMessageReference(command);
139 return send(d, message);
143 std::string SOAPMessenger::send(
const xdaq::ApplicationDescriptor* ind, std::string cmd,
145 throw (xdaq::exception::Exception)
150 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
151 const_cast<xdaq::ApplicationDescriptor*
>(ind);
153 xoap::MessageReference message;
156 message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
157 message->getMimeHeaders()->setHeader(
"Content-Location", d->getURN());
158 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
159 std::string replyString = receive(reply);
162 catch (xdaq::exception::Exception& e)
164 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
165 << d->getClassName() <<
" instance " << d->getInstance()
166 <<
" with command = " << cmd
167 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
169 std::string mystring;
170 message->writeTo(mystring);
171 mf::LogError(__FILE__) << mystring;
172 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
178 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
const xdaq::ApplicationDescriptor* ind,
180 throw (xdaq::exception::Exception)
185 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
186 const_cast<xdaq::ApplicationDescriptor*
>(ind);
189 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd);
190 message->getMimeHeaders()->setHeader(
"Content-Location", d->getURN());
191 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
194 catch (xdaq::exception::Exception& e)
196 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
197 << d->getClassName() <<
" instance " << d->getInstance()
198 <<
" with command = " << cmd
199 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
200 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
206 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
const xdaq::ApplicationDescriptor *ind,
207 xoap::MessageReference message)
208 throw (xdaq::exception::Exception)
213 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
214 const_cast<xdaq::ApplicationDescriptor*
>(ind);
217 message->getMimeHeaders()->setHeader(
"Content-Location", d->getURN());
218 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
221 catch (xdaq::exception::Exception& e)
223 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
224 << d->getClassName() <<
" instance " << d->getInstance()
225 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e);
226 std::string mystring;
227 message->writeTo(mystring);
228 mf::LogError(__FILE__)<< mystring << std::endl;
229 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
234 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
const xdaq::ApplicationDescriptor* ind,
236 throw (xdaq::exception::Exception)
241 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
242 const_cast<xdaq::ApplicationDescriptor*
>(ind);
245 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
246 message->getMimeHeaders()->setHeader(
"Content-Location", d->getURN());
247 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
250 catch (xdaq::exception::Exception& e)
252 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP message to "
253 << d->getClassName() <<
" instance " << d->getInstance()
254 <<
" with command = " << cmd
255 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e);
256 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);
298 std::string SOAPMessenger::sendStatus(
const xdaq::ApplicationDescriptor* ind,
300 throw (xdaq::exception::Exception)
305 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
306 const_cast<xdaq::ApplicationDescriptor*
>(ind);
308 std::string cmd =
"StatusNotification";
312 gettimeofday(&tv,NULL);
314 std::stringstream ss;
316 parameters.addParameter(
"Description",message);
317 ss.str(
""); ss << tv.tv_sec;
318 parameters.addParameter(
"Time",ss.str());
319 ss.str(
""); ss << tv.tv_usec;
320 parameters.addParameter(
"usec",ss.str());
321 return send(d, cmd, parameters);
323 catch (xdaq::exception::Exception& e)
325 std::cout << __COUT_HDR_FL__ <<
"This application failed to send a SOAP error message to "
326 << d->getClassName() <<
" instance " << d->getInstance()
327 <<
" with command = " << cmd
328 <<
" re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
329 XCEPT_RETHROW(xdaq::exception::Exception,
"Failed to send SOAP command.",e);