Search in sources :

Example 1 with MIMEEntity

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

the class Document method closeMIMEEntities.

/*
	 * (non-Javadoc)
	 *
	 * @see org.openntf.domino.Document#closeMIMEEntities(boolean, java.lang.String)
	 */
@Override
public boolean closeMIMEEntities(final boolean saveChanges, final String entityItemName) {
    // checkMimeOpen(); RPr: This is not needed here (just to tweak my grep command)
    if (isDeferred_) {
        return true;
    }
    if (isDead()) {
        return true;
    }
    try {
        // TODO: $Mime-xxx Fields to fieldNames_ List
        if (saveChanges) {
            if (entityItemName == null) {
                markDirty();
            } else {
                markDirty("$NoteHasNativeMIME", true);
                markDirty("MIME_Version", true);
                markDirty("$MIMETrack", true);
                markDirty(entityItemName, true);
            }
        }
        // // This has to be called BEFORE we recycle the Mime entity, otherwise the data may not be stored.
        boolean ret = false;
        if (null != entityItemName) {
            try {
                ret = getDelegate().closeMIMEEntities(saveChanges, entityItemName);
                if (saveChanges && !ret) {
                    if (log_.isLoggable(Level.SEVERE)) {
                        log_.log(Level.SEVERE, "closeMIMEEntities returned false for item " + entityItemName + " on doc " + getNoteID() + " in db " + getAncestorDatabase().getApiPath(), new Throwable());
                    }
                }
            } catch (NotesException e) {
                log_.log(Level.INFO, "Attempted to close a MIMEEntity called " + entityItemName + " even though we can't find an item by that name.", e);
            }
        } else {
            try {
                ret = getDelegate().closeMIMEEntities(saveChanges, null);
            } catch (NotesException e) {
                log_.log(Level.INFO, "Failed to close all MIMEEntities", e);
            }
        }
        // ensure that every MIME item is recycled before closing.
        if (openMIMEEntities_ != null) {
            if (entityItemName == null) {
                Collection<Set<MIMEEntity>> values = openMIMEEntities_.values();
                if (values != null && !values.isEmpty()) {
                    for (Set<MIMEEntity> currEntitySet : values) {
                        if (currEntitySet != null && !currEntitySet.isEmpty()) {
                            for (MIMEEntity currEntity : currEntitySet) {
                                ((org.openntf.domino.impl.MIMEEntity) currEntity).closeMIMEEntity();
                            }
                        }
                    }
                }
                openMIMEEntities_.clear();
            } else {
                String lcName = entityItemName.toLowerCase();
                if (openMIMEEntities_.containsKey(lcName)) {
                    Set<MIMEEntity> currEntitySet = openMIMEEntities_.remove(lcName);
                    if (currEntitySet != null) {
                        for (MIMEEntity currEntity : currEntitySet) {
                            ((org.openntf.domino.impl.MIMEEntity) currEntity).closeMIMEEntity();
                        }
                    }
                } else {
                    log_.log(Level.FINE, "A request was made to close MIMEEntity " + entityItemName + " but that entity isn't currently open");
                }
            }
        }
        if (saveChanges) {
            // This item is for debugging only, so keep 5-10 items in that list
            // http://www-01.ibm.com/support/docview.wss?uid=swg27002572
            Vector<Object> mt = getItemValue("$MIMETrack");
            if (mt.size() > 10) {
                replaceItemValue("$MIMETrack", mt.subList(mt.size() - 10, mt.size()));
            }
        // Other ideas: 1) Delete it completely, 2) write dummy entry
        // removeItem("$MIMETrack");
        // replaceItemValue("$MIMETrack", "Itemize by OpenNTF-Domino API on " + getAncestorSession().getServerName() + " at "
        // + new Date());
        }
        return ret;
    } catch (Exception e) {
        DominoUtils.handleException(e, this);
    }
    return false;
}
Also used : SortedSet(java.util.SortedSet) DocumentEntrySet(org.openntf.domino.helpers.DocumentEntrySet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) OpenNTFNotesException(org.openntf.domino.exceptions.OpenNTFNotesException) JsonException(com.ibm.commons.util.io.json.JsonException) DocumentWriteAccessException(org.openntf.domino.exceptions.DocumentWriteAccessException) DataNotCompatibleException(org.openntf.domino.exceptions.DataNotCompatibleException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) NotesException(lotus.domino.NotesException) DominoNonSummaryLimitException(org.openntf.domino.exceptions.DominoNonSummaryLimitException) IOException(java.io.IOException) ItemNotFoundException(org.openntf.domino.exceptions.ItemNotFoundException) OpenNTFNotesException(org.openntf.domino.exceptions.OpenNTFNotesException) NotesException(lotus.domino.NotesException) MIMEEntity(org.openntf.domino.MIMEEntity) EmbeddedObject(org.openntf.domino.EmbeddedObject)

Example 2 with MIMEEntity

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

the class Document method getItemValueCustomDataBytes.

/*
	 * (non-Javadoc)
	 *
	 * @see org.openntf.domino.Document#getItemValueCustomDataBytes(java.lang.String, java.lang.String)
	 */
@Override
public byte[] getItemValueCustomDataBytes(final String itemName, final String dataTypeName) throws IOException {
    checkMimeOpen(itemName);
    try {
        byte[] ret = getDelegate().getItemValueCustomDataBytes(itemName, dataTypeName);
        if (ret != null && ret.length != 0) {
            return ret;
        }
        MIMEEntity entity;
        if ((entity = getMIMEEntity(itemName)) == null) {
            return ret;
        }
        Object o = null;
        try {
            o = Documents.getItemValueMIME(this, itemName, entity);
        } finally {
            closeMIMEEntities(false, itemName);
        }
        if (o != null && o.getClass().getName().equals("[B")) {
            ret = (byte[]) o;
        }
        return ret;
    } catch (NotesException e) {
        DominoUtils.handleException(e, this, "Item=" + itemName);
    }
    return null;
}
Also used : MIMEEntity(org.openntf.domino.MIMEEntity) OpenNTFNotesException(org.openntf.domino.exceptions.OpenNTFNotesException) NotesException(lotus.domino.NotesException) EmbeddedObject(org.openntf.domino.EmbeddedObject)

Example 3 with MIMEEntity

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

the class Document method fromLotusMimeEntity.

// RPr: currently not used. So I commented this out
// private final transient Map<String, MIMEEntity> entityCache_ = new HashMap<String, MIMEEntity>();
/**
 * This is used to track all MIMEEntities in this document. EVERY MIME-Item should be routed over this method!
 *
 * @param lotus
 *            the lotus name
 * @param itemName
 *            the itemName
 * @return the wrapped and tracked {@link MIMEEntity}
 */
protected MIMEEntity fromLotusMimeEntity(final lotus.domino.MIMEEntity lotus, final String itemName) {
    if (lotus == null) {
        return null;
    }
    MIMEEntity wrapped = fromLotus(lotus, MIMEEntity.SCHEMA, this);
    if (wrapped != null) {
        ((org.openntf.domino.impl.MIMEEntity) wrapped).init(itemName);
        String lcName = itemName.toLowerCase();
        if (openMIMEEntities_ == null) {
            openMIMEEntities_ = new HashMap<String, Set<MIMEEntity>>();
        }
        Set<MIMEEntity> entityGroup = openMIMEEntities_.get(lcName);
        if (entityGroup == null) {
            entityGroup = new HashSet<MIMEEntity>();
            openMIMEEntities_.put(lcName, entityGroup);
        }
        entityGroup.add(wrapped);
    }
    return wrapped;
}
Also used : MIMEEntity(org.openntf.domino.MIMEEntity) SortedSet(java.util.SortedSet) DocumentEntrySet(org.openntf.domino.helpers.DocumentEntrySet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet)

Example 4 with MIMEEntity

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

the class Document method toJson.

@Override
public String toJson(final boolean compact) {
    StringWriter sw = new StringWriter();
    JsonWriter jw = new JsonWriter(sw, compact);
    try {
        jw.startObject();
        jw.outStringProperty("@unid", getUniversalID());
        jw.outStringProperty("@noteid", getNoteID());
        jw.outStringProperty("@replicaid", getParentDatabase().getReplicaID());
        jw.outStringProperty("@metaversalid", getMetaversalID());
        try {
            jw.outStringProperty("@created", getCreated().toGMTISO());
            jw.outStringProperty("@lastmodified", getLastModified().toGMTISO());
            jw.outStringProperty("@lastaccessed", getLastAccessed().toGMTISO());
        } catch (Exception e) {
            DominoUtils.handleException(e, "Exception trying to index Dates.");
        }
        Set<String> keys = keySet();
        for (String key : keys) {
            Item currItem = getFirstItem(key);
            // A beer to anyone who can work out how this could happen, except for the person who identified it!
            if (null != currItem) {
                Type itemType = currItem.getTypeEx();
                try {
                    if (itemType == Type.ATTACHMENT) {
                        jw.outProperty(key, "ATTACHMENT");
                    } else if (itemType == Type.AUTHORS || itemType == Type.READERS || itemType == Type.NAMES || itemType == Type.TEXT || itemType == Type.NUMBERS) {
                        Vector<Object> values = currItem.getValues();
                        if (values.size() == 1) {
                            jw.outProperty(key, values.elementAt(0));
                        } else {
                            jw.outProperty(key, values);
                        }
                    } else if (itemType == Type.DATETIMES) {
                        Vector<DateTime> values = currItem.getValueDateTimeArray();
                        if (values.size() == 1) {
                            jw.outProperty(key, values.get(0).toGMTISO());
                        } else {
                            jw.outProperty(key, TypeUtils.toStrings(values));
                        }
                    } else if (itemType == Type.EMBEDDEDOBJECT) {
                        jw.outProperty(key, "EMBEDDED_OBJECT");
                    } else if (itemType == Type.RICHTEXT) {
                        RichTextItem rtItem = (RichTextItem) currItem;
                        jw.outProperty(key, rtItem.getUnformattedText());
                    } else if (itemType == Type.MIME_PART) {
                        MIMEEntity mimeEntity = currItem.getMIMEEntity();
                        if (mimeEntity != null) {
                            jw.outProperty(key, mimeEntity.getContentAsText());
                        } else {
                            jw.outProperty(key, "MIME_PART null");
                        }
                    }
                } catch (Exception e) {
                    DominoUtils.handleException(e, this);
                    // NTF - temporary
                    e.printStackTrace();
                }
            }
            // if (currItem.getMIMEEntity() == null) {
            // jw.outProperty(key, currItem.getText());
            // } else {
            // String abstractedText = currItem.abstractText(0, false, false);
            // if (null == abstractedText) {
            // jw.outProperty(key, "**MIME ITEM, VALUE CANNOT BE DECODED TO JSON**");
            // } else {
            // jw.outProperty(key, abstractedText);
            // }
            // }
            // Now output attachments
            jw.outProperty("@attachments", getAttachmentNames());
        }
        jw.endObject();
        jw.flush();
    } catch (IOException e) {
        DominoUtils.handleException(e, this);
        return null;
    } catch (JsonException e) {
        DominoUtils.handleException(e, this);
        return null;
    }
    return sw.toString();
}
Also used : JsonException(com.ibm.commons.util.io.json.JsonException) IOException(java.io.IOException) JsonWriter(com.ibm.commons.util.io.json.util.JsonWriter) OpenNTFNotesException(org.openntf.domino.exceptions.OpenNTFNotesException) JsonException(com.ibm.commons.util.io.json.JsonException) DocumentWriteAccessException(org.openntf.domino.exceptions.DocumentWriteAccessException) DataNotCompatibleException(org.openntf.domino.exceptions.DataNotCompatibleException) UserAccessException(org.openntf.domino.exceptions.UserAccessException) NotesException(lotus.domino.NotesException) DominoNonSummaryLimitException(org.openntf.domino.exceptions.DominoNonSummaryLimitException) IOException(java.io.IOException) ItemNotFoundException(org.openntf.domino.exceptions.ItemNotFoundException) DateTime(org.openntf.domino.DateTime) RichTextItem(org.openntf.domino.RichTextItem) Item(org.openntf.domino.Item) Type(org.openntf.domino.Item.Type) MIMEEntity(org.openntf.domino.MIMEEntity) StringWriter(java.io.StringWriter) RichTextItem(org.openntf.domino.RichTextItem) Vector(java.util.Vector) ItemVector(org.openntf.domino.iterators.ItemVector)

Example 5 with MIMEEntity

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

the class DominoEmail method send.

/* (non-Javadoc)
	 * @see org.openntf.domino.email.IEmail#send()
	 */
@Override
public Document send() {
    try {
        Stream stream;
        MIMEEntity mimeEntity;
        MIMEHeader mimeHeader;
        Database currDb;
        Session currSess = getSession();
        // in case Khan is still in suspended animation!
        currSess.setConvertMime(false);
        // Create memo doc
        try {
            currDb = currSess.getCurrentDatabase();
            if (null == currDb) {
                // Will this work if we're running from DOTS or OSGi plugin??
                // $NON-NLS-1$
                currDb = currSess.getDatabase(currSess.getServerName(), "mail.box");
            }
        } catch (Throwable t) {
            // $NON-NLS-1$
            currDb = currSess.getDatabase(currSess.getServerName(), "mail.box");
        }
        Document memo = currDb.createDocument();
        // no replies from out of office agents //$NON-NLS-1$ //$NON-NLS-2$
        memo.put("RecNoOutOfOffice", "1");
        String mimeBoundary = memo.getUniversalID().toLowerCase();
        // $NON-NLS-1$
        MIMEEntity mimeRoot = memo.createMIMEEntity("Body");
        // $NON-NLS-1$
        mimeHeader = mimeRoot.createHeader("To");
        // $NON-NLS-1$
        mimeHeader.setHeaderVal(join(getTo(), ""));
        if (cc_.size() > 0) {
            // $NON-NLS-1$
            mimeHeader = mimeRoot.createHeader("CC");
            // $NON-NLS-1$
            mimeHeader.setHeaderVal(join(getCC(), ""));
        // memo.replaceItemValue("cc", getCC());
        }
        if (bcc_.size() > 0) {
            // $NON-NLS-1$
            mimeHeader = mimeRoot.createHeader("BCC");
            // $NON-NLS-1$
            mimeHeader.setHeaderVal(join(getBCC(), ""));
        // memo.replaceItemValue("bcc", getBCC());
        }
        // set subject
        // $NON-NLS-1$
        mimeHeader = mimeRoot.createHeader("Subject");
        // $NON-NLS-1$
        mimeHeader.addValText(getSubject(), "UTF-8");
        // create text/alternative directive: text/plain and text/html part will be childs of this entity
        MIMEEntity mimeRootChild = mimeRoot.createChildEntity();
        // $NON-NLS-1$
        mimeHeader = mimeRootChild.createHeader("Content-Type");
        // $NON-NLS-1$ //$NON-NLS-2$
        mimeHeader.setHeaderVal("multipart/alternative; boundary=\"" + mimeBoundary + "\"");
        // create plain text part
        if (getText().size() > 0) {
            mimeEntity = mimeRootChild.createChildEntity();
            stream = currSess.createStream();
            // $NON-NLS-1$
            stream.writeText(join(getText(), System.getProperty("line.separator")));
            // $NON-NLS-1$
            mimeEntity.setContentFromText(stream, "text/plain; charset=\"UTF-8\"", MIMEEntity.ENC_NONE);
            stream.close();
        }
        // create HTML part
        if (contentsHTML_.size() > 0) {
            mimeEntity = mimeRootChild.createChildEntity();
            stream = currSess.createStream();
            // $NON-NLS-1$
            stream.writeText(join(contentsHTML_, System.getProperty("line.separator")));
            // $NON-NLS-1$
            mimeEntity.setContentFromText(stream, "text/html; charset=\"UTF-8\"", MIMEEntity.ENC_NONE);
            stream.close();
        }
        // create embedded JSON part
        if (!StringUtil.isEmpty(getJSON())) {
            mimeEntity = mimeRootChild.createChildEntity();
            stream = currSess.createStream();
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            String json = "{\"url\" : \"" + getJSON() + "\"}" + System.getProperty("line.separator");
            stream.writeText(json);
            // $NON-NLS-1$
            mimeEntity.setContentFromText(stream, "application/embed+json; charset=\"UTF-8\"", MIMEEntity.ENC_NONE);
            stream.close();
        }
        // Add any attachments
        addAttachments(mimeRoot);
        // set the sender
        setSender(mimeRoot);
        // mime processing done.
        memo.closeMIMEEntities(true);
        // send the e-mail
        memo.send();
        return memo;
    } catch (Throwable t) {
        DominoUtils.handleException(t);
        return null;
    }
}
Also used : MIMEEntity(org.openntf.domino.MIMEEntity) Database(org.openntf.domino.Database) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Stream(org.openntf.domino.Stream) Document(org.openntf.domino.Document) MIMEHeader(org.openntf.domino.MIMEHeader) Session(org.openntf.domino.Session)

Aggregations

MIMEEntity (org.openntf.domino.MIMEEntity)11 NotesException (lotus.domino.NotesException)7 OpenNTFNotesException (org.openntf.domino.exceptions.OpenNTFNotesException)6 EmbeddedObject (org.openntf.domino.EmbeddedObject)4 JsonException (com.ibm.commons.util.io.json.JsonException)3 IOException (java.io.IOException)3 MIMEHeader (org.openntf.domino.MIMEHeader)3 DataNotCompatibleException (org.openntf.domino.exceptions.DataNotCompatibleException)3 DocumentWriteAccessException (org.openntf.domino.exceptions.DocumentWriteAccessException)3 DominoNonSummaryLimitException (org.openntf.domino.exceptions.DominoNonSummaryLimitException)3 ItemNotFoundException (org.openntf.domino.exceptions.ItemNotFoundException)3 UserAccessException (org.openntf.domino.exceptions.UserAccessException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 Vector (java.util.Vector)2 Database (org.openntf.domino.Database)2