I’ve been at this for days and it’s driving me mad. If anyone can help I will bear your children.
Here is the agent code:
/**************************************************
// Necessary imports.
import lotus.domino.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class JavaAgent extends AgentBase {
// Global vector, stores all items for later recycling.
// Every Domino-item created by non-default code MUST be recycled.
Vector recycler = new Vector();
public void NotesMain() {
try {
// These 2 items are created by default and do not need to be recycled.
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
System.out.println("(GetDocumentAsHtml) initialised");
// Get ref to this db.
Database db = agentContext.getCurrentDatabase();
recycler.addElement(db);
// Get parameter document (= NotesDocument just saved).
Document docProduct = db.getDocumentByID(agentContext.getCurrentAgent().getParameterDocID());
recycler.addElement(docProduct);
// Get profile document
Document docProfile = db.getProfileDocument("DBSettings","");
recycler.addElement(docProfile);
Item itmVariable = docProfile.getFirstItem("Variables");
recycler.addElement(itmVariable);
Item targetDB = docProfile.getFirstItem("IncomingDocTargetDB");
recycler.addElement(targetDB);
// Get the HTML document to store output in.
Document docHtml = getHtmlDocument(db, docProduct);
recycler.addElement(docHtml);
// Get a handle to the RichText-field to store data in.
RichTextItem rtitem = (RichTextItem) docHtml.getFirstItem("DplHTML");
recycler.addElement(rtitem);
RichTextItem rtHeaderitem = (RichTextItem) docHtml.getFirstItem("DplHTMLHeader");
recycler.addElement(rtHeaderitem);
// Run method to get the HTML and store in the RichText-field named "HTML".
Name n = session.createName(db.getServer());
String strUrl = "http://roc-a-a01.rockdale.nsw.gov.au/" + docProduct.getItemValueString("DBPath") + "/LUPInternalKey/";
//String strUrl = "http://"+n.getCommon()+"/" + docProduct.getItemValueString("DBPath") + "/LUPInternalKey/";
strUrl += docProduct.getItemValueString("InternalKeyHTML") + "?OpenDocument";
System.out.println(strUrl);
System.out.println("createHTMLContent(" + strUrl + ", " + rtitem + ", " + rtHeaderitem + ", " + itmVariable + ", Kire Tosevski, password" + ", " + targetDB.getText());
createHTMLContent(db, strUrl, rtitem, rtHeaderitem, itmVariable, "Kire Tosevski", "password", targetDB.getText());
// Don't forget to save.
docHtml.save(true, false);
}
catch ( NotesException ne ) {
System.out.println("NotesException caught.");
System.out.println("Notes error no: " + ne.id + " Notes error text: " + ne.text);
ne.printStackTrace();
}
catch ( java.io.FileNotFoundException nf ) {
System.out.println("FileNotFoundException caught.");
System.out.println("Probable cause: User not authenticated.");
nf.printStackTrace();
}
catch ( Exception e ) {
System.out.println("General Exception caught.");
e.printStackTrace();
}
finally {
// Recycle the Domino objects created by my own code.
for (int i = 0; i < recycler.size(); i++) {
Base tmp = (Base) recycler.elementAt(i);
if (tmp != null) {
try { tmp.recycle(); }
catch (NotesException n) { /* So we didn't get this object recycled, never mind.*/ }
}
}
}
}
/*=====================================================================
This method finds the HTML document for the agent to write data to.
If such a document already exists, the RichText-field HTML is removed.
==================================================================== */
private Document getHtmlDocument ( Database db, Document docProduct) throws NotesException, Exception {
// Look for existing document. If not existing, create.
View view = db.getView("LUPInternalKeyHTML");
Document docHtml = view.getDocumentByKey(docProduct.getItemValueString("InternalKeyHTML"), true);
if (docHtml == null) {
docHtml = db.createDocument();
docHtml.replaceItemValue("Form", "WebHTML");
System.out.println("docHTML created");
}
docHtml.replaceItemValue("InternalKey", docProduct.getItemValueString("InternalKey"));
docHtml.replaceItemValue("InternalKeyHTML", docProduct.getItemValueString("InternalKeyHTML"));
docHtml.replaceItemValue("Key", docProduct.getItemValueString("Key"));
docHtml.replaceItemValue("Status", docProduct.getItemValueString("Status"));
docHtml.replaceItemValue("Mandant", docProduct.getItemValueString("Mandant"));
docHtml.replaceItemValue("VersionId", docProduct.getItemValue("VersionId"));
docHtml.replaceItemValue("Version", docProduct.getItemValue("Version"));
docHtml.replaceItemValue("ValidDate", docProduct.getItemValue("ValidDate"));
docHtml.replaceItemValue("PublishedTo", docProduct.getItemValueString("PublishedTo"));
docHtml.replaceItemValue("$PublicAccess", docProduct.getItemValueString("$PublicAccess"));
docHtml.replaceItemValue("isAttachment", docProduct.getItemValueString("isAttachment"));
docHtml.replaceItemValue("Frozen", docProduct.getItemValueString("Frozen"));
docHtml.replaceItemValue("Parent", docProduct.getItemValueString("Parent"));
docHtml.replaceItemValue("ParentH", docProduct.getItemValue("ParentH"));
docHtml.replaceItemValue("IsNavEntry", docProduct.getItemValue("IsNavEntry"));
docHtml.replaceItemValue("BreadcrumbImgPath", docProduct.getItemValueString("BreadcrumbImgPath"));
docHtml.replaceItemValue("DplBreadcrumbImg", docProduct.getItemValueString("DplBreadcrumbImg"));
docHtml.replaceItemValue("DplMainHeading", docProduct.getItemValueString("DplMainHeading"));
docHtml.replaceItemValue("MainHeading", docProduct.getItemValueString("MainHeading"));
docHtml.replaceItemValue("MainHeadingH", docProduct.getItemValue("MainHeadingH"));
docHtml.replaceItemValue("ContentType", docProduct.getItemValueString("ContentType"));
docHtml.replaceItemValue("DocName", docProduct.getItemValueString("DocName"));
docHtml.replaceItemValue("NextDoc", docProduct.getItemValueString("NextDoc"));
docHtml.replaceItemValue("nPriority", docProduct.getItemValue("nPriority"));
docHtml.replaceItemValue("External", docProduct.getItemValue("External"));
docHtml.replaceItemValue("winTitle", docProduct.getItemValue("winTitle"));
docHtml.replaceItemValue("winParameter", docProduct.getItemValue("winParameter"));
docHtml.replaceItemValue("TermsAndConditions", docProduct.getItemValue("TermsAndConditions"));
docHtml.replaceItemValue("SubCategory", docProduct.getItemValue("SubCategory"));
// If RichText-field exists, remove it.
RichTextItem cResource = (RichTextItem) docHtml.getFirstItem("cResource");;
if (cResource != null) {
cResource.remove();
docHtml.save(true, false);
}
System.out.println("debug1:");
RichTextItem cResourceOrig = (RichTextItem) docProduct.getFirstItem("cResource");
cResourceOrig.copyItemToDocument(docHtml);
System.out.println("debug2:");
// If RichText-field exists, remove it.
RichTextItem itmHTML = (RichTextItem) docHtml.getFirstItem("htmlCode");;
if (itmHTML != null) {
itmHTML.remove();
docHtml.save(true, false);
}
System.out.println("debug3:");
RichTextItem itmHTMLOrig = (RichTextItem) docProduct.getFirstItem("htmlcode");
System.out.println("debug4:");
itmHTMLOrig.copyItemToDocument(docHtml);
// If RichText-field exists, remove it.
RichTextItem rtitem = null;
rtitem = (RichTextItem) docHtml.getFirstItem("DplHTML");;
if (rtitem != null) {
rtitem.remove();
docHtml.save(true, false);
}
// Create a new RichText-field.
rtitem = docHtml.createRichTextItem("DplHTML");
RichTextItem rtHeaderitem = null;
rtHeaderitem = (RichTextItem) docHtml.getFirstItem("DplHTMLHeader");;
if (rtHeaderitem != null) {
rtHeaderitem.remove();
docHtml.save(true, false);
}
// Create a new RichText-field.
rtHeaderitem = docHtml.createRichTextItem("DplHTMLHeader");
// Remember to save.
docHtml.save(true, false);
return docHtml;
}
/*=====================================================================
* Replaces a subset of a string with another string.
* This function is case sensitive.
==================================================================== */
public static String replaceSubString(String s, String search, String replace, String path) {
int p = 0;
System.out.println("search"+search);
if (search.endsWith("###ACTION###")){
replace = path;
}
while (p < s.length() && (p = s.indexOf(search, p)) >= 0) {
s = s.substring(0, p) + replace +
s.substring(p + search.length(), s.length());
p += replace.length();
}
return s;
}
/*=====================================================================
This method reads a Domino document as HTML.
Stores result in a RichText-field, line by line.
==================================================================== */
private void createHTMLContent (Database db, String docURL, RichTextItem rtitem, RichTextItem rtHeaderitem, Item itmVariable, String strUser, String strPass, String targetDB) throws NotesException, Exception {
System.out.println(docURL);
URL notesURL = new URL(docURL);
URLConnection con = notesURL.openConnection();
// If a username and password is supplied, use them.
if ((strUser != "") && (strPass != "")) {
String userPassword = strUser + ":" + strPass;
String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
con.setRequestProperty ("Authorization", "Basic " + encoding);
}
System.out.println("authentication");
con.setDefaultUseCaches(false);
BufferedReader in = new BufferedReader ( new InputStreamReader ( con.getInputStream() ) );
String inputLine;
System.out.println("inputLine");
boolean endHeader = false;
boolean endBody = false;
while ((inputLine = in.readLine()) != null) {
String str = inputLine;
String strReplaced = str;
Enumeration values = itmVariable.getValues().elements();
for (Enumeration e=values ; e.hasMoreElements() ;) {
String strFrom = (String)e.nextElement();
System.out.println("from" + strFrom.substring(0,strFrom.indexOf("@@@")));
System.out.println("to" + strFrom.substring(strFrom.indexOf("@@@") + 3,strFrom.length()));
strReplaced = replaceSubString(strReplaced, strFrom.substring(0,strFrom.indexOf("@@@")), strFrom.substring(strFrom.indexOf("@@@") + 3,strFrom.length()), targetDB);
}
System.out.println("test");
if (strReplaced.indexOf("</content>") > -1) {
endBody = true;
}
if (endHeader == true && endBody == false ){
// Store line by line in the RichText-field. Add a new line and force a new paragraph.
rtitem.appendText(strReplaced);
rtitem.addNewLine(1, true);
}else{
if (endHeader == false){
rtHeaderitem.appendText(strReplaced);
rtHeaderitem.addNewLine(1, true);
}
}
if (strReplaced.indexOf("<content>") > -1) {
endHeader = true;
}
}
rtitem.setSummary(false);
in.close();
}
}
/*************************************************/
Here is the output:
(GetDocumentAsHtml) initialised
debug1:
debug2:
debug3:
debug4:
http://roc-a-a01.rockdale.nsw.gov.au/misc/CMSmain.nsf/LUPInternalKey/RCCNews_Events136?OpenDocument
http://roc-a-a01.rockdale.nsw.gov.au/misc/CMSmain.nsf/LUPInternalKey/RCCNews_Events136?OpenDocument
authentication
FileNotFoundException caught.
Probable cause: User not authenticated.
java.io.FileNotFoundException: http://roc-a-a01.rockdale.nsw.gov.au/misc/CMSmain.nsf/LUPInternalKey/RCCNews_Events136?OpenDocument
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:604)
at JavaAgent.createHTMLContent(JavaAgent.java:235)
at JavaAgent.NotesMain(JavaAgent.java:65)
at lotus.domino.AgentBase.runNotes(Unknown Source)
at lotus.domino.NotesThread.run(NotesThread.java:215)
/********************************************
The exception is thrown during the createHTMLContent routine when it calls getInputStream on the URLConnection object.
As far as I can tell, all the permissions and ACL setting are in place (heck I’ve made everybody a manager!) and the document is most definitely there becauase when I put the URL in a browser it displays the document.
Help.