webservices/idwsfplugin/src/senidwsfplugin.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 // INCLUDE FILES
       
    26 #include <w32std.h>
       
    27 
       
    28 #include <SenServiceConnection.h> // error codes and framework ids
       
    29 #include <SenBaseFragment.h>
       
    30 #include <MSenServiceDescription.h>
       
    31 
       
    32 #include "msencoreservicemanager.h"
       
    33 #include "msentransport.h"
       
    34 #include "sendebug.h"
       
    35 #include "senlogger.h"
       
    36 
       
    37 #include "senidwsfplugin.h"
       
    38 #include "idwsfdiscoveryserviceclient.h"
       
    39 #include "idwsfservicesession.h"
       
    40 #include "idwsfcoreserviceconsumer.h"
       
    41 #include "midwsfsessionvalidator.h"
       
    42 
       
    43 #include <SenTransportProperties.h>
       
    44 #include <SenSoapConstants.h>
       
    45 
       
    46 
       
    47 // Create instance of concrete ECOM interface implementation
       
    48 CSenIdWsfPlugin* CSenIdWsfPlugin::NewL(TAny* aManager)
       
    49     {
       
    50     MSenCoreServiceManager* manager =
       
    51                         reinterpret_cast<MSenCoreServiceManager*>(aManager);
       
    52 
       
    53     CSenIdWsfPlugin* self = new (ELeave) CSenIdWsfPlugin(*manager);
       
    54     CleanupStack::PushL (self);
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop();
       
    57     return self;
       
    58     }
       
    59 
       
    60 // Implementation of CSIF
       
    61 
       
    62 // Constructor
       
    63 CSenIdWsfPlugin::CSenIdWsfPlugin(MSenCoreServiceManager& aManager)
       
    64 :   iManager(aManager)
       
    65     {
       
    66     }
       
    67 
       
    68 CSenIdWsfPlugin::~CSenIdWsfPlugin()
       
    69     {
       
    70     //CSenIdWsfPlugin::Log()->Write(_L8("CSenIdWsfPlugin::~CSenIdWsfPlugin()"));
       
    71     }
       
    72 
       
    73 // Second phase construction.
       
    74 void CSenIdWsfPlugin::ConstructL()
       
    75     {
       
    76     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"CSenIdWsfPlugin::ConstructL - Version 2 [2006-05-09]");
       
    77 
       
    78     // Results to add a DUPLICATE xmlns in parse!
       
    79     BaseConstructL(KIdWsfFrameworkXMLNS, KIdWsfFrameworkLocalname);
       
    80     }
       
    81 
       
    82 // New methods
       
    83 
       
    84 /*
       
    85 RFileLogger* CSenIdWsfPlugin::Log() const
       
    86     {
       
    87     return iManager.Log();
       
    88     }*/
       
    89 
       
    90 //////////////////////////////////////////////////////////////////////////
       
    91 
       
    92 
       
    93 // Implementation of CSIF
       
    94 
       
    95 /**
       
    96 * Return a system wide unique string to identify this framework
       
    97 *
       
    98 * @return TDesC8 that uniquely identifies this framework.
       
    99 */
       
   100 const TDesC8& CSenIdWsfPlugin::Id()
       
   101     {
       
   102     return KDefaultIdWsfFrameworkID();
       
   103     }
       
   104     
       
   105 TInt CSenIdWsfPlugin::CompareSDScore(const TSDScore& aSDScoreLeft,
       
   106                                                 const TSDScore& aSDScoreRight)
       
   107     {
       
   108      //"greater" means better matchScore, i.e. further down in a list!
       
   109     return (aSDScoreLeft.iScore - aSDScoreRight.iScore);
       
   110     }
       
   111 
       
   112 /**
       
   113 * Try to find one or more services that match the given ServiceDescription.
       
   114 * Register ServiceDescriptions for these to the owner of the framework,
       
   115 * i.e. to the ServiceManager. Return the number of ServiceDescriptions that
       
   116 * were added.
       
   117 *
       
   118 * @param    aPattern - A ServiceDescriptin that should be treated as a pattern
       
   119 * @param    aRemoteConsumer - the consumer sending the request
       
   120 * @param    aErrorMsg - in case that return value is negative, this may contain
       
   121 *           error as descriptor (SOAP Fault etc)
       
   122 *
       
   123 *   return (TInt) aCount of matching ServiceDescriptions that were added
       
   124 */
       
   125 TInt CSenIdWsfPlugin::AddServiceDescriptionL( MSenServiceDescription& aPattern,
       
   126                                               MSenRemoteServiceConsumer& aRemoteConsumer,
       
   127                                               HBufC8*& /* aErrorMsg */ )
       
   128     {
       
   129     CSLOG(aRemoteConsumer.ConnectionId() , KMinLogLevel,(_L("Entered CSenIdWsfPlugin::AddServiceDescriptionL")));
       
   130 
       
   131     if( aPattern.Contract().Length() == 0 
       
   132         || ( aPattern.FrameworkId() != KDefaultIdWsfFrameworkID &&
       
   133              aPattern.FrameworkId() != KNullDesC8 ) )
       
   134         {
       
   135         return 0;
       
   136         }
       
   137 
       
   138 
       
   139 #ifdef _SENDEBUG
       
   140     HBufC8* pAsXml = aPattern.AsXmlL();
       
   141     if(pAsXml)
       
   142         {
       
   143         CleanupStack::PushL(pAsXml);
       
   144         CSLOG_ALL(aRemoteConsumer.ConnectionId()  , KMaxLogLevel,(*pAsXml));
       
   145         CleanupStack::PopAndDestroy(); // pAsXml
       
   146         }
       
   147 #endif
       
   148 
       
   149     RArray<TSDScore> searchResults;
       
   150     RPointerArray<CSenWSDescription> serviceDescriptions;
       
   151     RPointerArray<CSenWSDescription> tempArray;
       
   152 
       
   153     CleanupClosePushL(searchResults);
       
   154     CleanupClosePushL(serviceDescriptions);
       
   155     CleanupClosePushL(tempArray);
       
   156     
       
   157     TLinearOrder<TSDScore> order(&CSenIdWsfPlugin::CompareSDScore);
       
   158 
       
   159     TInt retVal =
       
   160         Manager().ServiceDescriptionsL(serviceDescriptions,
       
   161                                     KIdWsfFrameworkDiscoveryServiceContract);
       
   162 
       
   163     if(retVal != KErrNone)
       
   164         {
       
   165         CleanupStack::PopAndDestroy(3); // close the RPointerArrays
       
   166         return 0; // zero SD's were added
       
   167         }
       
   168 
       
   169     CIdWsfDiscoveryServiceClient* pDSClient = NULL;
       
   170     CSenIdentityProvider* pIdentityProvider = IdentityProviderL(&aPattern);
       
   171 
       
   172     //////////////////////////////////////////////////////////////////////////
       
   173 
       
   174     if(!pIdentityProvider)
       
   175         {
       
   176         CleanupStack::PopAndDestroy(3); // close the RPointerArrays
       
   177         return 0;//no possibility to add any SD as long as NO IdP is available!
       
   178         }
       
   179     //////////////////////////////////////////////////////////////////////////
       
   180 
       
   181     TInt attempts = 0;
       
   182 
       
   183     RPointerArray<CSenWSDescription> invalidDSClients;
       
   184     CleanupClosePushL(invalidDSClients);
       
   185     MSenServiceDescription *pDSDescription = NULL;
       
   186 
       
   187     TInt i = 0;
       
   188     while((i < serviceDescriptions.Count()) && (searchResults.Count() < 1))
       
   189         {
       
   190         pDSDescription = (MSenServiceDescription*) serviceDescriptions[i];
       
   191         if(pDSDescription->DescriptionClassType() ==
       
   192                     MSenServiceDescription::EIdWsfDiscoveryServiceClient)
       
   193             {
       
   194             pDSClient = (CIdWsfDiscoveryServiceClient*) pDSDescription;
       
   195             if(pDSClient->IsReadyL())
       
   196                 {
       
   197                 if( (!pIdentityProvider &&
       
   198                             (pDSClient->TrustAnchor() == KNullDesC8())) ||
       
   199                      (pIdentityProvider &&
       
   200                             (pDSClient->TrustAnchor() ==
       
   201                                     pIdentityProvider->ProviderID()) ))
       
   202                     {
       
   203                     pDSClient->FindServiceL(tempArray, 
       
   204                                             aPattern, 
       
   205                                             aRemoteConsumer);
       
   206                     attempts++;
       
   207                     for(TInt j = 0; j < tempArray.Count(); j++)
       
   208                         {
       
   209                         TSDScore temp;
       
   210                         temp.iSD = tempArray[j];
       
   211                         temp.iScore = tempArray[j]->ScoreMatchL(aPattern);
       
   212                         searchResults.Append(temp);
       
   213                         //searchResults.Append(tempArray[j]);
       
   214                         }
       
   215                     tempArray.Reset();
       
   216                     searchResults.Sort(order);
       
   217                     }
       
   218                 else
       
   219                     {
       
   220                     invalidDSClients.Append(
       
   221                                 (CSenWSDescription*)pDSDescription);
       
   222                     }
       
   223                 }
       
   224             else
       
   225                 {
       
   226                 invalidDSClients.Append(
       
   227                                 (CSenWSDescription*)pDSDescription);
       
   228                 }
       
   229             }
       
   230         else
       
   231             {
       
   232             invalidDSClients.Append(
       
   233                                 (CSenWSDescription*)pDSDescription);
       
   234             }
       
   235         i++;
       
   236         }
       
   237 
       
   238     if(searchResults.Count() < 1)
       
   239         {
       
   240         if(attempts < 1)    // no DS trusted by the IDP
       
   241                             //that was in the database (senidentities.xml)
       
   242             {
       
   243             // get a DS with the IdP as trust anchor
       
   244             pDSClient = DSClientL(pIdentityProvider, aRemoteConsumer);
       
   245             if(pDSClient && pDSClient->IsReadyL())
       
   246                 {
       
   247                 pDSClient->FindServiceL(tempArray, 
       
   248                                         aPattern,
       
   249                                         aRemoteConsumer);
       
   250                 for(TInt j = 0; j < tempArray.Count(); j++)
       
   251                     {
       
   252                     TSDScore temp;
       
   253                     temp.iSD = tempArray[j];
       
   254                     temp.iScore = tempArray[j]->ScoreMatchL(aPattern);
       
   255                     searchResults.Append(temp);
       
   256                     //searchResults.Append(tempArray[j]);
       
   257                     }
       
   258                 tempArray.Reset();
       
   259                 searchResults.Sort(order);
       
   260                 }
       
   261             }
       
   262         }
       
   263 
       
   264     if(searchResults.Count() < 1)
       
   265         {
       
   266         // try to validate invalid DS descriptions (clients)
       
   267         i = 0;
       
   268         while((i < invalidDSClients.Count()) && (searchResults.Count() < 1))
       
   269             {
       
   270             pDSDescription = (MSenServiceDescription*) invalidDSClients[i];
       
   271             if(pDSDescription->DescriptionClassType() ==
       
   272                         MSenServiceDescription::EIdWsfDiscoveryServiceClient)
       
   273                 {
       
   274                 pDSClient = (CIdWsfDiscoveryServiceClient*) pDSDescription;
       
   275                 if(ValidateDSL(pDSClient,pIdentityProvider, aRemoteConsumer))
       
   276                     {
       
   277                     retVal = pDSClient->FindServiceL(tempArray, 
       
   278                                                      aPattern,
       
   279                                                      aRemoteConsumer);
       
   280                     CSLOG_FORMAT((aRemoteConsumer.ConnectionId()  , KNormalLogLevel, _L8("DS client FindServiceL() returned: (%d)"),
       
   281                         retVal));
       
   282                     retVal = 0;
       
   283 
       
   284                     for(TInt j = 0; j < tempArray.Count(); j++)
       
   285                         {
       
   286                         TSDScore temp;
       
   287                         temp.iSD = tempArray[j];
       
   288                         temp.iScore = tempArray[j]->ScoreMatchL(aPattern);
       
   289                         searchResults.Append(temp);
       
   290                         //searchResults.Append(tempArray[j]);
       
   291                         }
       
   292                     searchResults.Sort(order);
       
   293                     }
       
   294                 }
       
   295             i++;
       
   296             }
       
   297         }
       
   298 
       
   299     for(i=0; i<searchResults.Count(); i++)
       
   300         {
       
   301         Manager().AddServiceDescriptionL(searchResults[i].iSD);
       
   302         }
       
   303 
       
   304     TInt countOfAddedSDs = searchResults.Count();
       
   305 
       
   306     // close: invalidDSClients, tempArray, serviceDescriptions, searchResults
       
   307     CleanupStack::PopAndDestroy(4);
       
   308                                   
       
   309 	
       
   310 		
       
   311 	
       
   312 
       
   313     return countOfAddedSDs;
       
   314     }
       
   315 
       
   316 TInt CSenIdWsfPlugin::AddServiceDescriptionL( const TDesC8& aServiceTypeContract,
       
   317                                               MSenRemoteServiceConsumer& aRemoteConsumer,
       
   318                                               HBufC8*& aErrorMsg )
       
   319     {
       
   320 
       
   321     /*
       
   322     * Ask the DiscoveryService client to search for services of
       
   323     * the given type;
       
   324     * Add these services to the framework manager.
       
   325     */
       
   326     CSLOG(aRemoteConsumer.ConnectionId()   , KMinLogLevel,(_L("IdWsf-plugin: Searching for session with contract:")));
       
   327     CSLOG(aRemoteConsumer.ConnectionId()   , KMinLogLevel,(aServiceTypeContract));
       
   328 
       
   329 
       
   330     MSenServiceDescription *pPattern = CIdWsfServiceInstance::NewLC(aServiceTypeContract);
       
   331 
       
   332     TInt count = AddServiceDescriptionL(*pPattern, aRemoteConsumer, aErrorMsg);
       
   333     CleanupStack::PopAndDestroy( pPattern  );
       
   334     return count;
       
   335     }
       
   336 
       
   337 /**
       
   338 * Attempt to register the ServiceDescription to the ServiceManager that
       
   339 * owns the framework.
       
   340 * This is a callback used by the ServiceManager to give frameworks a chance
       
   341 * to replace a generic ServiceDescription into something that is more specific,
       
   342 * and more useful, to the ServiceInvocationFramework.
       
   343 *
       
   344 * @param aServiceDescription A ServiceDescription
       
   345 *
       
   346 *   return TInt aError - indicates the error or KErrNone if successfull
       
   347 */
       
   348 TInt CSenIdWsfPlugin::RegisterServiceDescriptionL(MSenServiceDescription& aServiceDescription)
       
   349     {
       
   350     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"CSenIdWsfPlugin::RegisterServiceDescriptionL");
       
   351 
       
   352     // Sanity checks
       
   353 
       
   354 #ifdef _SENDEBUG
       
   355     HBufC8* pAsXml = aServiceDescription.AsXmlL();
       
   356     if(pAsXml)
       
   357         {
       
   358         CleanupStack::PushL(pAsXml);
       
   359         TLSLOG_ALL(KSenCoreServiceManagerLogChannelBase  , KMaxLogLevel ,(*pAsXml));
       
   360         CleanupStack::PopAndDestroy(); // ptr
       
   361         }
       
   362 #endif // _SENDEBUG
       
   363 
       
   364 
       
   365     TInt retval = KErrNone;
       
   366 
       
   367     CIdWsfServiceSession *pSession = NULL;
       
   368 
       
   369     TPtrC8 contract = aServiceDescription.Contract();
       
   370 
       
   371     if(contract == KIdWsfFrameworkAuthenticationServiceContract)
       
   372         {
       
   373         retval = RegisterASClientL(&aServiceDescription);
       
   374         }
       
   375     else if(contract.Length()>0 &&
       
   376             (contract == KIdWsfFrameworkDiscoveryServiceContract))
       
   377         {
       
   378         retval = RegisterDSClientL(&aServiceDescription);
       
   379         }
       
   380     else
       
   381         {
       
   382         if(aServiceDescription.DescriptionClassType() ==
       
   383                     MSenServiceDescription::EIdWSFServiceSession)
       
   384             {
       
   385             pSession = (CIdWsfServiceSession*) &aServiceDescription;
       
   386 
       
   387             retval = Manager().AddServiceDescriptionL(pSession);
       
   388             }
       
   389         else
       
   390             {
       
   391             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("Creating session from description...")));
       
   392 #ifdef _SENDEBUG
       
   393             TPtrC8 con = aServiceDescription.Contract();
       
   394             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,_L8(" Using Contract: '%S'"), &con));
       
   395 #endif
       
   396 
       
   397             pSession = CIdWsfServiceSession::NewLC(*this);
       
   398             retval = pSession->InitializeFromL(aServiceDescription);
       
   399             if (retval == KErrNone)
       
   400                 {
       
   401                 retval = Manager().AddServiceDescriptionL(pSession);
       
   402                 CleanupStack::Pop(pSession);
       
   403                 }
       
   404             else
       
   405                 {
       
   406                 CleanupStack::PopAndDestroy(pSession);
       
   407                 }
       
   408             }
       
   409         }
       
   410 
       
   411     return retval;
       
   412     }
       
   413 
       
   414 TInt CSenIdWsfPlugin::UnregisterServiceDescriptionL(
       
   415                                 MSenServiceDescription& aServiceDescription)
       
   416     {
       
   417     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("UnRegisterServiceDescriptionL(.)")));
       
   418 
       
   419 
       
   420 #ifdef _SENDEBUG
       
   421     HBufC8* pAsXml = aServiceDescription.AsXmlL();
       
   422     if(pAsXml)
       
   423         {
       
   424         CleanupStack::PushL(pAsXml);
       
   425         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMaxLogLevel ,(*pAsXml));
       
   426         CleanupStack::PopAndDestroy(); // ptr
       
   427         }
       
   428 #endif // _SENDEBUG
       
   429 
       
   430 
       
   431     TInt retval = KErrNone;
       
   432     CIdWsfServiceSession *pSession = NULL;
       
   433     TPtrC8 contract = aServiceDescription.Contract();
       
   434 
       
   435     if(contract == KIdWsfFrameworkAuthenticationServiceContract)
       
   436         {
       
   437         retval = UnRegisterASClientL(&aServiceDescription);
       
   438         }
       
   439     else if(contract.Length()>0 &&
       
   440         (contract == KIdWsfFrameworkDiscoveryServiceContract))
       
   441         {
       
   442         retval = UnRegisterDSClientL(&aServiceDescription);
       
   443         }
       
   444     else
       
   445         {
       
   446         if(aServiceDescription.DescriptionClassType() ==
       
   447                 MSenServiceDescription::EIdWSFServiceSession)
       
   448             {
       
   449             pSession = (CIdWsfServiceSession*) &aServiceDescription;
       
   450             retval = Manager().RemoveServiceDescriptionL(*pSession);
       
   451             }
       
   452         else
       
   453             {
       
   454 #ifdef _SENDEBUG
       
   455             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"Creating session from description...");
       
   456             TPtrC8 con = aServiceDescription.Contract();
       
   457             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,_L8("-contract: '%S'"), &con));
       
   458 #endif // _SENDEBUG
       
   459 
       
   460             pSession = CIdWsfServiceSession::NewLC(*this);
       
   461             retval = pSession->InitializeFromL(aServiceDescription);
       
   462             if (retval == KErrNone)
       
   463                 {
       
   464                 retval = Manager().RemoveServiceDescriptionL(*pSession);
       
   465                 }
       
   466             CleanupStack::PopAndDestroy(pSession);
       
   467             }
       
   468         }
       
   469 
       
   470     return retval;
       
   471     }
       
   472 
       
   473 TInt CSenIdWsfPlugin::UnRegisterASClientL(
       
   474                                 MSenServiceDescription *aServiceDescription)
       
   475     {
       
   476 #ifdef _SENDEBUG
       
   477     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"CSenIdWsfPlugin::UnRegisterASClientL");
       
   478      TPtrC8 endpoint = aServiceDescription->Endpoint();
       
   479     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(endpoint));
       
   480 #endif
       
   481 
       
   482     TInt retval = KErrNone;
       
   483 
       
   484     CIdWsfServiceSession *pSession = NULL;
       
   485     CIdWsfAuthenticationServiceClient *pASClient = NULL;
       
   486 
       
   487     if(aServiceDescription->DescriptionClassType() ==
       
   488                 MSenServiceDescription::EIdWsfAuthenticationServiceClient)
       
   489         {
       
   490         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("This ServiceDescription is already an AS Client")));
       
   491         pASClient = (CIdWsfAuthenticationServiceClient*)aServiceDescription;
       
   492 
       
   493         retval = Manager().RemoveServiceDescriptionL(*pASClient);
       
   494         }
       
   495     else
       
   496         {
       
   497         pSession = CIdWsfServiceSession::NewLC(*this);
       
   498         retval = pSession->InitializeFromL(*aServiceDescription);
       
   499         if(retval != KErrNone)
       
   500             {
       
   501             CleanupStack::PopAndDestroy(pSession); 
       
   502             return retval;
       
   503             }
       
   504 #ifdef _SENDEBUG
       
   505         TPtrC8 sessionEndpoint = pSession->Endpoint();
       
   506         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"Creating ASClient and new Session using Endpoint:");
       
   507         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(sessionEndpoint));
       
   508 #endif
       
   509 
       
   510         pASClient = CIdWsfAuthenticationServiceClient::NewLC(*this);//, *Log());
       
   511         pASClient->SetServiceSessionL(pSession);    // pSession will be owned by ASClient
       
   512         CleanupStack::Pop(2); // pASClient, pSession
       
   513         CleanupStack::PushL(pASClient);
       
   514 
       
   515         retval = Manager().RemoveServiceDescriptionL(*pASClient);
       
   516         if(retval != KErrNone)
       
   517             {
       
   518             delete pASClient;
       
   519             }
       
   520         CleanupStack::Pop(); // pASClient
       
   521         }
       
   522 
       
   523     return retval;
       
   524     }
       
   525 
       
   526 TInt CSenIdWsfPlugin::RegisterASClientL(
       
   527                                 MSenServiceDescription *aServiceDescription)
       
   528     {
       
   529 #ifdef _SENDEBUG
       
   530     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"CSenIdWsfPlugin::RegisterASClientL");
       
   531     TPtrC8 endpoint = aServiceDescription->Endpoint();
       
   532     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(endpoint));
       
   533 #endif // _SENDEBUG
       
   534 
       
   535     TInt retval = KErrNone;
       
   536 
       
   537     CIdWsfServiceSession *pSession = NULL;
       
   538     CIdWsfAuthenticationServiceClient *pASClient = NULL;
       
   539 
       
   540     if(aServiceDescription->DescriptionClassType() ==
       
   541                 MSenServiceDescription::EIdWsfAuthenticationServiceClient)
       
   542         {
       
   543         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("This ServiceDescription is already an AS Client")));
       
   544         pASClient = (CIdWsfAuthenticationServiceClient*)aServiceDescription;
       
   545 
       
   546         retval = Manager().AddServiceDescriptionL(pASClient);
       
   547         }
       
   548     else
       
   549         {
       
   550         // Create new Authentication Service client and initialize
       
   551         // it from given description
       
   552         pSession = CIdWsfServiceSession::NewLC(*this);
       
   553         retval = pSession->InitializeFromL(*aServiceDescription);
       
   554 
       
   555         if(retval != KErrNone)
       
   556             {
       
   557             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"CSenIdWsfPlugin::RegisterASClientL");
       
   558             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KNormalLogLevel  ,_L("- InitializeFromL(SD) failed: %d"), retval));
       
   559             CleanupStack::PopAndDestroy(); // pSession
       
   560             return retval;
       
   561             }
       
   562         else if(pSession->Endpoint() == KNullDesC8)
       
   563             {
       
   564             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"- InitializeFromL => KErrSenNoEndpoint");
       
   565             CleanupStack::PopAndDestroy(); // pSession
       
   566             return KErrSenNoEndpoint;
       
   567             }
       
   568 
       
   569 #ifdef _SENDEBUG
       
   570         TPtrC8 sessionEndpoint = pSession->Endpoint();
       
   571         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,
       
   572             _L8("- Creating AS client and new session using endpoint: '%S'"),
       
   573             &sessionEndpoint));
       
   574 #endif // _SENDEBUG
       
   575 
       
   576         pASClient = CIdWsfAuthenticationServiceClient::NewLC(*this);//, *Log());
       
   577         pASClient->SetServiceSessionL(pSession);    // pSession will be
       
   578                                                     // owned by ASClient
       
   579         CleanupStack::Pop(2); // pASClient, pSession
       
   580         CleanupStack::PushL(pASClient);
       
   581 
       
   582         retval = Manager().AddServiceDescriptionL(pASClient);
       
   583         if(retval != KErrNone)
       
   584             {
       
   585             delete pASClient;
       
   586             }
       
   587         CleanupStack::Pop(); // pASClient
       
   588         }
       
   589 
       
   590     return retval;
       
   591     }
       
   592 
       
   593 TInt CSenIdWsfPlugin::RegisterDSClientL(
       
   594                                 MSenServiceDescription* aServiceDescription)
       
   595     {
       
   596     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("RegisterDSClientL(..)")));
       
   597 
       
   598     TInt retval = KErrNone;
       
   599 
       
   600     CIdWsfServiceSession *pSession = NULL;
       
   601     CIdWsfDiscoveryServiceClient *pDSClient = NULL;
       
   602 
       
   603     if(aServiceDescription->DescriptionClassType() ==
       
   604                     MSenServiceDescription::EIdWsfDiscoveryServiceClient)
       
   605         {
       
   606         /* There already is a session, but it may be necessary
       
   607          * to reset the user information.
       
   608          */
       
   609         pDSClient = (CIdWsfDiscoveryServiceClient*)aServiceDescription;
       
   610         retval = Manager().AddServiceDescriptionL(pDSClient);
       
   611         }
       
   612     else
       
   613         {
       
   614         pSession = CIdWsfServiceSession::NewLC(*this);
       
   615         retval = pSession->InitializeFromL(*aServiceDescription);
       
   616         if(retval != KErrNone)
       
   617             {
       
   618             CleanupStack::PopAndDestroy(); // pSession
       
   619             return retval;
       
   620             }
       
   621 #ifdef _SENDEBUG
       
   622         TPtrC8 endpoint = pSession->Endpoint();
       
   623         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,_L8("Creating DSClient for %S"), &endpoint));
       
   624 #endif // _SENDEBUG
       
   625 
       
   626         pDSClient = CIdWsfDiscoveryServiceClient::NewLC(*this);//, *Log());
       
   627         pDSClient->SetServiceSessionL(pSession);
       
   628         CleanupStack::Pop(2); // pDSClient, pSession
       
   629         CleanupStack::PushL(pDSClient);
       
   630 
       
   631         retval = Manager().AddServiceDescriptionL(pDSClient);
       
   632         if(retval != KErrNone)
       
   633             {
       
   634             delete pDSClient;
       
   635             }
       
   636         CleanupStack::Pop(); // pDSClient
       
   637         }
       
   638     return retval;
       
   639     }
       
   640 
       
   641 TInt CSenIdWsfPlugin::UnRegisterDSClientL(
       
   642                                 MSenServiceDescription* aServiceDescription)
       
   643     {
       
   644     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("RegisterDSClientL(..)")));
       
   645 
       
   646     TInt retval = KErrNone;
       
   647 
       
   648     CIdWsfServiceSession *pSession = NULL;
       
   649     CIdWsfDiscoveryServiceClient *pDSClient = NULL;
       
   650 
       
   651     if(aServiceDescription->DescriptionClassType() ==
       
   652             MSenServiceDescription::EIdWsfDiscoveryServiceClient)
       
   653         {
       
   654         // There already is a session, but it may be necessary
       
   655         // to reset the user information.
       
   656         //
       
   657         pDSClient = (CIdWsfDiscoveryServiceClient*)aServiceDescription;
       
   658         retval = Manager().RemoveServiceDescriptionL(*pDSClient);
       
   659         }
       
   660     else
       
   661         {
       
   662         pSession = CIdWsfServiceSession::NewLC(*this);
       
   663         retval = pSession->InitializeFromL(*aServiceDescription);
       
   664         if(retval != KErrNone)
       
   665             {
       
   666             CleanupStack::PopAndDestroy(); // pSession
       
   667             return retval;
       
   668             }
       
   669 #ifdef _SENDEBUG
       
   670         TPtrC8 endpoint = pSession->Endpoint();
       
   671         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,_L8("Creating DSClient for %S"), &endpoint));
       
   672 #endif
       
   673 
       
   674         pDSClient = CIdWsfAuthenticationServiceClient::NewLC(*this);//, *Log());
       
   675         pDSClient->SetServiceSessionL(pSession);
       
   676         CleanupStack::Pop(2); // pDSClient, pSession
       
   677         CleanupStack::PushL(pDSClient);
       
   678 
       
   679         retval = Manager().RemoveServiceDescriptionL(*pDSClient);
       
   680         if(retval != KErrNone)
       
   681             {
       
   682             delete pDSClient;
       
   683             }
       
   684         CleanupStack::Pop(); // pDSClient
       
   685         }
       
   686     return retval;
       
   687     }
       
   688 
       
   689 /**
       
   690 * Create a ServiceSession based upon the given ServiceDescription.
       
   691 * The ServiceSession is expected to be "valid", i.e. ready for use
       
   692 * by a client.
       
   693 *
       
   694 * @param aServiceDescription A ServiceDescription
       
   695 *
       
   696 * @param aNewSession (CSenServiceSession)
       
   697 *
       
   698 * return TInt error or KErrNone if successful
       
   699 */
       
   700 TInt CSenIdWsfPlugin::CreateServiceSessionL( MSenServiceDescription& aServiceDescription,
       
   701                                              MSenServiceDescription& aPattern,
       
   702                                              CSenServiceSession*& aNewSession,
       
   703                                              MSenRemoteServiceConsumer& aRemoteConsumer,
       
   704                                              HBufC8*& /* aErrorMsg */ ) // TODO: start providing aErrorMsg on SOAP faults(!)
       
   705     {
       
   706 #ifdef _SENDEBUG
       
   707     CSLOG(aRemoteConsumer.ConnectionId()  , KMinLogLevel,(_L("CreateServiceSessionL()")));
       
   708     TPtrC8 contract = aServiceDescription.Contract();
       
   709     CSLOG_FORMAT((aRemoteConsumer.ConnectionId()  , KMinLogLevel, _L8("- Asked to create ServiceSession using '%S' as Contract...)"),
       
   710             &contract));
       
   711 #endif
       
   712 
       
   713     delete aNewSession;
       
   714     aNewSession = NULL;
       
   715 
       
   716     if(aServiceDescription.DescriptionClassType() == MSenServiceDescription::EIdWSFServiceSession)
       
   717         {
       
   718         aNewSession = (CIdWsfServiceSession*) &aServiceDescription;
       
   719         CSLOG_L(aRemoteConsumer.ConnectionId()  , KMinLogLevel,"- service description is already an ID-WSF service session.");
       
   720         // Check for messageThreading! (and other facets that may have to be
       
   721         // set or cleared)
       
   722         // We can assume that the current session matches with the pattern
       
   723         // but that there may be a need to adjust some settings.
       
   724         ((CIdWsfServiceSession*)aNewSession)->CopyFacetsFromL(aPattern);
       
   725         }
       
   726     else
       
   727         {
       
   728         aNewSession = (CIdWsfServiceSession*) NewServiceDescriptionL();
       
   729         ((CIdWsfServiceSession*)aNewSession)->InitializeFromL( aServiceDescription );
       
   730         }
       
   731 
       
   732     if(aNewSession)
       
   733         {
       
   734         if(!aNewSession->IsReadyL())
       
   735             {
       
   736             if(ValidateL(*((CIdWsfServiceSession*)aNewSession), aRemoteConsumer))
       
   737                 {
       
   738                 // note (below): sessions were just previously written(!)
       
   739                 // -> due to nature of current CSenXmlDao implementation:
       
   740                 // it writes everything and every time it is saving some
       
   741                 // new object.
       
   742                 Manager().SaveL(*this);
       
   743                 return KErrNone;
       
   744                 }
       
   745             else
       
   746                 {
       
   747                 return KErrNotReady;
       
   748                 }
       
   749             }
       
   750         }
       
   751     return KErrNone;
       
   752     }
       
   753 
       
   754 
       
   755 TBool CSenIdWsfPlugin::ValidateL(CIdWsfServiceSession& aSession,
       
   756                                 MSenRemoteServiceConsumer& aRemoteConsumer)
       
   757     {
       
   758 #ifdef _SENDEBUG
       
   759     TPtrC8 endpoint = aSession.Endpoint();
       
   760     CSLOG_FORMAT((aRemoteConsumer.ConnectionId()  , KMinLogLevel, _L8("Session to '%S' Endpoint needs to be revalidated!"),
       
   761                             &endpoint));
       
   762 #endif // _SENDEBUG
       
   763 
       
   764     CIdWsfDiscoveryServiceClient* pDSClient = NULL;
       
   765     TPtrC8 trustAnchor = aSession.TrustAnchor();
       
   766     pDSClient = DSClientL(trustAnchor, aRemoteConsumer);
       
   767 
       
   768     if(!pDSClient)
       
   769         {
       
   770         // this typically occurs, if no IDP was obtained
       
   771         // the file might have been empty, or no matching
       
   772         // IDP was found(!)
       
   773         return EFalse;
       
   774         }
       
   775 
       
   776     TInt retVal = pDSClient->ValidateL(aSession, aRemoteConsumer);
       
   777 
       
   778     TBool success = (retVal == KErrNone) && aSession.IsReadyL();
       
   779     if(success)
       
   780         {
       
   781         // note (above): this leads to instant, second-time-write of sessions
       
   782         // -> due to nature of current CSenXmlDao implementation:
       
   783         // it writes everything and every time it is saving some new object.
       
   784         Manager().SaveL(aSession);
       
   785         }
       
   786 
       
   787     return success;
       
   788     }
       
   789 
       
   790 
       
   791 CIdWsfDiscoveryServiceClient* CSenIdWsfPlugin::DSClientL(
       
   792             const TDesC8& aTrustAnchor,
       
   793             MSenRemoteServiceConsumer& aRemoteConsumer)
       
   794     {
       
   795     CSLOG(aRemoteConsumer.ConnectionId()   , KMinLogLevel,(_L("DSClientL(const TDesC& aTrustAnchor, MSenRemoteServiceConsumer&)")));
       
   796     CSenIdentityProvider* pIdentityProvider = IdentityProviderL(aTrustAnchor);
       
   797     if(pIdentityProvider)
       
   798         {
       
   799         return DSClientL(pIdentityProvider, aRemoteConsumer);
       
   800         }
       
   801     else
       
   802         {
       
   803         return NULL;
       
   804         }
       
   805     }
       
   806 
       
   807 
       
   808 CSenIdentityProvider* CSenIdWsfPlugin::IdentityProviderL(
       
   809                                                     const TDesC8& aProviderId)
       
   810     {
       
   811     CIdWsfServiceInstance *pPattern = CIdWsfServiceInstance::NewLC();
       
   812     pPattern->SetProviderIdL( aProviderId );
       
   813 
       
   814     // 2004-08-10. one-line fix // this is checked by ID-MGR for matches
       
   815     if(aProviderId.Length()>0)
       
   816         {
       
   817         pPattern->SetEndPointL( aProviderId );
       
   818         }
       
   819 
       
   820     CSenIdentityProvider* pIdentityProvider = IdentityProviderL( pPattern );
       
   821     CleanupStack::PopAndDestroy( pPattern ); 
       
   822 
       
   823     return pIdentityProvider;
       
   824 
       
   825     }
       
   826 
       
   827 CSenIdentityProvider* CSenIdWsfPlugin::IdentityProviderL(
       
   828                                             MSenServiceDescription* aPattern)
       
   829     {
       
   830     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"CSenIdWsfPlugin::IdentityProviderL");
       
   831 
       
   832     CSenIdentityProvider* pIdentityProvider = NULL;
       
   833 
       
   834     if(aPattern)
       
   835         {
       
   836         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"Querying IdentityProvider using a pattern.");
       
   837         pIdentityProvider = Manager().IdentityProviderL(*aPattern);
       
   838         }
       
   839     else
       
   840         {
       
   841         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"No pattern given for IdentityProvider query.");
       
   842         }
       
   843 
       
   844     if(!pIdentityProvider)
       
   845         {
       
   846         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"Querying IdentityProvider without a pattern.");
       
   847         pIdentityProvider = Manager().IdentityProviderL();
       
   848         }
       
   849 
       
   850 #ifdef _SENDEBUG
       
   851     if(!pIdentityProvider)
       
   852         {
       
   853         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"FATAL: Did not obtain an IdentityProvider!");
       
   854         }
       
   855 #endif
       
   856 
       
   857     return pIdentityProvider;
       
   858     }
       
   859 
       
   860 TBool CSenIdWsfPlugin::ValidateDSL(CIdWsfDiscoveryServiceClient* aDSClient,
       
   861                                    CSenIdentityProvider* aIDP,
       
   862                                    MSenRemoteServiceConsumer& aRemoteConsumer)
       
   863     {
       
   864     CIdWsfAuthenticationServiceClient *pASClient = ASClientL(aIDP);
       
   865     if(!pASClient)
       
   866         {
       
   867         return EFalse;
       
   868         }
       
   869 
       
   870     CIdWsfServiceSession* pSession = aDSClient->IdWsfServiceSession();
       
   871     if(pSession)
       
   872         {
       
   873         pASClient->ValidateL(*pSession, aRemoteConsumer);
       
   874         }
       
   875     return aDSClient->IsReadyL();
       
   876     }
       
   877 
       
   878 /**
       
   879 * Obtain or construct a valid DiscoveryServiceClient. Ask
       
   880 * the owning ServiceManager for ServiceDescriptions that match
       
   881 * the ID-WSF Discovery Service contract, and that have the
       
   882 * given trust anchor (aIdentityProvider).
       
   883 * if given.
       
   884 *
       
   885 * @param aIdentityProvider
       
   886 * @return DiscoveryServiceClient
       
   887 */
       
   888 CIdWsfDiscoveryServiceClient* CSenIdWsfPlugin::DSClientL(
       
   889             CSenIdentityProvider* aIdentityProvider,
       
   890             MSenRemoteServiceConsumer& aRemoteConsumer)
       
   891     {
       
   892     CSLOG(aRemoteConsumer.ConnectionId()   , KMinLogLevel,(_L("DSClientL(CSenIdentityProvider*, MSenRemoteServiceConsumer&)")));
       
   893 
       
   894     TPtrC8 trustAnchor(KNullDesC8);
       
   895     if(aIdentityProvider)
       
   896         {
       
   897         trustAnchor.Set(aIdentityProvider->ProviderID());
       
   898         }
       
   899 
       
   900     CIdWsfDiscoveryServiceClient* pDSClient = NULL;
       
   901 
       
   902     RPointerArray<CSenWSDescription> matches;
       
   903     CleanupClosePushL(matches);
       
   904 
       
   905     CoreServiceDescriptionsL(matches, KIdWsfFrameworkDiscoveryServiceContract,
       
   906                                 KNullDesC8);
       
   907     CSLOG(aRemoteConsumer.ConnectionId()   , KMinLogLevel,(_L(">>> TrustAnchor to match:")));
       
   908 
       
   909     CSLOG(aRemoteConsumer.ConnectionId()   , KMaxLogLevel,(trustAnchor));
       
   910 
       
   911     // we now should have DiscoveryServiceClients or nothing
       
   912     for(TInt i = 0; i < matches.Count(); i++)
       
   913         {
       
   914         CIdWsfDiscoveryServiceClient* pCandidate =
       
   915             reinterpret_cast<CIdWsfDiscoveryServiceClient*>(matches[i]);
       
   916 
       
   917         if(pCandidate && trustAnchor.Length()>0)
       
   918             {
       
   919             TPtrC8 candidatesTrustAnchor = pCandidate->TrustAnchor();
       
   920             CSLOG(aRemoteConsumer.ConnectionId()   , KMaxLogLevel,(_L(">>> DS Candidate TrustAnchor:")));
       
   921             CSLOG(aRemoteConsumer.ConnectionId()   , KMaxLogLevel,(candidatesTrustAnchor));
       
   922 
       
   923             if(candidatesTrustAnchor.Length() == 0 ||
       
   924                 trustAnchor != candidatesTrustAnchor)
       
   925                 {
       
   926                 CSLOG(aRemoteConsumer.ConnectionId()   , KMinLogLevel,(_L(">>> DS Candidate not trusted")));
       
   927                 pCandidate = NULL;
       
   928                 }
       
   929             }
       
   930 
       
   931         if(pCandidate)
       
   932             {
       
   933             CSLOG(aRemoteConsumer.ConnectionId()   , KMinLogLevel,(_L(">>> DS Candidate trusted")));
       
   934             if(!pDSClient)
       
   935                 {
       
   936                 pDSClient = pCandidate;
       
   937                 }
       
   938             else if(pCandidate->ValidUntilL() > pDSClient->ValidUntilL())
       
   939                 {
       
   940                 CSLOG(aRemoteConsumer.ConnectionId()   , KMinLogLevel,(_L("Found: Discovery Service Session having longer validity!")));
       
   941                 pDSClient = pCandidate;
       
   942                 }
       
   943             }
       
   944         }
       
   945 
       
   946     if(!pDSClient)
       
   947         {
       
   948         /* Obtain an AuthenticationServiceClient and ask it to validate
       
   949         * a new DiscoveryServiceClient.
       
   950         */
       
   951         pDSClient = CIdWsfDiscoveryServiceClient::NewLC(*this);//, *Log());
       
   952         CIdWsfServiceSession* pBrandNewSession =
       
   953                                 CIdWsfServiceSession::NewL(*this);
       
   954 
       
   955         // The SetServiceSessionL() will also set the DS client
       
   956         // as the first consumer of this new service session.
       
   957         // Ownership of pBrandNewSession is transferred to pDSClient:
       
   958         pDSClient->SetServiceSessionL(pBrandNewSession);
       
   959 
       
   960         // NOTE
       
   961         //
       
   962         // This policy related feature is in still in progress, and
       
   963         // therefore: NO ready implementation has been provided.
       
   964         //
       
   965         // add the _REAL_ consumer to assure policy transaction(!)
       
   966         //pBrandNewSession->AddConsumerL(aRemoteConsumer);
       
   967         //
       
   968         // NOTE
       
   969 
       
   970         CIdWsfAuthenticationServiceClient* pASClient =
       
   971                                                 ASClientL(aIdentityProvider);
       
   972         if(pASClient)
       
   973             {
       
   974             CIdWsfServiceSession* pSession = pDSClient->IdWsfServiceSession();
       
   975             if(pSession)
       
   976                 {
       
   977                 pASClient->ValidateL(*pSession, aRemoteConsumer);
       
   978                 }
       
   979             }
       
   980 
       
   981         if(pDSClient->IsReadyL())
       
   982             {
       
   983             TInt retVal = Manager().AddServiceDescriptionL(pDSClient);
       
   984             if(retVal != KErrNone)
       
   985                 {
       
   986                 CleanupStack::PopAndDestroy(); // pDSClient
       
   987                 pDSClient = NULL;
       
   988                 }
       
   989             else 
       
   990                 {
       
   991                 CleanupStack::Pop(); // pDSClient
       
   992                 }
       
   993             }
       
   994         else
       
   995             {
       
   996             // bugfix 2005-03-02, matmatt
       
   997             CleanupStack::PopAndDestroy(); // pDSClient 
       
   998             pDSClient = NULL; // not valid
       
   999             }
       
  1000         }
       
  1001     else
       
  1002         {
       
  1003         if(!pDSClient->IsReadyL())
       
  1004             {
       
  1005             CIdWsfAuthenticationServiceClient* pASClient =
       
  1006                                             ASClientL(aIdentityProvider);
       
  1007             if(pASClient)
       
  1008                 {
       
  1009                 CIdWsfServiceSession* pSession = pDSClient->IdWsfServiceSession();
       
  1010                 if(pSession)
       
  1011                     {
       
  1012                     pASClient->ValidateL(*pSession, aRemoteConsumer);
       
  1013                     }
       
  1014                 }
       
  1015 
       
  1016             if(pDSClient->IsReadyL())
       
  1017                 {
       
  1018                 Manager().SaveL(*this);
       
  1019                 }
       
  1020             else
       
  1021                 {
       
  1022                 pDSClient = NULL; // not valid, fixed 2005-12-07
       
  1023                 }
       
  1024             }
       
  1025         }
       
  1026     CleanupStack::PopAndDestroy(); //matches.Close();
       
  1027 
       
  1028     return pDSClient;
       
  1029     }
       
  1030 
       
  1031 /**
       
  1032 * Obtain or construct a valid AuthenticationServiceClient. Ask
       
  1033 * the owning ServiceManager for ServiceDescriptions that match
       
  1034 * the ID-WSF Authentication Service contract, and the ProviderID
       
  1035 * of the ServiceDescription, if given.
       
  1036 *
       
  1037 * @param providerID
       
  1038 * @return AuthenticationServiceClient
       
  1039 */
       
  1040 CIdWsfAuthenticationServiceClient* CSenIdWsfPlugin::ASClientL(
       
  1041                                     CSenIdentityProvider* aIdentityProvider)
       
  1042     {
       
  1043     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("ASClientL(..)")));
       
  1044 
       
  1045     TPtrC8 providerId(KNullDesC8);
       
  1046     if(aIdentityProvider)
       
  1047         {
       
  1048         providerId.Set(aIdentityProvider->ProviderID());
       
  1049         }
       
  1050 
       
  1051     if(providerId.Length()>0)
       
  1052         {
       
  1053         TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,_L8("Searching for AS at %S"), &providerId));
       
  1054         }
       
  1055     else
       
  1056         {
       
  1057         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KNormalLogLevel ,(_L8("Searching for AS at NULL")));
       
  1058         }
       
  1059 
       
  1060     CIdWsfAuthenticationServiceClient* pASClient = NULL;
       
  1061 
       
  1062     RPointerArray<CSenWSDescription> matches;
       
  1063     CleanupClosePushL(matches);
       
  1064 
       
  1065     CoreServiceDescriptionsL(matches,
       
  1066                              KIdWsfFrameworkAuthenticationServiceContract(),
       
  1067                              providerId);
       
  1068 
       
  1069     if(matches.Count() > 0)
       
  1070         {
       
  1071         pASClient =
       
  1072             reinterpret_cast<CIdWsfAuthenticationServiceClient*>(matches[0]);
       
  1073         pASClient->SetAccount(aIdentityProvider);
       
  1074         }
       
  1075     else
       
  1076         {
       
  1077         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L("No matching AS description available!")));
       
  1078         }
       
  1079 
       
  1080     if(matches.Count() > 1)
       
  1081         {
       
  1082         // search for a instance with longest validity
       
  1083         for(TInt i = 0; i < matches.Count(); i++)
       
  1084             {
       
  1085             CIdWsfAuthenticationServiceClient* pNewerASClient =
       
  1086               reinterpret_cast<CIdWsfAuthenticationServiceClient*>(matches[i]);
       
  1087             pNewerASClient->SetAccount(aIdentityProvider);
       
  1088             if(pNewerASClient->ValidUntilL() > pASClient->ValidUntilL())
       
  1089                 {
       
  1090                 pASClient = pNewerASClient;
       
  1091                 }
       
  1092             }
       
  1093         }
       
  1094 
       
  1095     CleanupStack::PopAndDestroy(); // matches.Close();
       
  1096     return pASClient;
       
  1097     }
       
  1098 
       
  1099 TInt CSenIdWsfPlugin::CoreServiceDescriptionsL(
       
  1100                 RPointerArray<CSenWSDescription>& aMatches,
       
  1101                 const TDesC8& aContract,
       
  1102                 const TDesC8& aProviderID)
       
  1103     {
       
  1104     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("CoreServiceDescriptionsL(..)")));
       
  1105 
       
  1106     RPointerArray<CSenWSDescription> serviceDescriptions;
       
  1107     CleanupClosePushL(serviceDescriptions);
       
  1108 
       
  1109     TInt retVal =
       
  1110         Manager().ServiceDescriptionsL(serviceDescriptions, aContract);
       
  1111     if(retVal != KErrNone)
       
  1112         {
       
  1113         return retVal;
       
  1114         }
       
  1115 
       
  1116     CIdWsfCoreServiceConsumer* pDescription = NULL;
       
  1117     for(TInt i = 0; i < serviceDescriptions.Count(); i++)
       
  1118         {
       
  1119         pDescription = (CIdWsfCoreServiceConsumer*)serviceDescriptions[i];
       
  1120         if(aProviderID.Length()>0)
       
  1121             {
       
  1122 #ifdef _SENDEBUG
       
  1123             HBufC8* pAsXml = pDescription->AsXmlL();
       
  1124             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"CSenIdWsfPlugin::CoreServiceDescriptionsL:");
       
  1125             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMaxLogLevel ," - CoreServiceConsumer as XML:");
       
  1126             if(pAsXml)
       
  1127                 {
       
  1128                 CleanupStack::PushL(pAsXml);
       
  1129                 TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMaxLogLevel ,(*pAsXml));
       
  1130                 CleanupStack::PopAndDestroy(); // asXml
       
  1131                 }
       
  1132 #endif
       
  1133 
       
  1134             TPtrC8 providerID = pDescription->ProviderId();
       
  1135             if(providerID.Length()>0 && (aProviderID == providerID))
       
  1136                 {
       
  1137 #ifdef _SENDEBUG
       
  1138                 TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMaxLogLevel ,(_L8("    > added to matches")));
       
  1139 #endif
       
  1140                 aMatches.Append(pDescription);
       
  1141                 }
       
  1142             }
       
  1143         else
       
  1144             {
       
  1145             aMatches.Append(pDescription);
       
  1146             }
       
  1147         }
       
  1148     TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KNormalLogLevel  ,_L8("Found %d matches for"), aMatches.Count()));
       
  1149     TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,_L8("     a contract: %S"), &aContract));
       
  1150 
       
  1151     CleanupStack::PopAndDestroy(); // serviceDescriptions.Close();
       
  1152     return KErrNone;
       
  1153     }
       
  1154 
       
  1155 /**
       
  1156 * Return a new instance of a framework specific class that
       
  1157 * implements the ServiceDesccription interface.
       
  1158 *
       
  1159 * @param aNewSession (CSenServiceSession)
       
  1160 *
       
  1161 * return TInt error or KErrNone if successful
       
  1162 */
       
  1163 CSenWSDescription* CSenIdWsfPlugin::NewServiceDescriptionL()
       
  1164 
       
  1165     {
       
  1166     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("CSenIdWsfPlugin::NewServiceDescriptionL()")));
       
  1167     CIdWsfServiceSession* pSD = CIdWsfServiceSession::NewL(*this);
       
  1168     return pSD;
       
  1169     }
       
  1170 
       
  1171 /**
       
  1172 * Return the ServiceManager for which this instance is working.
       
  1173 * This is used by the ServiceSession instances created by this framework.
       
  1174 *
       
  1175 * Note, in Sen, MSenCoreServiceManager so all Frameworks
       
  1176 * return their "owner" - i.e. - the only service manager in Sen Framework
       
  1177 *
       
  1178 * @return (MSenCoreServiceManager)
       
  1179 */
       
  1180 MSenCoreServiceManager& CSenIdWsfPlugin::Manager()
       
  1181     {
       
  1182     return iManager;
       
  1183     }
       
  1184 
       
  1185 /**
       
  1186 * Set the ServiceManager to which this instance reports.
       
  1187 * The (singleton) ServiceManager calls this method when
       
  1188 * it instantiates a framework.
       
  1189 *
       
  1190 * @param aServiceManager The singleton ServiceManager in the system.
       
  1191 */
       
  1192 void CSenIdWsfPlugin::SetManager(MSenCoreServiceManager& aServiceManager)
       
  1193     {
       
  1194     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("CSenIdWsfPlugin::SetManager(.)")));
       
  1195     iManager = aServiceManager;
       
  1196     }
       
  1197 
       
  1198 
       
  1199 /**
       
  1200 * Return an object that can do SAX based parsing of a XML fragment
       
  1201 * that contains framework specific configuration information.
       
  1202 * The ServiceManager asks for this BaseFragment upon startup
       
  1203 * in order to give this framework a chance to read its configuration
       
  1204 * information from the central ServiceManager configuration document.
       
  1205 *
       
  1206 * @return (CSenBaseFragment)
       
  1207 */
       
  1208 CSenBaseFragment& CSenIdWsfPlugin::ConfigParser()
       
  1209     {
       
  1210     TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,(_L8("CSenIdWsfPlugin::ConfigParser()")));
       
  1211     return *this;
       
  1212     }
       
  1213 
       
  1214 /**
       
  1215 * Write the ServiceInvocationFramework configuration as XML to a HBufC8.
       
  1216 *
       
  1217 * This method is invoked by the ServiceManager when it saves
       
  1218 * its state and/or configuration.
       
  1219 *
       
  1220 * Upon startup the chunk of XML that is written will be passed back to
       
  1221 * the configuation parser of the framework.
       
  1222 *
       
  1223 * The "schema" that shall be adhered to is:
       
  1224 * <pre>
       
  1225 * &lt;element name="Framework"&gt;
       
  1226 *       &lt;any/&gt;&lt;!-- this is where implementations write additional
       
  1227 *       information --&gt;
       
  1228 *       &lt;attribute name="class" type="String"&gt;
       
  1229 *    &lt;!-- In Symbian the class attribute is used as cue for
       
  1230 *    the ECOM Resolver --&gt;
       
  1231 * &lt;/element&gt;
       
  1232 * </pre>
       
  1233 *
       
  1234 * @param aTo - a HBufC8 to which the framework should write its configuration
       
  1235 */
       
  1236 
       
  1237 
       
  1238 void CSenIdWsfPlugin::EndElementL(const TDesC8& aNsUri,
       
  1239                                   const TDesC8& aLocalName,
       
  1240                                   const TDesC8& aQName)
       
  1241     {
       
  1242     TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,
       
  1243         _L8("ID-WSF plugin @ end of element: %S (xmlns='%S', localname='%S')"),
       
  1244                 &aQName, &aNsUri, &aLocalName));
       
  1245     CSIF::EndElementL(aNsUri,aLocalName,aQName);
       
  1246     }
       
  1247 
       
  1248 // over-ride CSenBaseFragment
       
  1249 HBufC* CSenIdWsfPlugin::AsXmlUnicodeL()
       
  1250     {
       
  1251     HBufC8* pUtf8 = AsXmlL();
       
  1252     CleanupStack::PushL(pUtf8);
       
  1253     HBufC* pAsAxlInUnicode = SenXmlUtils::ToUnicodeLC(*pUtf8);
       
  1254     CleanupStack::Pop();           // pAsAxlInUnicode
       
  1255     CleanupStack::PopAndDestroy(); // pUtf8
       
  1256     return pAsAxlInUnicode;
       
  1257     }
       
  1258 
       
  1259 HBufC8* CSenIdWsfPlugin::AsXmlL()
       
  1260     {
       
  1261     _LIT8(KFramework,"<Framework ");
       
  1262     _LIT8(KXmlns,"xmlns=\"");
       
  1263     _LIT8(KQuot,"\" ");
       
  1264     _LIT8(KClass,"class=\"");
       
  1265     _LIT8(KClosedTag,"\"/>\n");
       
  1266     HBufC8* retVal = HBufC8::NewLC(256);
       
  1267     TPtr8 ptr = retVal->Des();
       
  1268     ptr.Append(KFramework); // Codescanner warnings
       
  1269     ptr.Append(KXmlns);
       
  1270     ptr.Append(KIdWsfFrameworkXMLNS);
       
  1271     ptr.Append(KQuot);
       
  1272 
       
  1273     ptr.Append(KClass);
       
  1274     ptr.Append(KIdWsFrameworkCue);
       
  1275     ptr.Append(KClosedTag);
       
  1276     CleanupStack::Pop(); // retVal
       
  1277     return retVal;
       
  1278     }
       
  1279 
       
  1280 TInt CSenIdWsfPlugin::OnEvent(const TInt aEvent,
       
  1281                             TAny* /* aArgument */)
       
  1282     {
       
  1283     TInt retVal(KErrNone);
       
  1284     TInt leaveCode(KErrNone);
       
  1285     switch(aEvent)
       
  1286         {
       
  1287         case KSenEventWsfReady:
       
  1288             // Core constructed                             
       
  1289             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"KSenEventWsfReady");
       
  1290             {
       
  1291             TRAP(leaveCode, retVal = AssignSessionValidatorsL());
       
  1292             }
       
  1293         break;
       
  1294         case KSenEventIdWsfSessionValidatorRequested:
       
  1295             {
       
  1296             // Someone needs validator
       
  1297             TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"KSenEventIdWsfSessionValidatorRequested");
       
  1298             TRAP(leaveCode, retVal = AssignSessionValidatorsL());
       
  1299             }
       
  1300         break;
       
  1301 #ifdef _SENDEBUG
       
  1302         default:
       
  1303             {
       
  1304             //TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"==> no action taken.");
       
  1305             }
       
  1306         break;
       
  1307 #endif // _SENDEBUG
       
  1308         }
       
  1309     // if leave occurred, return leave code instead
       
  1310     if(leaveCode)
       
  1311         {
       
  1312         retVal = leaveCode;
       
  1313         }
       
  1314 
       
  1315     return retVal;
       
  1316     }
       
  1317 
       
  1318 TAny* CSenIdWsfPlugin::ExtendedInterface()
       
  1319     {
       
  1320     // there is no extended interface in ID-WSF framework at the moment
       
  1321     return NULL;
       
  1322     }
       
  1323 
       
  1324 TInt CSenIdWsfPlugin::AssignSessionValidatorsL()
       
  1325     {
       
  1326     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"CSenIdWsfPlugin::AssignSessionValidatorsL()");
       
  1327 
       
  1328     CSenWSDescription* pOpenMask = CSenWSDescription::NewLC(
       
  1329             KNullDesC8, KNullDesC8); // this matches with *any* ServDescription
       
  1330     pOpenMask->SetFrameworkIdL(KDefaultIdWsfFrameworkID);
       
  1331     
       
  1332     RWSDescriptionArray all;
       
  1333     iManager.ServiceDescriptionsL(all, *pOpenMask);
       
  1334     CleanupClosePushL(all);
       
  1335 
       
  1336     RWSDescriptionArray asClients;
       
  1337     iManager.ServiceDescriptionsL(asClients, 
       
  1338                                 KIdWsfFrameworkAuthenticationServiceContract);
       
  1339     CleanupClosePushL(asClients);
       
  1340 
       
  1341     RWSDescriptionArray dsClients;
       
  1342     iManager.ServiceDescriptionsL(dsClients,
       
  1343                                 KIdWsfFrameworkDiscoveryServiceContract);
       
  1344     CleanupClosePushL(dsClients);
       
  1345 
       
  1346     TInt count(all.Count());
       
  1347     TInt asCount(asClients.Count());
       
  1348     TInt dsCount(dsClients.Count());
       
  1349 
       
  1350     for(TInt i=0; i<count; i++)
       
  1351         {   
       
  1352         CSenWSDescription* pSD = (CSenWSDescription*)all[i];
       
  1353 
       
  1354         if(pSD->DescriptionClassType() ==
       
  1355             MSenServiceDescription::EIdWSFServiceSession)
       
  1356             {
       
  1357             CIdWsfServiceSession* pSession = (CIdWsfServiceSession*)pSD;
       
  1358             if(!pSession->Validator() && pSession->TrustAnchor().Length()>0)
       
  1359                 {
       
  1360                 // session does not have validator, attempt to find one
       
  1361                 for(TInt d=0; d<dsCount; d++)
       
  1362                     {
       
  1363                     pSD = (CSenWSDescription*)dsClients[d];
       
  1364                     if(pSD->DescriptionClassType() ==
       
  1365                         MSenServiceDescription::EIdWsfDiscoveryServiceClient)
       
  1366                         {
       
  1367                         CIdWsfDiscoveryServiceClient* pDsClient =
       
  1368                             (CIdWsfDiscoveryServiceClient*)dsClients[d];
       
  1369                         TPtrC8 trustAnchor = pSession->TrustAnchor();
       
  1370                         if(trustAnchor == pDsClient->TrustAnchor())
       
  1371                             {
       
  1372                             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KNormalLogLevel  ,_L8("Validator (DSC): 0x%x"), pDsClient));
       
  1373                             pSession->SetValidator(pDsClient);
       
  1374                             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KNormalLogLevel  ,_L8("Validator (DSC): 0x%x"), pDsClient));
       
  1375                             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KNormalLogLevel  ,_L8("Validator for ID-WSF session found, trustanchor: %S"), &trustAnchor));
       
  1376                             d = dsCount; // break this loop
       
  1377                             }
       
  1378                         }
       
  1379 
       
  1380                     }
       
  1381                 }
       
  1382             }
       
  1383 
       
  1384         if(pSD->DescriptionClassType() ==
       
  1385             MSenServiceDescription::EIdWsfDiscoveryServiceClient)
       
  1386             {
       
  1387             CIdWsfDiscoveryServiceClient* pDsClient = 
       
  1388                         (CIdWsfDiscoveryServiceClient*)pSD;
       
  1389             if(!pDsClient->Validator() && 
       
  1390                 pDsClient->TrustAnchor().Length()>0)
       
  1391                 {
       
  1392                 // session does not have validator, attempt to find one
       
  1393                 for(TInt a=0; a<asCount; a++)
       
  1394                     {
       
  1395                     pSD = (CSenWSDescription*)asClients[a];
       
  1396                     if(pSD->DescriptionClassType() ==
       
  1397                         MSenServiceDescription::EIdWsfAuthenticationServiceClient)
       
  1398                         {
       
  1399                         CIdWsfAuthenticationServiceClient* pAsClient =
       
  1400                             (CIdWsfAuthenticationServiceClient*)asClients[a];
       
  1401 
       
  1402                         TPtrC8 trustAnchor = pDsClient->TrustAnchor();
       
  1403 
       
  1404                         if(trustAnchor == pAsClient->ProviderId())
       
  1405                             {
       
  1406                             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,_L8("Validator (ASC): 0x%x"), pAsClient));
       
  1407                             CSenIdentityProvider* pAccount =
       
  1408                                 IdentityProviderL(pAsClient->ProviderId());
       
  1409                             if(pAccount)
       
  1410                                 {
       
  1411                                 pAsClient->SetAccount(pAccount);
       
  1412                                 //pDsClient->SetValidator(pAsClient);
       
  1413 
       
  1414                                 pDsClient->IdWsfServiceSession()->SetValidator(pAsClient);
       
  1415                                 //pAsClient->ValidateL(*pDsClient->IdWsfServiceSession()); // TEST ONLY!!!!!
       
  1416                                 a = asCount; // break the loop, validator was found
       
  1417                                 TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase   , KMinLogLevel  ,_L8("Validator for DS client found, trustanchor: %S"), &trustAnchor));
       
  1418                                 }
       
  1419 #ifdef _SENDEBUG
       
  1420                             else
       
  1421                                 {
       
  1422                                 TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel ,"Account (IDP) for ASC not found! No DS validator available");
       
  1423                                 }
       
  1424 #endif
       
  1425                             }
       
  1426                         }
       
  1427 
       
  1428                     }
       
  1429                 }
       
  1430             }
       
  1431         }
       
  1432     
       
  1433     CleanupStack::PopAndDestroy(4); // close dsClients, close asClients
       
  1434                                     // close all, delete pOpenMask
       
  1435     return KErrNone;
       
  1436     }
       
  1437 
       
  1438 TInt CSenIdWsfPlugin::SetTransportPropertiesL(MSenTransport& aTransport)
       
  1439     {
       
  1440     //CSenHttpTransportProperties* pProps = CSenHttpTransportProperties::NewLC();
       
  1441     CSenTransportProperties* pProps = CSenTransportProperties::NewLC();
       
  1442     pProps->SetPropertyL(KSenSoapActionHeaderName, KSenSoapActionHeaderValueEmpty, KHttpHeaderType);        
       
  1443     HBufC8* pPropsAsXml = pProps->AsUtf8L();
       
  1444     CleanupStack::PopAndDestroy(pProps);
       
  1445 
       
  1446     CleanupStack::PushL( pPropsAsXml );
       
  1447     // In ID-WSF, even message layer will be forced to have an empty SOAPAction value(!), only
       
  1448     // the message's transport properties sent by consumer applicatoin may override this 
       
  1449     // (although they never should do that, it has been made possible for the application messages).
       
  1450     TInt retVal = aTransport.SetPropertiesL( *pPropsAsXml, MSenLayeredProperties::ESenMessageLayer, NULL );
       
  1451     TInt retVal2 = aTransport.SetPropertiesL( *pPropsAsXml, MSenLayeredProperties::ESenFrameworkLayer, NULL );
       
  1452     CleanupStack::PopAndDestroy( pPropsAsXml );
       
  1453     return retVal<retVal2?retVal:retVal2;
       
  1454     }
       
  1455 
       
  1456     
       
  1457 // End of File
       
  1458