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.model;
20  
21  import org.apache.neethi.Assertion;
22  import org.apache.neethi.Policy;
23  import org.apache.wss4j.policy.SPConstants;
24  
25  import javax.xml.namespace.QName;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  public class KeyValueToken extends AbstractToken {
30  
31      private boolean rsaKeyValue;
32  
33      public KeyValueToken(SPConstants.SPVersion version, SPConstants.IncludeTokenType includeTokenType,
34                           Policy nestedPolicy) {
35          super(version, includeTokenType, null, null, null, nestedPolicy);
36          setIncludeTokenType(includeTokenType);
37  
38          parseNestedPolicy(nestedPolicy, this);
39      }
40  
41      @Override
42      public QName getName() {
43          return getVersion().getSPConstants().getKeyValueToken();
44      }
45  
46      @Override
47      public boolean equals(Object object) {
48          if (object == this) {
49              return true;
50          }
51          if (!(object instanceof KeyValueToken)) {
52              return false;
53          }
54  
55          KeyValueToken that = (KeyValueToken)object;
56          if (rsaKeyValue != that.rsaKeyValue) {
57              return false;
58          }
59  
60          return super.equals(object);
61      }
62  
63      @Override
64      public int hashCode() {
65          int result = 17;
66          result = 31 * result + Boolean.hashCode(rsaKeyValue);
67  
68          return 31 * result + super.hashCode();
69      }
70  
71      @Override
72      protected AbstractSecurityAssertion cloneAssertion(Policy nestedPolicy) {
73          return new KeyValueToken(getVersion(), getIncludeTokenType(), nestedPolicy);
74      }
75  
76      protected void parseNestedPolicy(Policy nestedPolicy, KeyValueToken keyValueToken) {
77          Iterator<List<Assertion>> alternatives = nestedPolicy.getAlternatives();
78          //we just process the first alternative
79          //this means that if we have a compact policy only the first alternative is visible
80          //in contrary to a normalized policy where just one alternative exists
81          if (alternatives.hasNext()) {
82              List<Assertion> assertions = alternatives.next();
83              for (Assertion assertion : assertions) {
84                  String assertionName = assertion.getName().getLocalPart();
85                  String assertionNamespace = assertion.getName().getNamespaceURI();
86  
87                  QName rsaKeyValue = getVersion().getSPConstants().getRsaKeyValue();
88                  if (rsaKeyValue.getLocalPart().equals(assertionName)
89                      && rsaKeyValue.getNamespaceURI().equals(assertionNamespace)) {
90                      if (keyValueToken.isRsaKeyValue()) {
91                          throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
92                      }
93                      keyValueToken.setRsaKeyValue(true);
94                      continue;
95                  }
96              }
97          }
98      }
99  
100     public boolean isRsaKeyValue() {
101         return rsaKeyValue;
102     }
103 
104     protected void setRsaKeyValue(boolean rsaKeyValue) {
105         this.rsaKeyValue = rsaKeyValue;
106     }
107 }