webservices/wsstar/wsstarpolicy/src/PolicyIntersecter.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 #include "PolicyIntersecter.h"
       
    22 
       
    23 #include <XmlEngDdom.h>
       
    24 #include "wssecuritypolicy.h"
       
    25 #include "Wspolicy.h"
       
    26 
       
    27 
       
    28 using namespace WSPolicy;
       
    29 
       
    30 CPolicyIntersecter* CPolicyIntersecter::NewL()
       
    31 {
       
    32     CPolicyIntersecter* pSelf =
       
    33         CPolicyIntersecter::NewLC();
       
    34     CleanupStack::Pop(pSelf);
       
    35     return pSelf;
       
    36 }
       
    37 CPolicyIntersecter* CPolicyIntersecter::NewLC()
       
    38 {
       
    39     CPolicyIntersecter* pSelf = new (ELeave) CPolicyIntersecter();
       
    40     CleanupStack::PushL(pSelf);
       
    41     pSelf->ConstructL();
       
    42     return pSelf;  
       
    43 }
       
    44 void CPolicyIntersecter::ConstructL()
       
    45 {
       
    46     iSecurityPolicyDomain =    CWsSecurityPolicy::NewL();
       
    47    
       
    48 }
       
    49 CPolicyIntersecter::~CPolicyIntersecter()
       
    50 {
       
    51    iPolicy1.Close(); 
       
    52    iPolicy2.Close();
       
    53 
       
    54    delete iSecurityPolicyDomain;
       
    55 }
       
    56 CPolicyIntersecter::CPolicyIntersecter()
       
    57 {
       
    58     
       
    59 }
       
    60 
       
    61 TXmlEngElement CPolicyIntersecter::IntersectL(TDesC& /*aFirstPolicy*/, TDesC& /*aSecondPolicy*/)
       
    62 {
       
    63 //    TDOMImplementation domImpl = XmlEngine::DOM::DOMImplementationSource::DOMImplementationL (XmlEngine::DOM::KImplementationID);  
       
    64     RXmlEngDOMImplementation domImpl;
       
    65     RXmlEngDocument doc;
       
    66     doc.OpenL(domImpl);
       
    67 
       
    68 //    RDOMParser parser = domImpl.CreateDOMParser();
       
    69 
       
    70 //    iPolicy1 = parser.ParseFileL( aFirstPolicy );
       
    71 //    iPolicy2 = parser.ParseFileL( aSecondPolicy );
       
    72 
       
    73     TXmlEngElement& pPolicy1 = iPolicy1.AsElement();
       
    74     TXmlEngElement& pPolicy2 = iPolicy2.AsElement();
       
    75     
       
    76     
       
    77     TXmlEngElement result = IntersectL(pPolicy1,pPolicy2);
       
    78     return result;
       
    79 }
       
    80 
       
    81 
       
    82 TXmlEngElement CPolicyIntersecter::IntersectL(TXmlEngElement aFirstPolicy, TXmlEngElement aSecondPolicy)
       
    83 {
       
    84     RXmlEngDocument policyDoc  = aFirstPolicy.OwnerDocument().CloneDocumentL();
       
    85     TXmlEngElement& newIntersectedPolicy = policyDoc.AsElement();
       
    86 
       
    87     RXmlEngNodeList<TXmlEngElement> listPolicynew;
       
    88     newIntersectedPolicy.GetChildElements(listPolicynew); //get the children of policy  
       
    89 
       
    90     if(listPolicynew.HasNext()) 
       
    91     {
       
    92         iIntersectedPolicyElement = listPolicynew.Next();
       
    93         iIntersectedPolicyElement.RemoveChildren();
       
    94     }
       
    95 
       
    96         //now add all the intersecterd assertions in this AND Element
       
    97 
       
    98     RXmlEngNodeList<TXmlEngElement> listPolicyFirst;
       
    99     aFirstPolicy.GetChildElements(listPolicyFirst); //get the children of policy
       
   100 
       
   101     RXmlEngNodeList<TXmlEngElement> listPolicySecond;
       
   102     aSecondPolicy.GetChildElements(listPolicySecond); //get the children of policy
       
   103     
       
   104     TXmlEngElement XOR = iIntersectedPolicyElement.AddNewElementSameNsL(WSPolicy::KXorCompositeAssertion);
       
   105 
       
   106     if(listPolicyFirst.HasNext() && listPolicySecond.HasNext()) 
       
   107     {
       
   108         TXmlEngElement policyElement = listPolicyFirst.Next();
       
   109         
       
   110         TPtrC8 localName = policyElement.TagName();
       
   111                 
       
   112         RXmlEngNodeList<TXmlEngElement> xorList; //get the only xor element
       
   113         policyElement.GetElementsByTagNameL(xorList, WSPolicy::KXorCompositeAssertion, WSPolicy::KWsPolicyNsUri);
       
   114         TXmlEngElement pXORFirst = NULL;
       
   115         
       
   116         if(xorList.HasNext())
       
   117             pXORFirst = xorList.Next();
       
   118         //get all AND elements from the above xor. 
       
   119         
       
   120         RXmlEngNodeList<TXmlEngElement> andListFirst; //get the only xor element from First Policy
       
   121         pXORFirst.GetElementsByTagNameL(andListFirst, WSPolicy::KAndCompositeAssertion, WSPolicy::KWsPolicyNsUri);
       
   122 
       
   123     //second policy
       
   124         TXmlEngElement policyElementSec = listPolicySecond.Next();
       
   125         
       
   126         TPtrC8 localName2 = policyElementSec.TagName();
       
   127                 
       
   128         RXmlEngNodeList<TXmlEngElement> xorListSec; //get the only xor element
       
   129         policyElementSec.GetElementsByTagNameL(xorListSec, WSPolicy::KXorCompositeAssertion, WSPolicy::KWsPolicyNsUri);
       
   130         TXmlEngElement pXORSec = NULL;
       
   131         
       
   132         if(xorListSec.HasNext())
       
   133             pXORSec = xorListSec.Next();
       
   134         //we have to get all AND elements from the above xor. we will do it in loop
       
   135         
       
   136         
       
   137         while (andListFirst.HasNext())
       
   138         {
       
   139             TXmlEngElement AND = XOR.AddNewElementSameNsL(WSPolicy::KAndCompositeAssertion);
       
   140             
       
   141             TXmlEngElement andEleF = andListFirst.Next();
       
   142             
       
   143             TPtrC8 localName = andEleF.TagName();
       
   144             //find all assertions in this AND element
       
   145             
       
   146             
       
   147             IntersectCompareSecondAssertionsL(andEleF, pXORSec, AND);
       
   148         }
       
   149   
       
   150     }
       
   151     
       
   152 
       
   153     return XOR;
       
   154     
       
   155 }
       
   156 
       
   157 TXmlEngElement CPolicyIntersecter::IntersectCompareSecondAssertionsL(TXmlEngElement aFirstElement, TXmlEngElement aSecondElement, TXmlEngElement aIntersectedAND)
       
   158 {
       
   159 
       
   160     RXmlEngNodeList<TXmlEngElement> assertionListF; //get all of first assrtions
       
   161     aFirstElement.GetChildElements(assertionListF);
       
   162 
       
   163     RXmlEngNodeList<TXmlEngElement> andListSec; //get all of AND elements
       
   164     aSecondElement.GetElementsByTagNameL(andListSec, WSPolicy::KAndCompositeAssertion, WSPolicy::KWsPolicyNsUri);
       
   165     
       
   166     RArray<TXmlEngElement> result;
       
   167     while(andListSec.HasNext())
       
   168     {
       
   169     //for every of AND element, loop for every AND element in first policy
       
   170         TXmlEngElement andEleS = andListSec.Next();
       
   171         
       
   172         TPtrC8 localNameS = andEleS.TagName();
       
   173         //find all assertions in this AND element
       
   174         
       
   175         RXmlEngNodeList<TXmlEngElement> assertionListS; //get the only xor element
       
   176         andEleS.GetChildElements(assertionListS);
       
   177         //RESOLVE all assertion from same AND element  
       
   178 
       
   179          IntersectAllANDAssertionsL(aFirstElement, andEleS, result);
       
   180          if(result.Count() > 0)             
       
   181          {
       
   182            for (TInt i = 0; i<result.Count(); i++) 
       
   183            {
       
   184            TXmlEngElement resultEle = result[i].CopyL();
       
   185            aIntersectedAND.AppendChildL(resultEle);
       
   186            }
       
   187            result.Reset();
       
   188          }
       
   189         //if result then we have merged policy else we have NULL here
       
   190          
       
   191     }
       
   192     return NULL;
       
   193 }
       
   194 void CPolicyIntersecter::IntersectAllANDAssertionsL(TXmlEngElement aFirstElement, TXmlEngElement aSecondElement, RArray<TXmlEngElement>& aResult)
       
   195 {
       
   196     TInt outcount = 0;
       
   197     TInt inCount = 0;
       
   198 
       
   199     RXmlEngNodeList<TXmlEngElement> assertionListF; //get all of first assrtions
       
   200     aFirstElement.GetChildElements(assertionListF);
       
   201     
       
   202     while (assertionListF.HasNext())
       
   203     {
       
   204         TXmlEngElement assertionFirst = assertionListF.Next();
       
   205 
       
   206         TPtrC8 localName1 = assertionFirst.TagName();
       
   207         
       
   208         outcount++;
       
   209 
       
   210         RXmlEngNodeList<TXmlEngElement> assertionListS; //get all of AND elements
       
   211         aSecondElement.GetChildElements(assertionListS);
       
   212 
       
   213         while(assertionListS.HasNext())
       
   214         {
       
   215             TXmlEngElement assertionSecond = assertionListS.Next();
       
   216 
       
   217             TPtrC8 localName2 = assertionSecond.TagName();
       
   218             inCount++;
       
   219             if((localName1.Compare(localName2) == 0) && 
       
   220             VerifyNamespace(assertionFirst, assertionSecond))            
       
   221             {
       
   222                 //call domain specific policy to resolve
       
   223                 //check the namespaces here to find which domain we need to process
       
   224                 //both namespaces should be checked and if they are same then we can intersect them
       
   225 
       
   226                 TPtrC8 namespaceUri1 = assertionFirst.NamespaceUri();
       
   227 
       
   228                 //if its ws security policy
       
   229                 if(namespaceUri1.Compare(KWsSecurityPolicyNsUri) == 0)
       
   230                 {
       
   231 /*                
       
   232                     TXmlEngElement result = iSecurityPolicyDomain->Intersect(localName1, assertionFirst, assertionSecond);
       
   233                     if(result.NotNull())
       
   234                     {
       
   235                         aResult.Append(assertionFirst);
       
   236                         aResult.Append(assertionSecond);
       
   237                     }
       
   238 */                    
       
   239                 }                
       
   240             }
       
   241         }
       
   242         
       
   243     }
       
   244 }
       
   245 
       
   246 TInt CPolicyIntersecter::VerifyNamespace(TXmlEngElement aFirstElement, TXmlEngElement aSecondElement)
       
   247 {
       
   248 
       
   249     TPtrC8 namespaceUri1 = aFirstElement.NamespaceUri();
       
   250     TPtrC8 namespaceUri2 = aSecondElement.NamespaceUri();
       
   251 
       
   252     if(namespaceUri1 != KNullDesC8() && namespaceUri2 != KNullDesC8())
       
   253     {
       
   254       if(namespaceUri1.Compare(namespaceUri2) == 0)
       
   255         return ETrue;  
       
   256     }
       
   257 
       
   258   return KErrNotFound;  
       
   259 }