View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.wss4j.policy.stax.assertionStates;
20  
21  import org.apache.wss4j.policy.AssertionState;
22  import org.apache.wss4j.common.WSSPolicyException;
23  import org.apache.wss4j.policy.model.AbstractSecurityAssertion;
24  import org.apache.wss4j.policy.model.RequiredElements;
25  import org.apache.wss4j.policy.model.XPath;
26  import org.apache.xml.security.stax.securityEvent.SecurityEvent;
27  import org.apache.xml.security.stax.securityEvent.SecurityEventConstants;
28  import org.apache.wss4j.policy.stax.Assertable;
29  import org.apache.wss4j.policy.stax.DummyPolicyAsserter;
30  import org.apache.wss4j.policy.stax.PolicyAsserter;
31  import org.apache.wss4j.policy.stax.PolicyUtils;
32  import org.apache.wss4j.stax.securityEvent.RequiredElementSecurityEvent;
33  import org.apache.wss4j.stax.securityEvent.WSSecurityEventConstants;
34  import org.apache.wss4j.stax.utils.WSSUtils;
35  
36  import javax.xml.namespace.QName;
37  
38  import java.util.HashMap;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Map;
42  
43  /**
44   * WSP1.3, 4.3.1 RequiredElements Assertion
45   */
46  public class RequiredElementsAssertionState extends AssertionState implements Assertable {
47  
48      private final Map<List<QName>, Boolean> pathElements = new HashMap<>();
49      private PolicyAsserter policyAsserter;
50  
51      public RequiredElementsAssertionState(AbstractSecurityAssertion assertion,
52                                            PolicyAsserter policyAsserter,
53                                            boolean asserted) {
54          super(assertion, asserted);
55  
56          if (assertion instanceof RequiredElements) {
57              RequiredElements requiredElements = (RequiredElements) assertion;
58              for (int i = 0; i < requiredElements.getXPaths().size(); i++) {
59                  XPath xPath = requiredElements.getXPaths().get(i);
60                  List<QName> elements = PolicyUtils.getElementPath(xPath);
61                  pathElements.put(elements, Boolean.FALSE);
62              }
63          }
64  
65          this.policyAsserter = policyAsserter;
66          if (this.policyAsserter == null) {
67              this.policyAsserter = new DummyPolicyAsserter();
68          }
69  
70          if (asserted) {
71              policyAsserter.assertPolicy(getAssertion());
72          }
73      }
74  
75      public void addElement(List<QName> pathElement) {
76          this.pathElements.put(pathElement, Boolean.FALSE);
77      }
78  
79      @Override
80      public SecurityEventConstants.Event[] getSecurityEventType() {
81          return new SecurityEventConstants.Event[]{
82                  WSSecurityEventConstants.REQUIRED_ELEMENT
83          };
84      }
85  
86      @Override
87      public boolean assertEvent(SecurityEvent securityEvent) throws WSSPolicyException {
88          RequiredElementSecurityEvent requiredElementSecurityEvent = (RequiredElementSecurityEvent) securityEvent;
89  
90          Iterator<Map.Entry<List<QName>, Boolean>> elementMapIterator = pathElements.entrySet().iterator();
91          while (elementMapIterator.hasNext()) {
92              Map.Entry<List<QName>, Boolean> next = elementMapIterator.next();
93              List<QName> qNameList = next.getKey();
94              if (WSSUtils.pathMatches(qNameList, requiredElementSecurityEvent.getElementPath())) {
95                  next.setValue(Boolean.TRUE);
96                  break;
97              }
98          }
99          //if we return false here other required elements will trigger a PolicyViolationException
100         policyAsserter.assertPolicy(getAssertion());
101         return true;
102     }
103 
104     @Override
105     public boolean isAsserted() {
106         clearErrorMessage();
107         Iterator<Map.Entry<List<QName>, Boolean>> elementMapIterator = pathElements.entrySet().iterator();
108         while (elementMapIterator.hasNext()) {
109             Map.Entry<List<QName>, Boolean> next = elementMapIterator.next();
110             if (Boolean.FALSE.equals(next.getValue())) {
111                 setErrorMessage("Element " + WSSUtils.pathAsString(next.getKey()) + " must be present");
112                 policyAsserter.unassertPolicy(getAssertion(), getErrorMessage());
113                 return false;
114             }
115         }
116         policyAsserter.assertPolicy(getAssertion());
117         return true;
118     }
119 }