webservices/wsoviplugin/src/wsoviregisterhandler.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 
       
    22 
       
    23 
       
    24 
       
    25 #include "wsoviregisterhandler.h"
       
    26 #include "wsovioauthclient.h"
       
    27 #include "sendebug.h"
       
    28 #include "senlogger.h"
       
    29 
       
    30 
       
    31 class CWSOviHandlerContext;
       
    32 // Create instance of concrete ECOM interface implementation
       
    33 CWSOviRegisterHandler* CWSOviRegisterHandler::NewL(TAny* aHandlerCtx)
       
    34     {
       
    35     
       
    36     MSenHandlerContext* handlerCtx =
       
    37         reinterpret_cast<MSenHandlerContext*>(aHandlerCtx);
       
    38     CWSOviRegisterHandler* self   = new (ELeave) CWSOviRegisterHandler(*handlerCtx);
       
    39     CleanupStack::PushL (self);
       
    40     self->ConstructL();
       
    41     CleanupStack::Pop(self);
       
    42     return self;
       
    43     }
       
    44 
       
    45 // Constructor
       
    46 CWSOviRegisterHandler::CWSOviRegisterHandler(MSenHandlerContext& aCtx):CSenSessionHandler(aCtx)
       
    47     {
       
    48     
       
    49     }
       
    50 
       
    51 // Destructor
       
    52 CWSOviRegisterHandler::~CWSOviRegisterHandler()
       
    53     {
       
    54     }
       
    55 
       
    56 // Second phase construction.
       
    57 void CWSOviRegisterHandler::ConstructL()
       
    58     {
       
    59     }
       
    60     
       
    61 TInt CWSOviRegisterHandler::InvokeL(MSenSessionContext& aCtx)
       
    62     {
       
    63     MSenServiceDescription& pServiceDescription = *(MSenServiceDescription*)aCtx.GetSenWSDescriptionL(WSOviContextKeys::KServiceDescription());
       
    64     const TDesC8* action = aCtx.GetDesC8L(WSOviContextKeys::KRegisterAction());
       
    65     TInt result(KErrNone);
       
    66     if (*action == WSOviContextValues::KActionRegister())
       
    67         {
       
    68         result = RegisterServiceDescriptionL(pServiceDescription);
       
    69         }
       
    70     else if (*action == WSOviContextValues::KActionUnregister())
       
    71         {
       
    72         result = UnregisterServiceDescriptionL(pServiceDescription);
       
    73         };
       
    74     return result;  
       
    75     }
       
    76     
       
    77 SenHandler::THandlerDirection CWSOviRegisterHandler::Direction() const
       
    78     {
       
    79         return SenHandler::EBoth;
       
    80     };
       
    81 SenHandler::THandlerPhase CWSOviRegisterHandler::Phase()
       
    82     {
       
    83         return SenHandler::EDiscovery;
       
    84     };
       
    85 
       
    86 //---------------------------------------------------------------------------
       
    87 // Attempt to register the ServiceDescription to the ServiceManager 
       
    88 //---------------------------------------------------------------------------
       
    89 //
       
    90 TInt CWSOviRegisterHandler::RegisterServiceDescriptionL(MSenServiceDescription& aServiceDescription )
       
    91     {
       
    92     TInt retval(KErrNone);
       
    93     CWSOviServiceSession* pSession = NULL;
       
    94     TPtrC8 contract = aServiceDescription.Contract();
       
    95     TPtrC8 endpoint = aServiceDescription.Endpoint();
       
    96 
       
    97     if (contract == KNullDesC8) 
       
    98         {
       
    99         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviRegisterHandler::RegisterServiceDescriptionL failed - KErrSenNoContract");
       
   100         retval = KErrSenNoContract;
       
   101         }
       
   102     else if(endpoint == KNullDesC8)
       
   103         {
       
   104         TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviRegisterHandler::RegisterServiceDescriptionL failed - KErrSenNoEndpoint");
       
   105         retval = KErrSenNoEndpoint;
       
   106         }
       
   107     else
       
   108         {
       
   109         if(contract == KWSOviAuthenticationServiceContract)
       
   110             {
       
   111             retval = RegisterAuthClientL(&aServiceDescription);
       
   112             }
       
   113         else if (aServiceDescription.DescriptionClassType() ==
       
   114                         MSenServiceDescription::EOviServiceSession)
       
   115             {
       
   116             //  description already is a session so we just add to core-DAO
       
   117             //  usecase:    when connection has been made, and service is registered 
       
   118             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("CWSOviRegisterHandler::RegisterServiceDescriptionL - session is already so just adding")));
       
   119                 pSession = (CWSOviServiceSession*) &aServiceDescription;
       
   120                 retval = iHandlerContext.GetSenCoreServiceManager()->AddServiceDescriptionL(pSession);
       
   121             }
       
   122         else
       
   123             {
       
   124             TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("CWSOviRegisterHandler::RegisterServiceDescriptionL - Creating session from description...")));
       
   125             pSession = CWSOviServiceSession::NewLC(*(CSIF*)iHandlerContext.GetAnyL(HandlerContextKey::KSIF()));	//codescannerwarnings
       
   126             //usecase:  when description has been read from db during bootup
       
   127             //          session has to be initialized (set validity time for example)
       
   128             retval = pSession->InitializeFromL(aServiceDescription);
       
   129             if (retval == KErrNone)
       
   130                 {
       
   131                 retval = iHandlerContext.GetSenCoreServiceManager()->AddServiceDescriptionL(pSession);
       
   132                 CleanupStack::Pop(pSession);
       
   133                 }
       
   134             else
       
   135                 {
       
   136                 CleanupStack::PopAndDestroy(pSession);
       
   137                 }
       
   138             }
       
   139         }
       
   140     return retval;
       
   141     }
       
   142     
       
   143     
       
   144 //---------------------------------------------------------------------------
       
   145 // Attempt to unregister the ServiceDescription from the ServiceManager 
       
   146 //---------------------------------------------------------------------------
       
   147 //
       
   148 TInt CWSOviRegisterHandler::UnregisterServiceDescriptionL(
       
   149     MSenServiceDescription& aServiceDescription)
       
   150     {
       
   151     TInt retval(KErrNone);
       
   152     CWSOviServiceSession *pSession = NULL;
       
   153     TPtrC8 contract = aServiceDescription.Contract();
       
   154 
       
   155     if(contract == KWSOviAuthenticationServiceContract)
       
   156         {
       
   157         retval = UnRegisterAuthClientL(&aServiceDescription);
       
   158         }
       
   159     else
       
   160         {
       
   161         if(aServiceDescription.DescriptionClassType() ==
       
   162                 MSenServiceDescription::EOviServiceSession)
       
   163             {
       
   164             pSession = (CWSOviServiceSession*) &aServiceDescription;
       
   165             retval = iHandlerContext.GetSenCoreServiceManager()->RemoveServiceDescriptionL(*pSession);
       
   166             }
       
   167         else
       
   168             {
       
   169 
       
   170             pSession = CWSOviServiceSession::NewLC(*(CSIF*)iHandlerContext.GetAnyL(HandlerContextKey::KSIF()));	//codescannerwarnings
       
   171             retval = pSession->InitializeFromL(aServiceDescription);
       
   172             if (retval == KErrNone)
       
   173                 {
       
   174                 RWSDescriptionArray sessions;
       
   175                 retval = iHandlerContext.GetSenCoreServiceManager()->ServiceDescriptionsL(sessions,aServiceDescription);
       
   176                 if (!retval && sessions.Count())
       
   177                     {
       
   178                     RServiceConsumerArray consumers;
       
   179                     ((CWSOviServiceSession*)sessions[0])->Consumers(consumers);
       
   180                     TInt count = consumers.Count();
       
   181                     consumers.Reset();
       
   182                     consumers.Close();
       
   183                     sessions.Reset();
       
   184                     sessions.Close();
       
   185                     if (count)
       
   186                         {
       
   187                         CleanupStack::PopAndDestroy(pSession);
       
   188                         return KErrInUse;
       
   189                         }
       
   190                     }
       
   191                 retval = iHandlerContext.GetSenCoreServiceManager()->RemoveServiceDescriptionL(*pSession);
       
   192                 }
       
   193             CleanupStack::PopAndDestroy(pSession);
       
   194             }
       
   195         }
       
   196 
       
   197     return retval;
       
   198     }    
       
   199 
       
   200 //---------------------------------------------------------------------------
       
   201 // Register specific description (Auth contract)
       
   202 //---------------------------------------------------------------------------
       
   203 //    
       
   204 TInt CWSOviRegisterHandler::RegisterAuthClientL(
       
   205                                 MSenServiceDescription *aServiceDescription)
       
   206     {
       
   207     TLSLOG_L(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,"CWSOviRegisterHandler::RegisterNAUTHClientL");
       
   208     
       
   209     TPtrC8 endpoint = aServiceDescription->Endpoint();
       
   210     TInt retval = KErrNone;
       
   211     CWSOviServiceSession* pSession = NULL;
       
   212     CWSOviOAuthClient* pAuthClient = NULL;
       
   213 
       
   214     if(aServiceDescription->DescriptionClassType() ==
       
   215                 MSenServiceDescription::EOviOAuthClient)
       
   216         {
       
   217         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("CWSOviRegisterHandler::RegisterNAUTHClient - This ServiceDescription is already an NAUTH Client")));
       
   218         pAuthClient = (CWSOviOAuthClient*)aServiceDescription;
       
   219         retval = iHandlerContext.GetSenCoreServiceManager()->AddServiceDescriptionL(pAuthClient);
       
   220         }
       
   221     else
       
   222         {
       
   223         // Create new auth client and initialize
       
   224         // it from given description
       
   225         pSession = CWSOviServiceSession::NewLC(*(CSIF*)iHandlerContext.GetAnyL(HandlerContextKey::KSIF()));
       
   226         retval = pSession->InitializeFromL(*aServiceDescription);
       
   227 
       
   228         if(retval != KErrNone)
       
   229             {
       
   230             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("CWSOviRegisterHandler::RegisterNAUTHClientL failed %d"), retval));
       
   231             CleanupStack::PopAndDestroy(pSession);
       
   232             return retval;
       
   233             }
       
   234         TPtrC8 sessionEndpoint = pSession->Endpoint();
       
   235             TLSLOG_FORMAT((KSenCoreServiceManagerLogChannelBase  , KMinLogLevel, _L8("CWSOviRegisterHandler::RegisterNAUTHClientL created NAUTH using endpoint:'%S'"), &sessionEndpoint));
       
   236 
       
   237         pAuthClient = CWSOviOAuthClient::NewLC(*(CSIF*)iHandlerContext.GetAnyL(HandlerContextKey::KSIF()));	//codescannerwarnings
       
   238         pAuthClient->SetAuthSessionL(pSession);    // pSession will be owned by Auth Client
       
   239         CleanupStack::Pop(pAuthClient);
       
   240         CleanupStack::Pop(pSession);
       
   241         CleanupStack::PushL(pAuthClient);
       
   242 
       
   243         retval = iHandlerContext.GetSenCoreServiceManager()->AddServiceDescriptionL(pAuthClient);
       
   244         if(retval != KErrNone)
       
   245             {
       
   246             delete pAuthClient;
       
   247             }
       
   248         CleanupStack::Pop(pAuthClient); 
       
   249         pAuthClient = NULL;
       
   250         }
       
   251     return retval;
       
   252     }
       
   253 
       
   254 //---------------------------------------------------------------------------
       
   255 // Unregister specific description (Auth contract)
       
   256 //---------------------------------------------------------------------------
       
   257 //    
       
   258 
       
   259 TInt CWSOviRegisterHandler::UnRegisterAuthClientL(
       
   260                                 MSenServiceDescription *aServiceDescription)
       
   261     {
       
   262     TInt retval = KErrNone;
       
   263 
       
   264     CWSOviServiceSession* pSession = NULL;
       
   265     CWSOviOAuthClient* pAuthClient = NULL;
       
   266 
       
   267     if(aServiceDescription->DescriptionClassType() ==
       
   268                 MSenServiceDescription::EOviOAuthClient)
       
   269         {
       
   270         TLSLOG(KSenCoreServiceManagerLogChannelBase  , KMinLogLevel,(_L("CWSOviRegisterHandler::CWSOviRegisterHandler - This ServiceDescription is already an NAUTH Client")));
       
   271         pAuthClient = (CWSOviOAuthClient*)aServiceDescription;
       
   272         retval = iHandlerContext.GetSenCoreServiceManager()->RemoveServiceDescriptionL(*pAuthClient);
       
   273         }
       
   274     else
       
   275         {
       
   276         
       
   277         pSession = CWSOviServiceSession::NewLC(*(CSIF*)iHandlerContext.GetAnyL(HandlerContextKey::KSIF()));
       
   278         retval = pSession->InitializeFromL(*aServiceDescription);
       
   279         if(retval != KErrNone)
       
   280             {
       
   281             CleanupStack::PopAndDestroy(pSession);
       
   282             return retval;
       
   283             }
       
   284         pAuthClient = CWSOviOAuthClient::NewLC(*(CSIF*)iHandlerContext.GetAnyL(HandlerContextKey::KSIF));	
       
   285         pAuthClient->SetAuthSessionL(pSession);    // pSession will be owned by Auth Client
       
   286         CleanupStack::Pop(pAuthClient);
       
   287         CleanupStack::Pop(pSession);
       
   288         CleanupStack::PushL(pAuthClient);
       
   289 
       
   290         retval = iHandlerContext.GetSenCoreServiceManager()->RemoveServiceDescriptionL(*pAuthClient);
       
   291         CleanupStack::PopAndDestroy(pAuthClient);
       
   292         }
       
   293 
       
   294     return retval;
       
   295     }    
       
   296     
       
   297 //---------------------------------------------------------------------------
       
   298 // Logger using during DEBUG mode
       
   299 //---------------------------------------------------------------------------
       
   300 //    
       
   301 
       
   302 TInt CWSOviRegisterHandler::InitL(MSenHandlerContext& aCtx)
       
   303     {
       
   304     iHandlerContext = aCtx;
       
   305     return KErrNone;
       
   306     }
       
   307     
       
   308 // END OF FILE
       
   309