1 /* zxidwscprepdemo.java - Demonstrate calling web service using alternate API 2 * Copyright (c) 2010 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved. 3 * This is confidential unpublished proprietary source code of the author. 4 * NO WARRANTY, not even implied warranties. Contains trade secrets. 5 * Distribution prohibited unless authorized in writing. 6 * Licensed under Apache License 2.0, see file COPYING. 7 * $Id$ 8 * 21.3.2010, created --Sampo 9 * 10 * This servlet plays the role of "payload" servlet in ZXID SSO servlet 11 * integration demonstration. It illustrates the steps 12 * 1. Detect that there is no session and redirect to zxidsrvlet; and 13 * 7. Access to protected resource, with attributes already populated 14 * to the HttpSession (JSESSION) 15 * 9. Making a web service call by directly calling zxid_call() 16 * 17 * See also: zxid-java.pd, zxidwspdemo.java for server side 18 * http://sp.tas3.pt:8080/zxidservlet/sso/wscprepdemo 19 * ./servlet/WEB-INF/web.xml 20 */ 21 22 import zxidjava.*; // Pull in the zxidjni.az() API 23 import java.io.*; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 import java.util.regex.Pattern; 27 import java.util.regex.Matcher; 28 import java.util.Enumeration; 29 30 public class zxidwscprepdemo extends HttpServlet { 31 static final boolean verbose = false; 32 static final Pattern idpnid_pat = Pattern.compile("idpnid:[ ]([^\\n]*)"); 33 static final Pattern nidfmt_pat = Pattern.compile("nidfmt:[ ]([^\\n]*)"); 34 static final Pattern affid_pat = Pattern.compile("affid:[ ]([^\\n]*)"); 35 static final Pattern eid_pat = Pattern.compile("eid:[ ]([^\\n]*)"); 36 static final Pattern cn_pat = Pattern.compile("cn:[ ]([^\\n]*)"); 37 static final Pattern o_pat = Pattern.compile("o:[ ]([^\\n]*)"); 38 static final Pattern ou_pat = Pattern.compile("ou:[ ]([^\\n]*)"); 39 static final Pattern role_pat = Pattern.compile("role:[ ]([^\\n]*)"); 40 static final Pattern boot_pat = Pattern.compile("urn:liberty:disco:2006-08:DiscoveryEPR:[ ]([^\\n]*)"); 41 42 static final String conf = "URL=http://sp1.zxidsp.org:8080/sso&PATH=/var/zxid/"; 43 static zxidjava.zxid_conf cf; 44 static { 45 // CONFIG: You must have created /var/zxid directory hierarchy. See `make dir' 46 // CONFIG: You must create edit the URL to match your domain name and port 47 System.loadLibrary("zxidjni"); 48 cf = zxidjni.new_conf_to_cf(conf); zxidjni.set_opt(cf, 1, 1)49 zxidjni.set_opt(cf, 1, 1); 50 } 51 52 // public String zxid_dead_simple_call(String sid, String svctype, String url, String body) 53 // { 54 // //System.loadLibrary("zxidjni"); 55 // zxidjava.zxid_conf cf = zxidjni.new_conf_to_cf("PATH=/var/zxid/"); 56 // zxidjni.set_opt(cf, 1, 1); 57 // zxid_ses zxses = zxidjni.fetch_ses(cf, ***sid); 58 59 // ret = zxidjni.call(cf, zxses, 60 // svctype, 61 // url, 62 // null, null, 63 // body); 64 // return ret; 65 // } 66 hilite_fields(ServletOutputStream out, String ret, int n)67 public void hilite_fields(ServletOutputStream out, String ret, int n) 68 throws IOException 69 { 70 int i; 71 try { 72 Matcher matcher = idpnid_pat.matcher(ret); 73 for (i = n; i > 0; --i) 74 matcher.find(); 75 out.print("<b>idpnid</b>: " + matcher.group(1) + "<br>\n"); 76 } catch(IllegalStateException e) { } 77 78 try { 79 Matcher matcher2 = nidfmt_pat.matcher(ret); 80 for (i = n; i > 0; --i) 81 matcher2.find(); 82 out.print("<b>nidfmt</b>: " + matcher2.group(1) + "<br>\n"); 83 } catch(IllegalStateException e) { } 84 85 try { 86 Matcher matcher3 = affid_pat.matcher(ret); 87 for (i = n; i > 0; --i) 88 matcher3.find(); 89 out.print("<b>affid</b>: " + matcher3.group(1) + "<br>\n"); 90 } catch(IllegalStateException e) { } 91 92 try { 93 Matcher matcher = eid_pat.matcher(ret); 94 for (i = n; i > 0; --i) 95 matcher.find(); 96 out.print("<b>eid</b>: " + matcher.group(1) + "<br>\n"); 97 } catch(IllegalStateException e) { } 98 99 try { 100 Matcher matcher = cn_pat.matcher(ret); 101 for (i = n; i > 0; --i) 102 matcher.find(); 103 out.print("<b>cn</b>: " + matcher.group(1) + "<br>\n"); 104 } catch(IllegalStateException e) { } 105 106 try { 107 Matcher matcher = o_pat.matcher(ret); 108 for (i = n; i > 0; --i) 109 matcher.find(); 110 out.print("<b>o</b>: " + matcher.group(1) + "<br>\n"); 111 } catch(IllegalStateException e) { } 112 113 try { 114 Matcher matcher = ou_pat.matcher(ret); 115 for (i = n; i > 0; --i) 116 matcher.find(); 117 out.print("<b>ou</b>: " + matcher.group(1) + "<br>\n"); 118 } catch(IllegalStateException e) { } 119 120 try { 121 Matcher matcher = role_pat.matcher(ret); 122 for (i = n; i > 0; --i) 123 matcher.find(); 124 out.print("<b>role</b>: " + matcher.group(1) + "<br>\n"); 125 } catch(IllegalStateException e) { } 126 127 try { 128 Matcher matcher = boot_pat.matcher(ret); 129 for (i = n; i > 0; --i) 130 matcher.find(); 131 out.print("<b>urn:liberty:disco:2006-08:DiscoveryEPR</b>: " + matcher.group(1) + "<br>\n"); 132 } catch(IllegalStateException e) { } 133 134 } 135 doGet(HttpServletRequest req, HttpServletResponse res)136 public void doGet(HttpServletRequest req, HttpServletResponse res) 137 throws ServletException, IOException 138 { 139 String fullURL = req.getRequestURI(); 140 String qs = req.getQueryString(); 141 if (qs != null) 142 fullURL += "?" + req.getQueryString(); 143 else 144 qs = ""; 145 System.err.print("Start ZXID App Demo GET("+fullURL+")...\n"); 146 HttpSession ses = req.getSession(false); // Important: do not allow automatic session. 147 if (ses == null) { // Instead, redirect to sso servlet. 148 res.sendRedirect("sso?o=E&fr=" + fullURL); 149 return; 150 } 151 ServletOutputStream out = res.getOutputStream(); 152 153 res.setContentType("text/html"); 154 out.print("<title>ZXID Demo App Protected Content</title><body>\n"); 155 out.print("<table align=right><tr><td>"); 156 out.print("<a href=\"http://www.tas3.eu/\"><img src=\"tas3-logo.jpg\" height=64 border=0></a>"); 157 out.print("<a href=\"http://zxid.org/\"><img src=\"logo-zxid-128x128.png\" height=64 border=0></a>"); 158 out.print("</td></tr></table>"); 159 out.print("<h1>ZXID Demo App Protected Content</h1>\n"); 160 //out.print("<h1>ZXID Demo App Protected Content</h1> at " + fullURL + "\n"); 161 162 // Render logout buttons (optional) 163 164 out.print("[<a href=\"sso?gl=1&s="+ses.getAttribute("sesid")+"\">Local Logout</a> | <a href=\"sso?gr=1&s="+ses.getAttribute("sesid")+"\">Single Logout</a>]\n"); 165 166 out.print("<table align=right><tr><td>"); 167 out.print("<img src=\"tas3-recurs-demo.png\" border=0>"); 168 out.print("</td></tr></table>"); 169 170 // Render protected content page (your application starts working) 171 172 out.print("<h4>HttpSession dump:</h4>"); 173 Enumeration val_names = ses.getAttributeNames(); 174 while (val_names.hasMoreElements()) { 175 String name = (String)val_names.nextElement(); 176 if (name.equals("cn") 177 || name.equals("role") 178 || name.equals("o") 179 || name.equals("ou") 180 || name.equals("idpnid") 181 || name.equals("nidfmt") 182 || name.equals("affid") 183 || name.equals("eid") 184 || name.equals("urn:liberty:disco:2006-08:DiscoveryEPR")) { 185 out.print("<b>" + name + "</b>: " + ses.getAttribute(name) + "<br>\n"); 186 } else { 187 if (verbose) 188 out.print(name + ": " + ses.getAttribute(name) + "<br>\n"); 189 } 190 } 191 out.print("<p>"); 192 out.print("[ <a href=\"?leaf\">zxid_call(leaf)</a>"); 193 out.print(" | [ <a href=\"?leafprep\">zxid_wsc_prepare_call(leaf)</a>"); 194 out.print(" | <a href=\"?all\">All</a>"); 195 out.print(" | <a href=\"?exit\">Exit Java</a>"); 196 out.print("]<p>"); 197 198 // Demo web service call to zxidhrxmlwsp 199 200 String ret; 201 String sid = ses.getAttribute("sesid").toString(); 202 zxid_ses zxses = zxidjni.fetch_ses(cf, sid); 203 204 // Demo another web service call, this time the service by zxidwspdemo.java 205 206 if (qs.equals("leaf") || qs.equals("all")) { 207 ret = zxidjni.call(cf, zxses, "x-recurs", null, null, null, 208 "<foobar>Do it!</foobar>"); 209 210 ret = zxidjni.extract_body(cf, ret); 211 if (ret.indexOf("code=\"OK\"") == -1) { 212 out.print("<p>Error from call:<br>\n<textarea cols=80 rows=20>"); 213 out.print(ret); 214 out.print("</textarea>\n"); 215 } else { 216 out.print("<p>Output from Leaf web services call:<br>\n"); 217 hilite_fields(out, ret, 1); 218 if (true || verbose) { 219 out.print("<textarea cols=80 rows=20>"); 220 out.print(ret); 221 out.print("</textarea>\n"); 222 } 223 } 224 } 225 if (qs.equals("leafprep") || qs.equals("all")) { 226 //SWIGTYPE_p_zx_a_EndpointReference_s epr = zxidjni.get_epr(cf, zxses, "x-recurs", null, null, null, 1); 227 zxid_epr epr = zxidjni.get_epr(cf, zxses, "x-recurs", null, null, null, 1); 228 if (epr != null) { 229 String url = zxidjni.get_epr_address(cf, epr); 230 System.err.print("URL("+url+")\n"); 231 String req_soap = zxidjni.wsc_prepare_call(cf, zxses, epr, null, 232 "<foobar>Do it!</foobar>"); 233 //System.err.print("CALL("+url+") req_soap("+req_soap+")\n"); 234 String resp_soap = zxidjni.http_cli(cf, -1, url, -1, req_soap, null, null, 0); 235 if (zxidjni.wsc_valid_resp(cf, zxses, null, resp_soap) == 1) { 236 if (resp_soap.indexOf("code=\"OK\"") == -1) { 237 out.print("<p>Error from call:<br>\n<textarea cols=80 rows=20>"); 238 out.print(resp_soap); 239 out.print("</textarea>\n"); 240 } else { 241 out.print("<p>Output from Leaf web services call (prep):<br>\n"); 242 hilite_fields(out, resp_soap, 1); 243 if (true || verbose) { 244 out.print("<textarea cols=80 rows=20>"); 245 out.print(resp_soap); 246 out.print("</textarea>\n"); 247 } 248 } 249 } else { 250 out.print("<p>Invalid response:<br>\n<textarea cols=80 rows=20>"); 251 out.print(resp_soap); 252 out.print("</textarea>\n"); 253 } 254 } else { 255 out.print("<p>No EPR found<br>\n"); 256 } 257 } 258 259 if (qs.equals("exit")) { 260 System.err.print("Controlled exit forced (can be used to cause __gcov_flush())\n"); 261 zxidjni.set_opt(cf, 5, 0); 262 } 263 264 out.print("<p>Done.\n"); 265 } 266 } 267 268 /* EOF */ 269