Search in sources :

Example 1 with Field

use of org.apache.james.mime4j.stream.Field in project sling by apache.

the class MessageStoreImplRepositoryTestUtil method getResourcePath.

static String getResourcePath(Message msg, MessageStoreImpl store) {
    final Header hdr = msg.getHeader();
    final String listIdRaw = hdr.getField("List-Id").getBody();
    // remove < and >
    final String listId = listIdRaw.substring(1, listIdRaw.length() - 1);
    String msgId;
    final Field msgIdField = hdr.getField("Message-ID");
    if (msgIdField != null) {
        msgId = msgIdField.getBody();
        msgId = msgId.substring(1, msgId.length() - 1);
    } else {
        msgId = Integer.toHexString(hdr.getField("Date").hashCode());
    }
    msgId = makeJcrFriendly(msgId);
    String subject = null;
    final Field subjectField = hdr.getField("Subject");
    if (subjectField != null) {
        subject = subjectField.getBody();
    }
    String threadPath = store.threadKeyGen.getThreadKey(subject);
    String path = store.archivePath + getDomainNodeName(listId) + "/" + getListNodeName(listId) + "/" + threadPath + "/" + msgId;
    return path;
}
Also used : Field(org.apache.james.mime4j.stream.Field) Header(org.apache.james.mime4j.dom.Header)

Example 2 with Field

use of org.apache.james.mime4j.stream.Field in project webservices-axiom by apache.

the class MultipartBody method getNextPart.

PartImpl getNextPart() {
    if (currentPart != null) {
        currentPart.fetch();
    }
    if (parser.getState() == EntityState.T_END_MULTIPART) {
        currentPart = null;
    } else {
        String partContentID = null;
        boolean isRootPart;
        try {
            checkParserState(parser.next(), EntityState.T_START_HEADER);
            List<Header> headers = new ArrayList<Header>();
            while (parser.next() == EntityState.T_FIELD) {
                Field field = parser.getField();
                String name = field.getName();
                String value = field.getBody();
                if (log.isDebugEnabled()) {
                    log.debug("addHeader: (" + name + ") value=(" + value + ")");
                }
                headers.add(new Header(name, value));
                if (partContentID == null && name.equalsIgnoreCase("Content-ID")) {
                    partContentID = normalizeContentID(value);
                }
            }
            checkParserState(parser.next(), EntityState.T_BODY);
            if (rootPartContentID == null) {
                isRootPart = firstPart == null;
            } else {
                isRootPart = rootPartContentID.equals(partContentID);
            }
            PartImpl part = new PartImpl(this, isRootPart ? MemoryBlob.FACTORY : attachmentBlobFactory, partContentID, headers, parser);
            if (currentPart == null) {
                firstPart = part;
            } else {
                currentPart.setNextPart(part);
            }
            currentPart = part;
        } catch (IOException ex) {
            throw new MIMEException(ex);
        } catch (MimeException ex) {
            throw new MIMEException(ex);
        }
        partCount++;
        if (partContentID != null) {
            if (partMap.containsKey(partContentID)) {
                throw new MIMEException("Two MIME parts with the same Content-ID not allowed.");
            }
            partMap.put(partContentID, currentPart);
        }
        if (isRootPart) {
            rootPart = currentPart;
        }
        if (partCreationListener != null) {
            partCreationListener.partCreated(currentPart);
        }
    }
    return currentPart;
}
Also used : Field(org.apache.james.mime4j.stream.Field) ArrayList(java.util.ArrayList) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException)

Example 3 with Field

use of org.apache.james.mime4j.stream.Field in project Gargoyle by callakrsos.

the class MimeViewerExam method extracted.

private void extracted(StringBuilder sb, Body body) {
    if (body instanceof Multipart) {
        Multipart mbody = (Multipart) body;
        for (Entity part : mbody.getBodyParts()) {
            extracted(sb, part);
        }
    } else if (body instanceof MessageImpl) {
        extracted(sb, body);
    } else if (body instanceof TextBody) {
        /*
			 * A text body. Display its contents.
			 */
        TextBody textBody = (TextBody) body;
        try {
            Reader r = textBody.getReader();
            StringBuilder _sb = new StringBuilder();
            int c;
            while ((c = r.read()) != -1) {
                _sb.append((char) c);
            }
            System.out.println(_sb.toString());
            sb.append(_sb.toString());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    } else if (body instanceof BinaryBody) {
        BinaryBody bBody = (BinaryBody) body;
        Entity parent = bBody.getParent();
        //			String dispositionType = parent.getDispositionType();
        //			String filename = parent.getFilename();
        //			String contentTransferEncoding = parent.getContentTransferEncoding();
        //			String mimeType = parent.getMimeType();
        Field field = parent.getHeader().getField("Content-ID");
        String body2 = field.getBody();
        String contentId = body2.replace("<", "").replace(">", "");
        StringBuffer buf = new StringBuffer();
        try (InputStream is = bBody.getInputStream()) {
            int read = -1;
            while ((read = is.read()) != -1) {
                buf.append((char) read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        meta.put(contentId, buf.toString());
    } else /*
		 * Ignore Fields </br>
		 * 
		 * ContentTypeField,AddressListField,DateTimeField UnstructuredField,
		 * Field
		 * 
		 */
    {
        sb.append(body.toString());
    }
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) Multipart(org.apache.james.mime4j.dom.Multipart) InputStream(java.io.InputStream) Reader(java.io.Reader) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) TextBody(org.apache.james.mime4j.dom.TextBody) Field(org.apache.james.mime4j.stream.Field) MessageImpl(org.apache.james.mime4j.message.MessageImpl)

Example 4 with Field

use of org.apache.james.mime4j.stream.Field in project sling by apache.

the class MessageStoreImplAttachmentsTest method assertSaveMessageWithAttachments.

private void assertSaveMessageWithAttachments(Message msg, int num) throws IOException {
    store.save(msg);
    List<BodyPart> attList = new LinkedList<BodyPart>();
    MessageStoreImpl.recursiveMultipartProcessing((Multipart) msg.getBody(), new StringBuilder(), new StringBuilder(), false, attList);
    @SuppressWarnings("unchecked") Queue<BodyPart> attachmentsMsg = (Queue<BodyPart>) attList;
    assertTrue("No attachments found", attachmentsMsg.size() > 0);
    assertEquals("", num, attachmentsMsg.size());
    final Resource r = resolver.getResource(getResourcePath(msg, store));
    assertNotNull("Expecting non-null Resource", r);
    for (Resource aRes : r.getChildren()) {
        final ModifiableValueMap aMap = aRes.adaptTo(ModifiableValueMap.class);
        BodyPart aMsg = attachmentsMsg.poll();
        assertNotNull("JCR contains more attachments", aMsg);
        for (Field f : aMsg.getHeader().getFields()) {
            String name = f.getName();
            assertEquals("Field " + name + " is different", (aMap.get(name, String.class)), f.getBody());
        }
        if (aMsg.getBody() instanceof TextBody) {
            assertEquals("Content is not the same", MessageStoreImpl.getTextPart(aMsg), aMap.get(CONTENT, String.class));
        } else if (aMsg.getBody() instanceof BinaryBody) {
            assertEquals("Content is not the same", getBinPart(aMsg), aMap.get(CONTENT, String.class));
        } else {
            fail("Unknown type of attachment body");
        }
    }
    assertEquals("Message contains more attachments", attachmentsMsg.poll(), null);
}
Also used : BodyPart(org.apache.james.mime4j.message.BodyPart) Resource(org.apache.sling.api.resource.Resource) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) LinkedList(java.util.LinkedList) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) TextBody(org.apache.james.mime4j.dom.TextBody) RawField(org.apache.james.mime4j.stream.RawField) Field(org.apache.james.mime4j.stream.Field) Queue(java.util.Queue)

Example 5 with Field

use of org.apache.james.mime4j.stream.Field in project sling by apache.

the class MessageStoreImpl method parseHeaderToProps.

private static void parseHeaderToProps(Header hdr, Map<String, Object> props) {
    Set<String> processed = new HashSet<String>();
    for (Field f : hdr.getFields()) {
        String name = f.getName();
        if (!processed.contains(name)) {
            processed.add(name);
            String value = "";
            List<Field> fields = hdr.getFields(name);
            for (Field fl : fields) {
                value += fl.getBody() + FIELD_SEPARATOR;
            }
            props.put(name, value.substring(0, value.length() - FIELD_SEPARATOR.length()));
        }
    }
}
Also used : Field(org.apache.james.mime4j.stream.Field) HashSet(java.util.HashSet)

Aggregations

Field (org.apache.james.mime4j.stream.Field)5 IOException (java.io.IOException)2 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)2 TextBody (org.apache.james.mime4j.dom.TextBody)2 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Queue (java.util.Queue)1 MimeException (org.apache.james.mime4j.MimeException)1 Entity (org.apache.james.mime4j.dom.Entity)1 Header (org.apache.james.mime4j.dom.Header)1 Multipart (org.apache.james.mime4j.dom.Multipart)1 BodyPart (org.apache.james.mime4j.message.BodyPart)1 MessageImpl (org.apache.james.mime4j.message.MessageImpl)1 RawField (org.apache.james.mime4j.stream.RawField)1 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)1 Resource (org.apache.sling.api.resource.Resource)1