Getting attachments from document - lotus

This is all I need, nothing too fancy:
I'm creating an url from files that have been attached in the document, but the document is not opened. I have an xpage where I want to show attachments from specific document. How do I do this?
Thank you in advance.

The easiest way is to use #AttachmentNames (in a view column) to get the names of the files. Then you can construct the url using db.nsf/0/unid/$file/[filename] -- that's classic, won't run in XPiNC. There is a second URL syntax (need to check) that is XPages specific:
http(s)://[yourserver]/[application.nsf]/xsp/.ibmmodres/domino/OpenAttachment/[application.nsf]/[UNID|/$File/[AttachmentName]?Open
Read my full article on it here: http://www.wissel.net/blog/d6plinks/SHWL-86QKNM
(includes SSJS sample)

I found that DominoDocument.AttachmentValueHolder.getHref() works for getting the URL to an attached file or image, if you have a handle to the document. For example:
<xp:image
id="image1">
<xp:this.url>
<![CDATA[#{javascript:document1.getAttachmentList("Body").get(0).getHref()}]]>
</xp:this.url>
</xp:image>
You would need to handle multiple attachments by iterating over the elements returned from getAttachmentList().

If you can use Java (like in XPages) then
import com.ibm.xsp.extlib.util.ExtLibUtil;
import lotus.domino.MIMEEntity;
import lotus.domino.Document;
import lotus.domino.Session;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import lotus.domino.Database;
import lotus.domino.DocumentCollection;
import lotus.domino.EmbeddedObject;
import lotus.domino.Item;
import lotus.domino.MIMEHeader;
import lotus.domino.NotesException;
import lotus.domino.RichTextNavigator;
import lotus.domino.RichTextItem;
import lotus.domino.Stream;
import lotus.domino.View;
// ...
private String fileSeparator = File.separator;
private String tempPath = System.getProperty("java.io.tmpdir") + fileSeparator + "Temp" + fileSeparator;
// ...
private void saveFilesFromDoc(Document doc) throws NotesException {
if (doc.hasEmbedded()) {
RichTextItem body = null;
try {
body = (RichTextItem) doc.getFirstItem("body");
} catch (ClassCastException e) {
// save file from MIME (Rich text is converted to MIME)
MIMEEntity mime = doc.getMIMEEntity();
findMimeWithFile(mime);
return;
}
if (body != null) {
// save file from richtext
RichTextNavigator rtnav = body.createNavigator();
if (rtnav.findFirstElement(RichTextItem.RTELEM_TYPE_FILEATTACHMENT)) {
do {
EmbeddedObject att = (EmbeddedObject) rtnav.getElement();
String fileName = att.getSource();
fileName = notConflictFileName(fileName );
String path = tempPath + fileName ;
att.extractFile(path);
} while (rtnav.findNextElement());
}
} else {
// ("BODY is NULL");
}
}
Get file from richtext converted to Mime
private void findMimeWithFile(MIMEEntity mime) {
try {
askMimeForFiles(mime, "");
MIMEEntity child = mime.getFirstChildEntity();
while (child != null) {
askMimeForFiles(child, "child");
// String encoding = "ISO-8859-2";
String c = child.getContentType();
MIMEEntity subChild = child.getFirstChildEntity();
askMimeForFiles(subChild, "subChild");
if ("multipart".equals(c)) {
while (subChild != null) {
askMimeForFiles(subChild, "subChild2");
// String sc = subChild.getContentType();
subChild = subChild.getNextSibling();
}
}
child = child.getNextSibling();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Find out, if MIME Entity is file attachment (or some text)
private void askMimeForFiles(MIMEEntity mime, String prefix) throws NotesException {
if (mime != null) {
boolean thisMimeHasFile = false;
String fileName = "noname";
Vector<MIMEHeader> headers = mime.getHeaderObjects();
for (MIMEHeader header : headers) {
// (prefix + "-header: " + header.getHeaderName() + " :: " + header.getHeaderValAndParams());
if ("Content-Transfer-Encoding".equals(header.getHeaderName())) {
if ("binary".equals(header.getHeaderVal())) {
thisMimeHasFile = true;
}
}
if ("Content-Disposition".equals(header.getHeaderName())) {
String val = header.getHeaderValAndParams();
int odd = val.indexOf("filename=") + "filename=".length();
int doo = val.length();
fileName = val.substring(odd, doo);
this.fileNames.add(fileName);
}
}
if (thisMimeHasFile) {
safeFilesFromMIME(mime, fileName);
}
}
}
If MIME is file attachment, then save it
private void safeFilesFromMIME(MIMEEntity mime, String fileName) throws NotesException {
Session session = ExtLibUtil.getCurrentSession(); // or user variableResolver
Stream stream = session.createStream();
String pathname = tempPath + fileName;
stream.open(pathname, "binary");
mime.getContentAsBytes(stream);
stream.close();
}

Related

How do I call fn:sort() in Saxon from Java with multiple sort keys

How do I use the sort function in Saxon when calling it from Java (not from XSLT). For example, for the query (data modeled on the Northwind database) I can get unsorted data using:
/windward-studios/Employees/Employee
But I want to get it sorted like the following (using SQL syntax here):
/windward-studios/Employees/Employee order by City descending, LastName ascending
How do I write the query to accomplish this?
The full code for this is in SaxonQuestions.zip (minus license key) - TestSort.java.
TestSort.java
import net.sf.saxon.s9api.*;
import java.io.*;
import java.util.ArrayList;
public class TestSort {
public static void main(String[] args) throws Exception {
XmlDatasource datasource = new XmlDatasource(
new FileInputStream(new File("files", "SouthWind.xml").getCanonicalPath()),
new FileInputStream(new File("files", "SouthWind.xsd").getCanonicalPath()));
// what I want is sort like: "/windward-studios/Employees/Employee order by City descending, LastName ascending"
XdmValue nodeSet = datasource.getxPathCompiler().evaluate("/windward-studios/Employees/Employee", datasource.getXmlRootNode());
System.out.println(String.format("%10s %10s %10s", "firstName", "lastName", "city"));
for (int i = 0; i < nodeSet.size(); i++) {
XdmItem item = nodeSet.itemAt(i);
String firstName = ((XdmNode)((ArrayList)((XdmNode) item).children("FirstName")).get(0)).getStringValue();
String lastName = ((XdmNode)((ArrayList)((XdmNode) item).children("LastName")).get(0)).getStringValue();
String city = ((XdmNode)((ArrayList)((XdmNode) item).children("City")).get(0)).getStringValue();
System.out.println(String.format("%10s %10s %10s", firstName, lastName, city));
}
}
}
XmlDatasource.java
import com.saxonica.config.EnterpriseConfiguration;
import com.saxonica.ee.s9api.SchemaValidatorImpl;
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.FeatureKeys;
import net.sf.saxon.s9api.*;
import net.sf.saxon.type.SchemaException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
public class XmlDatasource {
/** the DOM all searches are against */
private XdmNode xmlRootNode;
private XPathCompiler xPathCompiler;
/** key == the prefix; value == the uri mapped to that prefix */
private HashMap<String, String> prefixToUriMap = new HashMap<>();
/** key == the uri mapped to that prefix; value == the prefix */
private HashMap<String, String> uriToPrefixMap = new HashMap<>();
public XmlDatasource (InputStream xmlData, InputStream schemaFile) throws SAXException, SchemaException, SaxonApiException, IOException {
boolean haveSchema = schemaFile != null;
// call this before any instantiation of Saxon classes.
Configuration config = createEnterpriseConfiguration();
if (haveSchema) {
Source schemaSource = new StreamSource(schemaFile);
config.addSchemaSource(schemaSource);
}
Processor processor = new Processor(config);
DocumentBuilder doc_builder = processor.newDocumentBuilder();
XMLReader reader = createXMLReader();
InputSource xmlSource = new InputSource(xmlData);
SAXSource saxSource = new SAXSource(reader, xmlSource);
if (haveSchema) {
SchemaValidator validator = new SchemaValidatorImpl(processor);
doc_builder.setSchemaValidator(validator);
}
xmlRootNode = doc_builder.build(saxSource);
xPathCompiler = processor.newXPathCompiler();
if (haveSchema)
xPathCompiler.setSchemaAware(true);
declareNameSpaces();
}
public XdmNode getXmlRootNode() {
return xmlRootNode;
}
public XPathCompiler getxPathCompiler() {
return xPathCompiler;
}
/**
* Create a XMLReader set to disallow XXE aattacks.
* #return a safe XMLReader.
*/
public static XMLReader createXMLReader() throws SAXException {
XMLReader reader = XMLReaderFactory.createXMLReader();
// stop XXE https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#JAXP_DocumentBuilderFactory.2C_SAXParserFactory_and_DOM4J
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
return reader;
}
private void declareNameSpaces() throws SaxonApiException {
// saxon has some of their functions set up with this.
prefixToUriMap.put("saxon", "http://saxon.sf.net");
uriToPrefixMap.put("http://saxon.sf.net", "saxon");
XdmValue list = xPathCompiler.evaluate("//namespace::*", xmlRootNode);
if (list == null || list.size() == 0)
return;
for (int index=0; index<list.size(); index++) {
XdmNode node = (XdmNode) list.itemAt(index);
String prefix = node.getNodeName() == null ? "" : node.getNodeName().getLocalName();
// xml, xsd, & xsi are XML structure ones, not ones used in the XML
if (prefix.equals("xml") || prefix.equals("xsd") || prefix.equals("xsi"))
continue;
// use default prefix if prefix is empty.
if (prefix == null || prefix.isEmpty())
prefix = "def";
// this returns repeats, so if a repeat, go on to next.
if (prefixToUriMap.containsKey(prefix))
continue;
String uri = node.getStringValue();
if (uri != null && !uri.isEmpty()) {
xPathCompiler.declareNamespace(prefix, uri);
prefixToUriMap.put(prefix, uri);
uriToPrefixMap.put(uri, prefix); }
}
}
public static EnterpriseConfiguration createEnterpriseConfiguration()
{
EnterpriseConfiguration configuration = new EnterpriseConfiguration();
configuration.supplyLicenseKey(new BufferedReader(new java.io.StringReader(deobfuscate(key))));
configuration.setConfigurationProperty(FeatureKeys.SUPPRESS_XPATH_WARNINGS, Boolean.TRUE);
return configuration;
}
}
In terms of using fn:sort in XPath 3.1 with multiple sort keys, the XPath expression is
sort(/windward-studios/Employees/Employee, (), function($emp) { $emp/City, $emp/LastName })
To get descending order (for the complete result) I think you can use fn:reverse:
sort(/windward-studios/Employees/Employee, (), function($emp) { $emp/City, $emp/LastName }) => reverse()
As for setting up an XSLT stylesheet defining functions to be used as functions in XPath 3.1 with Saxon 10, in XSLT you need to give the functions to be exposed the visibility="public" attribute e.g. <xsl:function name="pf:foo" visibility="public">...</xsl:function> in a stylesheet module (e.g. with the xsl:stylesheet root element) or an XSLT 3 package (e.g. with xsl:package, see the XSLT 3 spec for an example).
Then you need to use an XsltCompiler (I think created with same Processor as the other compilers for XPath) to compile the stylesheet into an XsltPackage:
Processor processor = new Processor(true);
XsltCompiler xsltCompiler = processor.newXsltCompiler();
XsltPackage xpathLibrary = xsltCompiler.compilePackage(new StreamSource("my-functions.xsl"));
finally, on the XPathCompiler you need to addXsltFunctionLibrary e.g.
compiler = processor.newXPathCompiler();
compiler.addXsltFunctionLibrary(xpathLibrary);
then your XPath expressions can use any of the public functions. Of course, as any functions need to be in a namespace, the stylesheet needs to declare a prefix for the namespace and the XPathCompiler needs to declare a prefix too for the same namespace, it makes sense probably to use the same prefix:
compiler.declareNamespace("pf", "http://example.com/pf");
Then any XPath expressions you compile with that compiler can call the function pf:foo.
With Saxon EE it might additionally be more efficient to compile and export the stylesheet in a separate step and to load the exported stylesheet. Probably better to ask on the Saxon support site for details.

Can't get my String switch statement to hit anything but default

I am trying to use a switch statement to pass a LinkedHashMap to the correct class constructor for a school project(I just added the rest of the code).
The code reads takes in a txt file and based off the first word in the text sends the hash map.
I can't seem to get a hit on the case report I am testing.
I have even tried just making everything into an if-else-if structure,
and that still didn't work out,
I've tried using a private enum method to no avail.
I am at a loss here.
I am running Java 8.
I am open to any suggestion on optimizing the code as well.
Thanks.
package linkedlist;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
/**
*
* #author admin
*/
public class TextReaderGUI extends javax.swing.JFrame {
JFileChooser fileChooser = new JFileChooser();
String rawText;
String[] text;
public String listType;
private JButton fileChooserButton;
private JLabel statusLabel;
/**
* Creates new form TextReaderGUI
*/
public TextReaderGUI() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
#SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
fileChooserButton = new javax.swing.JButton();
statusLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
fileChooserButton.setText("File Chooser");
fileChooserButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fileChooserButtonActionPerformed(evt);
}
});
statusLabel.setText("Status: ");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(14, 14, 14).addComponent(fileChooserButton))
.addGroup(layout.createSequentialGroup().addGap(36, 36, 36).addComponent(statusLabel)))
.addContainerGap(264, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(16, 16, 16).addComponent(fileChooserButton)
.addGap(18, 18, 18).addComponent(statusLabel).addContainerGap(221, Short.MAX_VALUE)));
pack();
}// </editor-fold>
private void fileChooserButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
rawText = "";
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder stringb = new StringBuilder();
String s;
while ((s = reader.readLine()) != null) {
stringb.append(s);
stringb.append("\n"); // this makes sure that java sees when a new line has started
}
rawText = stringb.toString();
statusLabel.setText("Status: " + file.getName());
}
} catch (IOException e) {
statusLabel.setText("Status" + e);
}
text = rawText.split("\n"); // creating a string array split at each line break
Map<String, String> lines = new LinkedHashMap<>();
for (int i = 0; i < text.length; i++) { // this sets the first word of the line = key
String[] currentLine = text[i].split("\\s+"); // splits the words in the current line to an array
if (i == 0) {
listType = currentLine[0].replaceAll("\n", "").replaceAll("\\s+", ""); // determines listType to pass
}
if (currentLine.length > 1 && i > 0) {
lines.put(currentLine[0] + " " + i, currentLine[1]); // if two words exist on a line
// the first is the key second is the value
} else if (currentLine.length == 1 && i > 0) { // keeps list type out of key values
lines.put(currentLine[0] + " " + i, ""); // " " + i is used to ensure that each command is unique key
}
}
lines.keySet().forEach((name) -> {// Testing to see if document was correctly placed into the HashMap
String key = name;
String value = lines.get(name);
System.out.println(key + " " + value + "\n");
});
System.out.println(listType); // testing to see if list type was correctly stored
switch (listType) {
case "stack":
Stack stack = new Stack((LinkedHashMap) lines);
break;
case "queue":
Queue queue = new Queue((LinkedHashMap) lines);
break;
case "dll":
Dll dll = new Dll((LinkedHashMap) lines);
break;
case "sll":
System.out.println("almost there");
Sll sll = new Sll((LinkedHashMap) lines);
break;
case "cll":
Cll cll = new Cll((LinkedHashMap) lines);
break;
default:
System.out.println("something went wrong here");
break;
}
}
}

Filter CSV file and create a new file with filtered rows

I am looking to filter rows by the description column. I want to filter out all rows that contain 'Free WiFi' and then create a csv file and make it comma separated.
HOTELID|AMENITYCODE|DESCRIPTION
722602|8|24-hour front desk
722602|109|Air conditioning
722602|23|Dry cleaning
722602|81|Fax/photocopying
722602|107|Free WiFi
723303|11|Fitness centre
723303|107|Free WiFi
723303|205|Fruits
723303|79|Hammam
723303|80|Heating
723303|44|Ironing service
723303|176|Kid meals
What shell/Java/C# script can I use in order to do this and be used by a task scheduler in Windows.
Thanks,
Spencer
You could use a PowerShell pipeline like this
Import-Csv .\input.txt -Delimiter '|' | ? { $_.Description -notlike '*Free Wifi*' } | Export-Csv -NoTypeInformation processed.csv
where input.txt is your input file and processed.csv will contain your new CSV without the WiFi stuff.
In basic Java we can do with the below code:
package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.CSVWriter;
public class Test {
public static void main(String[] args) {
String readFile = "C:\\test.txt";
String line = "";
String cvsSplitBy = "\\|";
String filter1="Free WiFi";
List<String[]> output = new ArrayList<String[]>();
String writeFile = "C:\\testout.csv";
try (BufferedReader br = new BufferedReader(new FileReader(readFile))) {
output.add(0, br.readLine().split(cvsSplitBy));
while ((line = br.readLine()) != null) {
String[] input = line.split(cvsSplitBy);
if(input[2].contains(filter1)) {
output.add(input);
}
}
File file = new File(writeFile);
FileWriter outputfile = new FileWriter(file);
CSVWriter writer = new CSVWriter(outputfile);
writer.writeAll(output);
writer.close();
System.out.println("Program ended");
} catch (IOException e) {
e.printStackTrace();
}
}
}

Use an alias on Pig UDF paramter

I need your help to know how to use an alias (stored tuple) on my Pig udf function, i exmplain:
my_file.csv
101,message here
102,message here
103,message here
...
My script PIG:
X = load'mydata.csv' using PigStorage(',') as (myVar:chararray);
A = load'my_file.csv' using PigStorage(',') as (key:chararray,value:chararray);
B = GROUP par ALL;
C = foreach B {
D = ORDER par BY key;
GENERATE BagToTuple(D);
};
the result of the C is something like (101,message here, 102, message here, 103, message here...)
Now what i need is to pass this result in my udf function like :
Z = foreach X generate MYUDF(myVar, C);
the alias "C" is the tuple key,value,key,value...
MYUDF :
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;
import org.apache.pig.PigWarning;
import org.apache.pig.data.DataType;
import org.apache.pig.impl.util.WrappedIOException;
import org.apache.pig.impl.logicalLayer.schema.Schema;
public class ReDecode extends EvalFunc<String> {
int numParams = -1;
Pattern mPattern = null;
#Override
public Schema outputSchema(Schema input) {
try {
return new Schema(new Schema.FieldSchema(getSchemaName(this
.getClass().getName().toLowerCase(), input),
DataType.CHARARRAY));
} catch (Exception e) {
return null;
}
}
#Override
public String exec(Tuple tuple) throws IOException {
if (numParams==-1) // Not initialized
{
numParams = tuple.size();
if (numParams <= 2) {
String msg = "Decode: Atleast an expression and default string is required.";
throw new IOException(msg);
}
if (tuple.size()%2!=0) {
String msg = "ItssPigUDFs.ReDecode : Some parameters are unmatched.";
throw new IOException(msg);
}
}
if (tuple.get(0)==null)
return null;
try {
for (int count = 1; count < numParams - 1; count += 2)
{
mPattern=Pattern.compile((String)tuple.get(count));
if (mPattern.matcher((String)tuple.get(0)).matches())
{
return (String)tuple.get(count+1);
}
}
} catch (ClassCastException e) {
warn("ItssPigUDFs.ReDecode : Data type error", PigWarning.UDF_WARNING_1);
return null;
} catch (NullPointerException e) {
String msg = "ItssPigUDFs.ReDecode : Encounter null in the input";
throw new IOException(msg);
}
return (String)tuple.get(tuple.size()-1);
}
Thank you for your help
I don't think numParams is needed; the number of params that you get to the UDF will be input.size().
Therefore, if you call MYUDF(myVar, C), then you should be able to get those values in Java like String myVar = (String) input.get(0) and Tuple param2 = input.get(1).

Copying rich text and images from one document to MIME in another document

I have a solution for copying rich text content from one document to MIME in another document. See http://per.lausten.dk/blog/2012/12/xpages-dynamically-updating-rich-text-content-in-a-ckeditor.html. I use this in an application as a way for the user to insert content templates in a new document and have the content appear on-the-fly in the CKEditor.
The problem is that inline images are not included in the copying - only a reference to temporary storage of the images. This means that the images are only visible for the current user in the current session. So not very useful.
How can I include images?
Update October 4, 2013:
I'm still looking for a solution to this.
I finally got it work. It was much more simple and did not even involve MIME. The trick was to modify the image tags in the working HTML to include the base64 encoded image so that the src tag could use this format (here shown with a gif as example):
src="data:image/gif;base64,<base64 encoded image>"
I already had the code necessary to get the HTML from the rich text field (see my blog post already mentioned in my question). So all I needed was to replace the image src tags with the correct src format including the base64 encoded image.
The following code gets the HTML and goes through each of the included images and modifies the src tag:
String html = this.document.getValue(fieldName).toString();
if (null != html) {
final List<FileRowData> fileRowDataList = document.getEmbeddedImagesList(fieldName);
if (null != fileRowDataList) {
final Matcher matcher = imgRegExp.matcher(html);
while (matcher.find()) {
String src = matcher.group();
final String srcToken = "src=\"";
final int x = src.indexOf(srcToken);
final int y = src.indexOf("\"", x + srcToken.length());
final String srcText = src.substring(x + srcToken.length(), y);
for (FileRowData fileRowData : fileRowDataList) {
final String srcImage = fileRowData.getHref();
final String cidImage = ((AttachmentValueHolder) fileRowData).getCID();
final String typeImage = ((AttachmentValueHolder) fileRowData).getType();
final String persistentName = ((AttachmentValueHolder) fileRowData).getPersistentName();
// Add base 64 image inline (src="data:image/gif;base64,<name>")
if (srcText.endsWith(srcImage)) {
final String newSrc = src.replace(srcText, "data:" + typeImage + ";base64," + getBase64(persistentName));
html = html.replace(src, newSrc);
}
}
}
}
}
Here is the getBase64() method that base64 encodes an image:
private String getBase64(final String fileName) {
String returnText = "";
try {
BASE64Encoder base64Enc = new BASE64Encoder();
ByteArrayOutputStream output = new ByteArrayOutputStream();
base64Enc.encode(this.getEmbeddedImageStream(fileName), output);
returnText = output.toString();
} catch (NotesException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return returnText;
}
Some of the code is from the emailBean by Tony McGuckin.
Can you get a handle to the inline image by the DominoDocument.AttachmentValueHolder, see http://public.dhe.ibm.com/software/dw/lotus/Domino-Designer/JavaDocs/XPagesExtAPI/8.5.2/com/ibm/xsp/model/domino/wrapped/DominoDocument.AttachmentValueHolder.html
I blogged about attachments inside notes documents, see http://www.domino-weblog.nl/weblogs/Domino_Blog.nsf/dx/xpages-tip-get-easily-access-to-your-attachments-in-java.htm
Gruesome hack (you need to sort out authentication and server names)
The SSJS (getting the source from a view)
var unid = curRow.getUniversalID();
var body = getComponent("body1");
var magic = new demo.HTMLMagic();
magic.doMagic(database, unid, body);
The Java
package demo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import net.htmlparser.jericho.Attribute;
import net.htmlparser.jericho.Attributes;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.OutputDocument;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;
import lotus.domino.Database;
import lotus.domino.NotesException;
import com.ibm.misc.BASE64Encoder;
import com.ibm.xsp.component.xp.XspInputRichText;
import com.ibm.xsp.http.IMimeMultipart;
import com.ibm.xsp.model.domino.wrapped.DominoRichTextItem;
public class HTMLMagic {
private HttpClient httpClient = null;
private HttpHost httpHost = null;
//TODO: that needs to be resolved smarter
private static final String HOSTNAME = "localhost";
public void doMagic(final Database database, final String unid, final XspInputRichText body) throws NotesException,
ClientProtocolException, IOException {
final String docURL = "http://" + HOSTNAME + "/__" + database.getReplicaID() + ".nsf/0/" + unid + "/Body?OpenField";
final String fixedHTML = this.fixHTML(docURL);
IMimeMultipart result = DominoRichTextItem.convertToMime("-- copied text--<br />" + fixedHTML);
body.setValue(result);
}
private String fixHTML(final String rawHTMLstring) throws ClientProtocolException, IOException {
HttpHost target = this.getHttpHost();
HttpClient client = this.getHttpClient();
HttpGet get = new HttpGet(rawHTMLstring);
HttpResponse response = client.execute(target, get);
InputStream data = response.getEntity().getContent();
Source rawHTML = new Source(data);
OutputDocument outputDocument = new OutputDocument(rawHTML);
StringBuilder sb = new StringBuilder();
String tagName = HTMLElementName.IMG;
String attName = "src";
List<StartTag> links = rawHTML.getAllStartTags(tagName);
for (StartTag onelink : links) {
String href = onelink.getAttributeValue(attName);
if (href != null) {
String replace = this.urltoData(href);
if (replace != null) {
sb.setLength(0);
sb.append("<");
sb.append(tagName);
sb.append(" ");
sb.append(attName);
sb.append("=\"");
sb.append(replace);
sb.append("\"");
Attributes atts = onelink.getAttributes();
if (!atts.isEmpty()) {
for (int i = 0; i < atts.size(); i++) {
Attribute att = atts.get(i);
if (!att.getName().equals(attName)) {
sb.append(" ");
sb.append(att.getName());
sb.append("=\"");
sb.append(att.getValue());
sb.append("\" ");
}
}
}
sb.append(">");
outputDocument.replace(onelink, sb.toString());
}
}
}
return outputDocument.toString();
}
private HttpClient getHttpClient() {
if (this.httpClient == null) {
// general setup
SchemeRegistry supportedSchemes = new SchemeRegistry();
// Register the "http" protocol scheme, it is required
// by the default operator to look up socket factories.
supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// prepare parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, supportedSchemes);
this.httpClient = new DefaultHttpClient(connMgr, params);
}
return this.httpClient;
}
private HttpHost getHttpHost() {
if (this.httpHost == null) {
this.httpHost = new HttpHost(HOSTNAME, 80, "http");
}
return this.httpHost;
}
private String urltoData(final String href) throws ClientProtocolException, IOException {
StringBuilder sb = new StringBuilder();
sb.append("data:image/");
sb.append(href.substring(href.lastIndexOf("FieldElemFormat=") + 1));
sb.append(";base64,");
// Here go the Image data
HttpHost target = this.getHttpHost();
HttpClient client = this.getHttpClient();
HttpGet get = new HttpGet(href);
HttpResponse response = client.execute(target, get);
InputStream data = response.getEntity().getContent();
BASE64Encoder encoder = new BASE64Encoder();
OutputStream output = new ByteArrayOutputStream();
encoder.encode(data, output);
sb.append(output.toString());
output.close();
return sb.toString();
}
}
Would be curious if that works for you. The Notes client can't show inline HTML images

Resources