logo

New Response

« Return to the main article

You are replying to:

    • avatar
    • Andy Cunliffe
    • Posted on Mon 10 Nov 2008

    I realise it's been some time since this article was first posted but thought I'd share the method I've come up with for getting XML from the server when authentication is required without hard-coding a username and password and optionally transforming it server-side. I got some ideas from here, some from breakingpar.com but I found the key to the solution here: http://darkmag.net/darkBlog/files/DominoProxy.java - it can be done in Lotusscript too though.

    Basically, you can borrow the existing authentication cookie when sending your request server-side. If you call the agent in javascript there will automatically be a HTTP_Cookie field accessible on the document context, but you'll need this field to be present if you use this method as a WQO agent.

    Dim session As NotesSession

    Dim doc As NotesDocument

    Dim cookie As String

    Dim xhr As Variant

    Dim xmlurl As String

    Dim xslurl As String

    Dim xmldoc As Variant

    Dim xsldoc As Variant

    Dim html As String

    xmlurl = "http://server/db.nsf/view?readviewentries"

    xslurl = "http://server/db.nsf/notesview.xsl"

    Set session = New NotesSession

    Set doc = session.DocumentContext

    Set cookie = doc.GetItemValue("HTTP_Cookie")(0)

    Set xhr = CreateObject("Msxml2.XMLHTTP")

    Call xhr.open("GET", xmlurl, False)

    Call xhr.setRequestHeader("Cookie", cookie) ' after open method

    Call xhr.send()

    Set xmldoc = CreateObject("Msxml2.DOMDocument")

    Call xmldoc.loadXML(xhr.ResponseText)

    Call xhr.open("GET", xslurl, False) ' cookie doesn't need re-setting

    Call xhr.send()

    Set xsldoc = CreateObject("Msxml2.DOMDocument")

    Call xsldoc.loadXML(xhr.ResponseText)

    html = xmldoc.transformNode(xsldoc)

    Print "Content-Type: text/html"

    Print html

    Things are slightly different in Java but the principle is the same:

    import lotus.domino.*;

    import java.net.*;

    import com.lotus.xsl.*;

    import com.lotus.xml.xml4j2dom.*;

    import com.lotus.xsl.XSLTInputSource;

    import com.lotus.xsl.XSLTResultTarget;

    import java.io.*;

    /*

    NB: When performing multiple transformations the XSLTInputSource and the

    HttpURLConnection parameter must not have been used in a prior transformation.

    This may necessitate multiple connections to the same stylesheet.

    */

    public class JavaAgent extends AgentBase {

    public void NotesMain() {

    try {

    PrintWriter pw = getAgentOutput();

    Session session = getSession();

    AgentContext agentContext = session.getAgentContext();

    Document doc = agentContext.getDocumentContext();

    URL xmlurl = new URL("http://server/db.nsf/view?readviewentries");

    HttpURLConnection xmlcon = (HttpURLConnection)xmlurl.openConnection();

    xmlcon.setRequestMethod("GET");

    xmlcon.setRequestProperty("Cookie", doc.getItemValueString("HTTP_Cookie"));

    URL xslurl = new URL("http://server/db.nsf/notesview.xsl");

    HttpURLConnection xslcon = (HttpURLConnection)xslurl.openConnection();

    xslcon.setRequestMethod("GET");

    xslcon.setRequestProperty("Cookie", doc.getItemValueString("HTTP_Cookie"));

    StringWriter sw = new StringWriter();

    XSLProcessor xp = new XSLProcessor(new XML4JLiaison4dom());

    XSLTInputSource xml = new XSLTInputSource(xmlcon.getInputStream());

    XSLTInputSource xsl = new XSLTInputSource(xslcon.getInputStream());

    XSLTResultTarget output = new XSLTResultTarget(sw);

    xp.process(xml, xsl, output);

    pw.println("Content-Type: text/html")

    pw.println(sw.toString());

    } catch(Exception e) {

    e.printStackTrace();

    }

    }

    }

    Hope someone finds this useful.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment: