webservices/wsoviplugin/src/wsoviservicesession.cpp
changeset 0 62f9d29f7211
child 1 272b002df977
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2008 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 // INCLUDE
       
    22 #include "wsoviservicesession.h"
       
    23 #include "sendebug.h"
       
    24 #include "senlogger.h"
       
    25 #include "senservicemanagerdefines.h"
       
    26 #include "SenServiceConnection.h" // err code definitions
       
    27 #include "senserviceinvocationframework.h"
       
    28 #include "SenHttpTransportProperties.h"
       
    29 #include "SenDateUtils.h"
       
    30 #include "sendebug.h"
       
    31 #include "senwspattern.h"
       
    32 #include "SenXmlUtils.h"
       
    33 #include <xmlengnodelist.h> 
       
    34 #include <SenHttpTransportProperties.h>
       
    35 #include <SenIdentityProvider.h>
       
    36 #include "wsovicredentialobserver.h"
       
    37 #include "sencryptoutils.h"
       
    38 
       
    39 const TInt64 KClockSlipSeconds = 60*15;
       
    40 
       
    41 
       
    42 CWSOviServiceSession* CWSOviServiceSession::NewL(MSIF& aFramework)
       
    43     {
       
    44     CWSOviServiceSession* self = CWSOviServiceSession::NewLC(aFramework);
       
    45     CleanupStack::Pop(self);
       
    46     return self;
       
    47     }
       
    48 
       
    49 CWSOviServiceSession* CWSOviServiceSession::NewLC(MSIF& aFramework)
       
    50     {
       
    51     CWSOviServiceSession* self = new (ELeave) CWSOviServiceSession(
       
    52         MSenServiceDescription::EOviServiceSession, aFramework);
       
    53     CleanupStack::PushL(self);
       
    54     self->ConstructL();
       
    55     return self;
       
    56     }
       
    57 
       
    58 CWSOviServiceSession::CWSOviServiceSession(TDescriptionClassType aType,
       
    59                                            MSIF& aSIF)
       
    60     : CSenWebServiceSession(aType, aSIF)
       
    61     {
       
    62     }
       
    63 
       
    64 CWSOviServiceSession::~CWSOviServiceSession()
       
    65     {
       
    66     delete iSessionContext;
       
    67     delete iProviderID;
       
    68     delete iTrustAnchor;
       
    69     delete iOutContext;
       
    70     delete iInContext;
       
    71     delete iTransProp;
       
    72     delete iOrginalEndpoint;
       
    73     
       
    74     TInt count(iConsumerList.Count());
       
    75     for(TInt i=0; i<count; i++)
       
    76         {
       
    77         // we can assume that every remote consumer
       
    78         // has been wrapped inside WSOviSessionConsumer
       
    79         // -wrapperclass. Cast and destroy:
       
    80         CWSOviSessionConsumer* pConsumer
       
    81             = (CWSOviSessionConsumer*) iConsumerList[i];
       
    82 
       
    83         // this destroys the wrapper, but the remote
       
    84         // consumer objects ownership remains in either
       
    85         // XMLDao or ClientSession (etc)
       
    86         delete pConsumer;
       
    87         }
       
    88     delete iTokenObs;
       
    89     }
       
    90 
       
    91 void CWSOviServiceSession::ConstructL()
       
    92     {
       
    93     CSenWebServiceSession::ConstructL();
       
    94     iTokenObs = CWSOviCredentialObserver::NewL(this,iFramework.Manager().Log());
       
    95     }
       
    96 
       
    97 TInt CWSOviServiceSession::AddConsumerL(MSenRemoteServiceConsumer& aConsumer)
       
    98     {
       
    99     TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId() ,KMinLogLevel ,"CWSOviServiceSession::AddConsumerL");            
       
   100     const TInt consumerCount(iConsumerList.Count());
       
   101     for(TInt i=0; i<consumerCount; i++)
       
   102         {
       
   103         if(iConsumerList[i]->Id() == aConsumer.Id())
       
   104             {
       
   105             return KErrAlreadyExists; // already added, nothing to do
       
   106             }
       
   107         }
       
   108 
       
   109     CWSOviSessionConsumer* pSessionConsumer =
       
   110                                     CWSOviSessionConsumer::NewL(aConsumer);
       
   111 
       
   112     return iConsumerList.Append(pSessionConsumer);
       
   113     }
       
   114 
       
   115 TBool CWSOviServiceSession::ExistConsumerL()
       
   116     {
       
   117     const TInt consumerCount(iConsumerList.Count());
       
   118     if( consumerCount == 0 )
       
   119         {
       
   120         return EFalse;
       
   121         }
       
   122     else
       
   123         {
       
   124         return ETrue;
       
   125         }
       
   126     }
       
   127 
       
   128 TInt CWSOviServiceSession::RemoveConsumerL(MSenRemoteServiceConsumer& aConsumer)
       
   129     {
       
   130     TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId() ,KMinLogLevel ,"CWSOviServiceSession::RemoveConsumerL:");    
       
   131     const TInt consumerCount(iConsumerList.Count());
       
   132     for(TInt i=0; i<consumerCount; i++)
       
   133         {
       
   134         if(iConsumerList[i]->Id() == aConsumer.Id())
       
   135             {
       
   136             CWSOviSessionConsumer* pConsumer
       
   137                             = (CWSOviSessionConsumer*) iConsumerList[i];
       
   138             delete pConsumer;
       
   139             iConsumerList.Remove(i);
       
   140             if( (consumerCount-1) == 0 )
       
   141                 {
       
   142                 iTokenObs->Cancel();
       
   143                 }
       
   144             break;
       
   145             }
       
   146         }
       
   147     // If this session has no consumers, we can hold our grip to the credential
       
   148     // (note: this does NOT mean that credential is removed, not at all(!), but
       
   149     //  that new search for the credential has to be performed).
       
   150     if( consumerCount == 0 )
       
   151         {
       
   152         TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId() ,KNormalLogLevel ,"- Consumer count == 0");
       
   153         TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId() ,KNormalLogLevel ,"-> Closing the handle to the credential owned by the Credential Manager.");
       
   154         SetSecurityL(KNullDesC8); 
       
   155         }           
       
   156     return CSenServiceSession::RemoveConsumerL(aConsumer);
       
   157     }
       
   158 
       
   159 TInt CWSOviServiceSession::MessageForSendingL( CSenHttpTransportProperties*& aTP,
       
   160                                                 const TDesC8& aSenderID)
       
   161     {
       
   162     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::MessageForSendingL");    
       
   163     TInt error(KErrNone);
       
   164     if (AmIHostletSession())
       
   165         {
       
   166         return KErrNotSupported;
       
   167         }
       
   168     else
       
   169         {
       
   170         CWSOviSessionConsumer* pConsumer = NULL;
       
   171         //senderID is internal id of consumer
       
   172         error = SessionConsumerL(aSenderID, pConsumer);
       
   173         if (error)
       
   174             {
       
   175             return error; 
       
   176             }
       
   177         
       
   178         CWSOviSessionContext* ssCtx = (CWSOviSessionContext*)SessionContext();
       
   179         
       
   180         iOutContext->UpdateFromSessionContextL(*ssCtx);
       
   181         iOutContext->UpdateTP((CSenTransportProperties*&)aTP);
       
   182         AdaptEndpointL(EFalse);
       
   183         iOutContext->Update(OAuth::KEndpointKey,Endpoint());
       
   184         AdaptEndpointL(ETrue);
       
   185 
       
   186         error = ((CWSOviPlugin&)iFramework).ProcessOutboundMessageL(
       
   187                 iOutContext);
       
   188         
       
   189         iSessionContext->UpdateFromMessageOutContextL(*iOutContext);
       
   190         error = pConsumer->MessageForSendingL(aTP, iOutContext);
       
   191         }
       
   192     return error;
       
   193     }
       
   194 
       
   195 
       
   196 
       
   197 
       
   198 MSenRemoteServiceConsumer* CWSOviServiceSession::RemoteConsumerL( const TDesC8& aSenderID )
       
   199     {
       
   200     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::RemoteConsumerL");        
       
   201     TInt consumersCount(iConsumerList.Count());
       
   202 
       
   203     for(TInt i=0; i<consumersCount; i++)
       
   204         {
       
   205         if(iConsumerList[i]->Id() == aSenderID)
       
   206             {
       
   207             return iConsumerList[i];
       
   208             }
       
   209         }
       
   210     return NULL; // not found
       
   211     }
       
   212 
       
   213 //---------------------------------------------------------------------------
       
   214 // To obtain consumer during processing INBOUND.
       
   215 //---------------------------------------------------------------------------
       
   216 //   
       
   217 
       
   218 TInt CWSOviServiceSession::SessionConsumerL( CWSOviMessageContext*& /*aMsgCtx*/,
       
   219                                               CWSOviSessionConsumer*& aSessionConsumer )
       
   220     {
       
   221     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::SessionConsumerL");                
       
   222     TInt consumersCount(iConsumerList.Count());
       
   223     TInt error(KErrNotFound);
       
   224     if (error && consumersCount)
       
   225         {
       
   226         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KNormalLogLevel,"CWSOviServiceSession::SessionConsumerL - consumer was not found - using the 1st one (as default)");
       
   227         aSessionConsumer = (CWSOviSessionConsumer*)iConsumerList[0];
       
   228         error = KErrNone;
       
   229         }
       
   230     return error;
       
   231     }
       
   232 
       
   233 //---------------------------------------------------------------------------
       
   234 // To obtain consumer during processing OUTBOUND.
       
   235 //---------------------------------------------------------------------------
       
   236 //   
       
   237         
       
   238 TInt CWSOviServiceSession::SessionConsumerL( const TDesC8& aSenderID,
       
   239                                               CWSOviSessionConsumer*& aSessionConsumer )
       
   240     {
       
   241     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::SessionConsumerL");                        
       
   242     aSessionConsumer = (CWSOviSessionConsumer*) RemoteConsumerL(aSenderID);
       
   243     if(aSessionConsumer)
       
   244         {
       
   245         return KErrNone;
       
   246         }
       
   247     else
       
   248         {
       
   249         return KErrNotFound;
       
   250         }
       
   251     }
       
   252 
       
   253 TBool CWSOviServiceSession::Matches(MSenServiceDescription& aPattern)
       
   254     {
       
   255     TBool matches = CSenWebServiceSession::Matches(aPattern);
       
   256     if (!matches) return EFalse;
       
   257     
       
   258     MSenElement& xmlPatternAsElement = ((CSenWSDescription&)aPattern).AsElement();
       
   259     MSenElement* pElement = xmlPatternAsElement.Element( KSenIdpProviderIdLocalname );
       
   260     if(pElement)
       
   261         {
       
   262         TPtrC8 patternProviderID = pElement->Content();
       
   263         TPtrC8 thisProviderID = ProviderID();
       
   264         if(patternProviderID.Length()>0)
       
   265             {
       
   266             if(!(thisProviderID.Length()>0 && patternProviderID == thisProviderID))
       
   267                 {
       
   268                 return EFalse;
       
   269                 }
       
   270             }
       
   271         }
       
   272         
       
   273     if (aPattern.DescriptionClassType() == MSenServiceDescription::EWSDescription)
       
   274         {
       
   275         TPtrC8 patternTransportCue = ((CSenWSDescription&)aPattern).TransportCue();
       
   276         if (patternTransportCue.Length() > 0)
       
   277             {
       
   278             if(!iTransportCue || (patternTransportCue != *iTransportCue))
       
   279                 {
       
   280                 return EFalse;
       
   281                 }
       
   282             }
       
   283         }
       
   284     return matches;
       
   285     }
       
   286 
       
   287 TInt CWSOviServiceSession::ScoreMatchL(MSenServiceDescription& aPattern)
       
   288     {
       
   289     TInt score = CSenWebServiceSession::ScoreMatchL(aPattern);
       
   290     
       
   291     MSenElement& xmlPatternAsElement = ((CSenWSDescription&)aPattern).AsElement();
       
   292     MSenElement* pElement = xmlPatternAsElement.Element(KSenIdpProviderIdLocalname);
       
   293     if(pElement)
       
   294         {
       
   295         TPtrC8 patternProviderID = pElement->Content();
       
   296         TPtrC8 thisProviderID = ProviderID();
       
   297         if(patternProviderID.Length()>0)
       
   298             {
       
   299             if ( thisProviderID.Length()>0 && patternProviderID == thisProviderID )
       
   300                 {
       
   301                 score++;
       
   302                 }
       
   303             }
       
   304         }
       
   305         
       
   306     if (aPattern.DescriptionClassType() == MSenServiceDescription::EWSDescription)
       
   307         {
       
   308         TPtrC8 patternTransportCue = ((CSenWSDescription&)aPattern).TransportCue();
       
   309         if (patternTransportCue.Length() > 0)
       
   310             {
       
   311             if ( iTransportCue && (patternTransportCue == *iTransportCue) )
       
   312                 {
       
   313                 score++;
       
   314                 }
       
   315             }
       
   316         }
       
   317         
       
   318     return score;
       
   319     }
       
   320 
       
   321 TInt CWSOviServiceSession::SetTransportPropertiesL(const TDesC8& aProperties,
       
   322                                              MSenRemoteServiceConsumer& aConsumer)
       
   323     {
       
   324         if(iTransProp)
       
   325             {
       
   326             delete iTransProp;
       
   327             iTransProp = NULL;
       
   328             }
       
   329         iTransProp = HBufC8::NewL(aProperties.Length());
       
   330         TPtr8 ptr = iTransProp->Des();
       
   331         ptr.Append(aProperties);
       
   332         CWSOviSessionConsumer* pConsumer = NULL;
       
   333         //senderID is internal id of consumer
       
   334         TInt error = SessionConsumerL(aConsumer.Id(), pConsumer);
       
   335         if (!error && pConsumer)
       
   336             {
       
   337             MSenTransport& tp = pConsumer->TransportL();
       
   338             CSenHttpTransportProperties* transProp = ApplyTransportPropertiesLC(aProperties);
       
   339             HBufC8* utfTP = transProp->AsUtf8L();
       
   340             CleanupStack::PushL(utfTP);
       
   341             tp.SetPropertiesL(*utfTP, MSenLayeredProperties::ESenTransportLayer, &aConsumer);
       
   342             CleanupStack::PopAndDestroy(utfTP);
       
   343             CleanupStack::PopAndDestroy(transProp);
       
   344             }
       
   345         return KErrNone;
       
   346     }
       
   347 
       
   348 CSenHttpTransportProperties* CWSOviServiceSession::ApplyTransportPropertiesLC(const TDesC8& aTransportProperties)
       
   349     {
       
   350     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::ApplyTransportPropertiesL():");
       
   351     TLSLOG_ALL(KSenCoreServiceManagerLogChannelBase  , KMaxLogLevel,(aTransportProperties));
       
   352     CSenXmlReader* reader = XmlReader();
       
   353    TPtrC8 value;
       
   354     CSenHttpTransportProperties* httpTransProp = NULL;
       
   355     if (aTransportProperties.Length() <= KSenXmlPropertiesLocalname().Length()*2 
       
   356                                                                     + KSenLessThanSlash().Length()
       
   357                                                                     + KSenLessThan().Length()
       
   358                                                                     + KSenGreaterThan().Length()*2)
       
   359         {
       
   360         if (iTransProp && iTransProp->Length())
       
   361             {
       
   362             httpTransProp = CSenHttpTransportProperties::NewLC(*iTransProp, *reader);
       
   363             }
       
   364         else
       
   365             {
       
   366             httpTransProp = CSenHttpTransportProperties::NewLC();
       
   367             }
       
   368         }
       
   369     else
       
   370         {
       
   371         httpTransProp = CSenHttpTransportProperties::NewLC(aTransportProperties, *reader);
       
   372         if (iTransProp) 
       
   373             {
       
   374             //1 merge session and message layer transport properties
       
   375             CSenHttpTransportProperties* conHttpTransProp = CSenHttpTransportProperties::NewLC(*iTransProp, *reader);
       
   376             
       
   377             if (httpTransProp->ProxyHostL(value) == KErrNotFound)
       
   378                 {
       
   379                 if (conHttpTransProp->ProxyHostL(value) != KErrNotFound)
       
   380                     {
       
   381                     httpTransProp->SetProxyHostL(value);
       
   382                     }
       
   383                 }
       
   384             if (httpTransProp->PropertyL(KNCIMEndpoint, value) == KErrNotFound)
       
   385                 {
       
   386                 if (conHttpTransProp->PropertyL(KNCIMEndpoint, value) != KErrNotFound)
       
   387                     {
       
   388                     httpTransProp->SetPropertyL(KNCIMEndpoint, value);
       
   389                     }
       
   390                 }
       
   391             if (httpTransProp->PropertyL(KNCIMConsumerKeyId, value) == KErrNotFound)
       
   392                 {
       
   393                 if (conHttpTransProp->PropertyL(KNCIMConsumerKeyId, value) != KErrNotFound)
       
   394                     {
       
   395                     httpTransProp->SetPropertyL(KNCIMConsumerKeyId, value);
       
   396                     }
       
   397                 }
       
   398             if (httpTransProp->PropertyL(KNCIMConsumerSecret, value) == KErrNotFound)
       
   399                 {
       
   400                 if (conHttpTransProp->PropertyL(KNCIMConsumerSecret, value) != KErrNotFound)
       
   401                     {
       
   402                     httpTransProp->SetPropertyL(KNCIMConsumerSecret, value);
       
   403                     }
       
   404                 }
       
   405             if (httpTransProp->PropertyL(KNCIMConsumerServiceId, value) == KErrNotFound)
       
   406                 {
       
   407                 if (conHttpTransProp->PropertyL(KNCIMConsumerServiceId, value) != KErrNotFound)
       
   408                     {
       
   409                     httpTransProp->SetPropertyL(KNCIMConsumerServiceId, value);
       
   410                     }
       
   411                 }
       
   412             
       
   413             TBool boolValue;
       
   414             if (httpTransProp->OnewayMessageOnOffL(boolValue) == KErrNotFound)
       
   415                 {
       
   416                 if (conHttpTransProp->OnewayMessageOnOffL(boolValue) != KErrNotFound)
       
   417                     {
       
   418                     httpTransProp->SetOnewayMessageOnOffL(boolValue);
       
   419                     }
       
   420                 }
       
   421             if (httpTransProp->BoolPropertyL(KSenIAPDoNotPrompt, boolValue) == KErrNotFound)
       
   422                 {
       
   423                 if (conHttpTransProp->BoolPropertyL(KSenIAPDoNotPrompt, boolValue) != KErrNotFound)
       
   424                     {
       
   425                     httpTransProp->SetBoolPropertyL(KSenIAPDoNotPrompt, boolValue);
       
   426                     }
       
   427                 }
       
   428             if (httpTransProp->UserAgentL(value) == KErrNotFound)
       
   429                 {
       
   430                 if (conHttpTransProp->UserAgentL(value) != KErrNotFound)
       
   431                     {
       
   432                     httpTransProp->SetUserAgentL(value);
       
   433                     }
       
   434                 }
       
   435             TUint32 uint32value;
       
   436             if (httpTransProp->IapIdL(uint32value) == KErrNotFound)
       
   437                 {
       
   438                 if (conHttpTransProp->IapIdL(uint32value) != KErrNotFound)
       
   439                     {
       
   440                     httpTransProp->SetIapIdL(uint32value);
       
   441                     httpTransProp->SetSnapIdL(0);
       
   442                     }
       
   443                 }
       
   444             if (httpTransProp->SnapIdL(uint32value) == KErrNotFound)
       
   445                 {
       
   446                 if (conHttpTransProp->SnapIdL(uint32value) != KErrNotFound)
       
   447                     {
       
   448                     httpTransProp->SetSnapIdL(uint32value);
       
   449                     httpTransProp->SetIapIdL(0);
       
   450                     }
       
   451                 }
       
   452             
       
   453             TInt intValue;
       
   454             if (httpTransProp->MaxTimeToLiveL(intValue) == KErrNotFound)
       
   455                 {
       
   456                 if (conHttpTransProp->MaxTimeToLiveL(intValue) != KErrNotFound)
       
   457                     {
       
   458                     httpTransProp->SetMaxTimeToLiveL(intValue);
       
   459                     }
       
   460                 }
       
   461 
       
   462             CleanupStack::PopAndDestroy(conHttpTransProp);    
       
   463             }
       
   464         }
       
   465     
       
   466     return httpTransProp;
       
   467     }
       
   468 
       
   469 TInt CWSOviServiceSession::SendL( const TDesC8& aMessage, 
       
   470                                    const TDesC8& aTransportProperties,
       
   471                                    MSenRemoteServiceConsumer& aConsumer,
       
   472                                    TInt& aTxnId,
       
   473                                    HBufC8*& aRevalidationError )
       
   474     {
       
   475     TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId() ,KMinLogLevel ,"CWSOviServiceSession::SendL(TDesC8&, ...)");
       
   476     TInt retVal(KErrNone);
       
   477     if ( AmIHostletSession() )
       
   478         {
       
   479         TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId() ,KMinLogLevel ,"- This is a session for a hostlet.");
       
   480         retVal  = CSenWebServiceSession::SendL( aMessage, aTransportProperties, aConsumer, aTxnId, aRevalidationError );
       
   481         TLSLOG_FORMAT((KSenClientSessionLogChannelBase+aConsumer.ConnectionId(), KNormalLogLevel , _L8("- Return value from CSenWebServiceSession::SendL: %d"), retVal ));
       
   482         TLSLOG_FORMAT((KSenClientSessionLogChannelBase+aConsumer.ConnectionId(), KNormalLogLevel , _L8("- Transaction ID from transport: %d"), aTxnId ));
       
   483         }
       
   484     else
       
   485         {
       
   486         TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId() ,KMinLogLevel ,"- This is a session for a consumer.");
       
   487         PrepareOutCtxL(aMessage);
       
   488         VerifyPermissionL();
       
   489         //we still have to check if validUntil is not expired.
       
   490         retVal = RevalidateMobileTicketIfExpiredL( aRevalidationError );
       
   491         iSessionContext->Update(WSOviContextKeys::KOnlySharing, ETrue);
       
   492 
       
   493         if( retVal == KErrNone )
       
   494             {
       
   495             delete aRevalidationError; 
       
   496             aRevalidationError = NULL;
       
   497             CSenHttpTransportProperties* transProp = ApplyTransportPropertiesLC(aTransportProperties);
       
   498             retVal = MessageForSendingL(transProp, aConsumer.Id());
       
   499             if (aMessage.Length())
       
   500                 {
       
   501                 transProp->SetContentTypeL(OAuth::KFormUrlEncoded());
       
   502                 }
       
   503             
       
   504             HBufC8* utfTP = transProp->AsUtf8L();
       
   505             CleanupStack::PushL(utfTP);
       
   506             MSenTransport& transport = aConsumer.TransportL();
       
   507             AdaptEndpointL(EFalse);
       
   508             const TDesC8* message = iOutContext->GetDesC8L(WSOviContextKeys::KMessageBody);
       
   509             if (message)
       
   510                 {
       
   511                 retVal = transport.SendL(Endpoint(), *message, *utfTP, *this, aConsumer, aTxnId );
       
   512                 }
       
   513             else
       
   514                 {
       
   515                 retVal = transport.SendL(Endpoint(), aMessage, *utfTP, *this, aConsumer, aTxnId );
       
   516                 }
       
   517             AdaptEndpointL(ETrue);
       
   518             CleanupStack::PopAndDestroy(utfTP);    
       
   519             CleanupStack::PopAndDestroy(transProp);    
       
   520             DestroyOutCtx();
       
   521             }
       
   522         }
       
   523     return retVal;
       
   524     }
       
   525 
       
   526 
       
   527 TInt CWSOviServiceSession::SubmitL( const TDesC8& aMessage, 
       
   528                                      const TDesC8& aTransportProperties,
       
   529                                      MSenRemoteServiceConsumer& aConsumer,
       
   530                                      HBufC8*& aResponse )
       
   531     {
       
   532     TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId() ,KMinLogLevel ,"CWSOviServiceSession::SubmitL(TDesC8&, ...)");
       
   533     TInt retVal(KErrNone);
       
   534     if (!AmIHostletSession())
       
   535         {
       
   536         PrepareOutCtxL(aMessage);
       
   537         CSenHttpTransportProperties* transProp = ApplyTransportPropertiesLC(aTransportProperties);
       
   538         retVal = MessageForSendingL(transProp, aConsumer.Id());
       
   539         if (aMessage.Length())
       
   540             {
       
   541             transProp->SetContentTypeL(OAuth::KFormUrlEncoded());
       
   542             }
       
   543 
       
   544         
       
   545         HBufC8* utfTP = transProp->AsUtf8L();
       
   546         CleanupStack::PushL(utfTP);
       
   547         MSenTransport& transport = aConsumer.TransportL();
       
   548         AdaptEndpointL(EFalse);
       
   549 
       
   550         const TDesC8* message = iOutContext->GetDesC8L(WSOviContextKeys::KMessageBody);
       
   551         if (message)
       
   552             {
       
   553             retVal = transport.SubmitL(Endpoint(), *message, *utfTP, aResponse, aConsumer);
       
   554             }
       
   555         else
       
   556             {
       
   557             retVal = transport.SubmitL(Endpoint(), aMessage, *utfTP, aResponse, aConsumer);
       
   558             }   
       
   559         
       
   560         MSenProperties& prop = transport.PropertiesL();
       
   561         ((CWSOviPlugin&)iFramework).ProcessInboundDispatchL(this, retVal, aResponse, &prop );
       
   562         
       
   563         AdaptEndpointL(ETrue);
       
   564         CleanupStack::PopAndDestroy(utfTP);
       
   565         CleanupStack::PopAndDestroy(transProp);
       
   566         DestroyOutCtx();
       
   567 
       
   568         if (retVal == KErrNone)
       
   569             {
       
   570             iSubmitState = WSOviSession::KSubmitStateOK;
       
   571             }
       
   572 
       
   573     //becouse of lack phase (dispatch to proprr consumer). 
       
   574     //It is SUBMIT nature, only used internally between Client and NAUTH
       
   575     //we have to make validation here
       
   576         CWSOviSessionConsumer* consumer = NULL;
       
   577         if (iInContext)
       
   578             {
       
   579             SessionConsumerL(iInContext, consumer);    
       
   580             if (!(consumer && consumer->Id() == aConsumer.Id()))
       
   581                 {
       
   582                 retVal = KErrNotFound;
       
   583                 }
       
   584             }
       
   585         }
       
   586     return retVal;
       
   587     }
       
   588 
       
   589 void CWSOviServiceSession::AdaptEndpointL(TBool aToOrginal)
       
   590     {
       
   591     TPtrC8 oldEndpoint = Endpoint();
       
   592     if (aToOrginal)
       
   593         {
       
   594         SetEndPointL(*iOrginalEndpoint);
       
   595         return;
       
   596         }
       
   597     
       
   598     delete iOrginalEndpoint;
       
   599     iOrginalEndpoint = NULL;
       
   600     iOrginalEndpoint = oldEndpoint.AllocL();
       
   601     
       
   602     HBufC8* newEp(NULL);
       
   603 /*    CSenIdentityProvider* pIdentityProvider = IdentityProviderFromCoreL(ProviderID());
       
   604     if (pIdentityProvider)
       
   605         {
       
   606         TPtrC8 advAuth = pIdentityProvider->AdvisoryAuthnID();
       
   607         
       
   608         newEp = HBufC8::NewLC(oldEndpoint.Length()
       
   609                                 + advAuth.Length());
       
   610         TPtr8 ptr = newEp->Des();
       
   611         _LIT8(KMask, "%S");
       
   612         TInt pos = oldEndpoint.Find(KMask());
       
   613         if (pos != KErrNotFound)
       
   614             {
       
   615             ptr.Append(oldEndpoint.Left(pos));
       
   616             ptr.Append(advAuth);
       
   617             ptr.Append(oldEndpoint.Right(oldEndpoint.Length() - pos - KMask().Length()));
       
   618             SetEndPointL(ptr);
       
   619             }
       
   620         
       
   621         CleanupStack::PopAndDestroy(newEp);    
       
   622         }
       
   623 */  
       
   624     CSenTransportProperties* tp = iOutContext->TP();
       
   625     if (tp)
       
   626         {
       
   627         TInt err(KErrNone);
       
   628         TPtrC8 suffixEndpoint;
       
   629         err = tp->EndpointResourceL(suffixEndpoint);
       
   630         if (!err)
       
   631             {
       
   632             newEp = HBufC8::NewLC(suffixEndpoint.Length()
       
   633                                   +oldEndpoint.Length());
       
   634             TPtr8 ptrNewEp = newEp->Des();
       
   635             ptrNewEp.Append(oldEndpoint);
       
   636             ptrNewEp.Append(suffixEndpoint);
       
   637             SetEndPointL(*newEp);
       
   638             CleanupStack::PopAndDestroy(newEp);
       
   639             }
       
   640         }
       
   641     }
       
   642 CSenIdentityProvider* CWSOviServiceSession::IdentityProviderFromCoreL(
       
   643                                             const TDesC8& aProviderID)
       
   644     {
       
   645     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::IdentityProviderLFromCore");
       
   646     CSenIdentityProvider* pIdentityProvider = NULL;
       
   647     if(aProviderID.Length() > 0)
       
   648         {
       
   649         CDesC8ArrayFlat* array = new (ELeave) CDesC8ArrayFlat(1);
       
   650         CleanupStack::PushL(array);
       
   651         array->AppendL(aProviderID);
       
   652         pIdentityProvider = ((CWSOviPlugin&)iFramework).Manager().IdentityProviderL(*array, ETrue);
       
   653         CleanupStack::PopAndDestroy(array);
       
   654         }
       
   655     return pIdentityProvider;
       
   656     }
       
   657 TInt CWSOviServiceSession::CanHandleErrorL()
       
   658     {
       
   659     TInt answer(KErrNone);
       
   660     const TInt* renewNeeded(NULL);
       
   661     const TInt* retry = iSessionContext->GetIntL(WSOviContextKeys::KRetryNeeded());
       
   662 
       
   663     
       
   664     
       
   665     
       
   666     if (iSessionContext)
       
   667         {
       
   668         renewNeeded = iSessionContext->GetIntL(WSOviContextKeys::KReAuthNeeded());
       
   669         }
       
   670     //retry in milisecunds
       
   671     if (retry && *retry > 0 &&
       
   672         iRetryCounter < WSOviSession::KCounterMax)
       
   673         {
       
   674         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::CanHandleErrorL - RETRY request (new timestamp needed)");
       
   675         iRetryCounter++;
       
   676         answer = KErrSenResendRequired;  
       
   677         }
       
   678     else if((renewNeeded && *renewNeeded &&
       
   679            iRenewCounter < WSOviSession::KCounterMax)
       
   680            )
       
   681         {
       
   682         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::CanHandleErrorL - RENEW request (new ticket is needed)");
       
   683         iRenewCounter++;
       
   684         answer = KErrSenReinitRequired;                    
       
   685         }
       
   686     else
       
   687         {
       
   688         iRenewCounter = 0;
       
   689         iRetryCounter = 0;
       
   690         }
       
   691     return answer;
       
   692     }
       
   693 TInt CWSOviServiceSession::SendToConsumerL(HBufC8* aMessage,
       
   694                               const TInt aTxnId,
       
   695                               MSenRemoteServiceConsumer& aConsumer,
       
   696                               MSenProperties* aResponseTransportProperties)
       
   697     {
       
   698     ((CWSOviPlugin&)iFramework).ProcessInboundDispatchL(this, KErrNone, aMessage, aResponseTransportProperties);
       
   699     TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId()  , KMinLogLevel,"CWSOviServiceSession::ParseMessageL(CSenSoapMessage& )");
       
   700     iRenewCounter = 0;
       
   701     iRetryCounter = 0;
       
   702     return aConsumer.HandleMessageL( aMessage, aTxnId, aResponseTransportProperties );
       
   703     }
       
   704     
       
   705 TInt CWSOviServiceSession::SendErrorToConsumerL( const TInt aErrorCode,
       
   706                                                      HBufC8* apError,
       
   707                                                      const TInt aTxnId,
       
   708                                                      MSenRemoteServiceConsumer& aConsumer,
       
   709                                                      MSenProperties* aResponseTransportProperties )
       
   710     {
       
   711     TLSLOG_L(KSenClientSessionLogChannelBase+aConsumer.ConnectionId()  , KMinLogLevel,"CWSOviServiceSession::SendErrorToConsumerL");
       
   712     TLSLOG_FORMAT((KSenClientSessionLogChannelBase+aConsumer.ConnectionId(), KNormalLogLevel , _L8("- ErrorCode: %d"), aErrorCode ));
       
   713     ((CWSOviPlugin&)iFramework).ProcessInboundDispatchL(this, aErrorCode, apError, aResponseTransportProperties);
       
   714     TInt answer = CanHandleErrorL();
       
   715     if (answer)
       
   716         {
       
   717         delete apError;
       
   718         return aConsumer.HandleErrorL(NULL, answer, aTxnId, aResponseTransportProperties);
       
   719         }
       
   720     else
       
   721         {
       
   722         return HandleErrorL(aErrorCode, apError, aTxnId, aConsumer, aResponseTransportProperties);
       
   723         }
       
   724     }
       
   725     
       
   726     
       
   727     
       
   728 TInt CWSOviServiceSession::SubmitSoapL( const TDesC8& /*aSoapMessage*/, 
       
   729                                          const TDesC8& /*aTransportProperties*/,
       
   730                                          MSenRemoteServiceConsumer& /*aConsumer*/,
       
   731                                          HBufC8*& /*aResponse*/ )
       
   732     {
       
   733     return KErrNotSupported;
       
   734     }
       
   735 
       
   736 TInt CWSOviServiceSession::SendSoapL( const TDesC8& /*aSoapMessage*/, 
       
   737                                        const TDesC8& /*aTransportProperties*/,
       
   738                                        MSenRemoteServiceConsumer& /*aConsumer*/,
       
   739                                        TInt& /*aTxnId*/,
       
   740                                        HBufC8*& /*aRevalidationError*/ )
       
   741     {
       
   742     return KErrNotSupported;
       
   743     }
       
   744 
       
   745   
       
   746 TBool CWSOviServiceSession::IsExpiredL()
       
   747     {
       
   748     CSenServiceSession::SetStatusL();
       
   749     return (CSenServiceSession::StatusL() == KSenConnectionStatusExpired);
       
   750     }
       
   751     
       
   752         
       
   753 void CWSOviServiceSession::SetSessionContext(CWSOviSessionContext* aContext)
       
   754     {
       
   755     delete iSessionContext;
       
   756     iSessionContext = aContext;
       
   757     }
       
   758 CWSOviSessionContext* CWSOviServiceSession::SessionContext()
       
   759     {
       
   760     return iSessionContext;
       
   761     }
       
   762     
       
   763 void CWSOviServiceSession::ClearCredentialL()
       
   764     {
       
   765     // Before removing the credential, checkt that
       
   766     // iCredentialPtr.Credential() != NULL
       
   767     if ( HasSecurity() ) // this performs the NULL check for credential
       
   768     	{
       
   769 	    // Remove Invalid Credential from Credentials DB
       
   770 	    TInt credentialId = iCredentialPtr.Credential()->IdentifierL().IdL();
       
   771 	    ((MSenServiceManager&)iFramework.Manager()).RemoveCredentialL(credentialId);
       
   772     	}
       
   773 
       
   774     // Remove Credential usage from Session
       
   775     SetSecurityL(KNullDesC8);
       
   776     iValidUntil.Set(_L("18000101:"));
       
   777     }
       
   778     
       
   779 TInt CWSOviServiceSession::InitializeFromL( MSenServiceDescription& aDescription)
       
   780     {
       
   781     CSenWebServiceSession::SetSecurityL(KNullDesC8);
       
   782     iValidUntil.Set(_L("19000101:"));
       
   783     
       
   784     TDescriptionClassType classType = aDescription.DescriptionClassType();
       
   785     if( classType == MSenServiceDescription::EWSDescription
       
   786                                             ||
       
   787         classType == MSenServiceDescription::EWSPattern
       
   788                                             ||
       
   789         classType == MSenServiceDescription::EIdentityProvider
       
   790         )
       
   791         {
       
   792         MSenElement& xmlSdAsElement = (
       
   793             (CSenWSDescription*)&aDescription)->AsElement();
       
   794 
       
   795 
       
   796         MSenElement* pElement = xmlSdAsElement.Element(KSenIdpProviderIdLocalname);
       
   797         delete iProviderID;
       
   798         iProviderID = NULL;
       
   799         if(pElement)
       
   800             {
       
   801             iProviderID = pElement->Content().AllocL();
       
   802             }
       
   803         else
       
   804             {
       
   805             CSenIdentityProvider* provider = ((CWSOviPlugin&)iFramework).
       
   806                     Manager().IdentityProviderL(aDescription);
       
   807             if (provider)
       
   808                 {
       
   809                 iProviderID = provider->ProviderID().AllocL();
       
   810                 }
       
   811             }
       
   812         pElement = xmlSdAsElement.Element(WSOviSession::KTrustAnchorElementLocalName);
       
   813         if(pElement)
       
   814             {
       
   815             delete iTrustAnchor;
       
   816             iTrustAnchor = NULL;
       
   817             iTrustAnchor = pElement->Content().AllocL();
       
   818             }
       
   819         
       
   820         CSenXmlReader* reader = XmlReader();
       
   821         if (!iSessionContext) 
       
   822             {
       
   823             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::InitializeFromL");
       
   824             iSessionContext = CWSOviSessionContext::NewL();
       
   825             
       
   826             //calling higher class load such things like iClientServerInterval
       
   827             CSenWebServiceSession::InitializeFromL(aDescription);
       
   828             if ( StatusL() != KSenConnectionStatusReady )
       
   829                 {
       
   830                 // Try to search Credential directly from CredentialManager
       
   831                 // if Connection/Credential is not ready
       
   832                 if ( TryToSearchValidCredentialL() == KErrNone )	//codescannerwarnings
       
   833                     {
       
   834                     SetStatusL();
       
   835                     }
       
   836                 }
       
   837             if (!iCredentialPtr.Credential())
       
   838                 {
       
   839                 pElement = xmlSdAsElement.Element(WSOviSession::KTokenCreationTimeLocalName);
       
   840                 if(pElement)
       
   841                     {
       
   842                     User::LeaveIfError(iSessionContext->Add(WSOviContextKeys::KTokenCreationTime, pElement->Content()));    
       
   843                     }
       
   844                 pElement = xmlSdAsElement.Element(WSOviSession::KTTLLocalName);
       
   845                 if(pElement)
       
   846                     {
       
   847                     User::LeaveIfError(iSessionContext->Add(WSOviContextKeys::KTTL, pElement->Content()));    
       
   848                     }
       
   849                 pElement = xmlSdAsElement.Element(WSOviSession::KTokenSecretLocalName);
       
   850                 if(pElement)
       
   851                     {
       
   852                     User::LeaveIfError(iSessionContext->Add(WSOviContextKeys::KTokenSecret, pElement->Content()));    
       
   853                     }
       
   854                 }
       
   855             AddSecurityTokenToContextL();
       
   856             }
       
   857         }
       
   858     else
       
   859         {
       
   860         CSenWebServiceSession::InitializeFromL(aDescription);
       
   861         }
       
   862         
       
   863     return KErrNone;
       
   864     }
       
   865     
       
   866 void CWSOviServiceSession::AddSecurityTokenToContextL()
       
   867     {
       
   868     if ( HasSecurity() )
       
   869         {
       
   870         CSenCredentialProperties& properties = iCredentialPtr.Credential()->PropertiesL();  //codescannerwarnings
       
   871         TInt retVal;
       
   872         TPtrC8 value;
       
   873         if ( iSessionContext )
       
   874             {
       
   875             retVal = properties.PropertyL(WSOviSession::KTokenCreationTimeLocalName, value);
       
   876             if ( retVal == KErrNone )
       
   877                 {
       
   878                 User::LeaveIfError(iSessionContext->Add(WSOviContextKeys::KTokenCreationTime, value));    
       
   879                 }
       
   880             retVal = properties.PropertyL(WSOviSession::KTTLLocalName, value);
       
   881             if ( retVal == KErrNone )
       
   882                 {
       
   883                 User::LeaveIfError(iSessionContext->Add(WSOviContextKeys::KTTL, value));    
       
   884                 }
       
   885             retVal = properties.PropertyL(WSOviSession::KTokenSecretLocalName, value);
       
   886             if ( retVal == KErrNone )
       
   887                 {
       
   888                 User::LeaveIfError(iSessionContext->Add(WSOviContextKeys::KTokenSecret, value));    
       
   889                 }
       
   890         
       
   891 	        HBufC8* pSecurity = SecurityL();
       
   892 	        if (pSecurity)
       
   893 	            {
       
   894 	            CleanupStack::PushL(pSecurity);
       
   895 	            TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::InitializeFromL - adding token to ctx");
       
   896 
       
   897 	            User::LeaveIfError(iSessionContext->Add(
       
   898 	                    WSOviContextKeys::KToken,
       
   899 	                    *pSecurity));  
       
   900 	            CleanupStack::PopAndDestroy(pSecurity);
       
   901 	            } 
       
   902 			}		
       
   903         }
       
   904     }
       
   905 
       
   906 void CWSOviServiceSession::AddPropertiesFromSessionContextToCredentialL()
       
   907     {
       
   908     if (iSessionContext)
       
   909         {
       
   910         if ( iCredentialPtr.Credential() )
       
   911             {
       
   912             CSenCredentialProperties& properties = iCredentialPtr.Credential()->PropertiesL();  //codescannerwarnings
       
   913             const TDesC8* pValue = iSessionContext->GetDesC8L(WSOviContextKeys::KTokenCreationTime);
       
   914             if ( pValue )
       
   915                 {
       
   916                 properties.SetPropertyL(WSOviSession::KTokenCreationTimeLocalName(), *pValue);
       
   917                 }        
       
   918             pValue = iSessionContext->GetDesC8L(WSOviContextKeys::KTTL);
       
   919             if ( pValue )
       
   920                 {
       
   921                 properties.SetPropertyL(WSOviSession::KTTLLocalName(), *pValue);
       
   922                 }        
       
   923             pValue = iSessionContext->GetDesC8L(WSOviContextKeys::KTokenSecret);
       
   924             if ( pValue )
       
   925                 {
       
   926                 properties.SetPropertyL(WSOviSession::KTokenSecretLocalName(), *pValue);
       
   927                 }        
       
   928 
       
   929             // Now, serialize this session (and all it's updated credentials) into database(s)
       
   930             iFramework.Manager().SaveL( *this ); // this will currently *also* serialize associated credential (new/updated MT)                
       
   931             }
       
   932         }
       
   933     }
       
   934 
       
   935 
       
   936 void CWSOviServiceSession::WriteExtensionsAsXMLToL(RWriteStream& aWriteStream)
       
   937     {
       
   938     CSenWebServiceSession::WriteExtensionsAsXMLToL(aWriteStream);
       
   939 
       
   940     if(iProviderID)
       
   941         {
       
   942         aWriteStream.WriteL(WSOviSession::KProviderIDTag);
       
   943         aWriteStream.WriteL(*iProviderID);
       
   944         aWriteStream.WriteL(WSOviSession::KProviderIDEndTag);
       
   945         }
       
   946 
       
   947     if(iTrustAnchor)
       
   948         {
       
   949         aWriteStream.WriteL(WSOviSession::KTrustAnchorTag);
       
   950         aWriteStream.WriteL(*iTrustAnchor);
       
   951         aWriteStream.WriteL(WSOviSession::KTrustAnchorEndTag);
       
   952         }
       
   953     if (iSessionContext)
       
   954     	{
       
   955         if (!iCredentialPtr.Credential() )
       
   956             {
       
   957             const TDesC8* value = iSessionContext->GetDesC8L(WSOviContextKeys::KTokenCreationTime);
       
   958             if (value)
       
   959                 {
       
   960                 aWriteStream.WriteL(WSOviSession::KTokenCreationTimeTag);
       
   961                 aWriteStream.WriteL(*value);
       
   962                 aWriteStream.WriteL(WSOviSession::KTokenCreationTimeEndTag);
       
   963                 }
       
   964             value = iSessionContext->GetDesC8L(WSOviContextKeys::KTTL);
       
   965             if (value)
       
   966                 {
       
   967                 aWriteStream.WriteL(WSOviSession::KTTLTag);
       
   968                 aWriteStream.WriteL(*value);
       
   969                 aWriteStream.WriteL(WSOviSession::KTTLEndTag);
       
   970                 }
       
   971             value = iSessionContext->GetDesC8L(WSOviContextKeys::KTokenSecret);
       
   972             if (value)
       
   973                 {
       
   974                 aWriteStream.WriteL(WSOviSession::KTokenSecretTag);
       
   975                 aWriteStream.WriteL(*value);
       
   976                 aWriteStream.WriteL(WSOviSession::KTokenSecretEndTag);
       
   977                 }
       
   978             }
       
   979     	}
       
   980     }
       
   981     
       
   982 TPtrC8 CWSOviServiceSession::ProviderID()
       
   983     {
       
   984     if(iProviderID)
       
   985         return *iProviderID;
       
   986     else
       
   987         return KNullDesC8();
       
   988    
       
   989     }
       
   990 
       
   991 void CWSOviServiceSession::SetProviderIDL(const TDesC8& aProviderId)
       
   992     {
       
   993     if (iProviderID)
       
   994         {
       
   995         delete iProviderID;
       
   996         iProviderID = NULL;
       
   997         }
       
   998     iProviderID = aProviderId.AllocL();
       
   999     }
       
  1000 
       
  1001 
       
  1002 void CWSOviServiceSession::SetTrustAnchorL(const TDesC8& aURI)
       
  1003     {
       
  1004     delete iTrustAnchor;
       
  1005     iTrustAnchor = NULL;
       
  1006     iTrustAnchor = aURI.AllocL();
       
  1007     }
       
  1008 TPtrC8 CWSOviServiceSession::TrustAnchor()
       
  1009     {
       
  1010     if(iTrustAnchor)
       
  1011         return *iTrustAnchor;
       
  1012     else
       
  1013         return KNullDesC8();
       
  1014     }
       
  1015 
       
  1016 TInt CWSOviServiceSession::ComputeStatusL()
       
  1017     {
       
  1018     
       
  1019     TInt retVal = CSenServiceSession::ComputeStatusL();
       
  1020     //we copy logic from CSenWebServiceSession and add :1) IProviderID condition
       
  1021     TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("CWSOviServiceSession::ComputeStatusL() - retval: %d"), retVal));
       
  1022     if ((retVal == KSenConnectionStatusReady) && iProviderID)
       
  1023         {
       
  1024         const TDesC8*  tokenCreatedTimeDes = NULL;
       
  1025         const TDesC8*  ttlDes = NULL;
       
  1026         TPtrC8 tokenCreationTimePtr;
       
  1027         TPtrC8 ttlPtr;
       
  1028         
       
  1029         if ( HasSecurity() )
       
  1030             {
       
  1031             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::ComputeStatusL() - HasSecurity true");
       
  1032             
       
  1033             CSenCredentialProperties& properties = iCredentialPtr.Credential()->PropertiesL();  //codescannerwarnings
       
  1034             TInt retVal(KErrNone);
       
  1035             retVal = properties.PropertyL(WSOviSession::KTokenCreationTimeLocalName(), tokenCreationTimePtr);
       
  1036             if (!retVal)
       
  1037                 {
       
  1038                 tokenCreatedTimeDes = &tokenCreationTimePtr;    
       
  1039                 }
       
  1040             retVal = properties.PropertyL(WSOviSession::KTTLLocalName(), ttlPtr);
       
  1041             if (!retVal)
       
  1042                 {
       
  1043                 ttlDes = &ttlPtr;
       
  1044                 }
       
  1045             }
       
  1046         else
       
  1047             {
       
  1048             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::ComputeStatusL() - HasSecurity false");
       
  1049             if (iSessionContext)
       
  1050                 {
       
  1051                 tokenCreatedTimeDes = iSessionContext->GetDesC8L(WSOviContextKeys::KTokenCreationTime);
       
  1052                 ttlDes = iSessionContext->GetDesC8L(WSOviContextKeys::KTTL);
       
  1053                 }
       
  1054             }
       
  1055         if (tokenCreatedTimeDes && ttlDes)
       
  1056 	        {
       
  1057             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("CWSOviServiceSession::ComputeStatusL() - ttlDes: %S & tokencreationDes: %S"), ttlDes, tokenCreatedTimeDes));
       
  1058 			TInt64 tokenCreatedTime(0);
       
  1059 			TInt64 ttl(0);
       
  1060 			TLex8 temp;
       
  1061 			  
       
  1062 			temp= *tokenCreatedTimeDes;
       
  1063 			temp.Val(tokenCreatedTime);
       
  1064 			  
       
  1065 			temp = *ttlDes;
       
  1066 			temp.Val(ttl);
       
  1067                
       
  1068 			if (tokenCreatedTime && ttl) 
       
  1069 				{
       
  1070 				HBufC8* nowTimeDes = SenCryptoUtils::GetTimestampL();
       
  1071 				TInt64 nowTime(0);
       
  1072 				temp = *nowTimeDes;
       
  1073 				temp.Val(nowTime);
       
  1074 				delete nowTimeDes;
       
  1075                	
       
  1076 				if (nowTime) 
       
  1077 					{
       
  1078 					TTimeIntervalSeconds margin(KClockSlipSeconds);
       
  1079 					TTimeIntervalSeconds diff = tokenCreatedTime + ttl -margin.Int() - nowTime;
       
  1080 	                TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("CWSOviServiceSession::ComputeStatusL() - diff: %d "), diff.Int()));
       
  1081 					if (diff.Int()<0) 
       
  1082 						{
       
  1083 						retVal = KSenConnectionStatusExpired;
       
  1084 						}
       
  1085 					}
       
  1086                }
       
  1087 	        }
       
  1088         else
       
  1089             {
       
  1090             retVal = KSenConnectionStatusExpired;
       
  1091             }
       
  1092         }
       
  1093     return retVal;
       
  1094 
       
  1095     } 
       
  1096     
       
  1097 void CWSOviServiceSession::PrepareOutCtxL(const TDesC8& aMessageBody)
       
  1098     {
       
  1099     iClientTime.UniversalTime();
       
  1100     
       
  1101     CSenXmlReader* reader = XmlReader();
       
  1102     if (iOutContext)
       
  1103         {
       
  1104         delete iOutContext;
       
  1105         iOutContext = NULL;
       
  1106         }
       
  1107     iOutContext = CWSOviMessageContext::NewL(SenContext::EOutgoing);
       
  1108     if (aMessageBody != KNullDesC8)
       
  1109         {
       
  1110         iOutContext->Update(WSOviContextKeys::KMessageBody, aMessageBody);
       
  1111         }
       
  1112     }
       
  1113 
       
  1114 void CWSOviServiceSession::DestroyOutCtx()
       
  1115     {
       
  1116     delete iOutContext;
       
  1117     iOutContext = NULL;
       
  1118     }
       
  1119 
       
  1120 TInt CWSOviServiceSession::RevalidateMobileTicketIfExpiredL( HBufC8*& aErrorMessage ) 
       
  1121     {
       
  1122     TInt result(KErrNone);
       
  1123     if (IsExpiredL())
       
  1124         {
       
  1125         //we need revalidate
       
  1126         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("CWSOviServiceSession::RevalidateMobileTicketIfExpiredL - revalidation is required.")));
       
  1127         iTokenObs->Cancel();
       
  1128         result = ((CWSOviPlugin&)iFramework).ProcessOutboundValidationL( *this, this, aErrorMessage );
       
  1129         if ( result == KErrNone )
       
  1130             {
       
  1131             // validation succeeded => serialize this session (and its credentials immediately)
       
  1132             iFramework.Manager().SaveL( *this ); // this will currently *also* serialize associated credential (new/updated MT)
       
  1133             }
       
  1134         if (result > KErrNone)
       
  1135             {
       
  1136             result = KErrSenAuthenticationFault;
       
  1137             }
       
  1138         return result;
       
  1139         }
       
  1140     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("CWSOviServiceSession::RevalidateMobileTicketIfExpiredL - ticket is still valid.")));        
       
  1141     return KErrNone;
       
  1142     }
       
  1143     
       
  1144 TBool CWSOviServiceSession::HasSuperClass( TDescriptionClassType aType )
       
  1145     {
       
  1146     if( aType == MSenServiceDescription::EWebServiceSession ) // direct superclass!
       
  1147         {
       
  1148         // If asked type is the know *direct* father/mother, return true:
       
  1149         return ETrue; 
       
  1150         } 
       
  1151     else
       
  1152         {
       
  1153         // Otherwise, ask from superclass (chain, recursively)
       
  1154         return CSenWebServiceSession::HasSuperClass( aType ); 
       
  1155         }
       
  1156     }
       
  1157 
       
  1158 TInt CWSOviServiceSession::ShareTokenWithL( CWSOviServiceSession* aWSOviSessionDst)
       
  1159     {
       
  1160     TInt error(KErrNone);
       
  1161     if ( !HasSecurity() )
       
  1162         {
       
  1163         return KErrNotFound;
       
  1164         }
       
  1165     //share token (only not expired), otherway it is sensless
       
  1166     //dont call SetStatus/IsExpired becouse it will infornm consumer/Core about chnge. 
       
  1167     //Session will not be able to revalidate by itself in next sending
       
  1168     if (ComputeStatusL() != KSenConnectionStatusExpired)
       
  1169         {
       
  1170 
       
  1171         error = iSessionContext->ShareTokenWithL(
       
  1172                 aWSOviSessionDst->SessionContext());        
       
  1173                 
       
  1174         aWSOviSessionDst->AddCredentialL(iCredentialPtr, iValidUntil);
       
  1175         return error;
       
  1176         }
       
  1177     else
       
  1178         {
       
  1179         return KErrNotFound;
       
  1180         }
       
  1181     }
       
  1182     
       
  1183 void CWSOviServiceSession::AddCredentialL( const TDesC8& aSecurity, TTime aValidUntil )
       
  1184     {
       
  1185     iValidUntil = aValidUntil;
       
  1186     SetSecurityL(aSecurity);
       
  1187     SetStatusL();
       
  1188     }
       
  1189     
       
  1190 void CWSOviServiceSession::AddCredentialL(RSenCredentialPtr aCredentialPtr, TTime aValidUntil)
       
  1191     {
       
  1192     iValidUntil = aValidUntil;
       
  1193     SetCredentialPtrL(aCredentialPtr); // Share same Credential between multiple Sessions	//codescannerwarnings
       
  1194     SetStatusL();
       
  1195     }
       
  1196     
       
  1197 TBool CWSOviServiceSession::AmIHostletSession()
       
  1198     {
       
  1199     if(iTransportCue && (KSenTransportCueHostletConnection() == *iTransportCue))
       
  1200         {
       
  1201         return ETrue;
       
  1202         }
       
  1203     return EFalse;
       
  1204     }
       
  1205     
       
  1206 
       
  1207 void CWSOviServiceSession::FillCredentialIdentifierL(CSenCredentialIdentifier& aIdentifier)
       
  1208     {
       
  1209     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::FillCredentialIdentifierL:");
       
  1210     CSenWebServiceSession::FillCredentialIdentifierL(aIdentifier);
       
  1211     //_LIT8(KProviderIdElementLocalName,  "ProviderID");
       
  1212     //aIdentifier.SetPropertyL(KProviderIdElementLocalName, ProviderID());
       
  1213 
       
  1214     TPtrC8 providerId = ProviderID();
       
  1215     aIdentifier.SetPropertyL(KSenIdpProviderIdLocalname, providerId);
       
  1216     
       
  1217     // Add information about the account that is priviledged to consume this credential
       
  1218     
       
  1219     // (a) find matching IDP description (which represents the account / userinfo)
       
  1220     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"- About to call LookupIdentityProviderL");
       
  1221     CSenIdentityProvider* pIdentityProvider = LookupIdentityProviderL();
       
  1222 //    CSenIdentityProvider* pIdentityProvider = NULL;
       
  1223 //    if ( providerId != KNullDesC8 )
       
  1224 //        {
       
  1225 //        LOG_WRITEFORMAT((_L8("- Searching for IdentityProvider using ProviderId : %S"), &providerId));
       
  1226 //        CSenWSDescription* pIdpPattern = CSenWSDescription::NewLC();
       
  1227 //        pIdpPattern->SetEndPointL(providerId);
       
  1228 //        pIdentityProvider = iFramework.Manager().IdentityProviderL(*pIdpPattern);
       
  1229 //        CleanupStack::PopAndDestroy(pIdpPattern);
       
  1230 //        }
       
  1231 
       
  1232     if ( pIdentityProvider )
       
  1233         {
       
  1234         TPtrC8 username = pIdentityProvider->UserName();
       
  1235         if( username.Length() > 0 )
       
  1236             {
       
  1237             aIdentifier.SetPropertyL(KSenIdpAuthzIDLocalname, username);
       
  1238             }
       
  1239         TPtrC8 password = pIdentityProvider->Password();
       
  1240         if( password.Length() > 0 )
       
  1241             {
       
  1242             aIdentifier.SetPropertyL(KSenIdpPasswordLocalname, password);
       
  1243             }
       
  1244         }
       
  1245     }
       
  1246     
       
  1247 void CWSOviServiceSession::VerifyPermissionL()
       
  1248     {
       
  1249     if( HasSecurity() )
       
  1250         {
       
  1251         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::VerifyPermissionL:");
       
  1252         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"- Making call to LookupIdentityProviderL");
       
  1253         CSenIdentityProvider* pProvider = LookupIdentityProviderL();
       
  1254         if ( pProvider )
       
  1255             {
       
  1256             CSenCredentialIdentifier& identifier = iCredentialPtr.Credential()->IdentifierL();
       
  1257             TPtrC8 username; // that was used to acquire this credential
       
  1258             TInt retVal = identifier.PropertyL(KSenIdpAuthzIDLocalname, username);
       
  1259             
       
  1260             TPtrC8 password; // that was used to acquire this credential
       
  1261             retVal = identifier.PropertyL(KSenIdpPasswordLocalname, password);
       
  1262             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMaxLogLevel, _L8("=> Credential properties: username = '%S', password = '%S'"), &username, &password));
       
  1263 
       
  1264             TPtrC8 accountUsername = pProvider->UserName();
       
  1265             TPtrC8 accountPassword = pProvider->Password();
       
  1266             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMaxLogLevel, _L8("=> Candidate IDP details: username = '%S', password = '%S'"), &accountUsername, &accountPassword));
       
  1267             
       
  1268             if( ( username.Length() > 0 && username != accountUsername ) ||
       
  1269                 ( password.Length() > 0 && password != accountPassword ) )
       
  1270                 {
       
  1271                 // either username or password DOES NOT MATCH (and the mismatching
       
  1272                 // one is not zero-length string)
       
  1273                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"-> Will not grant permission to consume current credential,");
       
  1274 
       
  1275                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMaxLogLevel,"   clearing credential & it's properties from context.");
       
  1276                 SetSecurityL(KNullDesC8);
       
  1277                 ClearCredentialPropertiesFromContext();
       
  1278                 }
       
  1279              else 
       
  1280                 { 
       
  1281                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"--> Permission to consume current credential is granted.");
       
  1282                 // => Permission to consume this credential is granted(!) 
       
  1283                 }
       
  1284             }
       
  1285         else
       
  1286             {
       
  1287             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"- There is no IDP (account), clearing credential & it's properties from context.");
       
  1288             // It is not possible to consumer WS-* service(s) without IDP (account)
       
  1289             SetSecurityL(KNullDesC8);
       
  1290             ClearCredentialPropertiesFromContext();
       
  1291             }
       
  1292         }
       
  1293     else
       
  1294         {
       
  1295         // If session has no credential (neither MT or SCT), neither should it have any 
       
  1296         // related data in context(!)
       
  1297         //--> LOG_WRITE_L("- There is no credential, clearing credential properties from context.");
       
  1298         //--> SetSecurityL(KNullDesC8);
       
  1299         ClearCredentialPropertiesFromContext();
       
  1300         }        
       
  1301     }
       
  1302     
       
  1303 CSenIdentityProvider* CWSOviServiceSession::LookupIdentityProviderL()
       
  1304     {
       
  1305     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::LookupIdpL()");
       
  1306     TPtrC8 providerId = ProviderID();
       
  1307     CSenIdentityProvider* pIdentityProvider = NULL;
       
  1308     if ( providerId != KNullDesC8 )
       
  1309         {
       
  1310         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("- Searching for IdentityProvider using ProviderId : %S"), &providerId));
       
  1311         CSenWSDescription* pIdpPattern = CSenWSDescription::NewLC();
       
  1312         pIdpPattern->SetEndPointL(providerId);
       
  1313         pIdentityProvider = iFramework.Manager().IdentityProviderL(*pIdpPattern);
       
  1314         CleanupStack::PopAndDestroy(pIdpPattern);
       
  1315         }
       
  1316     return pIdentityProvider;
       
  1317     }
       
  1318      
       
  1319     
       
  1320   
       
  1321 TBool CWSOviServiceSession::HasEqualPrimaryKeysL( MSenServiceDescription& aCandidate )
       
  1322     {
       
  1323     TBool retVal(EFalse);
       
  1324     if ( CSenWSDescription::HasEqualPrimaryKeysL(aCandidate) )
       
  1325         {
       
  1326         // Endpoint, Contract and FrameworkID match, all of them.
       
  1327         // Now, in WS-Ovi, also check ProviderID
       
  1328         if (aCandidate.DescriptionClassType() == DescriptionClassType())
       
  1329             {
       
  1330             CWSOviServiceSession& sessionCandidate = (CWSOviServiceSession&)aCandidate;
       
  1331             if (sessionCandidate.ProviderID() == ProviderID())
       
  1332                 {
       
  1333                 retVal = ETrue;
       
  1334                 }
       
  1335             }
       
  1336         }
       
  1337     return retVal; 
       
  1338     }
       
  1339     
       
  1340 void CWSOviServiceSession::CredentialChanged( TSenCredentialChange aChange,
       
  1341 		                                       TAny* apPointer )
       
  1342     {
       
  1343     if ( aChange == MSenCredentialObserver::EDestroyed && iSessionContext )
       
  1344         {
       
  1345         ClearCredentialPropertiesFromContext();
       
  1346         }
       
  1347     else if ( aChange == MSenCredentialObserver::EAdded && iSessionContext)
       
  1348     	{
       
  1349     	AddSecurityTokenToContextL();
       
  1350     	}
       
  1351     CSenWebServiceSession::CredentialChanged( aChange, apPointer );
       
  1352     }
       
  1353 
       
  1354 
       
  1355 void CWSOviServiceSession::ClearCredentialPropertiesFromContext()
       
  1356     {
       
  1357     if( iSessionContext )
       
  1358         {
       
  1359         iSessionContext->Remove(WSOviContextKeys::KToken);    
       
  1360         iSessionContext->Remove(WSOviContextKeys::KTokenCreationTime);
       
  1361         iSessionContext->Remove(WSOviContextKeys::KTTL);
       
  1362         iSessionContext->Remove(WSOviContextKeys::KTokenSecret);
       
  1363         }
       
  1364     }
       
  1365 void CWSOviServiceSession::SetMaxValidUntil()
       
  1366     {
       
  1367     iValidUntil = Time::MaxTTime();
       
  1368     }
       
  1369 
       
  1370 void CWSOviServiceSession::ActiveTicketObserverL()
       
  1371     {
       
  1372     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::ActiveTicketObserverL()");
       
  1373     if (iSessionContext)
       
  1374         {
       
  1375         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::ActiveTicketObserverL()- SessionContext exists");
       
  1376         const TDesC8*  tokenCreatedTimeDes = iSessionContext->GetDesC8L(WSOviContextKeys::KTokenCreationTime);
       
  1377         const TDesC8*  ttlDes = iSessionContext->GetDesC8L(WSOviContextKeys::KTTL);
       
  1378         if (tokenCreatedTimeDes && ttlDes)
       
  1379         	{
       
  1380             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::ActiveTicketObserverL() - ttl & time creation exists");
       
  1381             TInt64 tokenCreatedTime(0);
       
  1382             TInt64 ttl(0);
       
  1383             TLex8 temp;
       
  1384             temp.Assign(*tokenCreatedTimeDes);
       
  1385             temp.Val(tokenCreatedTime);
       
  1386             
       
  1387             temp = *ttlDes;
       
  1388             temp.Val(ttl);
       
  1389             
       
  1390             if (tokenCreatedTime && ttl)
       
  1391             	{
       
  1392                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::ActiveTicketObserverL() - ttl & time creation is valid");
       
  1393             	HBufC8* nowTimeDes = SenCryptoUtils::GetTimestampL();
       
  1394             	TInt64 nowTime(0);
       
  1395             	temp = *nowTimeDes;
       
  1396             	temp.Val(nowTime);
       
  1397             	delete nowTimeDes;
       
  1398             	if (nowTime)
       
  1399             		{
       
  1400             	    TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::ActiveTicketObserverL() - now time exists");
       
  1401             	    TInt marginForComputeStatus(10);
       
  1402                 	TTimeIntervalSeconds margin(KClockSlipSeconds-marginForComputeStatus);
       
  1403                     TTimeIntervalSeconds diff = tokenCreatedTime + ttl -margin.Int() - nowTime;
       
  1404             		if (diff.Int()>=0)
       
  1405             			{
       
  1406             		    TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviServiceSession::ActiveTicketObserverL() - start observer");
       
  1407             			iTokenObs->Start(diff);
       
  1408             			}
       
  1409             		}
       
  1410             	}
       
  1411         	}
       
  1412         }
       
  1413     }
       
  1414 
       
  1415 
       
  1416 // End of file
       
  1417