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.Constants;
22 import org.apache.neethi.Policy;
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.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32
33 public class RequiredElements extends AbstractSecurityAssertion {
34
35 private String xPathVersion;
36 private final List<XPath> xPaths = new ArrayList<>();
37
38 public RequiredElements(SPConstants.SPVersion version, String xPathVersion, List<XPath> xPaths) {
39 super(version);
40
41 this.xPathVersion = xPathVersion;
42 this.xPaths.addAll(xPaths);
43 }
44
45 @Override
46 public QName getName() {
47 return getVersion().getSPConstants().getRequiredElements();
48 }
49
50 @Override
51 public boolean equals(Object object) {
52 if (object == this) {
53 return true;
54 }
55
56 if (!(object instanceof RequiredElements)) {
57 return false;
58 }
59
60 RequiredElements that = (RequiredElements)object;
61 if (xPathVersion != null && !xPathVersion.equals(that.xPathVersion)
62 || xPathVersion == null && that.xPathVersion != null) {
63 return false;
64 }
65
66 if (!xPaths.equals(that.xPaths)) {
67 return false;
68 }
69
70 return super.equals(object);
71 }
72
73 @Override
74 public int hashCode() {
75 int result = 17;
76 if (xPathVersion != null) {
77 result = 31 * result + xPathVersion.hashCode();
78 }
79 result = 31 * result + xPaths.hashCode();
80
81 return 31 * result + super.hashCode();
82 }
83
84 @Override
85 protected AbstractSecurityAssertion cloneAssertion(Policy nestedPolicy) {
86 return new RequiredElements(getVersion(), getXPathVersion(), getXPaths());
87 }
88
89 @Override
90 public void serialize(XMLStreamWriter writer) throws XMLStreamException {
91 writer.writeStartElement(getName().getPrefix(), getName().getLocalPart(), getName().getNamespaceURI());
92 writer.writeNamespace(getName().getPrefix(), getName().getNamespaceURI());
93 if (!isNormalized() && isOptional()) {
94 writer.writeAttribute(Constants.ATTR_WSP,
95 writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
96 Constants.ATTR_OPTIONAL, "true");
97 }
98 if (getXPathVersion() != null) {
99 writer.writeAttribute(SPConstants.XPATH_VERSION, getXPathVersion());
100 }
101 if (isIgnorable()) {
102 writer.writeAttribute(Constants.ATTR_WSP,
103 writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
104 Constants.ATTR_IGNORABLE, "true");
105 }
106 for (XPath xPath : xPaths) {
107 if (XPath.Version.V1 == xPath.getVersion()) {
108 writer.writeStartElement(
109 getVersion().getSPConstants().getXPathExpression().getPrefix(),
110 getVersion().getSPConstants().getXPathExpression().getLocalPart(),
111 getVersion().getSPConstants().getXPathExpression().getNamespaceURI());
112 } else if (XPath.Version.V2 == xPath.getVersion()) {
113 writer.writeStartElement(
114 getVersion().getSPConstants().getXPath2Expression().getPrefix(),
115 getVersion().getSPConstants().getXPath2Expression().getLocalPart(),
116 getVersion().getSPConstants().getXPath2Expression().getNamespaceURI());
117 writer.writeNamespace(
118 getVersion().getSPConstants().getXPath2Expression().getPrefix(),
119 getVersion().getSPConstants().getXPath2Expression().getNamespaceURI());
120 writer.writeAttribute(SPConstants.FILTER, xPath.getFilter());
121 }
122 Iterator<Map.Entry<String, String>> namespaceIterator =
123 xPath.getPrefixNamespaceMap().entrySet().iterator();
124 while (namespaceIterator.hasNext()) {
125 Map.Entry<String, String> namespaceEntry = namespaceIterator.next();
126 writer.writeNamespace(namespaceEntry.getKey(), namespaceEntry.getValue());
127 }
128 writer.writeCharacters(xPath.getXPath());
129 writer.writeEndElement();
130 }
131 writer.writeEndElement();
132 }
133
134 public List<XPath> getXPaths() {
135 return xPaths;
136 }
137
138 public String getXPathVersion() {
139 return xPathVersion;
140 }
141
142 protected void setXPathVersion(String xPathVersion) {
143 this.xPathVersion = xPathVersion;
144 }
145 }