可怕的 hack(您需要整理身份验证和服务器名称)
SSJS(从视图中获取源代码)
var unid = curRow.getUniversalID();
var body = getComponent("body1");
var magic = new demo.HTMLMagic();
magic.doMagic(database, unid, body);
爪哇
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();
}
}
如果这对你有用,我会很好奇。Notes 客户端无法显示内联 HTML 图像