-
Notifications
You must be signed in to change notification settings - Fork 0
/
benedemo.java
362 lines (314 loc) · 13.1 KB
/
benedemo.java
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* benedemo.java - Demonstrate detecting missing session and redirection to zxidsrvlet
* Copyright (c) 2010-2011 Sampo Kellomaki ([email protected]), All Rights Reserved.
* Copyright (c) 2009 Symlabs ([email protected]), All Rights Reserved.
* Author: Sampo Kellomaki ([email protected])
* This is confidential unpublished proprietary source code of the author.
* NO WARRANTY, not even implied warranties. Contains trade secrets.
* Distribution prohibited unless authorized in writing.
* Licensed under Apache License 2.0, see file COPYING.
* $Id: zxidappdemo.java,v 1.4 2009-11-29 12:23:06 sampo Exp $
* 16.10.2009, created --Sampo
*
* This servlet plays the role of "payload" servlet in ZXID SSO servlet
* integration demonstration. It illustrates the steps
* 1. Detect that there is no session and redirect to zxidsrvlet; and
* 7. Access to protected resource, with attributes already populated
* to the HttpSession (JSESSION)
* 9. Making a web service call by directly calling zxid_call()
*
* See also: zxid-java.pd, zxidwspdemo.java for server side
*/
import zxidjava.*; // Pull in the zxidjni.az() API
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Enumeration;
public class benedemo extends HttpServlet {
static final boolean verbose = false;
static final Pattern fedusername_pat = Pattern.compile("fedusername:[ ]([^\\n]*)");
static final Pattern idpnid_pat = Pattern.compile("idpnid:[ ]([^\\n]*)");
static final Pattern nidfmt_pat = Pattern.compile("nidfmt:[ ]([^\\n]*)");
static final Pattern affid_pat = Pattern.compile("affid:[ ]([^\\n]*)");
static final Pattern eid_pat = Pattern.compile("eid:[ ]([^\\n]*)");
static final Pattern cn_pat = Pattern.compile("cn:[ ]([^\\n]*)");
static final Pattern o_pat = Pattern.compile("o:[ ]([^\\n]*)");
static final Pattern ou_pat = Pattern.compile("ou:[ ]([^\\n]*)");
static final Pattern role_pat = Pattern.compile("role:[ ]([^\\n]*)");
static final Pattern boot_pat = Pattern.compile("urn:liberty:disco:2006-08:DiscoveryEPR:[ ]([^\\n]*)");
static final String conf = "URL=https://benefit.gov.uk:8443/serv/benesso&PATH=/home/sampo/zxid/postoffice/conf/";
static zxidjava.zxid_conf cf;
static {
// CONFIG: You must have created /var/zxid directory hierarchy. See `make dir'
// CONFIG: You must create edit the URL to match your domain name and port
System.loadLibrary("zxidjni");
cf = zxidjni.new_conf_to_cf(conf);
zxidjni.set_opt(cf, 1, 1);
}
public void hilite_fields(ServletOutputStream out, String ret, int n)
throws IOException
{
int i;
try {
Matcher matcher = idpnid_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher.find();
out.print("<b>fedusername</b>: " + matcher.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
try {
Matcher matcher = idpnid_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher.find();
out.print("<b>idpnid</b>: " + matcher.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
try {
Matcher matcher2 = nidfmt_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher2.find();
out.print("<b>nidfmt</b>: " + matcher2.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
try {
Matcher matcher3 = affid_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher3.find();
out.print("<b>affid</b>: " + matcher3.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
try {
Matcher matcher = eid_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher.find();
out.print("<b>eid</b>: " + matcher.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
try {
Matcher matcher = cn_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher.find();
out.print("<b>cn</b>: " + matcher.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
try {
Matcher matcher = o_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher.find();
out.print("<b>o</b>: " + matcher.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
try {
Matcher matcher = ou_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher.find();
out.print("<b>ou</b>: " + matcher.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
try {
Matcher matcher = role_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher.find();
out.print("<b>role</b>: " + matcher.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
try {
Matcher matcher = boot_pat.matcher(ret);
for (i = n; i > 0; --i)
matcher.find();
out.print("<b>urn:liberty:disco:2006-08:DiscoveryEPR</b>: " + matcher.group(1) + "<br>\n");
} catch(IllegalStateException e) { }
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String fullURL = req.getRequestURI();
String qs = req.getQueryString();
if (qs != null)
fullURL += "?" + req.getQueryString();
else
qs = "";
System.err.print("Start ZXID App Demo GET("+fullURL+")...\n");
HttpSession ses = req.getSession(false); // Important: do not allow automatic session.
if (ses == null) { // Instead, redirect to sso servlet.
res.sendRedirect("benesso?o=E&fr=" + fullURL);
return;
}
ServletOutputStream out = res.getOutputStream();
res.setContentType("text/html");
out.print("<title>Benefits Demo Protected Content</title><body>\n");
out.print("<link rel=\"shortcut icon\" href=\"/favicon.ico\" type=\"image/x-icon\" />");
out.print("<link type=\"text/css\" rel=stylesheet href=\"bene.css\"><body bgcolor=white>");
//out.print("<table align=right><tr><td>");
//out.print("<a href=\"http://www.tas3.eu/\"><img src=\"tas3-logo.jpg\" height=64 border=0></a>");
//out.print("<a href=\"http://zxid.org/\"><img src=\"logo-zxid-128x128.png\" height=64 border=0></a>");
//out.print("</td></tr></table>");
out.print("<h1 class=zxtop><img align=right src=\"DirectgovLogo.gif\"><br>Benefits Demo<br>Protected Content</h1>\n");
//out.print("<h1>ZXID Demo App Protected Content</h1> at " + fullURL + "\n");
// Render logout buttons (optional)
out.print("[<a href=\"benesso?gl=1&s="+ses.getAttribute("sesid")+"\">Local Logout</a> | <a href=\"benesso?gr=1&s="+ses.getAttribute("sesid")+"\">Single Logout</a>]\n");
// The SSO servlet will have done one iteration of authorization. The following
// serves to illustrate, how to explicitly call a PDP from your code.
if (zxidjni.az_cf(cf, "Action=Show", ses.getAttribute("sesid").toString()) == null) {
out.print("<p><b>Denied.</b> Normally page would not be shown, but we show the session attributes for debugging purposes.\n");
//res.setStatus(302, "Denied");
} else {
out.print("<p>Authorized.\n");
}
out.print("<table align=right><tr><td>");
out.print("<img src=\"tas3-recurs-demo.png\" border=0>");
out.print("</td></tr></table>");
// Render protected content page (your application starts working)
out.print("<h4>HttpSession dump:</h4>");
Enumeration val_names = ses.getAttributeNames();
while (val_names.hasMoreElements()) {
String name = (String)val_names.nextElement();
if (name.equals("cn")
|| name.equals("role")
|| name.equals("o")
|| name.equals("ou")
|| name.equals("idpnid")
|| name.equals("nidfmt")
|| name.equals("affid")
|| name.equals("eid")
|| name.equals("urn:liberty:disco:2006-08:DiscoveryEPR")) {
out.print("<b>" + name + "</b>: " + ses.getAttribute(name) + "<br>\n");
} else {
if (verbose)
out.print(name + ": " + ses.getAttribute(name) + "<br>\n");
}
}
out.print("<p>");
out.print("[ <a href=\"?idhrxml\">tas3_call(idhrxml)</a>");
out.print(" | <a href=\"?x-foobar\">Recursive Echo</a>");
out.print(" | <a href=\"?leaf\">Leaf Echo</a>");
out.print(" | <a href=\"?multidi\">Multi discovery</a>");
out.print(" | <a href=\"?multi\">Multi discovery and call</a>");
out.print(" | <a href=\"?all\">All</a>");
out.print(" | <a href=\"?exit\">Exit Java</a>");
out.print("]</p>");
// Demo web service call to zxidhrxmlwsp
String ret;
String sid = ses.getAttribute("sesid").toString();
zxid_ses zxses = zxidjni.fetch_ses(cf, sid);
if (qs.equals("idhrxml") || qs.equals("all")) {
out.print("<p>Output from idhrxml web service call sid("+sid+"):<br>\n<textarea cols=80 rows=20>");
ret = zxidjni.call(cf, zxses,
zxidjni.zx_xmlns_idhrxml,
"http://sp.tas3.pt:8081/zxidhrxmlwsp?o=B",
null, null,
"<idhrxml:Query>"
+ "<idhrxml:QueryItem>"
+ "<idhrxml:Select></idhrxml:Select>"
+ "</idhrxml:QueryItem>" +
"</idhrxml:Query>");
ret = zxidjni.extract_body(cf, ret);
out.print(ret);
out.print("</textarea>");
}
// Demo another web service call, this time the service by zxidwspdemo.java
if (qs.equals("x-foobar") || qs.equals("all")) {
out.print("<p>Output from recursive web service call:<br>\n");
ret = zxidjni.call(cf, zxses, "urn:x-foobar",
"http://sp.tas3.pt:8080/zxidservlet/wspdemo?o=B", null, null,
"<foobar>Do it!</foobar>");
ret = zxidjni.extract_body(cf, ret);
if (ret == null || ret.indexOf("code=\"OK\"") == -1) {
out.print("<p>Error from call:<br>\n<textarea cols=80 rows=20>");
out.print(ret);
out.print("</textarea>\n");
} else {
out.print("<p>Output from Leaf web services call (relayed by middle call):<br>\n");
hilite_fields(out, ret, 1);
out.print("<p>Output from Middle web services call:<br>\n");
hilite_fields(out, ret, 2);
if (true || verbose) {
out.print("<textarea cols=80 rows=20>");
out.print(ret);
out.print("</textarea>\n");
}
}
}
// Demo another web service call, this time the service by zxidwspleaf.java
if (qs.equals("leaf") || qs.equals("all")) {
ret = zxidjni.call(cf, zxses, "x-recurs", null, null, null,
"<foobar>Do it!</foobar>");
ret = zxidjni.extract_body(cf, ret);
if (ret == null || ret.indexOf("code=\"OK\"") == -1) {
out.print("<p>Error from call:<br>\n<textarea cols=80 rows=20>");
out.print(ret);
out.print("</textarea>\n");
} else {
out.print("<p>Output from Leaf web services call:<br>\n");
hilite_fields(out, ret, 1);
if (true || verbose) {
out.print("<textarea cols=80 rows=20>");
out.print(ret);
out.print("</textarea>\n");
}
}
}
// Multidiscovery
if (qs.equals("multidi")) {
out.print("<h4>Multidiscovery</h4>\n");
zxid_epr epr[] = new zxid_epr[100];
for (int i=1; i<100; ++i) {
epr[i] = zxidjni.get_epr(cf, zxses, "urn:x-foobar", null, null, null, i);
if (epr[i] == null)
break;
out.print("<p>EPR "+i+" <a href=\"?o=call&url="+zxidjni.get_epr_address(cf, epr[i])+"\">Call</a>");
out.print(" desc("+zxidjni.get_epr_desc(cf, epr[i])+") svctype(urn:x-foobar)<br>\n");
out.print(" address("+zxidjni.get_epr_address(cf, epr[i])+")<br>\n");
out.print(" entid("+zxidjni.get_epr_entid(cf, epr[i])+")<br>\n");
}
}
// Call specific
if (qs.startsWith("o=call&url=")) {
String url = qs.substring(11);
out.print("<h4>Specific Call</h4>\n");
ret = zxidjni.call(cf, zxses, "urn:x-foobar", url, null, null,
"<foobar>do it</foobar>");
ret = zxidjni.extract_body(cf, ret);
if (ret.indexOf("code=\"OK\"") == -1) {
out.print("<p>Error from call address("+url+"):<br>\n<textarea cols=80 rows=20>");
out.print(ret);
out.print("</textarea>\n");
} else {
out.print("<p>Output from call address("+url+"):<br>\n");
hilite_fields(out, ret, 1);
if (verbose) {
out.print("<textarea cols=80 rows=20>");
out.print(ret);
out.print("</textarea>\n");
}
}
}
// Multidiscovery and call
if (qs.equals("multi") || qs.equals("all")) {
out.print("<h4>Multidiscovery and Call</h4>\n");
zxid_epr epr[] = new zxid_epr[100];
for (int i=1; i<100; ++i) {
epr[i] = zxidjni.get_epr(cf, zxses, "urn:x-foobar", null, null, null, i);
if (epr[i] == null)
break;
out.print("<p>EPR "+i+" address("+zxidjni.get_epr_address(cf, epr[i])+")\n");
out.print("<p>EPR "+i+" entid("+zxidjni.get_epr_entid(cf, epr[i])+")\n");
out.print("<p>EPR "+i+" desc("+zxidjni.get_epr_desc(cf, epr[i])+")\n");
}
for (int i=1; i<100; ++i) {
if (epr[i] == null)
break;
out.print("<p>Output from multicall "+i+" entid:<br>\n<textarea cols=80 rows=20>");
ret = zxidjni.call(cf, zxses, "urn:x-foobar", zxidjni.get_epr_entid(cf, epr[i]), null, null,
"<foobar>do i="+i+"</foobar>");
ret = zxidjni.extract_body(cf, ret);
out.print(ret);
out.print("</textarea>\n");
out.print("<p>Output from multicall "+i+" address:<br>\n<textarea cols=80 rows=20>");
ret = zxidjni.call(cf, zxses, "urn:x-foobar", zxidjni.get_epr_address(cf, epr[i]), null, null,
"<foobar>do i="+i+"</foobar>");
ret = zxidjni.extract_body(cf, ret);
out.print(ret);
out.print("</textarea>\n");
}
}
if (qs.equals("exit")) {
System.err.print("Controlled exit forced (can be used to cause __gcov_flush())\n");
zxidjni.set_opt(cf, 5, 0);
}
out.print("<p>Done.\n<div class=zxbot><a class=zx href=\"/index.html\">Demo Index</a></div>");
}
}
/* EOF */