Reusing some existing java code in my xpage application now that who is asking is important

I’m not sure what I should have called this little tip but Imagine the scenario:

I have a lot of code I’ve already written and packaged up as a servlet which interacts with Notes/Domino and creates a session using it’s own set of credentials.  

Now what if the situation were a little different and the web application required someone to login and I wanted the Java code to use the credentials of the user that logged in.

I tried various methods, including trying to reuse an LTPAToken nothing worked. 

After talking with Serdar Başeğmez (thanks to you) I came to terms with the fact that I’d have to replace the servlet with a headless XPage (sometimes known as an XAgent). I’ve been doing just that with SSJS for a little while but it’s a shame to use SSJS and I haven’t found a great way to debug it.

I wanted to try another approach on the off chance:  I can use Java code in my headless XPage (either via the Java component or via a jar file), and I have a session object in my SSJS.

So if I create a new class:

package com.test;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.Session;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.View;
public class Identity {
public Session session=null;

public void setSession(lotus.domino.Session s){
session = s;
}
public Session getSession(){
return this.session;
}

public String whoami(){
try{
return getSession().getEffectiveUserName();
} catch(Exception e){
return e.getMessage();
}

}
}

 

And if I create an XPage:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.afterRenderResponse><![CDATA[#{javascript:
try { 
     var identity:com.test.Identity = new com.test.Identity();
     identity.setSession(session);
     print(identity.whoami());
}catch(e){
     print(e);
}
}]]>
</xp:this.afterRenderResponse>
</xp:view>

Now when I build the application, and visit the xpage: /text.xsp?open&login the server console will print the name of the logged in user. I haven’t explored the limitations yet but if that works in anger then I’ll have a mechanism for reusing the Java code I already have, which is nice 🙂

Advertisements
Tagged with: , , , , ,
Posted in Uncategorized
3 comments on “Reusing some existing java code in my xpage application now that who is asking is important
  1. Serdar says:

    Glad I helped you (I hope…) 🙂

  2. Hmmm..
    be careful what you wish for 😉 – You shouldn’t fall into the temptation to use this class as a managed bean (I would work for a request scope, but not beyond).

    How about:
    public class Identity {
    private final Session session;

    public Identity(Session s) {
    this.session = s;
    }

    public String whoami() {
    ….
    }
    }

    This way: less to code and tempation of bean use removed.

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

%d bloggers like this: