webservices/wsdescription/src/servicepolicy.cpp
changeset 0 62f9d29f7211
child 1 272b002df977
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 <e32std.h>
       
    27 #include <s32strm.h>
       
    28 #include "sendebug.h"                 // internal Utils\inc - logging MACROs
       
    29 
       
    30 #include <SenElement.h>
       
    31 #include <SenXmlUtils.h>
       
    32 #include "SenXmlElement.h"
       
    33 #include "servicepolicy.h"
       
    34 #include "SenIdentityProviderIdArray8.h"
       
    35 #include "senguidgen.h"
       
    36 #include "senlogger.h"
       
    37 	
       
    38 namespace
       
    39     {
       
    40     _LIT8(KServicePolicyLocalName, "ServicePolicy");
       
    41     _LIT8(KClientServicePolicyLocalName, "ClientPolicy");
       
    42     _LIT8(KPolicyArrayLocalName, "PolicyArray");
       
    43     _LIT8(KMetaDataLocalName, "MetadataEndpoint");
       
    44     _LIT8(KName, "Name");
       
    45     _LIT8(KSenNsUri, "urn:com.nokia.Sen.config.1.0");
       
    46     _LIT8(KPolicyLocalName, "Policy");
       
    47     _LIT8(KWsPolicyNsUri , "http://schemas.xmlsoap.org/ws/2004/09/policy"); // Namespace of WS-Policy
       
    48     _LIT8(KId, "wsu:Id"); 
       
    49 	_LIT8(KWsuPrefix, "wsu"); 
       
    50 	_LIT8(KWsuNsUri, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd");
       
    51     _LIT8(KAppInfoLocalName, "AppInfo");
       
    52     _LIT8(KApplicationLocalName, "Application");
       
    53     _LIT8(KDeviceOptionsLocalName, "DeviceOptions");
       
    54     _LIT8(KUserAgentLocalName, "UserAgent");
       
    55     _LIT8(KOSLocalName, "OS");
       
    56     //_LIT8(KAppInfoNsUri , "http://schemas.live.com/mws/2006/10/core");
       
    57     }
       
    58 
       
    59 
       
    60 CServicePolicy* CServicePolicy::NewL()
       
    61     {
       
    62     CServicePolicy* pNew = NewLC();
       
    63     CleanupStack::Pop();
       
    64     return(pNew) ;
       
    65     }
       
    66 
       
    67 CServicePolicy* CServicePolicy::NewLC()
       
    68     {
       
    69     CServicePolicy* pNew = new (ELeave) CServicePolicy();
       
    70     CleanupStack::PushL(pNew);
       
    71     pNew->BaseConstructL(KServicePolicyLocalName());
       
    72     return pNew;
       
    73     }
       
    74 
       
    75 CServicePolicy::CServicePolicy()
       
    76     {
       
    77     }
       
    78 
       
    79 CServicePolicy::~CServicePolicy()
       
    80     {
       
    81     TLSLOG_CLOSE(KSenServConnLogChannelBase);
       
    82     //LOG_CLOSE;
       
    83     }
       
    84 
       
    85 void CServicePolicy::BaseConstructL(const TDesC8& aPolicyLocalName)
       
    86     {
       
    87     CSenBaseFragment::BaseConstructL(aPolicyLocalName);
       
    88 #ifdef _SENDEBUG    
       
    89     _LIT(KSenServConn, "SenServicePolicy");
       
    90     _LIT(KSenServConnLog, "SenServicePolicy.log");
       
    91     
       
    92     
       
    93     // Open connection to the file logger server
       
    94     TLSLOG_OPEN(KSenServConnLogChannelBase,KSenServConnLogLevel ,KSenServConn,KSenServConnLog);
       
    95     //LOG_CREATELOG((KSenServConn, KSenServConnLog, EFileLoggingModeOverwrite));
       
    96 #endif    
       
    97     }
       
    98 
       
    99 //Do nothing
       
   100 TBool CServicePolicy::Accepts(MSenServicePolicy& aPolicyPattern)
       
   101     {
       
   102     TBool retVal(ETrue);
       
   103     TRAPD(err, retVal = AcceptsL(aPolicyPattern));
       
   104     if (err) return EFalse;
       
   105     return retVal;
       
   106     }
       
   107 
       
   108 //Do nothing
       
   109 TBool CServicePolicy::AcceptsL(MSenServicePolicy& /*aPolicyPattern*/)
       
   110     {
       
   111     return ETrue;
       
   112     }
       
   113 
       
   114 //Return all of the Services polices from SD only <wsp:Policy> tags
       
   115 TInt CServicePolicy::ServicePolicies(RPolicyArray& aPolicies)
       
   116     {
       
   117     TInt err( KErrNone );
       
   118     CSenElement* pPolicyArrayElement = AsElement().Element(KPolicyArrayLocalName);
       
   119     if(pPolicyArrayElement)
       
   120         {
       
   121         TRAP( err, aPolicies = pPolicyArrayElement->ElementsL(); ) //, KWsPolicyNsUri, KPolicyLocalName);
       
   122         if(!err && aPolicies.Count() > 0)
       
   123            {
       
   124            return KErrNone;
       
   125            }
       
   126         return KErrNotFound;
       
   127         }
       
   128     return KErrNotFound;        
       
   129     }
       
   130 
       
   131 //Adding client policy to SD
       
   132 TInt CServicePolicy::AddClientPolicyL(CSenElement* aTemplate)
       
   133     {
       
   134     CSenElement* pServicePolicyElement = AsElement().Element(KServicePolicyLocalName);
       
   135     CSenElement* pClientPolicyElement = AsElement().Element(KClientServicePolicyLocalName);
       
   136     
       
   137     if(!pClientPolicyElement)
       
   138         {
       
   139         pClientPolicyElement = AsElement().Element(KSenNsUri, KClientServicePolicyLocalName);
       
   140         if(!pClientPolicyElement)
       
   141             {
       
   142             pClientPolicyElement = &(AsElement().AddElementL(KClientServicePolicyLocalName));  
       
   143             }
       
   144         }
       
   145     
       
   146     if(!pClientPolicyElement->ConsistsOfL(*aTemplate))
       
   147         {
       
   148         RPointerArray<CSenElement> children = aTemplate->ElementsL();
       
   149         for(TInt i = 0; i< children.Count(); i++)
       
   150             {
       
   151                 CSenElement* ele = children[i];
       
   152                 
       
   153                 if(!ele->LocalName().Compare(KAppInfoLocalName))
       
   154                 {
       
   155                     AddApplicationInfoElementsL(ele);
       
   156                 }
       
   157                 else
       
   158                 {
       
   159                      CSenElement& added = pClientPolicyElement->AddElementL(ele->LocalName());
       
   160                      
       
   161                     TPtrC8 sourceContent = ele->Content();
       
   162                     if(sourceContent.Length() > 0)
       
   163                         {
       
   164                         added.SetContentL(sourceContent);
       
   165                         }
       
   166                     RPointerArray<CSenBaseAttribute> sourceAttributes = ele->AttributesL();
       
   167                     if (sourceAttributes.Count() > 0)
       
   168                         {
       
   169                         for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   170                             {
       
   171                             CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   172                             SenXmlUtils::AddAttributeL(added, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   173                             }
       
   174                         }
       
   175                             
       
   176                 }
       
   177                 
       
   178             }
       
   179         }
       
   180     return KErrNone;
       
   181     }
       
   182 void CServicePolicy::AddApplicationInfoElementsL(CSenElement* aApplicationInfoElement)
       
   183     {
       
   184     CSenElement* pClientPolicyElement = AsElement().Element(KClientServicePolicyLocalName);
       
   185     CSenElement* pAppPolicyElement = pClientPolicyElement->Element(KAppInfoLocalName);
       
   186     
       
   187     if(!pAppPolicyElement)
       
   188         {
       
   189         pAppPolicyElement = pClientPolicyElement->Element(KSenNsUri, KAppInfoLocalName);
       
   190         if(!pAppPolicyElement)
       
   191             {
       
   192             pAppPolicyElement = &(pClientPolicyElement->AddElementL(KAppInfoLocalName));  
       
   193             }
       
   194         }
       
   195              
       
   196     TPtrC8 sourceContent = aApplicationInfoElement->Content();
       
   197     if(sourceContent.Length() > 0)
       
   198         {
       
   199         pAppPolicyElement->SetContentL(sourceContent);
       
   200         }
       
   201         
       
   202     RPointerArray<CSenBaseAttribute> sourceAttributes = aApplicationInfoElement->AttributesL();
       
   203     if (sourceAttributes.Count() > 0)
       
   204         {
       
   205         for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   206             {
       
   207             CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   208             SenXmlUtils::AddAttributeL(*pAppPolicyElement, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   209             }
       
   210         }
       
   211         
       
   212     RPointerArray<CSenElement> children = aApplicationInfoElement->ElementsL();
       
   213     for(TInt i = 0; i< children.Count(); i++)
       
   214         {
       
   215             CSenElement* ele = children[i];
       
   216             
       
   217             if(!ele->LocalName().Compare(KApplicationLocalName))
       
   218             {
       
   219                 AddAppicationElementsL(ele);
       
   220             }
       
   221             else if(!ele->LocalName().Compare(KDeviceOptionsLocalName))
       
   222             {
       
   223                 AddDeviceElementsL(ele);
       
   224             }
       
   225             else
       
   226             {
       
   227                 CSenElement& added = pAppPolicyElement->AddElementL(ele->LocalName());
       
   228                 TPtrC8 sourceContent = ele->Content();
       
   229                 if(sourceContent.Length() > 0)
       
   230                     {
       
   231                     added.SetContentL(sourceContent);
       
   232                     }
       
   233                 RPointerArray<CSenBaseAttribute> sourceAttributes = ele->AttributesL();
       
   234                 if (sourceAttributes.Count() > 0)
       
   235                     {
       
   236                     for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   237                         {
       
   238                         CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   239                         SenXmlUtils::AddAttributeL(added, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   240                         }
       
   241                     }
       
   242             }
       
   243          }
       
   244 
       
   245      }
       
   246 void CServicePolicy::AddAppicationElementsL(CSenElement* aApplicationElement)
       
   247     {
       
   248     CSenElement* pClientPolicyElement = AsElement().Element(KClientServicePolicyLocalName);
       
   249     CSenElement* pAppInfoPolicyElement = pClientPolicyElement->Element(KAppInfoLocalName);
       
   250     CSenElement* pAppPolicyElement = pAppInfoPolicyElement->Element(KApplicationLocalName);
       
   251     
       
   252     if(!pAppPolicyElement)
       
   253         {
       
   254         pAppPolicyElement = pAppInfoPolicyElement->Element(KSenNsUri, KApplicationLocalName);
       
   255         if(!pAppPolicyElement)
       
   256             {
       
   257             pAppPolicyElement = &(pAppInfoPolicyElement->AddElementL(KApplicationLocalName));  
       
   258             }
       
   259         }
       
   260     TPtrC8 sourceContent = aApplicationElement->Content();
       
   261     if(sourceContent.Length() > 0)
       
   262         {
       
   263         pAppPolicyElement->SetContentL(sourceContent);
       
   264         }
       
   265         
       
   266     RPointerArray<CSenBaseAttribute> sourceAttributes = aApplicationElement->AttributesL();
       
   267     if (sourceAttributes.Count() > 0)
       
   268         {
       
   269         for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   270             {
       
   271             CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   272             SenXmlUtils::AddAttributeL(*pAppPolicyElement, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   273             }
       
   274         }
       
   275         
       
   276     RPointerArray<CSenNamespace> sourceNamespaces = aApplicationElement->NamespacesL();
       
   277     if (sourceNamespaces.Count() > 0)
       
   278         {
       
   279         for (TInt i=0;i<sourceNamespaces.Count(); i++)
       
   280             {
       
   281             CSenNamespace* pNameSapce = sourceNamespaces[i];
       
   282             pAppPolicyElement->AddNamespaceL(*pNameSapce,ETrue);
       
   283             }
       
   284         }
       
   285         
       
   286     RPointerArray<CSenElement> children = aApplicationElement->ElementsL();
       
   287     for(TInt i = 0; i< children.Count(); i++)
       
   288         {
       
   289             CSenElement* ele = children[i];
       
   290             CSenElement& added = pAppPolicyElement->AddElementL(ele->LocalName());
       
   291              
       
   292           TPtrC8 sourceContent = ele->Content();
       
   293             if(sourceContent.Length() > 0)
       
   294                 {
       
   295                 added.SetContentL(sourceContent);
       
   296                 }
       
   297             RPointerArray<CSenBaseAttribute> sourceAttributes = ele->AttributesL();
       
   298             if (sourceAttributes.Count() > 0)
       
   299                 {
       
   300                 for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   301                     {
       
   302                     CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   303                     SenXmlUtils::AddAttributeL(added, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   304                     }
       
   305                 }
       
   306              
       
   307         }
       
   308 
       
   309      }
       
   310 void CServicePolicy::AddDeviceElementsL(CSenElement* aDeviceElement)
       
   311     {
       
   312     CSenElement* pClientPolicyElement = AsElement().Element(KClientServicePolicyLocalName);
       
   313     CSenElement* pAppPolicyElement = pClientPolicyElement->Element(KAppInfoLocalName);
       
   314     CSenElement* pDevicePolicyElement = pAppPolicyElement->Element(KDeviceOptionsLocalName);
       
   315     
       
   316     if(!pDevicePolicyElement)
       
   317         {
       
   318         pDevicePolicyElement = pAppPolicyElement->Element(KSenNsUri, KDeviceOptionsLocalName);
       
   319         if(!pDevicePolicyElement)
       
   320             {
       
   321             pDevicePolicyElement = &(pAppPolicyElement->AddElementL(KDeviceOptionsLocalName));  
       
   322             }
       
   323         }
       
   324             
       
   325     TPtrC8 sourceContent = aDeviceElement->Content();
       
   326     if(sourceContent.Length() > 0)
       
   327         {
       
   328         pDevicePolicyElement->SetContentL(sourceContent);
       
   329         }
       
   330         
       
   331     RPointerArray<CSenBaseAttribute> sourceAttributes = aDeviceElement->AttributesL();
       
   332     if (sourceAttributes.Count() > 0)
       
   333         {
       
   334         for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   335             {
       
   336             CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   337             SenXmlUtils::AddAttributeL(*pDevicePolicyElement, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   338             }
       
   339         }
       
   340         
       
   341     RPointerArray<CSenNamespace> sourceNamespaces = aDeviceElement->NamespacesL();
       
   342     if (sourceNamespaces.Count() > 0)
       
   343         {
       
   344         for (TInt i=0;i<sourceNamespaces.Count(); i++)
       
   345             {
       
   346             CSenNamespace* pNameSapce = sourceNamespaces[i];
       
   347             pDevicePolicyElement->AddNamespaceL(*pNameSapce,ETrue);
       
   348             }
       
   349         }
       
   350             
       
   351     RPointerArray<CSenElement> children = aDeviceElement->ElementsL();
       
   352     for(TInt i = 0; i< children.Count(); i++)
       
   353         {
       
   354             CSenElement* ele = children[i];
       
   355                     
       
   356             if(!ele->LocalName().Compare(KUserAgentLocalName))
       
   357             {
       
   358                 AddUserAgentElementsL(ele);
       
   359             }
       
   360             else
       
   361             {
       
   362 
       
   363             CSenElement& added = pDevicePolicyElement->AddElementL(ele->LocalName());
       
   364              
       
   365           TPtrC8 sourceContent = ele->Content();
       
   366             if(sourceContent.Length() > 0)
       
   367                 {
       
   368                 added.SetContentL(sourceContent);
       
   369                 }
       
   370             RPointerArray<CSenBaseAttribute> sourceAttributes = ele->AttributesL();
       
   371             if (sourceAttributes.Count() > 0)
       
   372                 {
       
   373                 for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   374                     {
       
   375                     CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   376                     SenXmlUtils::AddAttributeL(added, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   377                     }
       
   378                 }
       
   379                 
       
   380             } 
       
   381              
       
   382         }
       
   383 
       
   384      }
       
   385 void CServicePolicy::AddUserAgentElementsL(CSenElement* aUserAgentElement)
       
   386     {
       
   387     CSenElement* pClientPolicyElement = AsElement().Element(KClientServicePolicyLocalName);
       
   388     CSenElement* pAppPolicyElement = pClientPolicyElement->Element(KAppInfoLocalName);
       
   389     CSenElement* pDevicePolicyElement = pAppPolicyElement->Element(KDeviceOptionsLocalName);
       
   390     CSenElement* pUserAgentElement = pDevicePolicyElement->Element(KUserAgentLocalName);
       
   391     
       
   392     if(!pUserAgentElement)
       
   393         {
       
   394         pUserAgentElement = pDevicePolicyElement->Element(KSenNsUri, KUserAgentLocalName);
       
   395         if(!pUserAgentElement)
       
   396             {
       
   397             pUserAgentElement = &(pDevicePolicyElement->AddElementL(KUserAgentLocalName));  
       
   398             }
       
   399         }
       
   400             
       
   401     TPtrC8 sourceContent = aUserAgentElement->Content();
       
   402     if(sourceContent.Length() > 0)
       
   403         {
       
   404         pUserAgentElement->SetContentL(sourceContent);
       
   405         }
       
   406         
       
   407     RPointerArray<CSenBaseAttribute> sourceAttributes = aUserAgentElement->AttributesL();
       
   408     if (sourceAttributes.Count() > 0)
       
   409         {
       
   410         for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   411             {
       
   412             CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   413             SenXmlUtils::AddAttributeL(*pUserAgentElement, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   414             }
       
   415         }
       
   416         
       
   417     RPointerArray<CSenElement> children = aUserAgentElement->ElementsL();
       
   418     for(TInt i = 0; i< children.Count(); i++)
       
   419         {
       
   420             CSenElement* ele = children[i];
       
   421             
       
   422             if(!ele->LocalName().Compare(KOSLocalName))
       
   423             {
       
   424                 AddOSElementsL(ele);
       
   425             }
       
   426             else
       
   427             {
       
   428 
       
   429             CSenElement& added = pUserAgentElement->AddElementL(ele->LocalName());
       
   430              
       
   431           TPtrC8 sourceContent = ele->Content();
       
   432             if(sourceContent.Length() > 0)
       
   433                 {
       
   434                 added.SetContentL(sourceContent);
       
   435                 }
       
   436             RPointerArray<CSenBaseAttribute> sourceAttributes = ele->AttributesL();
       
   437             if (sourceAttributes.Count() > 0)
       
   438                 {
       
   439                 for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   440                     {
       
   441                     CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   442                     SenXmlUtils::AddAttributeL(added, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   443                     }
       
   444                 }
       
   445              
       
   446             } 
       
   447         }
       
   448 
       
   449      }
       
   450 void CServicePolicy::AddOSElementsL(CSenElement* aOSElement)
       
   451     {
       
   452     CSenElement* pClientPolicyElement = AsElement().Element(KClientServicePolicyLocalName);
       
   453     CSenElement* pAppPolicyElement = pClientPolicyElement->Element(KAppInfoLocalName);
       
   454     CSenElement* pDevicePolicyElement = pAppPolicyElement->Element(KDeviceOptionsLocalName);
       
   455     CSenElement* pUserAgentElement = pDevicePolicyElement->Element(KUserAgentLocalName);
       
   456     CSenElement* pOSElement = pUserAgentElement->Element(KOSLocalName);
       
   457     
       
   458     if(!pOSElement)
       
   459         {
       
   460         pOSElement = pUserAgentElement->Element(KSenNsUri, KOSLocalName);
       
   461         if(!pOSElement)
       
   462             {
       
   463             pOSElement = &(pUserAgentElement->AddElementL(KOSLocalName));  
       
   464             }
       
   465         }
       
   466             
       
   467     TPtrC8 sourceContent = aOSElement->Content();
       
   468     if(sourceContent.Length() > 0)
       
   469         {
       
   470         pOSElement->SetContentL(sourceContent);
       
   471         }
       
   472         
       
   473     RPointerArray<CSenBaseAttribute> sourceAttributes = aOSElement->AttributesL();
       
   474     if (sourceAttributes.Count() > 0)
       
   475         {
       
   476         for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   477             {
       
   478             CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   479             SenXmlUtils::AddAttributeL(*pOSElement, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   480             }
       
   481         }
       
   482         
       
   483     RPointerArray<CSenElement> children = aOSElement->ElementsL();
       
   484     for(TInt i = 0; i< children.Count(); i++)
       
   485         {
       
   486             CSenElement* ele = children[i];
       
   487             CSenElement& added = pOSElement->AddElementL(ele->LocalName());
       
   488              
       
   489           TPtrC8 sourceContent = ele->Content();
       
   490             if(sourceContent.Length() > 0)
       
   491                 {
       
   492                 added.SetContentL(sourceContent);
       
   493                 }
       
   494             RPointerArray<CSenBaseAttribute> sourceAttributes = ele->AttributesL();
       
   495             if (sourceAttributes.Count() > 0)
       
   496                 {
       
   497                 for (TInt i=0;i<sourceAttributes.Count(); i++)
       
   498                     {
       
   499                     CSenBaseAttribute* pBaseAttribute = sourceAttributes[i];
       
   500                     SenXmlUtils::AddAttributeL(added, pBaseAttribute->Name(), pBaseAttribute->Value());
       
   501                     }
       
   502                 }
       
   503              
       
   504         }
       
   505 
       
   506      }
       
   507 
       
   508 // Public function for adding a policy
       
   509 TInt CServicePolicy::AddPolicyL(CSenElement* aPolicy)
       
   510     {
       
   511     TInt retVal(KErrNotFound);
       
   512     retVal = ValidatePolicy(aPolicy);
       
   513     
       
   514 #ifdef _SENDEBUG
       
   515     {
       
   516     TLSLOG(KSenServConnLogChannelBase,KMaxLogLevel,(_L("TInt CServicePolicy::AddPolicyL  POLICY")));
       
   517     TLSLOG(KSenServConnLogChannelBase,KMaxLogLevel,(_L("-----------DIRECT ADDING---------------")));
       
   518     TLSLOG(KSenServConnLogChannelBase,KMaxLogLevel,(_L("---------------------------------------")));
       
   519     TLSLOG(KSenServConnLogChannelBase,KMaxLogLevel,(_L("---------------------------------------")));
       
   520     HBufC8* xml = aPolicy->AsXmlL();
       
   521     if(xml)
       
   522         {
       
   523         TLSLOG_ALL(KSenCoreServiceManagerLogChannelBase  , KSenCoreServiceManagerLogLevel,(*xml));
       
   524 
       
   525         }
       
   526         delete xml;
       
   527     }    
       
   528 #endif // _SENDEBUG
       
   529 
       
   530     if(retVal)
       
   531         {
       
   532         retVal =  AddWspPolicyL(aPolicy); 
       
   533         }
       
   534     return retVal;
       
   535     }
       
   536 //Add a policy to SD
       
   537 TInt CServicePolicy::AddWspPolicyL(CSenElement* aPolicy)
       
   538     {
       
   539     TInt retVal(KErrNotFound);
       
   540     CSenElement& pServicePolicy = AsElement();
       
   541     CSenElement* pPolicyArray = pServicePolicy.Element(KPolicyArrayLocalName);
       
   542     //get the PolicyArray which is holding all the <wsp:policy> elements
       
   543     if(!pPolicyArray)
       
   544         {
       
   545         pPolicyArray = pServicePolicy.Element(KSenNsUri, KPolicyArrayLocalName);
       
   546         if(!pPolicyArray)
       
   547             pPolicyArray = &(pServicePolicy.AddElementL(KPolicyArrayLocalName));
       
   548         }
       
   549     //get all the policies from <PolicyArray>        
       
   550     RPointerArray<CSenElement> policies;
       
   551     pPolicyArray->ElementsL(policies,KWsPolicyNsUri,KPolicyLocalName);
       
   552     
       
   553     CSenElement* pPolicy = NULL;
       
   554    
       
   555     TInt count(policies.Count());
       
   556     //If we have existing polcies then verify that this new policy doesnt exist there
       
   557     if(count > 0)
       
   558         {    
       
   559         for(TInt i=0; i<count; i++)
       
   560             {
       
   561             pPolicy = policies[i];
       
   562             if(pPolicy)
       
   563                 {
       
   564                     if(pPolicy->LocalName().Compare(aPolicy->LocalName()) == 0 &&
       
   565                     (PolicyIdL(pPolicy).Compare(PolicyIdL(aPolicy)) != 0))	//codescannerwarnings
       
   566                         {
       
   567                         //same policy with same wsu:Id then 
       
   568                         // We are trying to add a policy with endpoint same
       
   569                         // it means that our current policy is expired and we have 
       
   570                         // fetched new policy
       
   571                         // we have to delete here old policy and then add new policy
       
   572                             if(PolicyUriL(pPolicy).Compare(PolicyUriL(aPolicy)) == 0)
       
   573                             {
       
   574                                 CSenElement* deletedPolicy = pPolicyArray->RemoveElement(*pPolicy);
       
   575                                 delete deletedPolicy;
       
   576                             }
       
   577                         //then add fresh policy here
       
   578                         CSenElement& apol= pPolicyArray->AddElementL(KPolicyLocalName);
       
   579                         apol.CopyFromL(*aPolicy);                        
       
   580                         retVal = KErrNone;
       
   581                         policies.Close();
       
   582                         return retVal;
       
   583                         }
       
   584                 }
       
   585 
       
   586             }
       
   587         }
       
   588     else //Just add this policy in the <PolicyArray>
       
   589         {
       
   590         
       
   591         CSenElement& apol= pPolicyArray->AddElementL(KPolicyLocalName);
       
   592         apol.CopyFromL(*aPolicy);                                
       
   593         PolicyIdL(&apol);	//codescannerwarnings
       
   594         retVal = KErrNone;
       
   595         }
       
   596      
       
   597      policies.Close();
       
   598     return retVal;
       
   599     }
       
   600 
       
   601 //Remove a policy from current SD
       
   602 TInt CServicePolicy::RemovePolicyL(TDesC8& aPolicyUri)
       
   603     {
       
   604     TInt retVal(KErrNone);
       
   605     CSenElement& pServicePolicy = AsElement();
       
   606     CSenElement* pPolicyArray = pServicePolicy.Element(KPolicyArrayLocalName);
       
   607 //get the PolicyArray which is holding all the <wsp:policy> elements
       
   608     if(!pPolicyArray)
       
   609         {
       
   610         pPolicyArray = pServicePolicy.Element(KSenNsUri, KPolicyArrayLocalName);
       
   611         if(!pPolicyArray)
       
   612                retVal = KErrNotFound; 
       
   613         }
       
   614     if(retVal == KErrNone)
       
   615 		{
       
   616 		//get all the policies from <PolicyArray>        
       
   617 		RPointerArray<CSenElement> policies;
       
   618 		CleanupClosePushL(policies);
       
   619 		pPolicyArray->ElementsL(policies,KWsPolicyNsUri,KPolicyLocalName);
       
   620     
       
   621 		CSenElement* pPolicy = NULL;
       
   622    
       
   623 		TInt count(policies.Count());
       
   624 //If we have existing polcies then verify that this new policy doesnt exist there
       
   625 		if(count > 0)
       
   626 			{    
       
   627 			for(TInt i=0; i<count; i++)
       
   628 				{
       
   629 				pPolicy = policies[i];
       
   630 				if(pPolicy)
       
   631 					{
       
   632 						if(PolicyNameL(pPolicy).Compare(aPolicyUri) == 0)	//codescannerwarnings
       
   633 							{
       
   634                         //same policy with same NAME 
       
   635                         CSenElement* removed = pPolicyArray->RemoveElement(*pPolicy);
       
   636                         delete removed;
       
   637                         retVal = KErrNone; 
       
   638 							}
       
   639 					}
       
   640 
       
   641 				}
       
   642 			}
       
   643 		CleanupStack::PopAndDestroy(); // policies
       
   644 		}
       
   645     return retVal;
       
   646     }
       
   647 
       
   648 
       
   649 // sets (rebuilds) the Service Policy in SD from template
       
   650 // @return  - a error, if at least one addition of new contents has
       
   651 // failed OR KerrNone if every property was successfully reset
       
   652 TInt CServicePolicy::RebuildServicePolicyFrom(MSenServicePolicy& aTemplate)
       
   653     {
       
   654     TInt retVal(KErrNone);
       
   655     TRAPD(err, retVal = RebuildServicePolicyFromL(aTemplate));
       
   656     if (err != KErrNone)
       
   657         {
       
   658         retVal = err;
       
   659         }
       
   660     return retVal;
       
   661     }
       
   662 
       
   663 TInt CServicePolicy::RebuildServicePolicyFromL(MSenServicePolicy& /*aTemplate*/)
       
   664     {
       
   665     TInt retVal(KErrNone);
       
   666     // check for possible provider policy definition
       
   667     return retVal;
       
   668     }
       
   669     
       
   670 // Returns a Policy ID value. 
       
   671 // If the value not found then generate a random 
       
   672 // value and assign it to the policy
       
   673 TPtrC8 CServicePolicy::PolicyIdL(CSenElement* aPolicy)	//codescannerwarnings
       
   674     {
       
   675     RPointerArray<CSenBaseAttribute>& attrs = aPolicy->AttributesL();
       
   676     CSenBaseAttribute* bs = NULL;
       
   677     TInt ele_count = attrs.Count();
       
   678     for (TInt j=0; j < ele_count; j++)
       
   679         {
       
   680         bs = (attrs)[j];
       
   681         if(bs && bs->Name().Compare(KId) == 0)
       
   682             {
       
   683             return bs->Value();
       
   684             }
       
   685          }
       
   686     // If there is no ID for a policy, then add a random
       
   687     // Policy ID to the policy and put it into Service Description
       
   688     GenerateDeviceIdL(aPolicy);
       
   689     return PolicyIdL(aPolicy);
       
   690     }
       
   691     
       
   692 //Generate a random ID
       
   693 void CServicePolicy::GenerateDeviceIdL(CSenElement* aPolicy)	//codescannerwarnings
       
   694     {
       
   695 
       
   696     CSenGuidGen* pGuidGenerator = CSenGuidGen::NewLC();
       
   697     HBufC8* iPolicyId = pGuidGenerator->GetRandomGuid8LC();
       
   698     TPtr8 policyIdDes = iPolicyId->Des();
       
   699     policyIdDes.LowerCase();
       
   700     
       
   701     aPolicy->AddAttrL(KId, policyIdDes);
       
   702     aPolicy->AddNamespaceL(KWsuPrefix, KWsuNsUri);
       
   703     
       
   704     CleanupStack::PopAndDestroy(iPolicyId);
       
   705     CleanupStack::PopAndDestroy(pGuidGenerator);
       
   706     }
       
   707 
       
   708 //return Policy atribute Name
       
   709 TPtrC8 CServicePolicy::PolicyNameL(CSenElement* aPolicy)		//codescannerwarnings
       
   710     {
       
   711     RPointerArray<CSenBaseAttribute>& attrs = aPolicy->AttributesL();
       
   712 
       
   713     CSenBaseAttribute* bs = NULL;
       
   714     TInt ele_count = attrs.Count();
       
   715          
       
   716     for (TInt j=0; j < ele_count; j++)
       
   717         {
       
   718             
       
   719         bs = (attrs)[j];
       
   720         if(bs)
       
   721 
       
   722         if(bs->Name() == KName)
       
   723             {
       
   724              return bs->Value();
       
   725             }
       
   726          }
       
   727          return NULL;
       
   728     }
       
   729 
       
   730 TPtrC8 CServicePolicy::PolicyUriL(CSenElement* aPolicy)
       
   731 {
       
   732 
       
   733     if(aPolicy == NULL) { User::Leave(KErrGeneral); }
       
   734     
       
   735     RPointerArray<CSenBaseAttribute>& attrs = aPolicy->AttributesL();
       
   736     
       
   737     CSenBaseAttribute* bs = NULL;
       
   738     TInt ele_count = attrs.Count();
       
   739          
       
   740     for (TInt j=0; j < ele_count; j++)
       
   741         {
       
   742             
       
   743         bs = (attrs)[j];
       
   744         if(bs)
       
   745         {
       
   746             if(bs->Name().Compare(KMetaDataLocalName) == 0)
       
   747             {
       
   748                  return bs->Value();
       
   749             }
       
   750          }
       
   751 
       
   752             
       
   753         }
       
   754     return KNullDesC8();
       
   755 }
       
   756 
       
   757 TBool CServicePolicy::ValidatePolicy(CSenElement* /*aPolicy*/)
       
   758     {
       
   759     return ETrue;  
       
   760     }
       
   761 CSenElement* CServicePolicy::ClientPolicy()
       
   762 {
       
   763     CSenElement* pClientPolicyElement = AsElement().Element(KClientServicePolicyLocalName);
       
   764     
       
   765     if(!pClientPolicyElement)
       
   766         {
       
   767         return AsElement().Element(KSenNsUri, KClientServicePolicyLocalName);
       
   768         }
       
   769         
       
   770     return NULL;
       
   771 }    
       
   772 TBool CServicePolicy::ClientPolicy(const TDesC8& aKey)
       
   773 {
       
   774 
       
   775     CSenElement* pClientPolicy = ClientPolicy();
       
   776 
       
   777     if(!pClientPolicy)
       
   778         return EFalse;
       
   779     
       
   780     if(pClientPolicy)
       
   781     {
       
   782         CSenElement* result = NULL;
       
   783         result = pClientPolicy->Element(aKey);
       
   784         if(result)
       
   785             return ETrue;
       
   786         else
       
   787             return EFalse;
       
   788     }                    
       
   789 
       
   790     return EFalse;
       
   791     
       
   792 }
       
   793 HBufC8* CServicePolicy::ClientPolicyValue(const TDesC8& aKey)
       
   794     {
       
   795     HBufC8* retVal = NULL;        
       
   796     TInt err(KErrNone);
       
   797     CSenElement* pClientPolicy = ClientPolicy();
       
   798     if( pClientPolicy )
       
   799         {
       
   800         CSenElement* result = NULL;
       
   801         result = pClientPolicy->Element(aKey);
       
   802         if( result && result->HasContent() )
       
   803             {
       
   804             TRAP( err, retVal = result->Content().AllocL(); )
       
   805             if( err ) 
       
   806                 {
       
   807                 retVal = NULL;
       
   808                 }
       
   809             }
       
   810         }                    
       
   811     return retVal;
       
   812     }
       
   813 /*    
       
   814 RFileLogger* CServicePolicy::Log() const 
       
   815     {
       
   816     return (RFileLogger*) &iLog;
       
   817     }    
       
   818 */
       
   819 // End of file