webservices/wsrestplugin/src/senrestservicesession.cpp
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:           
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 
       
    28 
       
    29 // INCLUDE FILES
       
    30 #include "senrestservicesession.h"
       
    31 #include "senservicesession.h"
       
    32 #include "SenSoapMessage.h"
       
    33 #include "senwsdescription.h"
       
    34 #include "msencoreservicemanager.h"
       
    35 #include "msenremoteserviceconsumer.h"
       
    36 #include "SenXmlUtils.h"
       
    37 #include "SenDateUtils.h"
       
    38 #include "msentransport.h"
       
    39 #include "SenServiceConnection.h"
       
    40 #include "sendebug.h"
       
    41 #include "senlogger.h"
       
    42 // #include "SenHttpTransportProperties.h"
       
    43 
       
    44 CSenRestServiceSession* CSenRestServiceSession::NewL(MSIF& aFramework)
       
    45     {
       
    46     CSenRestServiceSession* pNew =
       
    47         CSenRestServiceSession::NewLC(aFramework);
       
    48     CleanupStack::Pop();
       
    49     return pNew;
       
    50     }
       
    51 
       
    52 CSenRestServiceSession* CSenRestServiceSession::NewLC(MSIF& aFramework)
       
    53     {
       
    54     CSenRestServiceSession* pNew =
       
    55         new (ELeave) CSenRestServiceSession(ERestServiceSession,
       
    56                                                 aFramework);
       
    57     CleanupStack::PushL(pNew);
       
    58     pNew->ConstructL();
       
    59     return pNew;
       
    60     }
       
    61 
       
    62 CSenRestServiceSession::CSenRestServiceSession(TDescriptionClassType aType, MSIF& aFramework)
       
    63 : CSenWebServiceSession(aType, aFramework)
       
    64     {
       
    65     }
       
    66 
       
    67 void CSenRestServiceSession::ConstructL()
       
    68     {
       
    69     // sets the local name to "ServiceDescription" and
       
    70     // initiates the inner ipElement
       
    71     CSenWebServiceSession::ConstructL();
       
    72     }
       
    73 
       
    74 CSenRestServiceSession::~CSenRestServiceSession()
       
    75     {
       
    76     }
       
    77 
       
    78 // Override compute status to make REST sessions to be ALWAYS valid (ready)
       
    79 TInt CSenRestServiceSession::ComputeStatusL()
       
    80     {
       
    81     // session is ready, if an endpoint is defined
       
    82     return CSenServiceSession::ComputeStatusL();
       
    83     }
       
    84 
       
    85 TInt CSenRestServiceSession::SendSoapL( const TDesC8& aSoapMessage, 
       
    86                                         const TDesC8& aTransportProperties,
       
    87                                         MSenRemoteServiceConsumer& aConsumer,
       
    88                                         TInt& aTxnId,
       
    89                                         HBufC8*& /*aRevalidationError*/ ) 
       
    90     {
       
    91     CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"CSenRestServiceSession::SendSoapL");
       
    92     TPtrC8 endpoint = Endpoint();
       
    93 #ifdef _SENDEBUG
       
    94     CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"///////////////////////////////////////////////////////");
       
    95     CSLOG_FORMAT((aConsumer.ConnectionId()   , KMinLogLevel, _L8("- Endpoint: %S"), &endpoint));
       
    96     CSLOG_FORMAT((aConsumer.ConnectionId()   , KMaxLogLevel, _L8("- Request (%d bytes):"), aSoapMessage.Length()));
       
    97     CSLOG_ALL(aConsumer.ConnectionId()  , KMaxLogLevel,(aSoapMessage));
       
    98     CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"///////////////////////////////////////////////////////");
       
    99 #endif
       
   100 
       
   101 
       
   102 
       
   103     TInt retVal( aConsumer.TransportL().SendL( endpoint, 
       
   104                                                aSoapMessage,
       
   105                                                aTransportProperties, 
       
   106                                                *this, 
       
   107                                                aConsumer, 
       
   108                                                aTxnId) );
       
   109     CSLOG_FORMAT((aConsumer.ConnectionId()   , KNormalLogLevel, _L8("- Transaction ID: %d"), aTxnId));
       
   110     return retVal;
       
   111     }
       
   112 
       
   113 TInt CSenRestServiceSession::SubmitSoapL(const TDesC8& aSoapMessage, 
       
   114                                          const TDesC8& aTransportProperties,
       
   115                                          MSenRemoteServiceConsumer& aConsumer,
       
   116                                          HBufC8*& aResponse)
       
   117     {
       
   118     CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"CSenRestServiceSession::SubmitSoapL");
       
   119     TPtrC8 endpoint = Endpoint();
       
   120 #ifdef _SENDEBUG
       
   121     CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"///////////////////////////////////////////////////////");
       
   122     CSLOG_FORMAT((aConsumer.ConnectionId()   , KMinLogLevel, _L8("- Endpoint: %S"), &endpoint));
       
   123     CSLOG_FORMAT((aConsumer.ConnectionId()   , KMaxLogLevel, _L8("- Request (%d bytes):"), aSoapMessage.Length()));
       
   124     CSLOG_ALL(aConsumer.ConnectionId()  , KMaxLogLevel,(aSoapMessage));
       
   125    	CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"///////////////////////////////////////////////////////");
       
   126 #endif
       
   127 
       
   128     MSenTransport& transport = aConsumer.TransportL();
       
   129     SetFrameworkPropertiesL(transport);
       
   130     TInt retVal = transport.SubmitL( endpoint,
       
   131                                      aSoapMessage, 
       
   132                                      aTransportProperties, 
       
   133                                      aResponse,
       
   134 									 aConsumer);
       
   135     if ( !aResponse )
       
   136         {
       
   137         // response was NULL: probably either out
       
   138         // of heap or some transport malfunction.
       
   139         return retVal;
       
   140         }
       
   141     else if ( aResponse->Length() < KSenSoapEnvelopeName().Length()*2 )
       
   142         {
       
   143         // no use parsing, Envelope -root element not there
       
   144         // deliver non-soap body to consumer
       
   145         return retVal;
       
   146         }
       
   147 
       
   148     CleanupStack::PushL(aResponse);
       
   149 
       
   150     CSenSoapMessage* pResponseSoapMsg = NULL;
       
   151     TInt leaveCode(KErrNone);
       
   152 
       
   153     // attempt to parse the response
       
   154     // here. SOAP faults are to be
       
   155     // searched after,
       
   156 
       
   157     TInt parseRetCode(KErrNone);
       
   158     TRAP( leaveCode, (parseRetCode =
       
   159         ParseResponseL(*aResponse, pResponseSoapMsg)) );
       
   160 
       
   161     if(leaveCode!=KErrNone)
       
   162         {
       
   163         // THE RESPONSE could not be parsed
       
   164         delete pResponseSoapMsg;
       
   165 
       
   166         // THE RESPONSE IS NOT SOAP
       
   167         if(retVal==KErrNone)
       
   168             {
       
   169             // indicate with return value, that response is
       
   170             // invalid - even though submit was ok, the
       
   171             // response could NOT be parsed!
       
   172             retVal = leaveCode;
       
   173             }
       
   174         CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"(NOK) Response is not a SOAP message");
       
   175         }
       
   176     else
       
   177         {
       
   178 
       
   179         if (parseRetCode != KErrNone)
       
   180             {
       
   181             CSLOG_FORMAT((aConsumer.ConnectionId()   , KMinLogLevel, _L8("(NOK) Parsing SOAP msg failed (%d)"),
       
   182                     parseRetCode));
       
   183 
       
   184             if(retVal==KErrNone) // submit was ok
       
   185                 {
       
   186                 // indicate with return value, that response is
       
   187                 // invalid - even though submit was ok, the
       
   188                 // response could NOT be parsed!
       
   189                 retVal = parseRetCode;
       
   190                 }
       
   191 
       
   192             // THE RESPONSE could not be parsed
       
   193             delete pResponseSoapMsg;
       
   194             }
       
   195         else
       
   196             {
       
   197             CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"REST: (OK) Response is a SOAP message");
       
   198             CleanupStack::PushL(pResponseSoapMsg);
       
   199 
       
   200             TBool completeServerMessages(ETrue);
       
   201             HasFacetL(KCompleteMessagesFacet, completeServerMessages);
       
   202 
       
   203             // response is OK and in SOAP form.
       
   204 			if(pResponseSoapMsg)
       
   205 			   {
       
   206 			   if (pResponseSoapMsg->IsFault())
       
   207                    {
       
   208                 	// response is SOAP fault
       
   209                 	CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"REST: Response is a SOAP fault.");
       
   210                 	retVal = KErrSenSoapFault;
       
   211 
       
   212                 if (!completeServerMessages)
       
   213                     {
       
   214                     // try to detach the SOAP fault
       
   215                     HBufC8* pDetached = NULL;
       
   216                     CleanupStack::Pop(pResponseSoapMsg);
       
   217                     retVal = CSenWebServiceSession::HandleSoapFaultL(pResponseSoapMsg, pDetached);
       
   218                     if(retVal == KErrSenSoapFault || retVal == KErrNone)
       
   219                         {
       
   220                         // KErrNone means that original fault was handled
       
   221                         // properly and we now have the wanted result 
       
   222                         // and consumer may receive it (message body)
       
   223 
       
   224                         // Note: currently we don't handle any SOAP faults
       
   225                         // in WebServiceSession class, so KErrNone should
       
   226                         // not be returned. The OR clause (||) here is 
       
   227                         // only for future extensions.
       
   228                         delete aResponse;
       
   229                         aResponse = NULL;
       
   230                         aResponse = pDetached;
       
   231                         pDetached = NULL;
       
   232                         }
       
   233                     else if(retVal == KErrNotFound)
       
   234                         {
       
   235                         // SOAP fault element  could not be found
       
   236                         CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"REST: Major; SOAP fault element not found \
       
   237                                     even though assumed SOAP fault.");
       
   238                         retVal = KErrSenInternal;
       
   239                         }
       
   240                     }
       
   241                 else
       
   242                     {
       
   243                     CleanupStack::PopAndDestroy(pResponseSoapMsg);
       
   244                     }
       
   245                 }
       
   246             else
       
   247                 {
       
   248                 // Check complete server messages on/off
       
   249                 if (!completeServerMessages)
       
   250                     {
       
   251                     HBufC8* pBody = pResponseSoapMsg->BodyAsStringL();
       
   252                     if(pBody)
       
   253                         {
       
   254                         delete aResponse;
       
   255                         aResponse = NULL;
       
   256                         aResponse = pBody;
       
   257                         pBody = NULL;
       
   258                         CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"- Detached SOAP message body.");
       
   259                         }
       
   260                     }
       
   261                 CleanupStack::PopAndDestroy(pResponseSoapMsg);
       
   262 				}
       
   263               }
       
   264             }
       
   265         }
       
   266 
       
   267 #ifdef _SENDEBUG
       
   268     if(aResponse)
       
   269         {
       
   270         CleanupStack::PushL(aResponse);
       
   271         CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"--------------------------------------------------");
       
   272         CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"CSenRestServiceSession::SubmitSoapL:");
       
   273         CSLOG_FORMAT((aConsumer.ConnectionId()   , KMaxLogLevel, _L8("- Response (%d bytes):"), 
       
   274             aResponse->Length()));
       
   275         CSLOG_ALL(aConsumer.ConnectionId()  , KMaxLogLevel,(*aResponse));
       
   276         CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"--------------------------------------------------");
       
   277         CleanupStack::Pop(); // aResponse
       
   278         }
       
   279 #endif
       
   280     CleanupStack::Pop(); // aResponse
       
   281     return retVal;
       
   282     }
       
   283 
       
   284 TInt CSenRestServiceSession::CompleteServerMessagesOnOffL(const TBool& aCompleteOnOff)
       
   285     {
       
   286     // Now REST SIF supports CompleteServerMessagesOnOff
       
   287     return CSenServiceSession::CompleteServerMessagesOnOffL(aCompleteOnOff);
       
   288     }
       
   289 
       
   290 
       
   291 TInt CSenRestServiceSession::SubmitL( const TDesC8& aMessage, 
       
   292                                       const TDesC8& aTransportProperties,
       
   293                                       MSenRemoteServiceConsumer& aConsumer,
       
   294                                       HBufC8*& aResponse )
       
   295     {
       
   296     CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"CSenRestServiceSession::SubmitL(TDesC8&)");
       
   297     TPtrC8 endpoint = Endpoint();
       
   298 #ifdef _SENDEBUG
       
   299     CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"///////////////////////////////////////////////////////");
       
   300     CSLOG_FORMAT((aConsumer.ConnectionId()   , KMaxLogLevel, _L8("- Endpoint: %S"), &endpoint));
       
   301     CSLOG_FORMAT((aConsumer.ConnectionId()   , KMaxLogLevel, _L8("- Request (%d bytes):"), aMessage.Length()));
       
   302     CSLOG_ALL(aConsumer.ConnectionId()  , KMaxLogLevel,(aMessage));
       
   303     CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"///////////////////////////////////////////////////////");
       
   304 #endif
       
   305 
       
   306     MSenTransport& transport = aConsumer.TransportL();
       
   307     SetFrameworkPropertiesL( transport );
       
   308     TInt retVal( transport.SubmitL( endpoint, 
       
   309                                     aMessage, 
       
   310                                     aTransportProperties, 
       
   311                                     aResponse,
       
   312 									aConsumer) );
       
   313 
       
   314 #ifdef _SENDEBUG
       
   315     if(aResponse)
       
   316         {
       
   317         CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"---------------------------------------------------");
       
   318         CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"CSenRestServiceSession::SubmitL:");
       
   319         CSLOG_FORMAT((aConsumer.ConnectionId()   , KMaxLogLevel, _L8("- Response (%d bytes):"), aResponse->Length()));
       
   320         CSLOG_ALL(aConsumer.ConnectionId()  , KMaxLogLevel,( *aResponse ));
       
   321         CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"---------------------------------------------------");
       
   322         }
       
   323 #endif
       
   324 
       
   325     return retVal;
       
   326     }
       
   327 
       
   328 
       
   329 TInt CSenRestServiceSession::SendL( const TDesC8& aMessage, 
       
   330                                     const TDesC8& aTransportProperties,
       
   331                                     MSenRemoteServiceConsumer& aConsumer,
       
   332                                     TInt& aTxnId,
       
   333                                     HBufC8*& /* aRevalidationError*/ )
       
   334     {
       
   335     // One could call the base class implementation in CSenServiceSession as well...
       
   336     CSLOG_L(aConsumer.ConnectionId()  , KMinLogLevel,"CSenRestServiceSession::SendL");
       
   337     TPtrC8 endpoint = Endpoint();
       
   338 #ifdef _SENDEBUG
       
   339     CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"///////////////////////////////////////////////////////");
       
   340     CSLOG_FORMAT((aConsumer.ConnectionId()   , KMaxLogLevel, _L8("- Endpoint: %S"), &endpoint));
       
   341     CSLOG_FORMAT((aConsumer.ConnectionId()   , KMaxLogLevel, _L8("- Request (%d bytes):"), aMessage.Length()));
       
   342     CSLOG_ALL(aConsumer.ConnectionId()  , KMaxLogLevel,( aMessage ));
       
   343     CSLOG_L(aConsumer.ConnectionId()  , KMaxLogLevel,"///////////////////////////////////////////////////////");
       
   344 #endif
       
   345     
       
   346     MSenTransport& transport = aConsumer.TransportL();
       
   347     SetFrameworkPropertiesL(transport);
       
   348 
       
   349     TInt retVal ( transport.SendL( endpoint, 
       
   350                                    aMessage,
       
   351                                    aTransportProperties,
       
   352                                    *this, 
       
   353                                    aConsumer, 
       
   354                                    aTxnId) );
       
   355     CSLOG_FORMAT((aConsumer.ConnectionId()   , KNormalLogLevel, _L8("- Transaction id: %d"), aTxnId));
       
   356     return retVal;
       
   357     }
       
   358 
       
   359 
       
   360 
       
   361 
       
   362 TInt CSenRestServiceSession::SendToConsumerL(HBufC8* apMessage,
       
   363                                              const TInt aTxnId,
       
   364                                              MSenRemoteServiceConsumer& aConsumer,
       
   365                                              MSenProperties* aResponseTransportProperties)
       
   366     {
       
   367     TInt retVal(KErrNone);
       
   368 
       
   369     aConsumer.HandleMessageL(apMessage, aTxnId, aResponseTransportProperties);
       
   370 
       
   371     return retVal;
       
   372     }
       
   373 
       
   374 // THIS IS WHAT TRANSPORT SEES:
       
   375 TInt CSenRestServiceSession::SendErrorToConsumerL(const TInt aErrorCode,
       
   376                                                   HBufC8* apError,
       
   377                                                   const TInt aTxnId,
       
   378                                                   MSenRemoteServiceConsumer& aConsumer,
       
   379                                                   MSenProperties* aResponseTransportProperties)
       
   380     {
       
   381     TInt retVal(KErrNone);
       
   382 
       
   383     retVal = HandleErrorL(aErrorCode, apError, aTxnId, aConsumer, aResponseTransportProperties);
       
   384     return retVal;
       
   385     }
       
   386 
       
   387 TInt CSenRestServiceSession::HandleErrorL(const TInt aErrorCode,
       
   388                                           HBufC8* apError,
       
   389                                           const TInt aTxnId,
       
   390                                           MSenRemoteServiceConsumer& aConsumer,
       
   391                                           MSenProperties* aResponseTransportProperties)
       
   392     {
       
   393     TInt retVal(KErrNone);
       
   394     // Implementation does not handle non-SOAP errors.
       
   395     retVal = aConsumer.HandleErrorL(apError, aErrorCode, aTxnId, aResponseTransportProperties); 
       
   396     return retVal;
       
   397     }
       
   398 
       
   399 TBool CSenRestServiceSession::HasSuperClass( TDescriptionClassType aType )
       
   400     {
       
   401     if( aType == MSenServiceDescription::EWebServiceSession ) // direct superclass!
       
   402         {
       
   403         // If asked type is the know *direct* father/mother, return true:
       
   404         return ETrue; 
       
   405         } 
       
   406     else
       
   407         {
       
   408         // Otherwise, ask from superclass (chain, recursively)
       
   409         return CSenWebServiceSession::HasSuperClass( aType ); 
       
   410         }
       
   411     }
       
   412 
       
   413 // TEMPORARY: remove when "message context"- and "transport factory taking 
       
   414 // properties from all layers"- tasks are ready
       
   415 TInt CSenRestServiceSession::SetFrameworkPropertiesL(MSenTransport& aTransport)
       
   416     {
       
   417     return iFramework.SetTransportPropertiesL(aTransport);
       
   418     }
       
   419 //  End of File