Policy.java
11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xfa.wspolicy;
import com.adobe.xfa.AppModel;
import com.adobe.xfa.Document;
import com.adobe.xfa.Element;
import com.adobe.xfa.LogMessenger;
import com.adobe.xfa.Model;
import com.adobe.xfa.Node;
import com.adobe.xfa.dom.DOM;
import com.adobe.xfa.dom.NamespaceContextImpl;
import com.adobe.xfa.ut.ExFull;
import com.adobe.xfa.ut.ObjectHolder;
import com.adobe.xfa.ut.ResId;
import com.adobe.xfa.ut.StringUtils;
import com.adobe.xfa.wspolicy.CompoundAssertion;
import com.adobe.xfa.wspolicy.PrimitiveAssertion;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
public class Policy
extends Element {
public static final String aWSP_NAMESPACE_URI = "http://schemas.xmlsoap.org/ws/2004/09/policy";
public static final String aWSP_NAMESPACE_PREFIX = "wsp";
public static final String aWSP_POLICYURIS = "PolicyURIs";
public static final String aWSP_POLICY = "Policy";
public static final String aWSP_POLICYREFERENCE = "PolicyReference";
public static final String aWSP_URIATTR = "URI";
public static final String aWSP_ALL = "All";
public static final String aWSP_EXACTLYONE = "ExactlyOne";
public static final String aSP_NAMESPACE_URI = "http://schemas.xmlsoap.org/ws/2005/07/securitypolicy";
public static final String aSP_NAMESPACE_PREFIX = "sp";
public static final String aSP_TRANSPORTBINDING = "TransportBinding";
public static final String aSP_TRANSPORTTOKEN = "TransportToken";
public static final String aSP_HTTPSTOKEN = "HttpsToken";
public static final String aSP_HTTPBASICAUTHENTICATION = "HttpBasicAuthentication";
public static final String aSP_HTTPDIGESTAUTHENTICATION = "HttpDigestAuthentication";
public static final String aSP_REQUIRECLIENTCERTIFICATE = "RequireClientCertificate";
public static final String aSP_USERNAMETOKEN = "UsernameToken";
public static final String aSP_HASHPASSWORD = "HashPassword";
public static final String aSP_X509TOKEN = "X509Token";
public static final String aSP_SUPPORTINGTOKENS = "SupportingTokens";
public static final String aWSU_NAMESPACE_URI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd";
public static final String aWSU_NAMESPACE_PREFIX = "wsu";
public static final String aWSU_ID = "Id";
public static final int WSP_PRIMITIVE_ASSERTION = 0;
public static final int WSP_ALL_ASSERTION = 1;
public static final int WSP_EXACTLY_ONE_ASSERTION = 2;
private static XPathFactory mXPathFactory;
private Element moDomNode;
private int meAssertionType;
private org.w3c.dom.Document mShadowDoc;
private org.w3c.dom.Node mShadowNode;
public final Policy getFirstPolicyChild() {
return Policy.getNextPolicySibling(this.getFirstXMLChild());
}
public final Policy getNextPolicySibling() {
return Policy.getNextPolicySibling(this.getNextXMLSibling());
}
public static CompoundAssertion resolvePolicy(Element oHostNode) {
CompoundAssertion poPolicy;
AppModel appModel = new AppModel(null);
Document oDestDoc = appModel.getDocument();
oDestDoc.declareGlobalXMLId("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd", "Id");
CompoundAssertion poMerged = null;
String sPolicyURIs = null;
int attrIndex = oHostNode.findAttr("http://schemas.xmlsoap.org/ws/2004/09/policy", "PolicyURIs");
if (attrIndex >= 0) {
sPolicyURIs = oHostNode.getAttrVal(attrIndex);
}
if (!StringUtils.isEmpty(sPolicyURIs)) {
StringBuilder uriTokenizer = new StringBuilder(sPolicyURIs);
String sURI = StringUtils.parseToken(uriTokenizer);
while (sURI != null) {
poPolicy = Policy.loadPolicyByReference(oDestDoc, oHostNode.getOwnerDocument(), sURI);
poMerged = poPolicy != null && poMerged != null ? Policy.mergePolicies(poMerged, poPolicy) : poPolicy;
sURI = StringUtils.parseToken(uriTokenizer);
}
}
for (Node oChild = oHostNode.getFirstXMLChild(); oChild != null; oChild = oChild.getNextXMLSibling()) {
Element e;
if (!(oChild instanceof Element) || !Policy.isPolicyOrRef(e = (Element)oChild)) continue;
poPolicy = (CompoundAssertion)Policy.loadPolicy(oDestDoc, e);
poMerged = poPolicy != null && poMerged != null ? Policy.mergePolicies(poMerged, poPolicy) : poPolicy;
}
if (poMerged != null) {
CompoundAssertion policy = poMerged;
oDestDoc.appendChild(policy.moDomNode);
}
return poMerged;
}
public static void deletePolicyInfo(Element oHostNode) {
String policyURIsAttr = null;
int attrIndex = oHostNode.findAttr("http://schemas.xmlsoap.org/ws/2004/09/policy", "PolicyURIs");
if (attrIndex >= 0 && !StringUtils.isEmpty(policyURIsAttr = oHostNode.getAttrVal(attrIndex))) {
oHostNode.removeAttr(attrIndex);
}
for (Node oChild = oHostNode.getFirstXMLChild(); oChild != null; oChild = oChild.getNextXMLSibling()) {
if (!(oChild instanceof Element) || !Policy.isPolicyOrRef((Element)oChild)) continue;
oHostNode.removeChild(oChild);
}
}
static Policy loadPolicy(Document oDestDoc, Element oSrc) {
String aNameSpaceURI = oSrc.getNS();
String aLocalName = oSrc.getLocalName();
Policy result = null;
if (aNameSpaceURI == "http://schemas.xmlsoap.org/ws/2004/09/policy" && aLocalName == "PolicyReference") {
String uriAttr;
int attrIndex = oSrc.findAttr("http://schemas.xmlsoap.org/ws/2004/09/policy", "URI");
if (attrIndex >= 0 && !StringUtils.isEmpty(uriAttr = oSrc.getAttrVal(attrIndex))) {
result = Policy.loadPolicyByReference(oDestDoc, oSrc.getOwnerDocument(), uriAttr);
}
} else {
result = aNameSpaceURI == "http://schemas.xmlsoap.org/ws/2004/09/policy" && aLocalName == "Policy" ? new CompoundAssertion(oDestDoc, oSrc, 1) : (aNameSpaceURI == "http://schemas.xmlsoap.org/ws/2004/09/policy" && aLocalName == "PolicyReference" ? new CompoundAssertion(oDestDoc, oSrc, 1) : (aNameSpaceURI == "http://schemas.xmlsoap.org/ws/2004/09/policy" && aLocalName == "All" ? new CompoundAssertion(oDestDoc, oSrc, 1) : (aNameSpaceURI == "http://schemas.xmlsoap.org/ws/2004/09/policy" && aLocalName == "ExactlyOne" ? new CompoundAssertion(oDestDoc, oSrc, 2) : new PrimitiveAssertion(oDestDoc, oSrc))));
}
return result;
}
public static CompoundAssertion newPolicy(Document oDestDoc, ObjectHolder<Element> oNewDomNodeOut) {
CompoundAssertion poPolicy = new CompoundAssertion(oDestDoc, "http://schemas.xmlsoap.org/ws/2004/09/policy", "Policy");
if (oNewDomNodeOut != null) {
oNewDomNodeOut.value = poPolicy.getDomNode();
}
return poPolicy;
}
public int getAssertionType() {
return this.meAssertionType;
}
private static CompoundAssertion loadPolicyByReference(Document oDestDoc, Document oSrcDoc, String sURI) {
String sID;
Element oSrc;
if (sURI.length() > 0 && sURI.charAt(0) == '#' && (oSrc = oSrcDoc.getElementByXMLId(sID = sURI.substring(1))) != null) {
return new CompoundAssertion(oDestDoc, oSrc, 1);
}
return null;
}
private static CompoundAssertion mergePolicies(Policy poLeft, Policy poRight) {
String sPrefix = poLeft.getDomNode().getPrefix();
String sLocal = "All";
String aQName = (sPrefix + ':' + sLocal).intern();
poLeft.getDomNode().setQName(aQName);
poRight.getDomNode().setQName(aQName);
Document oDestDoc = poLeft.getDomNode().getOwnerDocument();
CompoundAssertion poMerged = new CompoundAssertion(oDestDoc, "http://schemas.xmlsoap.org/ws/2004/09/policy", "Policy");
poMerged.addPolicy(poLeft);
poMerged.addPolicy(poRight);
return poMerged;
}
public String getProperty(String sXPath) {
org.w3c.dom.Node shadowNode = this.getShadowNode();
XPath xpath = Policy.getXPathFactory().newXPath();
NamespaceContextImpl namespaceContext = new NamespaceContextImpl(shadowNode);
namespaceContext.addNamespaceMapping("wsp", "http://schemas.xmlsoap.org/ws/2004/09/policy");
namespaceContext.addNamespaceMapping("sp", "http://schemas.xmlsoap.org/ws/2005/07/securitypolicy");
namespaceContext.addNamespaceMapping("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd");
xpath.setNamespaceContext(namespaceContext);
try {
return xpath.evaluate(sXPath, shadowNode);
}
catch (XPathExpressionException e) {
throw new ExFull(ResId.XPATH_ERROR, e.getMessage());
}
}
public Policy getParentPolicy() {
Element parent = this.getXMLParent();
if (parent == null) {
return null;
}
assert (parent instanceof Policy);
return (Policy)parent;
}
void addPolicy(Policy poPolicy) {
this.moDomNode.appendChild(poPolicy.getDomNode());
this.appendChild(poPolicy);
}
private org.w3c.dom.Node getShadowNode() {
if (this.mShadowNode == null) {
org.w3c.dom.Document shadowDoc = this.getShadowDocument(this.moDomNode);
this.mShadowNode = DOM.attach(shadowDoc, this.moDomNode);
}
return this.mShadowNode;
}
private static XPathFactory getXPathFactory() {
if (mXPathFactory == null) {
mXPathFactory = XPathFactory.newInstance();
}
return mXPathFactory;
}
static final boolean isPolicyOrRef(Element e) {
String localName = e.getLocalName();
return e.getNS() == "http://schemas.xmlsoap.org/ws/2004/09/policy" && (localName == "Policy" || localName == "PolicyReference");
}
protected Policy(Document oDestDoc, Element oSrcNode) {
this.init(oDestDoc, (Element)oDestDoc.importNode(oSrcNode, false));
}
protected Policy(Document oDestDoc, String aNameSpaceURI, String aLocalName) {
String sPrefix = "";
if (aNameSpaceURI == "http://schemas.xmlsoap.org/ws/2004/09/policy") {
sPrefix = "wsp:";
} else if (aNameSpaceURI == "http://schemas.xmlsoap.org/ws/2005/07/securitypolicy") {
sPrefix = "sp:";
}
this.init(oDestDoc, oDestDoc.createElementNS(aNameSpaceURI, sPrefix + aLocalName, null));
}
protected final void setAssertionType(int eAssertionType) {
this.meAssertionType = eAssertionType;
}
protected final Element getDomNode() {
return this.moDomNode;
}
private void init(Document doc, Element clonedNode) {
this.moDomNode = clonedNode;
this.moDomNode.setModel(doc.getModel());
this.setNameSpaceURI(this.moDomNode.getNS(), true, false, false);
this.setLocalName(this.moDomNode.getLocalName());
this.meAssertionType = 0;
}
private static Policy getNextPolicySibling(Node node) {
while (node != null) {
if (node instanceof Policy) {
return (Policy)node;
}
node = node.getNextXMLSibling();
}
return null;
}
private org.w3c.dom.Document getShadowDocument(Node domNode) {
if (this.mShadowDoc == null) {
Policy parentPolicy = this.getParentPolicy();
if (parentPolicy != null) {
this.mShadowDoc = parentPolicy.getShadowDocument(domNode);
} else {
org.w3c.dom.Node shadowNode = DOM.attach(this.moDomNode);
this.mShadowDoc = shadowNode.getOwnerDocument();
}
}
return this.mShadowDoc;
}
}