1 /* zxidwspaxismod.java  -  Axis2 module for TAS3 WSP
2  * Copyright (c) 2009 Symlabs (symlabs@symlabs.com), All Rights Reserved.
3  * Author: Sampo Kellomaki (sampo@iki.fi)
4  * This is confidential unpublished proprietary source code of the author.
5  * NO WARRANTY, not even implied warranties. Contains trade secrets.
6  * Distribution prohibited unless authorized in writing.
7  * Licensed under Apache License 2.0, see file COPYING.
8  * $Id: zxidappdemo.java,v 1.4 2009-11-29 12:23:06 sampo Exp $
9  * 15.12.2009, created --Sampo
10  *
11  * See also: zxid-java.pd, zxidwspdemo.java for server side
12  * http://ws.apache.org/axis2/1_1/modules.html
13  */
14 
15 package zxidjava;
16 
17 import zxidjava.*;   // Pull in the zxidjni.az() API
18 import java.io.*;
19 
20 import org.apache.axis2.AxisFault;
21 import org.apache.axis2.context.ConfigurationContext;
22 import org.apache.axis2.description.AxisDescription;
23 import org.apache.axis2.description.AxisModule;
24 import org.apache.axis2.modules.Module;
25 import org.apache.neethi.Assertion;
26 import org.apache.neethi.Policy;
27 
28 public class zxidwspaxismod implements Module {
29 
30     public static final String MODULE_NAME = "ZXID.org WSP Axis2 Module";
31 
applyPolicy(Policy arg0, AxisDescription arg1)32     public void applyPolicy(Policy arg0, AxisDescription arg1) throws AxisFault {
33         // TODO Auto-generated method stub
34     }
35 
canSupportAssertion(Assertion arg0)36     public boolean canSupportAssertion(Assertion arg0) {
37         // TODO Auto-generated method stub
38         return false;
39     }
40 
engageNotify(AxisDescription arg0)41     public void engageNotify(AxisDescription arg0) throws AxisFault {
42         // TODO Auto-generated method stub
43     }
44 
45     static final String conf = "URL=http://sp1.zxidsp.org:8080/sso&PATH=/var/zxid/";
46     static zxidjava.zxid_conf cf;
47 
init(ConfigurationContext arg0, AxisModule arg1)48     public void init(ConfigurationContext arg0, AxisModule arg1) throws AxisFault {
49         // TODO Auto-generated method stub
50 	// CONFIG: You must have created /var/zxid directory hierarchy. See `make dir'
51 	// CONFIG: You must create edit the URL to match your domain name and port
52 	System.loadLibrary("zxidjni");
53 	cf = zxidjni.new_conf_to_cf(conf);
54 	zxidjni.set_opt(cf, 1, 1);
55     }
56 
shutdown(ConfigurationContext arg0)57     public void shutdown(ConfigurationContext arg0) throws AxisFault {
58         // TODO Auto-generated method stub
59     }
60 }
61 
62 
63 public class zxidwspaxismod extends HttpServlet {
64     static final String conf = "URL=http://sp1.zxidsp.org:8080/sso&PATH=/var/zxid/";
65     static zxidjava.zxid_conf cf;
66     static {
67 	// CONFIG: You must have created /var/zxid directory hierarchy. See `make dir'
68 	// CONFIG: You must create edit the URL to match your domain name and port
69 	System.loadLibrary("zxidjni");
70 	cf = zxidjni.new_conf_to_cf(conf);
zxidjni.set_opt(cf, 1, 1)71 	zxidjni.set_opt(cf, 1, 1);
72     }
doGet(HttpServletRequest req, HttpServletResponse res)73     public void doGet(HttpServletRequest req, HttpServletResponse res)
74 	throws ServletException, IOException
75     {
76 	String fullURL = req.getRequestURI();
77 	if (req.getQueryString() != null)
78 	    fullURL += "?" + req.getQueryString();
79 	System.err.print("Start ZXID App Demo GET("+fullURL+")...\n");
80 	HttpSession ses = req.getSession(false);  // Important: do not allow automatic session.
81 	if (ses == null) {                        // Instead, redirect to sso servlet.
82 	    res.sendRedirect("sso?o=E&fr=" + fullURL);
83 	    return;
84 	}
85 
86 	res.setContentType("text/html");
87 	res.getOutputStream().print("<title>ZXID Demo App Protected Content</title><body><h1>ZXID Demo App Protected Content at " + fullURL + "</H1>\n");
88 
89 	// Render logout buttons (optional)
90 
91 	res.getOutputStream().print("[<a href=\"sso?gl=1&s="+ses.getValue("sesid")+"\">Local Logout</a> | <a href=\"sso?gr=1&s="+ses.getValue("sesid")+"\">Single Logout</a>]\n");
92 
93 	// The SSO servlet will have done one iteration of authorization. The following
94 	// serves to illustrate, how to explicitly call a PDP from your code.
95 
96 	if (zxidjni.az_cf(cf, "Action=Show", ses.getValue("sesid").toString()) == 0) {
97 	    res.getOutputStream().print("<p><b>Denied.</b> Normally page would not be shown, but we show the session attributes for debugging purposes.\n");
98 	    //res.setStatus(302, "Denied");
99 	} else {
100 	    res.getOutputStream().print("<p>Authorized.\n");
101 	}
102 
103 	// Render protected content page (your application starts working)
104 
105 	res.getOutputStream().print("<pre>HttpSession dump:\n");
106 	String[] val_names = ses.getValueNames();
107 	for (int i = 0; i < val_names.length; ++i) {
108 	    res.getOutputStream().print(val_names[i] + ": " + ses.getValue(val_names[i]) + "\n");
109 	}
110 	res.getOutputStream().print("</pre>");
111 
112 	// Demo web service call
113 
114 	String ret;
115 	String sid = ses.getValue("sesid").toString();
116 	res.getOutputStream().print("<p>Output from idhrxml web service call sid("+sid+"):<br>\n<textarea cols=80 rows=20>");
117 	ret = zxidjni.call(cf, zxidjni.fetch_ses(cf, sid),
118 			   zxidjni.zx_xmlns_idhrxml, null, null, null,
119 			   "<idhrxml:Query>" +
120 			     "<idhrxml:QueryItem>" +
121 			       "<idhrxml:Select></idhrxml:Select>" +
122 			     "</idhrxml:QueryItem>" +
123 			   "</idhrxml:Query>");
124 
125 	res.getOutputStream().print(ret);
126 	res.getOutputStream().print("</textarea>");
127 
128 	// Demo another web service call, this time the service by zxidwspdemo.java
129 
130 	res.getOutputStream().print("<p>Output from foobar web service call:<br>\n<textarea cols=80 rows=20>");
131 	ret = zxidjni.call(cf, zxidjni.fetch_ses(cf, sid), "urn:x-foobar", null, null, null,
132 			   "<foobar>Do it!</foobar>");
133 
134 	res.getOutputStream().print(ret);
135 	res.getOutputStream().print("</textarea>");
136     }
137 }
138 
139 /* EOF */
140