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.*;
22  import org.apache.wss4j.policy.AssertionState;
23  import org.apache.wss4j.policy.SPConstants;
24  
25  import javax.xml.namespace.QName;
26  import javax.xml.stream.XMLStreamException;
27  import javax.xml.stream.XMLStreamWriter;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  public abstract class AbstractSecurityAssertion implements Assertion {
33  
34      private boolean isOptional;
35      private boolean isIgnorable;
36  
37      // if normalized is null, then this policy hasn't been normalized yet
38      // if normalized == this, then this policy is already in normalized form
39      // else, normalized contains the normalized version of this policy
40      private volatile PolicyComponent normalized;
41  
42      private SPConstants.SPVersion version;
43  
44      protected AbstractSecurityAssertion(SPConstants.SPVersion version) {
45          this.version = version;
46      }
47  
48      @Override
49      public boolean isOptional() {
50          return isOptional;
51      }
52  
53      public void setOptional(boolean isOptional) {
54          this.isOptional = isOptional;
55      }
56  
57      @Override
58      public boolean isIgnorable() {
59          return isIgnorable;
60      }
61  
62      public void setIgnorable(boolean isIgnorable) {
63          this.isIgnorable = isIgnorable;
64      }
65  
66      @Override
67      public short getType() {
68          return org.apache.neethi.Constants.TYPE_ASSERTION;
69      }
70  
71      @Override
72      public boolean equal(PolicyComponent policyComponent) {
73          return policyComponent == this;
74      }
75  
76      @Override
77      public boolean equals(Object object) {
78          if (object == this) {
79              return true;
80          }
81          if (!(object instanceof AbstractSecurityAssertion)) {
82              return false;
83          }
84  
85          AbstractSecurityAssertion that = (AbstractSecurityAssertion)object;
86          if (isOptional != that.isOptional) {
87              return false;
88          }
89          if (isIgnorable != that.isIgnorable) {
90              return false;
91          }
92  
93          return !(version != null && !version.equals(that.version)
94              || version == null && that.version != null);
95      }
96  
97      @Override
98      public int hashCode() {
99          int result = 17;
100         if (version != null) {
101             result = 31 * result + version.hashCode();
102         }
103         result = 31 * result + Boolean.hashCode(isOptional);
104         result = 31 * result + Boolean.hashCode(isIgnorable);
105 
106         return result;
107     }
108 
109     @Override
110     public PolicyComponent normalize() {
111         if (normalized == null) {
112             Policy policy = new Policy();
113             ExactlyOne exactlyOne = new ExactlyOne();
114             policy.addPolicyComponent(exactlyOne);
115 
116             if (isOptional()) {
117                 exactlyOne.addPolicyComponent(new All());
118             }
119 
120             AbstractSecurityAssertion a = clone(null);
121             a.normalized = a;
122             a.setOptional(false);
123 
124             All all = new All();
125             all.addPolicyComponent(a);
126             exactlyOne.addPolicyComponent(all);
127 
128             normalized = policy;
129         }
130         return normalized;
131     }
132 
133     public boolean isNormalized() {
134         return normalized == this;
135     }
136 
137     public PolicyComponent normalize(Policy nestedPolicy) {
138         if (normalized == null) {
139             Policy normalizedNestedPolicy = nestedPolicy.normalize(true);
140 
141             Policy policy = new Policy();
142             ExactlyOne exactlyOne = new ExactlyOne();
143             policy.addPolicyComponent(exactlyOne);
144 
145             if (isOptional()) {
146                 exactlyOne.addPolicyComponent(new All());
147             }
148 
149             // for all alternatives in normalized nested policy
150             Iterator<List<Assertion>> alternatives = normalizedNestedPolicy.getAlternatives();
151             while (alternatives.hasNext()) {
152                 List<Assertion> alternative = alternatives.next();
153 
154                 Policy ncp = new Policy(nestedPolicy.getPolicyRegistry(), nestedPolicy.getNamespace());
155                 ExactlyOne nceo = new ExactlyOne();
156                 ncp.addPolicyComponent(nceo);
157 
158                 All nca = new All();
159                 nceo.addPolicyComponent(nca);
160                 nca.addPolicyComponents(alternative);
161 
162                 AbstractSecurityAssertion a = clone(ncp);
163                 a.normalized = a;
164                 a.setOptional(false);
165 
166                 All all = new All();
167                 all.addPolicyComponent(a);
168                 exactlyOne.addPolicyComponent(all);
169 
170             }
171             normalized = policy;
172         }
173         return normalized;
174     }
175 
176     public SPConstants.SPVersion getVersion() {
177         return version;
178     }
179 
180     public void serialize(XMLStreamWriter writer, Policy nestedPolicy) throws XMLStreamException {
181         writer.writeStartElement(getName().getPrefix(), getName().getLocalPart(), getName().getNamespaceURI());
182         writer.writeNamespace(getName().getPrefix(), getName().getNamespaceURI());
183         if (isOptional()) {
184             writer.writeAttribute(Constants.ATTR_WSP,
185                                   writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
186                                   Constants.ATTR_OPTIONAL, "true");
187         }
188         if (isIgnorable()) {
189             writer.writeAttribute(Constants.ATTR_WSP,
190                                   writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
191                                   Constants.ATTR_IGNORABLE, "true");
192         }
193         nestedPolicy.serialize(writer);
194         writer.writeEndElement();
195     }
196 
197     protected abstract AbstractSecurityAssertion cloneAssertion(Policy nestedPolicy);
198 
199     public AbstractSecurityAssertion clone(Policy nestedPolicy) {
200         AbstractSecurityAssertion assertion = cloneAssertion(nestedPolicy);
201         assertion.setIgnorable(isIgnorable());
202         assertion.setOptional(isOptional());
203         return assertion;
204     }
205 
206     public boolean isAsserted(Map<QName, List<AssertionState>> assertionStatesMap) {
207         List<AssertionState> assertionStateList = assertionStatesMap.get(getName());
208         if (assertionStateList != null) {
209             for (AssertionState assertionState : assertionStateList) {
210                 if (assertionState.getAssertion() == this && !assertionState.isAsserted()) {
211                     return false;
212                 }
213             }
214         }
215         return true;
216     }
217 }