Get all Browser and Client Info from a ZK session

It is important to capture certain client information, such as browser, locale, ip-address and others as part of logging or auditing. I need to do this in a controller and was curious what I can retrieve from ZK. The information you can find here and there in the ZK docs. Here I summarized all essential objects and methods in one simple app.

ZKSessionInfo Web App

Pre-Requirements:

  • Netbeans with the ZK plugin (link)
  • A Groovy installation or at least the lib folder from the package (link)

Tutorial:

  1. Create a new Web Project ZKSessionInfo with the ZK Framework

    New Web Project

    New Web Project

  2. Update the index.zul file
    (Add a textbox and a button)

    <?xml version="1.0" encoding="UTF-8"?>
    <zk xmlns="http://www.zkoss.org/2005/zul">
    
        <window id="info" apply="controller.indexController" title="ZK Session Info 0.1 (20110711)" >
    
            <style dynamic="true">
                .style1 {
                font-family: monospace, courier;font-size: 13px;
                }
            </style>
    
            <vbox>
                <textbox id="txtSessionInfo" sclass="style1" value="" cols="140" rows="40"/>
                <button id="btnExec" label="Show infos" onClientInfo="onClientInfo(event)"/>
            </vbox>
    
        </window>
    </zk>
    
  3. Add a controller indexController

    Controller Class

    Modify the sourcecode:

    package controller;
    
    import java.util.Date;
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpSession;
    import org.zkoss.zk.ui.Component;
    import org.zkoss.zk.ui.Executions;
    import org.zkoss.zk.ui.Session;
    import org.zkoss.zk.ui.Sessions;
    import org.zkoss.zk.ui.event.ClientInfoEvent;
    import org.zkoss.zk.ui.event.Event;
    import org.zkoss.zk.ui.util.GenericForwardComposer;
    
    import org.zkoss.zul.Textbox;
    
    public class indexController extends GenericForwardComposer {
    
        private Textbox txtSessionInfo;
        private StringBuilder clientInfo = new StringBuilder();
    
        @Override
        public void doAfterCompose(Component comp) throws Exception {
            super.doAfterCompose(comp);
        }
    
        public void onClick$btnExec(Event evt) throws InterruptedException {
            collectInfo();
        }
    
        public void onClientInfo$info(ClientInfoEvent evt) {
            clientInfo.append("ZK ClientInfo: \r");
            clientInfo.append("getScreenWidth():\t\t").append(evt.getScreenWidth()).append(" x ").append(evt.getScreenHeight()).append("\r");
            clientInfo.append("getColorDepth():\t\t").append(evt.getColorDepth()).append("bit\r");
            clientInfo.append("getDesktopWidth():\t\t").append(evt.getDesktopWidth()).append(" x ").append(evt.getDesktopHeight()).append("\r");
            clientInfo.append("getTimeZone():\t\t\t").append(evt.getTimeZone().getDisplayName()).append("\r");
            clientInfo.append("getName():\t\t\t").append(evt.getName()).append("\r");
            clientInfo.append("--------------------------------------------------------------------------------------------------\r");
    
        }
    
        private void collectInfo() {
            StringBuilder result = new StringBuilder();
    
            try {
                result.append("--------------------------------------------------------------\r");
                result.append("ZK Session\r");
                Session sess = Sessions.getCurrent();
                result.append(".getLocalAddr():\t\t" + sess.getLocalAddr() + "\r");
                result.append(".getLocalName():\t\t" + sess.getLocalName() + "\r");
                result.append(".getRemoteAddr():\t\t" + sess.getRemoteAddr() + "\r");
                result.append(".getRemoteHost():\t\t" + sess.getRemoteHost() + "\r");
                result.append(".getServerName():\t\t" + sess.getServerName() + "\r");
                result.append(".getWebApp().getAppName():\t" + sess.getWebApp().getAppName() + "\r");
    
                HttpSession hses = (HttpSession) sess.getNativeSession();
                result.append("--------------------------------------------------------------------------------------------------\r");
                result.append("HttpSession\r");
                result.append(".getId():\t\t\t" + hses.getId() + "\r");
                result.append(".getCreationTime():\t\t" + new Date(hses.getCreationTime()).toString() + "\r");
                result.append(".getLastAccessedTime():\t\t" + new Date(hses.getLastAccessedTime()).toString() + "\r");
    
                result.append("--------------------------------------------------------------------------------------------------\r");
                result.append("ServletContext\r");
                ServletContext sCon = hses.getServletContext();
                result.append(".getServerInfo():\t\t" + sCon.getServerInfo() + "\r");
                result.append(".getContextPath():\t\t" + sCon.getContextPath() + "\r");
                result.append(".getServletContextName():\t" + sCon.getServletContextName() + "\r");
    
                result.append("--------------------------------------------------------------------------------------------------\r");
                result.append("ZK Executions\r");
                result.append(".getHeader('user-agent'):\t" + Executions.getCurrent().getHeader("user-agent") + "\r");
                result.append(".getHeader('accept-language'):\t" + Executions.getCurrent().getHeader("accept-language") + "\r");
                result.append(".getHeader('referer'):\t\t" + Executions.getCurrent().getHeader("referer") + "\r");
                result.append(".getHeader('connection'):\t" + Executions.getCurrent().getHeader("connection") + "\r");
                result.append(".getHeader('zk-sid'):\t\t" + Executions.getCurrent().getHeader("zk-sid") + "\r");
                result.append(".getHeader('origin'):\t\t" + Executions.getCurrent().getHeader("origin") + "\r");
                result.append(".getHeader('host'):\t\t" + Executions.getCurrent().getHeader("host") + "\r");
                result.append(".getHeader('cookie'):\t\t" + Executions.getCurrent().getHeader("cookie") + "\r");
                result.append("--------------------------------------------------------------------------------------------------\r");
    
                result.append(clientInfo);
                txtSessionInfo.setValue(result.toString());
    
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    
    

Some Remarks:

  • The session and execution object come with ZK, httpsession and servletcontext is part of Java EE.
  • The browser information cant be retrieved from the session or servlet, but you can make use of ZK’s onClientInfo() method

    onClientInfo(ClientInfoEvent evt)

    onClientInfo(ClientInfoEvent evt)

  • Change the font to the nice old fashion typewriter font (courier new)
    You can embed a style element into the zul page and use the sclass attribute.

    Change Font

Advertisements

3 thoughts on “Get all Browser and Client Info from a ZK session

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s