SOAPEncoder.cpp

Go to the documentation of this file.
00001 
00023 // SOAPEncoder.cpp: implementation of the SOAPEncoder class.
00024 //
00026 
00027 const bool g_KbDebugging = true;
00028 #pragma warning ( disable: 4786 )
00029 #include "SOAPEncoder.h"
00030 
00031 #if !defined(BASE64ENCODER_H)
00032     #include "Base64Encoder.h"
00033 #endif // !defined(BASE64ENCODER_H)
00034 
00035 #ifndef _SSTREAM_
00036     #include <sstream>
00037 #endif // _SSTREAM_
00038 
00039 #if !defined(SOAPMETHOD_H)
00040     #include "SOAPMethod.h"
00041 #endif // SOAPMETHOD_H
00042 
00043 #if !defined(SOAPFAULT_H)
00044     #include "SOAPFault.h"
00045 #endif // !defined(SOAPFAULT_H)
00046 
00047 #ifndef _IOSTREAM_
00048     #include <iostream>
00049 #endif // _IOSTREAM_
00050 
00052 // Construction/Destruction
00054 
00055 
00056 
00057 SOAPEncoder::SOAPEncoder() : 
00058     m_bIsResponse( false )
00059 {
00060 
00061 }
00062 
00063 SOAPEncoder::~SOAPEncoder()
00064 {
00065 
00066 }
00067 
00068 bool SOAPEncoder::isEncodingResponse()
00069 {
00070     return m_bIsResponse;
00071 }
00072 
00073 std::string SOAPEncoder::encodeMethodCall( SOAPMethod& aMethod )
00074 {
00075     m_bIsResponse = false;
00076     return encodeMethod( aMethod );
00077 }
00078 
00079 std::string SOAPEncoder::encodeMethodResponse( SOAPMethod& aMethod )
00080 {
00081     m_bIsResponse = true;
00082     return encodeMethod( aMethod );
00083 }
00084 
00085 std::string SOAPEncoder::encodeMethodResonseContents( SOAPMethod& aMethod )
00086 {
00087     // Set the begin tag for the method element.
00088         std::ostringstream szStream;
00089         std::string szMethodName = aMethod.methodName() + "Response";
00090 
00091         if ( !aMethod.isGeneric() )
00092                 szStream << "<" << szMethodName << " " << aMethod.methodNameAttributes() << ">" << std::endl;
00093 
00094     // Encode the method.
00095     m_encodedValue = "";
00096     aMethod.encode( *this );
00097     szStream << m_encodedValue;
00098 
00099         if ( !aMethod.isGeneric() )
00100                 szStream << "</" << szMethodName << ">" << std::endl;
00101 
00102         m_encodedValue = szStream.str();
00103     return m_encodedValue;
00104 
00105 }
00106 std::string SOAPEncoder::encodeMethod( SOAPMethod& aMethod )
00107 {
00108     std::string szHeaderContents = headerContents();
00109     std::string szMethodName = aMethod.methodName();
00110     std::ostringstream szStream;
00111     bool bHeaderNeeded = szHeaderContents.length() > 0;
00112     // Initialize the envelope.
00113     szStream << envelopeOpen();
00114 
00115     if ( isEncodingResponse() )
00116     {
00117         szMethodName += "Response";
00118     }
00119 
00120     if ( bHeaderNeeded )
00121     {
00122         szStream << headerOpen() << szHeaderContents << headerClose(); 
00123     }
00124 
00125     szStream << bodyOpen();
00126 
00127     // Set the begin tag for the method element if we are not in a Genric which has the tags already.
00128         if ( !aMethod.isGeneric() )
00129                 szStream << "<" << szMethodName << " " << aMethod.methodNameAttributes() << ">" << std::endl;
00130 
00131     m_encodedValue = "";
00132 
00133     // Encode the method.
00134     aMethod.encode( *this );
00135 
00136     szStream << m_encodedValue;
00137 
00138     // Set the end tag for the method element the generic already has a tag.
00139         if ( !aMethod.isGeneric() )
00140                 szStream << "</" << szMethodName << ">" << std::endl;
00141 
00142     // Close the body.
00143     szStream << bodyClose();
00144 
00145     // Close the envelope.
00146     szStream << envelopeClose();
00147     m_encodedValue = szStream.str();
00148     return m_encodedValue;
00149 }
00150 
00151 // here is a walk around encoder for a pure XML argument - GJPC
00152 std::string SOAPEncoder::encodeArgument( const std::string& szArg )
00153 {
00154     std::ostringstream szStream;    
00155 
00156     // Have an encoder for pure XML
00157     szStream << szArg << std::endl;
00158     m_encodedValue += szStream.str();
00159     return szStream.str();
00160 }
00161 
00162 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const std::string& value )
00163 {
00164     std::ostringstream szStream;
00165 
00166     // Set the begin tag for the argument element.
00167     szStream << "<" << szArgName << " xsi:type=\"xsd:string\">" << value;
00168        
00169 
00170     // Set the end tag for the argument element.
00171     szStream << "</" << szArgName << ">" << std::endl;
00172     m_encodedValue += szStream.str();
00173     return szStream.str();
00174 }
00175 
00176 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const int& value )
00177 {
00178     std::ostringstream szStream;
00179 
00180     // Set the begin tag for the argument element.
00181     szStream << "<" << szArgName << " xsi:type=\"xsd:int\">" << value;
00182        
00183 
00184     // Set the end tag for the argument element.
00185     szStream << "</" << szArgName << ">" << std::endl;
00186     m_encodedValue += szStream.str();
00187     return szStream.str();
00188 }
00189 
00190 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const __int64& value )
00191 {
00192     std::ostringstream szStream;
00193 
00194     // Set the begin tag for the argument element.
00195 #ifndef _MSC_VER
00196         szStream << "<" << szArgName << " xsi:type=\"xsd:long\">" << value;
00197 #else   // TODO:: see if this is really required
00198     wchar_t buffer[10];
00199     memset( buffer, 0, 10 );
00200     _i64tow( value, buffer, 10 );
00201     szStream << "<" << szArgName << " xsi:type=\"xsd:long\">" << buffer;
00202 #endif       
00203 
00204     // Set the end tag for the argument element.
00205     szStream << "</" << szArgName << ">" << std::endl;
00206     m_encodedValue += szStream.str();
00207     return szStream.str();
00208 }
00209 
00210 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const short& value )
00211 {
00212     std::ostringstream szStream;
00213 
00214     // Set the begin tag for the argument element.
00215     szStream << "<" << szArgName << " xsi:type=\"xsd:short\">" << value;
00216        
00217 
00218     // Set the end tag for the argument element.
00219     szStream << "</" << szArgName << ">" << std::endl;
00220     m_encodedValue += szStream.str();
00221     return szStream.str();
00222 }
00223 
00224 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const char& value )
00225 {
00226     std::ostringstream szStream;
00227 
00228     // Set the begin tag for the argument element.
00229     szStream << "<" << szArgName << " xsi:type=\"xsd:byte\">" << value;
00230        
00231 
00232     // Set the end tag for the argument element.
00233     szStream << "</" << szArgName << ">" << std::endl;
00234     m_encodedValue += szStream.str();
00235     return szStream.str();
00236 }
00237 
00238 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const unsigned short& value )
00239 {
00240     std::ostringstream szStream;
00241 
00242     // Set the begin tag for the argument element.
00243     szStream << "<" << szArgName << " xsi:type=\"xsd:unsignedShort\">" << value;
00244        
00245 
00246     // Set the end tag for the argument element.
00247     szStream << "</" << szArgName << ">" << std::endl;
00248     m_encodedValue += szStream.str();
00249     return szStream.str();
00250 }
00251 
00252 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const unsigned __int64& value )
00253 {
00254     std::ostringstream szStream;
00255 
00256     // Set the begin tag for the argument element.
00257 #ifndef _MSC_VER
00258         szStream << "<" << szArgName << " xsi:type=\"xsd:unsignedLong\">" << value;
00259 #else   // TODO:: see if this is really required
00260     wchar_t buffer[10];
00261     memset( buffer, 0, 10 );
00262     _ui64tow( value, buffer, 10 );
00263     szStream << "<" << szArgName << " xsi:type=\"xsd:unsignedLong\">" << buffer;
00264 #endif       
00265 
00266     // Set the end tag for the argument element.
00267     szStream << "</" << szArgName << ">" << std::endl;
00268     m_encodedValue += szStream.str();
00269     return szStream.str();
00270 }
00271 
00272 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const unsigned int& value )
00273 {
00274     std::ostringstream szStream;
00275 
00276     // Set the begin tag for the argument element.
00277     szStream << "<" << szArgName << " xsi:type=\"xsd:unsignedInt\">" << value;
00278        
00279 
00280     // Set the end tag for the argument element.
00281     szStream << "</" << szArgName << ">" << std::endl;
00282     m_encodedValue += szStream.str();
00283     return szStream.str();
00284 }
00285 
00286 std::string SOAPEncoder::encodeArgument( const std::string& szArgName,
00287     const unsigned char& value )
00288 {
00289     std::ostringstream szStream;
00290 
00291     // Set the begin tag for the argument element.
00292     szStream << "<" << szArgName << " xsi:type=\"xsd:unsignedByte\">" 
00293         << value;
00294 
00295     // Set the end tag for the argument element.
00296     szStream << "</" << szArgName << ">" << std::endl;
00297     m_encodedValue += szStream.str();
00298     return szStream.str();
00299 }
00300 
00301 std::string SOAPEncoder::encodeArgument( const std::string& szArgName,
00302     const float& value )
00303 {
00304     std::ostringstream szStream;
00305 
00306     // Set the begin tag for the argument element.
00307     szStream << "<" << szArgName << " xsi:type=\"xsd:float\">" << value;
00308        
00309 
00310     // Set the end tag for the argument element.
00311     szStream << "</" << szArgName << ">" << std::endl;
00312     m_encodedValue += szStream.str();
00313     return szStream.str();
00314 }
00315 
00316 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const double& value )
00317 {
00318     std::ostringstream szStream;
00319 
00320     // Set the begin tag for the argument element.
00321     szStream << "<" << szArgName << " xsi:type=\"xsd:double\">" << value;
00322        
00323 
00324     // Set the end tag for the argument element.
00325     szStream << "</" << szArgName << ">" << std::endl;
00326     m_encodedValue += szStream.str();
00327     return szStream.str();
00328 }
00329 
00330 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, const bool& value )
00331 {
00332     std::ostringstream szStream;
00333 
00334     // Set the begin tag for the argument element.
00335     szStream << "<" << szArgName << " xsi:type=\"xsd:boolean\">" << value ? 1 : 0;
00336        
00337 
00338     // Set the end tag for the argument element.
00339     szStream << "</" << szArgName << ">" << std::endl;
00340     m_encodedValue += szStream.str();
00341     return szStream.str();
00342 }
00343 
00344 std::string SOAPEncoder::encodeBase64  ( const std::string& szArgName, void* value, unsigned long ulSizeofValue )
00345 {
00346     std::ostringstream szStream;
00347 
00348     // Set the begin tag for the argument element.
00349     szStream << "<" << szArgName << " xsi:type=\"xsd:binary\">" << Base64Encoder().encodeValue( value, ulSizeofValue );
00350     
00351 
00352     // Set the end tag for the argument element.
00353     szStream << "</" << szArgName << ">" << std::endl;
00354     m_encodedValue += szStream.str();
00355     return szStream.str();
00356 }
00357 
00358 std::string SOAPEncoder::envelopeOpen()
00359 {
00360     std::string retval ( "<SOAP-ENV:Envelope\n"
00361         " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
00362         " xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\"\n"
00363         " xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\"\n"        
00364         " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" );
00365    
00366     return retval;
00367 }
00368 
00369 std::string SOAPEncoder::envelopeClose()
00370 {
00371     std::string retval( "</SOAP-ENV:Envelope>\n" );
00372     return retval;
00373 }
00374 
00375 std::string SOAPEncoder::headerOpen()
00376 {
00377     std::string retval( "<SOAP-ENV:Header\n" );
00378     return retval;
00379 }
00380 
00381 std::string SOAPEncoder::headerContents()
00382 {
00383     std::string retval;
00384     
00385     return retval;
00386 }
00387 
00388 std::string SOAPEncoder::headerClose()
00389 {
00390     std::string retval( "</SOAP-ENV:Header\n" );
00391     return retval;
00392 }
00393 
00394 std::string SOAPEncoder::bodyOpen()
00395 {
00396     std::string retval( "<SOAP-ENV:Body>\n" );
00397    
00398     return retval;
00399 }
00400 
00401 std::string SOAPEncoder::bodyClose()
00402 {
00403     std::string retval( "</SOAP-ENV:Body>\n" );
00404     return retval;
00405 }
00406 
00407 std::string SOAPEncoder::faultOpen()
00408 {
00409     std::string retval( "<SOAP-ENV:Fault>\n" );
00410     return retval;
00411 }
00412 
00413 std::string SOAPEncoder::faultClose()
00414 {
00415     std::string retval( "</SOAP-ENV:Fault>\n" );
00416     return retval;
00417 }
00418 
00419 std::string SOAPEncoder::clientFaultClass()
00420 {
00421     std::string retval( "Client" );
00422     return retval;
00423 }
00424 
00425 std::string SOAPEncoder::serverFaultClass()
00426 {
00427     std::string retval( "Server" );
00428     return retval;
00429 }
00430 
00431 std::string SOAPEncoder::versionMismatchFaultClass()
00432 {
00433     std::string retval( "VersionMismatch" );
00434     return retval;
00435 }
00436 
00437 std::string SOAPEncoder::mustUnderstandFaultClass()
00438 {
00439     std::string retval( "MustUnderstand" );
00440     return retval;
00441 }
00442 
00443 std::string SOAPEncoder::addBeginTag( const std::string& szValue )
00444 {
00445     std::ostringstream szStream;
00446 
00447     // Set the begin tag for the element.
00448     szStream << "<" << szValue << ">" << std::endl;
00449 
00450     m_encodedValue += szStream.str();
00451     return szStream.str();
00452 }
00453 
00454 std::string SOAPEncoder::addEndTag( const std::string& szValue )
00455 {
00456     std::ostringstream szStream;
00457 
00458     // Set the end tag for the element.
00459     szStream << "</" << szValue << ">" << std::endl;
00460 
00461     m_encodedValue += szStream.str();
00462     return szStream.str();
00463 }
00464 
00465 void SOAPEncoder::encodeFaultContent( SOAPFault& soapFault, std::ostringstream &szStream )
00466 {
00467     // Set the begin tag for the Fault.
00468     szStream << faultOpen();
00469 
00470     // Encode the faultcode
00471     szStream << faultcodeOpen() << soapFault.getFaultCode()
00472         << faultcodeClose() << std::endl;
00473 
00474     // Encode the faultstring
00475     szStream << faultstringOpen() << soapFault.faultString()
00476         << faultstringClose() << std::endl;
00477 
00478     // Only encode the faultactor if it is filled in.
00479     if ( soapFault.faultActor().length() > 0 )
00480     {
00481         szStream << faultactorOpen() << soapFault.faultActor()
00482             << faultactorClose() << std::endl;
00483     }
00484 
00485     // Only encode the detail if it is filled in.
00486     if ( soapFault.detail().length() > 0 )
00487     {
00488         szStream << faultdetailOpen() << soapFault.detail()
00489             << faultdetailClose() << std::endl;
00490     }
00491 
00492         // close off the fault
00493     szStream << faultClose();
00494 }
00495 std::string SOAPEncoder::encodeFault( SOAPFault& soapFault )
00496 {
00497     std::ostringstream szStream;
00498     // Initialize the envelope.
00499     szStream << envelopeOpen();
00500 
00501     szStream << bodyOpen();
00502 
00503     // Encode the fault
00504         encodeFaultContent( soapFault, szStream );
00505 
00506         // Close the body.
00507     szStream << bodyClose();
00508 
00509     // Close the envelope.
00510     szStream << envelopeClose();
00511     m_encodedValue = szStream.str();
00512     return m_encodedValue;
00513 }
00514 
00515 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, LongArray& value )
00516 {
00517     std::ostringstream szStream;
00518 
00519     // Set the begin tag for the argument element.
00520     szStream << "<" << szArgName << " SOAP-ENC:arrayType=\"xsi:int[" << value.size() << "]\">" << std::endl;
00521        
00522         for (LongArray::iterator i = value.begin(); i != value.end(); ++i) {
00523                 szStream << "<SOAP-ENC:int>" << *i << "</SOAP-ENC:int>" << std::endl;
00524         }
00525         
00526         szStream << "</" << szArgName << ">" << std::endl;
00527 
00528     m_encodedValue += szStream.str();
00529     return szStream.str();
00530 }
00531 
00532 std::string SOAPEncoder::encodeArgument( const std::string& szArgName, StringArray& value )
00533 {
00534     std::ostringstream szStream;
00535 
00536     // Set the begin tag for the argument element.
00537     szStream << "<" << szArgName << " SOAP-ENC:arrayType=\"xsd:string[" << value.size() << "]\">" << std::endl;
00538        
00539         for (StringArray::iterator i = value.begin(); i != value.end(); ++i) {
00540                 szStream << "<SOAP-ENC:string>" << *i << "</SOAP-ENC:string>" << std::endl;
00541         }
00542         
00543         szStream << "</" << szArgName << ">" << std::endl;
00544 
00545     m_encodedValue += szStream.str();
00546     return szStream.str();
00547 }
00548 
00549 std::string SOAPEncoder::faultcodeOpen()
00550 {
00551     return "<SOAP-ENV:faultcode>";
00552 }
00553 
00554 std::string SOAPEncoder::faultcodeClose()
00555 {
00556     return "</SOAP-ENV:faultcode>";
00557 }
00558 
00559 std::string SOAPEncoder::faultstringOpen()
00560 {
00561     return "<SOAP-ENV:faultstring>";
00562 }
00563 
00564 std::string SOAPEncoder::faultstringClose()
00565 {
00566     return "</SOAP-ENV:faultstring>";
00567 }
00568 
00569 std::string SOAPEncoder::faultactorOpen()
00570 {
00571     return "<SOAP-ENV:faultactor>";
00572 }
00573 
00574 std::string SOAPEncoder::faultactorClose()
00575 {
00576     return "</SOAP-ENV:faultactor>";
00577 }
00578 
00579 std::string SOAPEncoder::faultdetailOpen()
00580 {
00581     return "<SOAP-ENV:detail>";
00582 }
00583 
00584 std::string SOAPEncoder::faultdetailClose()
00585 {
00586     return "</SOAP-ENV:detail>";
00587 }
00588 

Generated on Tue Mar 28 09:10:14 2006 for Simple SOAP by  doxygen 1.4.6