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.namespace.QName;
28  import javax.xml.stream.XMLStreamException;
29  import javax.xml.stream.XMLStreamWriter;
30  import java.util.*;
31  
32  public class Layout extends AbstractSecurityAssertion implements PolicyContainingAssertion {
33  
34      public enum LayoutType {
35          Strict,
36          Lax,
37          LaxTsFirst,
38          LaxTsLast;
39  
40          private static final Map<String, LayoutType> LOOKUP = new HashMap<>();
41  
42          static {
43              for (LayoutType u : EnumSet.allOf(LayoutType.class)) {
44                  LOOKUP.put(u.name(), u);
45              }
46          }
47  
48          public static LayoutType lookUp(String name) {
49              return LOOKUP.get(name);
50          }
51      }
52  
53      private Policy nestedPolicy;
54      private LayoutType layoutType = LayoutType.Lax;
55  
56      public Layout(SPConstants.SPVersion version, Policy nestedPolicy) {
57          super(version);
58          this.nestedPolicy = nestedPolicy;
59  
60          parseNestedPolicy(nestedPolicy, this);
61      }
62  
63      @Override
64      public Policy getPolicy() {
65          return nestedPolicy;
66      }
67  
68      @Override
69      public QName getName() {
70          return getVersion().getSPConstants().getLayout();
71      }
72  
73      @Override
74      public boolean equals(Object object) {
75          if (object == this) {
76              return true;
77          }
78  
79          if (!(object instanceof Layout)) {
80              return false;
81          }
82  
83          Layout that = (Layout)object;
84          if (layoutType != that.layoutType) {
85              return false;
86          }
87  
88          return super.equals(object);
89      }
90  
91      @Override
92      public int hashCode() {
93          int result = 17;
94          if (layoutType != null) {
95              result = 31 * result + layoutType.hashCode();
96          }
97  
98          return 31 * result + super.hashCode();
99      }
100 
101     @Override
102     public PolicyComponent normalize() {
103         return super.normalize(getPolicy());
104     }
105 
106     @Override
107     public void serialize(XMLStreamWriter writer) throws XMLStreamException {
108         super.serialize(writer, getPolicy());
109     }
110 
111     @Override
112     protected AbstractSecurityAssertion cloneAssertion(Policy nestedPolicy) {
113         return new Layout(getVersion(), nestedPolicy);
114     }
115 
116     protected void parseNestedPolicy(Policy nestedPolicy, Layout layout) {
117         Iterator<List<Assertion>> alternatives = nestedPolicy.getAlternatives();
118         //we just process the first alternative
119         //this means that if we have a compact policy only the first alternative is visible
120         //in contrary to a normalized policy where just one alternative exists
121         if (alternatives.hasNext()) {
122             List<Assertion> assertions = alternatives.next();
123             for (Assertion assertion : assertions) {
124                 String assertionName = assertion.getName().getLocalPart();
125                 LayoutType layoutType = LayoutType.lookUp(assertionName);
126                 if (layoutType != null) {
127                     layout.setLayoutType(layoutType);
128                     continue;
129                 }
130             }
131         }
132     }
133 
134     public LayoutType getLayoutType() {
135         return layoutType;
136     }
137 
138     protected void setLayoutType(LayoutType layoutType) {
139         this.layoutType = layoutType;
140     }
141 }