@Override public void field(Field rawField) throws MimeException { String name = rawField.getName(); String raw = rawField.getRaw().toString(); part.addRawHeader(name, raw); }
@Override public void field(Field parsedField) throws MimeException { expect(Part.class); String name = parsedField.getName(); String raw = parsedField.getRaw().toString(); ((Part) stack.peek()).addRawHeader(name, raw); } }
@Override public void field(Field parsedField) throws MimeException { String name = parsedField.getName(); String raw = parsedField.getRaw().toString(); Part part = (Part) stack.peek(); part.addRawHeader(name, raw); String fieldImmediateValue = MimeUtility.getHeaderParameter(parsedField.getBody(), null); if ("Content-Type".equalsIgnoreCase(name) && MimeUtility.isMessage(fieldImmediateValue)) { isMessagePart = true; } if ("Content-Disposition".equalsIgnoreCase(name) && "attachment".equalsIgnoreCase(fieldImmediateValue)) { isContentDispositionAttachment = true; } }
/** * Gets the name of the field (<code>Subject</code>, * <code>From</code>, etc). * * @return the field name. */ public String getName() { return rawField.getName(); }
String fieldname = field.getName();
/** * Adds a field to the end of the list of fields. * * @param field the field to add. */ public void addField(Field field) { List<Field> values = fieldMap.get(field.getName().toLowerCase(Locale.US)); if (values == null) { values = new LinkedList<Field>(); fieldMap.put(field.getName().toLowerCase(Locale.US), values); } values.add(field); fields.add(field); }
/** * Adds a field to the end of the list of fields. * * @param field the field to add. */ public AbstractEntityBuilder addField(Field field) { List<Field> values = fieldMap.get(field.getName().toLowerCase(Locale.US)); if (values == null) { values = new LinkedList<Field>(); fieldMap.put(field.getName().toLowerCase(Locale.US), values); } values.add(field); fields.add(field); return this; }
/** * Removes all <code>Field</code>s having the specified field name. * * @param name * the field name (e.g. From, Subject). */ public AbstractEntityBuilder removeFields(String name) { final String lowerCaseName = name.toLowerCase(Locale.US); List<Field> removed = fieldMap.remove(lowerCaseName); if (removed == null || removed.isEmpty()) { return this; } for (Iterator<Field> iterator = fields.iterator(); iterator.hasNext();) { Field field = iterator.next(); if (field.getName().equalsIgnoreCase(name)) { iterator.remove(); } } return this; }
/** * Removes all <code>Field</code>s having the specified field name. * * @param name * the field name (e.g. From, Subject). * @return number of fields removed. */ public int removeFields(String name) { final String lowerCaseName = name.toLowerCase(Locale.US); List<Field> removed = fieldMap.remove(lowerCaseName); if (removed == null || removed.isEmpty()) return 0; for (Iterator<Field> iterator = fields.iterator(); iterator.hasNext();) { Field field = iterator.next(); if (field.getName().equalsIgnoreCase(name)) iterator.remove(); } return removed.size(); }
public Builder add(Field field) { Preconditions.checkNotNull(field); String headerName = field.getName().toLowerCase(Locale.US); String sanitizedValue = MimeUtil.unscrambleHeaderValue(field.getBody()); if (!headerName.contains(".")) { headers.put(headerName, sanitizedValue); } handleSpecificHeader(headerName, sanitizedValue); return this; }
protected RawField getRawField() { if (rawField instanceof RawField) { return ((RawField) rawField); } else { return new RawField(rawField.getName(), rawField.getBody()); } }
public ParsedField parse(final Field rawField, final DecodeMonitor monitor) { final FieldParser<? extends ParsedField> parser = getParser(rawField.getName()); return parser.parse(rawField, monitor); } }
/** * Create a node given a Multipart body. * Add the Preamble, all Body parts and the Epilogue to the node. * * @return the root node of the tree. */ private DefaultMutableTreeNode createNode(Header header) { DefaultMutableTreeNode node = new DefaultMutableTreeNode( new ObjectWrapper("Header", header)); for (Field field : header.getFields()) { String name = field.getName(); node.add(new DefaultMutableTreeNode(new ObjectWrapper(name, field))); } return node; }
/** * Write the specified <code>Field</code> to the specified * <code>OutputStream</code>. * * @param field * the <code>Field</code> to write. * @param out * the OutputStream to write to. * @throws IOException * if an I/O error occurs. */ public void writeField(Field field, OutputStream out) throws IOException { ByteSequence raw = field.getRaw(); if (raw == null) { StringBuilder buf = new StringBuilder(); buf.append(field.getName()); buf.append(": "); String body = field.getBody(); if (body != null) { buf.append(body); } raw = ContentUtil.encode(MimeUtil.fold(buf.toString(), 0)); } writeBytes(raw, out); out.write(CRLF); }
private List readHeaders() throws IOException, MimeException { if(log.isDebugEnabled()){ log.debug("readHeaders"); } checkParserState(parser.next(), EntityState.T_START_HEADER); List headers = new ArrayList(); 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)); } checkParserState(parser.next(), EntityState.T_BODY); return headers; }
private static MimeDescriptorImpl createDescriptor( final MimeTokenStream parser) throws IOException, MimeException { EntityState next = parser.next(); final Collection<MessageResult.Header> headers = new ArrayList<>(); while (next != EntityState.T_BODY && next != EntityState.T_END_OF_STREAM && next != EntityState.T_START_MULTIPART) { if (next == EntityState.T_FIELD) { headers.add(new ResultHeader(parser.getField().getName(), parser .getField().getBody().trim())); } next = parser.next(); } final MimeDescriptorImpl mimeDescriptorImpl; switch (next) { case T_BODY: mimeDescriptorImpl = simplePartDescriptor(parser, headers); break; case T_START_MULTIPART: mimeDescriptorImpl = compositePartDescriptor(parser, headers); break; case T_END_OF_STREAM: throw new MimeException("Premature end of stream"); default: throw new MimeException("Unexpected parse state"); } return mimeDescriptorImpl; }
/** Process a multipart body part */ private boolean processMultipart(JCas jCas, Multipart mp, String sourceUri) throws IOException { boolean doneBody = false; for (Entity e : mp.getBodyParts()) { if (e.getFilename() != null) { // Part has a filename, and is therefore an attachment String extension = FilenameUtils.getExtension(e.getFilename()).toLowerCase(); if (ignoreExtensionsList.contains(extension)) { getMonitor().info("Skipping attachment {}", e.getFilename()); continue; } attachments.put(sourceUri + "/" + e.getFilename(), e.getBody()); } else if (!doneBody) { // Part has no filename, and we've not already processed a part to use as a body processBody(jCas, e.getBody(), sourceUri); // Add metadata for (Field f : e.getParent().getHeader().getFields()) { addMetadata(jCas, f.getName(), f.getBody()); } doneBody = true; } } return doneBody; }
/** Process a multipart body part */ private boolean processMultipart(JCas jCas, Multipart mp, String sourceUri) throws IOException { boolean doneBody = false; for (Entity e : mp.getBodyParts()) { if (e.getFilename() != null) { // Part has a filename, and is therefore an attachment String extension = FilenameUtils.getExtension(e.getFilename()).toLowerCase(); if (ignoreExtensionsList.contains(extension)) { getMonitor().info("Skipping attachment {}", e.getFilename()); continue; } attachments.put(sourceUri + "/" + e.getFilename(), e.getBody()); } else if (!doneBody) { // Part has no filename, and we've not already processed a part to use as a body processBody(jCas, e.getBody(), sourceUri); // Add metadata for (Field f : e.getParent().getHeader().getFields()) { addMetadata(jCas, f.getName(), f.getBody()); } doneBody = true; } } return doneBody; }
/** Process a single body part */ private boolean processBody(JCas jCas, Body body, String sourceUri) throws IOException { if (body instanceof TextBody) { // Process plain text body processTextBody(jCas, (TextBody) body); // Add fields from parent for (Field f : body.getParent().getHeader().getFields()) { addMetadata(jCas, f.getName(), f.getBody()); } // Set up document annotation - this is done by the content extractor in other cases DocumentAnnotation doc = UimaSupport.getDocumentAnnotation(jCas); doc.setSourceUri(sourceUri); doc.setTimestamp(System.currentTimeMillis()); } else if (body instanceof BinaryBody) { processBinaryBody(jCas, (BinaryBody) body, sourceUri); } else if (body instanceof Multipart) { // Multipart message, so recurse Multipart mp = (Multipart) body; return processMultipart(jCas, mp, sourceUri); } else { // No body processed return false; } return true; }
/** Process a single body part */ private boolean processBody(JCas jCas, Body body, String sourceUri) throws IOException { if (body instanceof TextBody) { // Process plain text body processTextBody(jCas, (TextBody) body); // Add fields from parent for (Field f : body.getParent().getHeader().getFields()) { addMetadata(jCas, f.getName(), f.getBody()); } // Set up document annotation - this is done by the content extractor in other cases DocumentAnnotation doc = UimaSupport.getDocumentAnnotation(jCas); doc.setSourceUri(sourceUri); doc.setTimestamp(System.currentTimeMillis()); } else if (body instanceof BinaryBody) { processBinaryBody(jCas, (BinaryBody) body, sourceUri); } else if (body instanceof Multipart) { // Multipart message, so recurse Multipart mp = (Multipart) body; return processMultipart(jCas, mp, sourceUri); } else { // No body processed return false; } return true; }