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  import org.apache.wss4j.policy.SPConstants.SPVersion;
27  
28  import javax.xml.stream.XMLStreamException;
29  import javax.xml.stream.XMLStreamWriter;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  public abstract class AbstractBinding extends AbstractSecurityAssertion implements PolicyContainingAssertion {
34  
35      private Policy nestedPolicy;
36      private AlgorithmSuite algorithmSuite;
37      private Layout layout;
38      private boolean includeTimestamp;
39  
40      protected AbstractBinding(SPConstants.SPVersion version, Policy nestedPolicy) {
41          super(version);
42          this.nestedPolicy = nestedPolicy;
43          parseNestedBindingPolicy(nestedPolicy, this);
44          if (layout == null) {
45              layout = new Layout(version, new Policy());
46          }
47      }
48  
49      @Override
50      public Policy getPolicy() {
51          return nestedPolicy;
52      }
53  
54      @Override
55      public boolean equals(Object object) {
56          if (object == this) {
57              return true;
58          }
59  
60          if (!(object instanceof AbstractBinding)) {
61              return false;
62          }
63  
64          AbstractBinding that = (AbstractBinding)object;
65          if (algorithmSuite != null && !algorithmSuite.equals(that.algorithmSuite)
66              || algorithmSuite == null && that.algorithmSuite != null) {
67              return false;
68          }
69  
70          if (layout != null && !layout.equals(that.layout)
71              || layout == null && that.layout != null) {
72              return false;
73          }
74  
75          if (includeTimestamp != that.includeTimestamp) {
76              return false;
77          }
78  
79          return super.equals(object);
80      }
81  
82      @Override
83      public int hashCode() {
84          int result = 17;
85          if (algorithmSuite != null) {
86              result = 31 * result + algorithmSuite.hashCode();
87          }
88          if (layout != null) {
89              result = 31 * result + layout.hashCode();
90          }
91          result = 31 * result + Boolean.hashCode(includeTimestamp);
92  
93          return 31 * result + super.hashCode();
94      }
95  
96      @Override
97      public PolicyComponent normalize() {
98          return super.normalize(getPolicy());
99      }
100 
101     @Override
102     public void serialize(XMLStreamWriter writer) throws XMLStreamException {
103         super.serialize(writer, getPolicy());
104     }
105 
106     protected void parseNestedBindingPolicy(Policy nestedPolicy, AbstractBinding binding) {
107         Iterator<List<Assertion>> alternatives = nestedPolicy.getAlternatives();
108         //we just process the first alternative
109         //this means that if we have a compact policy only the first alternative is visible
110         //in contrary to a normalized policy where just one alternative exists
111         if (alternatives.hasNext()) {
112             List<Assertion> assertions = alternatives.next();
113             for (Assertion assertion : assertions) {
114                 String assertionName = assertion.getName().getLocalPart();
115                 if (SPConstants.ALGORITHM_SUITE.equals(assertionName)) {
116                     if (binding.getAlgorithmSuite() != null) {
117                         throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
118                     }
119                     binding.setAlgorithmSuite((AlgorithmSuite) assertion);
120                     continue;
121                 }
122                 if (SPConstants.LAYOUT.equals(assertionName)) {
123                     if (binding.getLayout() != null) {
124                         throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
125                     }
126                     Layout layout = (Layout) assertion;
127                     binding.setLayout(layout);
128                     if (layout.getLayoutType() == Layout.LayoutType.LaxTsFirst
129                             || layout.getLayoutType() == Layout.LayoutType.LaxTsLast) {
130                         binding.setIncludeTimestamp(true);
131                     }
132                     continue;
133                 }
134                 if (SPConstants.INCLUDE_TIMESTAMP.equals(assertionName)) {
135                     binding.setIncludeTimestamp(true);
136                     continue;
137                 }
138             }
139         }
140         if (binding.getAlgorithmSuite() == null && binding.getVersion() != SPVersion.SP11) {
141             throw new IllegalArgumentException("sp:" + getName().getLocalPart()
142                                                + " must have an inner sp:AlgorithmSuite element");
143         }
144     }
145 
146     public AlgorithmSuite getAlgorithmSuite() {
147         return algorithmSuite;
148     }
149 
150     protected void setAlgorithmSuite(AlgorithmSuite algorithmSuite) {
151         this.algorithmSuite = algorithmSuite;
152     }
153 
154     public Layout getLayout() {
155         return layout;
156     }
157 
158     protected void setLayout(Layout layout) {
159         this.layout = layout;
160     }
161 
162     public boolean isIncludeTimestamp() {
163         return includeTimestamp;
164     }
165 
166     protected void setIncludeTimestamp(boolean includeTimestamp) {
167         this.includeTimestamp = includeTimestamp;
168     }
169 }