1 /* zxidhlo.java  -  Hello World Java/Tomcat servlet script that calls libzxid using JNI
2  * Copyright (c) 2007-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: zxidhlo.java,v 1.8 2009-10-16 13:36:33 sampo Exp $
9  * 12.1.2007, created --Sampo
10  *
11  * See also: README-zxid section 10 "zxid_simple() API"
12  */
13 
14 import zxidjava.*;
15 import java.io.*;
16 import javax.servlet.*;
17 import javax.servlet.http.*;
18 
19 public class zxidhlo extends HttpServlet {
20     static { System.loadLibrary("zxidjni"); }
21 
22     // CONFIG: You must have created /var/zxid directory hierarchy. See `make dir'
23     // CONFIG: You must edit the URL to match your domain name and port
24     static final String conf = "URL=http://sp1.zxidsp.org:8080/zxidservlet/zxidHLO&PATH=/var/zxid/";
25 
26     //public static void main(String argv[]) throws java.io.IOException  {  }
do_zxid(HttpServletRequest req, HttpServletResponse res, String qs)27     public void do_zxid(HttpServletRequest req, HttpServletResponse res, String qs)
28 	throws ServletException, IOException
29     {
30 	String ret = zxidjni.simple(conf, qs, 0x1d54);
31 	System.err.print(ret);
32 	switch (ret.charAt(0)) {
33 	case 'L':  /* Redirect: ret == "LOCATION: urlCRLF2" */
34 	    res.sendRedirect(ret.substring(10, ret.length() - 4));
35 	    return;
36 	case '<':
37 	    switch (ret.charAt(1)) {
38 	    case 's':  /* <se:  SOAP envelope */
39 	    case 'm':  /* <m20: metadata */
40 		res.setContentType("text/xml");
41 		break;
42 	    default:
43 		res.setContentType("text/html");
44 		break;
45 	    }
46 	    res.setContentLength(ret.length());
47 	    res.getOutputStream().print(ret);
48 	    break;
49 	case 'd': /* Logged in case */
50 	    //my_parse_ldif(ret);
51 	    int x = ret.indexOf("\nsesid: ");
52 	    int y = ret.indexOf('\n', x + 8);
53 	    String sid = ret.substring(x + 8, y);
54 	    System.err.print("Logged in. sid="+sid+"\n");
55 	    res.setContentType("text/html");
56 	    res.getOutputStream().print(zxidjni.fed_mgmt(conf, sid, 0xd54));
57 	    break;
58 	default:
59 	    System.err.print("Unknown zxid_simple() response.\n");
60 	}
61     }
62 
doGet(HttpServletRequest req, HttpServletResponse res)63     public void doGet(HttpServletRequest req, HttpServletResponse res)
64 	throws ServletException, IOException
65     {
66 	System.err.print("Start GET...\n");
67 	// LECP/ECP PAOS header checks
68 	do_zxid(req, res, req.getQueryString());
69     }
70 
doPost(HttpServletRequest req, HttpServletResponse res)71     public void doPost(HttpServletRequest req, HttpServletResponse res)
72 	throws ServletException, IOException
73     {
74 	System.err.print("Start POST...\n");
75 	String qs;
76 	int len = req.getContentLength();
77 	byte[] b = new byte[len];
78 	int here, got;
79 	for (here = 0; here < len; here += got)
80 	    got = req.getInputStream().read(b, here, len - here);
81 	qs = new String(b, 0, len);
82 	do_zxid(req, res, qs);
83     }
84 }
85 
86 /* EOF */
87