Dec 4, 2011 1:39 AM
64 Posts

Get User Session in Xpages using JavaAgent/JavaBean

  • Category: Managed Beans in NSF
  • Platform: Windows
  • Release: 8.5.2
  • Role: Developer
  • Tags: Xpages,JavaAgent,JavaBean
  • Replies: 0
 

Get Sessioned-User

Xpages/JavaAgent/JavaBean/Domino Designer


System Requirements:

Download Domino Designer 8.5.2 Environment (DDE)

http://www.ibm.com/developerworks/downloads/ls/dominodesigner/


Introduction:

Build a complete application that retrieves a sessioned-user with a JavaAgent, creates an XML file to be read by a JavaBean, feeds into an Xpages form.


Step 1:

Use JavaAgent, retrieve user in session:

Copy and Paste below JavaAgent over the skeleton version generated by DDE. You will not need to do much in properties to run this one, no docs need returned; add secure user to run on behalf of.


/**

* Created: 2011.04.15.11.54.AM

* JavaAgentdataAsXML

* XML data for Xpage to be read by JavaBean

*/


//load imports

import lotus.domino.*;

import java.io.PrintWriter;


/**

* @author kcherizard

* @version 2011.04.15.11.54.AM

*

*/


// begin class

public class JavaAgent extends AgentBase {


public void NotesMain() {


// let's add a try catch here...

try {

// open our session...

Session session = getSession();

// load info console for debugging purposes

System.out.println("We've got a session..." + session);

// load agentContext

AgentContext agentContext = session.getAgentContext();

// load info console for debugging purposes

System.out.println("We've got agentContext..." + agentContext);

// Declare/Initialize logged on user

String SystemsUser = session.getCommonUserName();

System.out.println("We've got a UserName..." + SystemsUser);

// Create a Java PrintWriter object variable

PrintWriter pw = getAgentOutput();


// Set the content type

pw.println("Content-type:text/xml");

pw.println("");

// Write out the HTML Opening Tags

pw.println("<?xml version='1.0' ?>");

pw.println("<contacts>");

pw.println("<person>");

pw.println("<emailaddress>");

pw.println(SystemsUser);

pw.println("</emailaddress>");

pw.println("</person>");


// close the root tag

pw.println("</contacts>");


} catch (Exception e) {

e.printStackTrace();

}

}

}


Step 2:

Write a JavaBean, read JavaAgent session value:

Copy and Paste code and go through to ensure you understand what the code is doing, good practice... You are going to read an XML file and will pass a session value to the variable personName.


package com.osc.blackberry.test;


/**

* Created: 2011.07.13.9.29.AM

* Test JavaBean to read XML file into Xpages

*

* Modifed: 2011.08.16.1.31.PM to reflect emailaddress as opposed to fullname,

* item to be used by Ticket System

*/


/**

* @author kcherizard

* @version 2011.07.13.9.29.AM

*

*/


import javax.faces.context.FacesContext;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;


import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;


public class XMLUserBean {

static String UserID;

static String PersonName;

/**

* @return the personName

*/

public String getPersonName() {

//entering try catch block

System.out.println("entering try catch block...");

try {

//initialize builder and grab XML file

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

//load data through via URL

Document doc = db

.parse("http://localhost/Docu.nsf/javaagentusersessiondata.xml");

doc.getDocumentElement().normalize();

System.out.println("Root element "

+ doc.getDocumentElement().getNodeName());

//retrieve person tag

NodeList nodeLst = doc.getElementsByTagName("person");


for (int s = 0; s < nodeLst.getLength(); s++) {

Node fstNode = nodeLst.item(s);


if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

Element fstElmnt = (Element) fstNode;

//search emailaddress tag

NodeList fstNmElmntLst = fstElmnt

.getElementsByTagName("emailaddress");

//@Author: Köll S. Cherizard

//@version: 2011.08.16.1.31.PM

//changed from fullname to emailaddress

Element fstNmElmnt = (Element) fstNmElmntLst.item(0);

NodeList fstNm = fstNmElmnt.getChildNodes();

System.out.println("Sessioned User : "

+ ((Node) fstNm.item(0)).getNodeValue());

UserID = ((Node) fstNm.item(0)).getNodeValue().toString();

//Ensure the session value is available

//TO DO: Uncomment this item

//(if (PersonName==null){)

//when you are comparing session against

//data from names.nsf file using same JavaAgent.

//@version 2011.12.04.12.32.AM


/*if (PersonName==null){

// call FacesContext to redirect to page in question

FacesContext.getCurrentInstance().getExternalContext()

.redirect("xpnotauthorized.xsp");

}*/

//load the session value from JavaAgent to personName variable

PersonName = UserID.toString();

}

}

} catch (Exception e) {

e.printStackTrace();

}

return PersonName;

}

/**

* @param personName the personName to set

*/

public void setPersonName(String personName) {

PersonName = personName;

}

public void retrieveXMLData() {

//initialize search for user session value from file

getPersonName();

}

/**

* @return the userID

*/

public static String getUserID() {

return UserID;

}

/**

* @param userID the userID to set

*/

public static void setUserID(String userID) {

UserID = userID;

}

}



Step 3:

Building an Xpages file:

Let's load the user session value to Xpages, we will need to connect the Xpages form to above JavaBean using a faces XML config file. For now, just Copy and Paste below code for the Xpage; using 'beforePageLoad' ensures the value will load to Xpages before the page loads. xpnotauthorized.xsp is rendered when session is null, where names.nsf data did not compare against the session picked up by the initial user search.


    <?xml version="1.0" encoding="UTF-8"?>

<xp:view xmlns:xp="http://www.ibm.com/xsp/core">


<xp:this.beforePageLoad><![CDATA[#{javascript:XMLUserBean.retrieveXMLData()}]]></xp:this.beforePageLoad>

<xp:dataTable rows="30" id="dataTable1" var="XMLUserBean"

value="#{XMLUserBean}">

<xp:this.facets>

<xp:pager partialRefresh="true" layout="Previous Group Next"

xp:key="header" id="pager1" for="dataTable1">

</xp:pager>

<xp:pager partialRefresh="true" layout="Previous Group Next"

xp:key="footer" id="pager2" for="dataTable1">

</xp:pager>

</xp:this.facets>



<xp:column id="column2">

<xp:this.facets>

<xp:label value="Person Name" id="personName1" xp:key="header">

</xp:label>

</xp:this.facets>

<xp:text escape="true" id="computedField2"

value="#{javascript: XMLUserBean.personName}"></xp:text>

</xp:column>

</xp:dataTable>

<xp:br></xp:br>

</xp:view>


Step 4:

Configure faces-config.xml file:

Faces config file bridges the JavaBean and the Xpages form. Copy and Paste XML code into your environment's faces file.


<managed-bean>

<managed-bean-name>XMLUserBean

</managed-bean-name>

<managed-bean-class>com.osc.blackberry.test.XMLUserBean

</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

</managed-bean>


Run it:-)


Conclusion:

We recommend logging on to names.nsf, grab values and compare against the sessioned-user, and forward the user's id, or which ever column you specified to file, to ensure the user is a valid networked-user. This item can also be added to a session (facesContext) within the JavaBean, to be used throughout the application.


References:

http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html

http://www-10.lotus.com/ldd/ddwiki.nsf/dx/Using_XML_as_an_XPage_Data_Source



Happy Coding!