1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
package org.openpermis.xacml.io.v2; |
9 | |
|
10 | |
import java.io.IOException; |
11 | |
import java.io.StringWriter; |
12 | |
import java.util.Set; |
13 | |
|
14 | |
|
15 | |
import org.dom4j.Document; |
16 | |
import org.dom4j.DocumentHelper; |
17 | |
import org.dom4j.Element; |
18 | |
import org.dom4j.io.OutputFormat; |
19 | |
import org.dom4j.io.XMLWriter; |
20 | |
|
21 | |
import org.openpermis.policy.AccessDecision; |
22 | |
import org.openpermis.xacml.io.XacmlStatus; |
23 | |
import org.openpermis.xacml.io.XacmlWriter; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class BasicXacmlWriter implements XacmlWriter { |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public static final String XACML_CONTEXT_NAMESPACE = |
38 | |
"urn:oasis:names:tc:xacml:2.0:context:schema:os"; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public static final String XACML_POLICY_NAMESPACE = |
44 | |
"urn:oasis:names:tc:xacml:2.0:policy:schema:os"; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 8 | public BasicXacmlWriter () { |
53 | |
|
54 | 8 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
private String formatXml (Document document) throws IOException { |
65 | 8 | final OutputFormat format = OutputFormat.createPrettyPrint(); |
66 | 8 | final StringWriter out = new StringWriter(); |
67 | 8 | final XMLWriter writer = new XMLWriter(out, format); |
68 | 8 | writer.write(document); |
69 | 8 | return out.getBuffer().toString(); |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private void addDecision (Element parent, AccessDecision inputDecision) { |
79 | 8 | final Element decision = parent.addElement("Decision"); |
80 | 8 | if (inputDecision == null) { |
81 | 2 | decision.addText("Indeterminate"); |
82 | 6 | } else if (inputDecision.isAccessGranted()) { |
83 | 5 | decision.addText("Permit"); |
84 | |
} else { |
85 | 1 | decision.addText("Deny"); |
86 | |
} |
87 | 8 | } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private void addObligations (Element parent, AccessDecision inputDecision) { |
98 | 8 | if (inputDecision != null && !inputDecision.getObligations().isEmpty()) { |
99 | 1 | final Element obligations = parent.addElement("Obligations"); |
100 | 1 | obligations.addNamespace("xmlns", XACML_POLICY_NAMESPACE); |
101 | |
|
102 | 1 | for (Set<String> obligationSet : inputDecision.getObligations()) { |
103 | 1 | for (String obligationId : obligationSet) { |
104 | 2 | final Element obligation = obligations.addElement("Obligation"); |
105 | 2 | obligation.addAttribute("ObligationId", obligationId); |
106 | 2 | obligation.addAttribute("FulfillOn", "Permit"); |
107 | 2 | } |
108 | |
} |
109 | |
} |
110 | 8 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
private void addStatus (Element parent, XacmlStatus inputStatus) { |
119 | 8 | if (inputStatus != null) { |
120 | 4 | final Element status = parent.addElement("Status"); |
121 | 4 | status.addElement("StatusCode").addAttribute("Value", inputStatus.getCode()); |
122 | 4 | if (inputStatus.getMessage() != null) { |
123 | 2 | status.addElement("StatusMessage").addText(inputStatus.getMessage()); |
124 | |
} |
125 | |
} |
126 | 8 | } |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public String writeResponse (AccessDecision inputDecision, XacmlStatus inputStatus) { |
134 | 8 | final Document document = DocumentHelper.createDocument(); |
135 | 8 | document.setXMLEncoding("UTF-8"); |
136 | |
|
137 | 8 | final Element response = document.addElement("Response"); |
138 | 8 | response.addNamespace("xmlns", XACML_CONTEXT_NAMESPACE); |
139 | |
|
140 | 8 | final Element result = response.addElement("Result"); |
141 | |
|
142 | 8 | addDecision(result, inputDecision); |
143 | 8 | addStatus(result, inputStatus); |
144 | 8 | addObligations(result, inputDecision); |
145 | |
|
146 | |
try { |
147 | 8 | return formatXml(document); |
148 | 0 | } catch (IOException e) { |
149 | 0 | return document.asXML(); |
150 | |
} |
151 | |
} |
152 | |
|
153 | |
} |