1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
119
120
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 }