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;
}
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;
}
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());
}
}
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);
}
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()));
}
}
}
Aggregations