23 #include <sys/socket.h>
24 #include <arpa/inet.h>
40 #include "OSParameters.h"
52 using std::ostringstream;
71 ostringstream ret_message, outStr;
73 struct sockaddr_in httpServAddr;
74 unsigned short httpServPort = servicePortNumber;
75 char *servIP = &serviceIP[0];
78 char* message = &theSOAP[0];
84 if( WSAStartup(MAKEWORD(2, 0), &wsaData) != 0 )
throw ErrorClass(
"WSAStartup failed");
87 if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
throw ErrorClass(
"failure creating socket");
89 memset(&httpServAddr, 0,
sizeof(httpServAddr));
90 httpServAddr.sin_family = AF_INET;
91 httpServAddr.sin_addr.s_addr =
ResolveName( servIP);
92 httpServAddr.sin_port = htons(httpServPort);
94 if (connect(sock, (
struct sockaddr *) &httpServAddr,
sizeof(httpServAddr)) < 0)
96 string sipadd = &serviceIP[0];
97 string errormsg =
"failure connecting with remote socket at address: " + sipadd ;
103 httpStringLen = strlen( message);
109 if (
send(sock, message, httpStringLen, 0) != httpStringLen)
110 throw ErrorClass(
"send() sent a different number of bytes than expected");
120 while (recvMsgSize > 0)
125 if ((recvMsgSize = recv(sock, httpBuffer,
RCVBUFSIZE-1, 0)) < 0)
126 throw ErrorClass(
"socket error receiving data");
130 outStr <<
"Message size = " << recvMsgSize << endl;
131 outStr << httpBuffer << endl;
134 ret_message << httpBuffer;
147 return ret_message.str();
156 string* msInputs,
string* msInputNames,
string sSoapAction)
158 ostringstream request, body, outStr;
160 string mynamespace =
"xmlns:ns1=\"http://www.optimizationservices.org\"";
164 outStr <<
"Solver address = " << solverAddress << endl;
165 outStr <<
"SOAP action = " << sSoapAction << endl;
166 outStr <<
"postURI = " << postURI << endl;
169 request <<
"POST " << postURI <<
" HTTP/1.0" << endl ;
170 request <<
"Content-Type: text/xml; charset=UTF-8" << endl;
171 request <<
"Host: " ;
172 request << solverAddress << endl;
173 request <<
"Connection: close" << endl;
174 request <<
"Accept: application/soap+xml, application/dime, multipart/related, text/*" << endl;
175 request <<
"Cache-Control: no-cache" << endl;
176 request <<
"Pragma: no-cache" << endl;
177 request <<
"SOAPAction: ";
178 request <<
"\"" << sSoapAction <<
"\"" << endl;
180 body <<
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" << endl;
181 body <<
"<SOAP-ENV:Body>" << endl;
182 body <<
"<ns1:" << smethod <<
" " << mynamespace <<
">" << endl;
183 for(i = 0; i < numInputs; i++)
185 body <<
"<" << msInputNames[ i] <<
" xsi:type=\"xsd:string\"" <<
">" ;
186 body << msInputs[ i] ;
187 body <<
"</" << msInputNames[ i] <<
">" << endl;
189 body <<
"</ns1:" << smethod <<
">" << endl;
190 body <<
"</SOAP-ENV:Body>" << endl;
191 body <<
"</SOAP-ENV:Envelope>" << endl;
193 request <<
"Content-Length: " << body.str().length();
194 request << endl << endl;
196 request << body.str();
197 return request.str();
201 std::string fileName, std::string theFile, std::string boundaryName)
203 ostringstream request, body, outStr;
207 outStr <<
"Solver address = " << solverAddress << endl;
208 outStr <<
"postURI = " << postURI << endl;
211 request <<
"POST " << postURI <<
" HTTP/1.0" <<
"\r\n";
212 request <<
"Host: " ;
213 request << solverAddress <<
"\r\n";
214 request <<
"Content-Type: multipart/form-data; boundary=" ;
215 request << boundaryName <<
"\r\n";
216 request <<
"Connection: keep-alive" <<
"\r\n";
219 body << boundaryName ;
221 body <<
"Content-Disposition: form-data; name=\"";
225 body <<
" filename=\"";
227 body <<
"\"" <<
"\r\n";
229 body <<
"Content-Type: text/plain" ;
235 body << boundaryName;
239 request <<
"Content-Length: " << body.str().length();
242 request << body.str();
243 return request.str();
258 int loopsize = inputstring.length();
269 switch( inputstring[i])
284 body << inputstring[i];
300 int loopsize = inputstring.length();
303 string::size_type pos1 = inputstring.find(
"<![CDATA[" );
304 string::size_type pos2 = inputstring.find(
"]]>" );
305 body << inputstring.substr( pos1 + 1, pos2 - pos1 - 1);
314 if(inputstring[i] ==
'&')
316 switch (inputstring[i+1])
319 if (inputstring[i + 2] ==
't' && inputstring[i + 3] ==
';')
326 if (inputstring[i + 2] ==
't' && inputstring[i + 3] ==
';')
333 if (inputstring[i + 2] ==
'u' && inputstring[i + 3] ==
'o' && inputstring[i + 4] ==
't' && inputstring[i + 5] ==
';')
340 body << inputstring[i];
347 body << inputstring[i];
357 struct hostent *host;
360 if ((host = gethostbyname(name)) == NULL)
362 string s1 = &name[0];
363 string errormsg =
"cannot resolve the domain name: " +
s1;
366 return *((
unsigned long *) host->h_addr_list[0]);
382 string::size_type startxml = soapstring.find(serviceMethod+
"Return" , 1);
383 string::size_type
pos;
384 if (startxml == string::npos)
393 startxml = soapstring.find(
"<faultstring>" , 1);
394 if(startxml == string::npos)
396 osresult->
setGeneralMessage(
"we had a problem contacting the server which we cannot figure out -- check address of server");
400 pos = soapstring.find(
"</faultstring>" , startxml + 1);
401 std::string tmpString = soapstring.substr(startxml + 13 , pos - startxml - 13);
402 osresult->
setGeneralMessage(
"There was a communication problem with server, SOAP error message: " + tmpString);
406 result = osrlwriter->
writeOSrL( osresult);
416 startxml = soapstring.find(
">", startxml + 1);
417 if(startxml == string::npos)
return result;
419 string::size_type endxml = soapstring.find(
"</", startxml);
422 if(endxml == string::npos)
430 result = soapstring.substr(startxml, endxml - startxml);
unsigned long ResolveName(char *name)
static std::string createSOAPMessage(int numInputs, std::string solverAddress, std::string postURI, std::string smethod, std::string *msInputs, std::string *msInputNames, std::string sSoapAction)
create the SOAP message that is send to the solver Web Service
void send(OSCommandLine *oscommandline, OSnl2OS *osnl2os)
const OSSmartPtr< OSOutput > osoutput
pos
position where the operator should be printed when printing the expression
static std::string getOSxL(std::string soapstring, std::string serviceMethod)
extract the appropriate OSxL protocol from the SOAP envelop
std::string errormsg
errormsg is the error that is causing the exception to be thrown
Take an OSResult object and write a string that validates against OSrL.
std::string writeOSrL(OSResult *theosresult)
create an osrl string from an OSResult object
WSUtil()
Default constructor.
static std::string createFormDataUpload(std::string solverAddress, std::string postURI, std::string fileName, std::string theFile, std::string boundaryName)
create the SOAP message that is sent to the solver Web Service
bool setGeneralMessage(std::string message)
Set the general message.
bool setGeneralStatusType(std::string type)
Set the general status type, which can be: success, error, warning.
static std::string deSOAPify(std::string theXmlString, bool useCDATA)
take the XML from a SOAP envelop and replace < with < replace > with > replace &quot with "; ...
~WSUtil()
Class destructor.
static std::string SOAPify(std::string theXmlString, bool useCDATA)
prepare XML to be put into a SOAP envelop, replace < with < replace > with > replace " and ' with ...
static std::string sendSOAPMessage(std::string theSOAP, std::string serviceIP, unsigned int servicePortNumber)
open a socket and send a SOAP message to the solver Web Service
used for throwing exceptions.