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.neethi.PolicyComponent;
24  import org.apache.neethi.PolicyContainingAssertion;
25  import org.apache.wss4j.policy.SPConstants;
26  
27  import javax.xml.stream.XMLStreamException;
28  import javax.xml.stream.XMLStreamWriter;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  public abstract class AbstractTokenWrapper extends AbstractSecurityAssertion implements PolicyContainingAssertion {
33  
34      private Policy nestedPolicy;
35      private AbstractToken token;
36      private AbstractSecurityAssertion parentAssertion;
37  
38      protected AbstractTokenWrapper(SPConstants.SPVersion version, Policy nestedPolicy) {
39          super(version);
40          this.nestedPolicy = nestedPolicy;
41  
42          parseNestedPolicy(nestedPolicy, this);
43      }
44  
45      @Override
46      public Policy getPolicy() {
47          return nestedPolicy;
48      }
49  
50      @Override
51      public boolean equals(Object object) {
52          if (object == this) {
53              return true;
54          }
55          if (!(object instanceof AbstractTokenWrapper)) {
56              return false;
57          }
58  
59          AbstractTokenWrapper that = (AbstractTokenWrapper)object;
60          if (token != null && !token.equals(that.token)
61              || token == null && that.token != null) {
62              return false;
63          }
64  
65          return super.equals(object);
66      }
67  
68      @Override
69      public int hashCode() {
70          int result = 17;
71          if (token != null) {
72              result = 31 * result + token.hashCode();
73          }
74  
75          return 31 * result + super.hashCode();
76      }
77  
78      @Override
79      public PolicyComponent normalize() {
80          return super.normalize(getPolicy());
81      }
82  
83      @Override
84      public void serialize(XMLStreamWriter writer) throws XMLStreamException {
85          super.serialize(writer, getPolicy());
86      }
87  
88      protected void parseNestedPolicy(Policy nestedPolicy, AbstractTokenWrapper tokenWrapper) {
89          Iterator<List<Assertion>> alternatives = nestedPolicy.getAlternatives();
90          //we just process the first alternative
91          //this means that if we have a compact policy only the first alternative is visible
92          //in contrary to a normalized policy where just one alternative exists
93          if (alternatives.hasNext()) {
94              List<Assertion> assertions = alternatives.next();
95              for (Assertion assertion : assertions) {
96                  if (assertion instanceof AbstractToken) {
97                      if (tokenWrapper.getToken() != null) {
98                          throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
99                      }
100                     final AbstractToken abstractToken = (AbstractToken) assertion;
101                     tokenWrapper.setToken(abstractToken);
102                     abstractToken.setParentAssertion(tokenWrapper);
103                     continue;
104                 }
105             }
106         }
107     }
108 
109     public AbstractToken getToken() {
110         return token;
111     }
112 
113     protected void setToken(AbstractToken token) {
114         this.token = token;
115     }
116 
117     public AbstractSecurityAssertion getParentAssertion() {
118         return parentAssertion;
119     }
120 
121     public void setParentAssertion(AbstractSecurityAssertion parentAssertion) {
122         this.parentAssertion = parentAssertion;
123     }
124 }