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 javax.xml.namespace.QName;
22  
23  import org.apache.wss4j.policy.AssertionState;
24  import org.apache.wss4j.policy.SPConstants;
25  import org.apache.wss4j.policy.model.AbstractBinding;
26  import org.apache.wss4j.policy.model.AbstractSecurityAssertion;
27  import org.apache.xml.security.stax.securityEvent.SecurityEvent;
28  import org.apache.xml.security.stax.securityEvent.SecurityEventConstants;
29  import org.apache.wss4j.policy.stax.Assertable;
30  import org.apache.wss4j.policy.stax.DummyPolicyAsserter;
31  import org.apache.wss4j.policy.stax.PolicyAsserter;
32  import org.apache.wss4j.stax.securityEvent.WSSecurityEventConstants;
33  
34  /**
35   * WSP1.3, 6.2 Timestamp Property
36   */
37  public class IncludeTimeStampAssertionState extends AssertionState implements Assertable {
38  
39      private PolicyAsserter policyAsserter;
40  
41      public IncludeTimeStampAssertionState(AbstractSecurityAssertion assertion,
42                                            PolicyAsserter policyAsserter,
43                                            boolean asserted) {
44          super(assertion, asserted);
45          this.policyAsserter = policyAsserter;
46          if (this.policyAsserter == null) {
47              this.policyAsserter = new DummyPolicyAsserter();
48          }
49  
50          if (asserted) {
51              String namespace = getAssertion().getName().getNamespaceURI();
52              policyAsserter.assertPolicy(new QName(namespace, SPConstants.INCLUDE_TIMESTAMP));
53          }
54      }
55  
56      @Override
57      public SecurityEventConstants.Event[] getSecurityEventType() {
58          return new SecurityEventConstants.Event[]{
59                  WSSecurityEventConstants.TIMESTAMP
60          };
61      }
62  
63      @Override
64      public boolean assertEvent(SecurityEvent securityEvent) {
65          // TimestampSecurityEvent timestampSecurityEvent = (TimestampSecurityEvent) securityEvent;
66          boolean isIncludeTimestamp = ((AbstractBinding) getAssertion()).isIncludeTimestamp();
67  
68          String namespace = getAssertion().getName().getNamespaceURI();
69          if (isIncludeTimestamp) {
70              setAsserted(true);
71              policyAsserter.assertPolicy(new QName(namespace, SPConstants.INCLUDE_TIMESTAMP));
72          } else {
73              setAsserted(false);
74              setErrorMessage("Timestamp must not be present");
75              policyAsserter.unassertPolicy(new QName(namespace, SPConstants.INCLUDE_TIMESTAMP),
76                  getErrorMessage());
77          }
78          return isAsserted();
79      }
80  }