Search in sources :

Example 1 with EmbeddedObject

use of org.openntf.domino.EmbeddedObject in project org.openntf.domino by OpenNTF.

the class RichTextItem method replaceAttachment.

@Override
public EmbeddedObject replaceAttachment(final String filename, final String sourcePath) {
    // System.out.println("TEMP DEBUG replacing filename " + filename + " from source at " + sourcePath);
    EmbeddedObject result = null;
    RichTextNavigator navigator = this.createNavigator();
    EmbeddedObject eo = (EmbeddedObject) navigator.getFirstElement(RTELEM_TYPE_FILEATTACHMENT);
    boolean replaced = false;
    while (eo != null) {
        if (filename.equals(eo.getSource())) {
            // System.out.println("TEMP DEBUG Found matching embeddedobject for name " + filename);
            beginInsert(eo, true);
            // $NON-NLS-1$ //$NON-NLS-2$
            result = embedObject(org.openntf.domino.EmbeddedObject.Type.EMBED_ATTACHMENT.getValue(), "", sourcePath, "");
            endInsert();
            eo.remove();
            replaced = true;
            break;
        }
        eo = (EmbeddedObject) navigator.getNextElement(RTELEM_TYPE_FILEATTACHMENT);
    }
    if (!replaced) {
        // $NON-NLS-1$ //$NON-NLS-2$
        result = embedObject(org.openntf.domino.EmbeddedObject.Type.EMBED_ATTACHMENT.getValue(), "", sourcePath, "");
    }
    return result;
}
Also used : EmbeddedObject(org.openntf.domino.EmbeddedObject) RichTextNavigator(org.openntf.domino.RichTextNavigator)

Example 2 with EmbeddedObject

use of org.openntf.domino.EmbeddedObject in project org.openntf.domino by OpenNTF.

the class RichTextItem method removeAttachment.

@Override
public boolean removeAttachment(final String filename) {
    boolean result = false;
    RichTextNavigator navigator = this.createNavigator();
    EmbeddedObject eo = (EmbeddedObject) navigator.getFirstElement(RTELEM_TYPE_FILEATTACHMENT);
    while (eo != null) {
        if (filename.equals(eo.getSource())) {
            eo.remove();
            result = true;
            break;
        }
        eo = (EmbeddedObject) navigator.getNextElement(RTELEM_TYPE_FILEATTACHMENT);
    }
    return result;
}
Also used : EmbeddedObject(org.openntf.domino.EmbeddedObject) RichTextNavigator(org.openntf.domino.RichTextNavigator)

Example 3 with EmbeddedObject

use of org.openntf.domino.EmbeddedObject in project org.openntf.domino by OpenNTF.

the class DominoEmail method addAttachments.

/* (non-Javadoc)
	 * @see org.openntf.domino.email.IEmail#addAttachments(java.util.ArrayList, org.openntf.domino.MIMEEntity)
	 */
@Override
public void addAttachments(final MIMEEntity parent) {
    try {
        Stream streamFile = null;
        for (IEmailAttachment attach : getAttachments()) {
            InputStream is = null;
            EmbeddedObject eo = null;
            String fileName = attach.getFileName();
            // Get content type
            String contentType = URLConnection.guessContentTypeFromName(fileName);
            if (null == contentType) {
                // $NON-NLS-1$
                contentType = "application/octet-stream";
            }
            // $NON-NLS-1$
            int idex = StringUtil.indexOfIgnoreCase(fileName, ".", fileName.length() - 6);
            if (idex > -1) {
                String extension = fileName.substring(idex);
                if (StringUtil.equals("gif", extension)) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    contentType = "image/gif";
                } else if (StringUtil.equals("jpg", extension) || StringUtil.equals("jpeg", extension)) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    // $NON-NLS-1$
                    contentType = "image/jpeg";
                } else if (StringUtil.equals("png", extension)) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    contentType = "image/png";
                }
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            contentType += "; name=\"" + fileName + "\"";
            try {
                Type attachmentType = attach.getAttachmentType();
                if (attachmentType == Type.DOCUMENT) {
                    // retrieve the document containing the attachment to send from the relevant
                    Database dbFile = getSession().getDatabase(getSession().getServerName(), attach.getDbPath());
                    Document docFile = dbFile.getDocumentByUNID(attach.getUnid());
                    if (null != docFile) {
                        eo = docFile.getAttachment(attach.getFileName());
                        is = eo.getInputStream();
                    }
                } else if (attachmentType == Type.FILE) {
                    Path path = Paths.get(attach.getPath()).resolve(attach.getFileName());
                    is = Files.newInputStream(path);
                } else if (attachmentType == Type.STREAM) {
                    is = attach.getInputStream();
                } else {
                    is = new ByteArrayInputStream(attach.getBytes());
                }
                if (null != is) {
                    MIMEEntity mimeChild = parent.createChildEntity();
                    // $NON-NLS-1$
                    MIMEHeader mimeHeader = mimeChild.createHeader("Content-Disposition");
                    if (attach.isInlineImage()) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        mimeHeader.setHeaderVal("inline; filename=\"" + attach.getFileName() + "\"");
                    } else {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        mimeHeader.setHeaderVal("attachment; filename=\"" + attach.getFileName() + "\"");
                    }
                    // $NON-NLS-1$
                    mimeHeader = mimeChild.createHeader("Content-ID");
                    // $NON-NLS-1$ //$NON-NLS-2$
                    mimeHeader.setHeaderVal("<" + attach.getContentId() + ">");
                    streamFile = getSession().createStream();
                    streamFile.setContents(is);
                    mimeChild.setContentFromBytes(streamFile, contentType, MIMEEntity.ENC_IDENTITY_BINARY);
                    streamFile.close();
                }
            } catch (Exception e) {
                DominoUtils.handleException(e);
            } finally {
                if (null != is) {
                    is.close();
                }
            }
        }
    } catch (Throwable t) {
        DominoUtils.handleException(t);
    }
}
Also used : Path(java.nio.file.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EmbeddedObject(org.openntf.domino.EmbeddedObject) Document(org.openntf.domino.Document) Type(org.openntf.domino.email.IEmailAttachment.Type) SessionType(org.openntf.domino.utils.Factory.SessionType) MIMEEntity(org.openntf.domino.MIMEEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Database(org.openntf.domino.Database) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Stream(org.openntf.domino.Stream) MIMEHeader(org.openntf.domino.MIMEHeader)

Aggregations

EmbeddedObject (org.openntf.domino.EmbeddedObject)3 RichTextNavigator (org.openntf.domino.RichTextNavigator)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 Database (org.openntf.domino.Database)1 Document (org.openntf.domino.Document)1 MIMEEntity (org.openntf.domino.MIMEEntity)1 MIMEHeader (org.openntf.domino.MIMEHeader)1 Stream (org.openntf.domino.Stream)1 Type (org.openntf.domino.email.IEmailAttachment.Type)1 SessionType (org.openntf.domino.utils.Factory.SessionType)1