List of usage examples for javax.mail BodyPart getSize
public int getSize() throws MessagingException;
From source file:org.alfresco.cacheserver.MultipartTest.java
private List<Patch> getPatches(String nodeId, long nodeVersion) throws MessagingException, IOException { List<Patch> patches = new LinkedList<>(); StringBuilder sb = new StringBuilder(); sb.append("/patch/"); sb.append(nodeId);/*from w ww . ja v a2 s . c o m*/ sb.append("/"); sb.append(nodeVersion); String url = sb.toString(); final ClientConfig config = new DefaultClientConfig(); config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); final Client client = Client.create(config); final WebResource resource = client.resource(url); final MimeMultipart response = resource.get(MimeMultipart.class); // This will iterate the individual parts of the multipart response for (int i = 0; i < response.getCount(); i++) { final BodyPart part = response.getBodyPart(i); System.out.printf("Embedded Body Part [Mime Type: %s, Length: %s]\n", part.getContentType(), part.getSize()); InputStream in = part.getInputStream(); // Patch patch = new Patch(); // patches.add(patch); } return patches; }
From source file:fr.gouv.culture.vitam.eml.EmlExtract.java
private static final String addSubIdentities(Element identification, BodyPart bp, InputStream inputStream, VitamArgument argument, ConfigLoader config) { Element newElt = XmlDom.factory.createElement(EMAIL_FIELDS.subidentity.name); String filename = null;/* ww w . j a v a 2 s. co m*/ String result = ""; try { filename = bp.getFileName(); filename = StringUtils.toFileName(filename); if (filename != null) { Element elt = XmlDom.factory.createElement(EMAIL_FIELDS.filename.name); elt.setText(filename); newElt.add(elt); } else { filename = "eml.eml"; } } catch (MessagingException e) { } try { int size = bp.getSize(); if (size > 0) { Element elt = XmlDom.factory.createElement(EMAIL_FIELDS.attSize.name); elt.setText(Integer.toString(size)); newElt.add(elt); } } catch (MessagingException e) { } try { String description = bp.getDescription(); if (description != null) { Element elt = XmlDom.factory.createElement(EMAIL_FIELDS.description.name); elt.setText(description); newElt.add(elt); } } catch (MessagingException e) { } try { String disposition = bp.getDisposition(); if (disposition != null) { Element elt = XmlDom.factory.createElement(EMAIL_FIELDS.disposition.name); elt.setText(disposition); newElt.add(elt); } } catch (MessagingException e) { } File filetemp = null; FileOutputStream outputStream = null; try { // Force out to analysis if (config.extractFile) { filetemp = new File(argument.currentOutputDir, filename); } else { filetemp = File.createTempFile(StaticValues.PREFIX_TEMPFILE, filename); } byte[] buffer = new byte[8192]; int read = 0; outputStream = new FileOutputStream(filetemp); while ((read = inputStream.read(buffer)) >= 0) { outputStream.write(buffer, 0, read); } outputStream.close(); outputStream = null; } catch (IOException e1) { if (filetemp != null && !config.extractFile) { filetemp.delete(); } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { } } String status = "Error during access to attachment"; newElt.addAttribute(EMAIL_FIELDS.status.name, status); identification.add(newElt); return ""; } try { Commands.addFormatIdentification(newElt, filename, filetemp, config, argument); if (argument.extractKeyword) { // get back keyword in the main list Element keyw = (Element) newElt.selectSingleNode(EMAIL_FIELDS.keywords.name); if (keyw != null) { StringBuilder builder = new StringBuilder(); @SuppressWarnings("unchecked") List<Element> elts = (List<Element>) keyw.selectNodes(EMAIL_FIELDS.keywordRank.name); for (Element elt : elts) { String value = elt.attributeValue(EMAIL_FIELDS.keywordOccur.name); int occur = Integer.parseInt(value) / 2 + 1; @SuppressWarnings("unchecked") List<Element> words = (List<Element>) elt.selectNodes(EMAIL_FIELDS.keywordWord.name); for (Element eword : words) { String word = eword.attributeValue(EMAIL_FIELDS.keywordValue.name) + " "; for (int i = 0; i < occur; i++) { builder.append(word); } } } result = builder.toString().trim(); } } } catch (Exception e) { String status = "Error during identification"; e.printStackTrace(); config.addRankId(newElt); newElt.addAttribute(EMAIL_FIELDS.status.name, status); } if (filetemp != null && !config.extractFile) { filetemp.delete(); } identification.add(newElt); return result; }
From source file:com.ikon.util.MailUtils.java
/** * Add attachments to an imported mail./*from www.j a v a 2 s . c o m*/ */ public static void addAttachments(String token, com.ikon.bean.Mail mail, Part p, String userId) throws MessagingException, IOException, UnsupportedMimeTypeException, FileSizeExceededException, UserQuotaExceededException, VirusDetectedException, ItemExistsException, PathNotFoundException, AccessDeniedException, RepositoryException, DatabaseException, ExtensionException, AutomationException { if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); for (int i = 1; i < count; i++) { BodyPart bp = mp.getBodyPart(i); if (bp.getFileName() != null) { String name = MimeUtility.decodeText(bp.getFileName()); String fileName = FileUtils.getFileName(name); String fileExtension = FileUtils.getFileExtension(name); String testName = name; // Test if already exists a document with the same name in the mail for (int j = 1; OKMRepository.getInstance().hasNode(token, mail.getPath() + "/" + testName); j++) { // log.info("Trying with: {}", testName); testName = fileName + " (" + j + ")." + fileExtension; } Document attachment = new Document(); String mimeType = MimeTypeConfig.mimeTypes.getContentType(bp.getFileName().toLowerCase()); attachment.setMimeType(mimeType); attachment.setPath(mail.getPath() + "/" + testName); InputStream is = bp.getInputStream(); if (Config.REPOSITORY_NATIVE) { new DbDocumentModule().create(token, attachment, is, bp.getSize(), userId); } else { new JcrDocumentModule().create(token, attachment, is, userId); } is.close(); } } } }
From source file:com.openkm.util.MailUtils.java
/** * Add attachments to an imported mail.//from ww w . j a v a 2 s .c o m */ public static void addAttachments(String token, com.openkm.bean.Mail mail, Part p, String userId) throws MessagingException, IOException, UnsupportedMimeTypeException, FileSizeExceededException, UserQuotaExceededException, VirusDetectedException, ItemExistsException, PathNotFoundException, AccessDeniedException, RepositoryException, DatabaseException, ExtensionException, AutomationException { if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); for (int i = 1; i < count; i++) { BodyPart bp = mp.getBodyPart(i); if (bp.getFileName() != null) { String name = MimeUtility.decodeText(bp.getFileName()); String fileName = FileUtils.getFileName(name); String fileExtension = FileUtils.getFileExtension(name); String testName = name; // Test if already exists a document with the same name in the mail for (int j = 1; OKMRepository.getInstance().hasNode(token, mail.getPath() + "/" + testName); j++) { // log.info("Trying with: {}", testName); testName = fileName + " (" + j + ")." + fileExtension; } Document attachment = new Document(); String mimeType = MimeTypeConfig.mimeTypes.getContentType(bp.getFileName().toLowerCase()); attachment.setMimeType(mimeType); attachment.setPath(mail.getPath() + "/" + testName); InputStream is = bp.getInputStream(); if (Config.REPOSITORY_NATIVE) { new DbDocumentModule().create(token, attachment, is, bp.getSize(), userId); } else { new JcrDocumentModule().create(token, attachment, is, userId); } is.close(); } } } }
From source file:com.zimbra.cs.mailbox.calendar.Invite.java
/** * Returns the meeting notes. Meeting notes is the text/plain part in an * invite. It typically includes CUA-generated meeting summary as well as * text entered by the user.//from w w w . j a va 2 s. c o m * * @return null if notes is not found * @throws ServiceException */ public static String getDescription(Part mmInv, String mimeType) throws ServiceException { if (mmInv == null) return null; try { // If top-level is text/calendar, parse the iCalendar object and return // the DESCRIPTION of the first VEVENT/VTODO encountered. String mmCtStr = mmInv.getContentType(); if (mmCtStr != null) { ContentType mmCt = new ContentType(mmCtStr); if (mmCt.match(MimeConstants.CT_TEXT_CALENDAR)) { boolean wantHtml = MimeConstants.CT_TEXT_HTML.equalsIgnoreCase(mimeType); Object mmInvContent = mmInv.getContent(); InputStream is = null; try { String charset = MimeConstants.P_CHARSET_UTF8; if (mmInvContent instanceof InputStream) { charset = mmCt.getParameter(MimeConstants.P_CHARSET); if (charset == null) charset = MimeConstants.P_CHARSET_UTF8; is = (InputStream) mmInvContent; } else if (mmInvContent instanceof String) { String str = (String) mmInvContent; charset = MimeConstants.P_CHARSET_UTF8; is = new ByteArrayInputStream(str.getBytes(charset)); } if (is != null) { ZVCalendar iCal = ZCalendarBuilder.build(is, charset); for (Iterator<ZComponent> compIter = iCal.getComponentIterator(); compIter.hasNext();) { ZComponent component = compIter.next(); ICalTok compTypeTok = component.getTok(); if (compTypeTok == ICalTok.VEVENT || compTypeTok == ICalTok.VTODO) { if (!wantHtml) return component.getPropVal(ICalTok.DESCRIPTION, null); else return component.getDescriptionHtml(); } } } } finally { ByteUtil.closeStream(is); } } } Object mmInvContent = mmInv.getContent(); if (!(mmInvContent instanceof MimeMultipart)) { if (mmInvContent instanceof InputStream) { ByteUtil.closeStream((InputStream) mmInvContent); } return null; } MimeMultipart mm = (MimeMultipart) mmInvContent; // If top-level is multipart, get description from text/* part. int numParts = mm.getCount(); String charset = null; for (int i = 0; i < numParts; i++) { BodyPart part = mm.getBodyPart(i); String ctStr = part.getContentType(); try { ContentType ct = new ContentType(ctStr); if (ct.match(mimeType)) { charset = ct.getParameter(MimeConstants.P_CHARSET); if (charset == null) charset = MimeConstants.P_CHARSET_DEFAULT; byte[] descBytes = ByteUtil.getContent(part.getInputStream(), part.getSize()); return new String(descBytes, charset); } // If part is a multipart, recurse. if (ct.getBaseType().matches(MimeConstants.CT_MULTIPART_WILD)) { String str = getDescription(part, mimeType); if (str != null) { return str; } } } catch (javax.mail.internet.ParseException e) { ZimbraLog.calendar.warn("Invalid Content-Type found: \"" + ctStr + "\"; skipping part", e); } } } catch (IOException e) { throw ServiceException.FAILURE("Unable to get calendar item notes MIME part", e); } catch (MessagingException e) { throw ServiceException.FAILURE("Unable to get calendar item notes MIME part", e); } return null; }
From source file:org.alfresco.cacheserver.CacheServer.java
public List<Patch> getPatches(String host, int port, String nodeId, long nodeVersion) throws MessagingException, IOException { List<Patch> patches = new LinkedList<>(); StringBuilder sb = new StringBuilder(); sb.append("/patch/"); sb.append(nodeId);//from ww w . j a v a2s. co m sb.append("/"); sb.append(nodeVersion); String url = sb.toString(); final ClientConfig config = new DefaultClientConfig(); config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); final Client client = Client.create(config); final WebResource resource = client.resource(url); final MimeMultipart response = resource.get(MimeMultipart.class); // This will iterate the individual parts of the multipart response for (int i = 0; i < response.getCount(); i++) { final BodyPart part = response.getBodyPart(i); System.out.printf("Embedded Body Part [Mime Type: %s, Length: %s]\n", part.getContentType(), part.getSize()); InputStream in = part.getInputStream(); // Patch patch = new Patch(); // patches.add(patch); } return patches; }
From source file:org.mule.module.http.internal.HttpParser.java
public static Collection<HttpPart> parseMultipartContent(InputStream content, String contentType) throws IOException { MimeMultipart mimeMultipart = null;//from w w w .java 2s .c o m List<HttpPart> parts = Lists.newArrayList(); try { mimeMultipart = new MimeMultipart(new ByteArrayDataSource(content, contentType)); } catch (MessagingException e) { throw new IOException(e); } try { int partCount = mimeMultipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart part = mimeMultipart.getBodyPart(i); String filename = part.getFileName(); String partName = filename; String[] contentDispositions = part.getHeader(CONTENT_DISPOSITION_PART_HEADER); if (contentDispositions != null) { String contentDisposition = contentDispositions[0]; if (contentDisposition.contains(NAME_ATTRIBUTE)) { partName = contentDisposition.substring( contentDisposition.indexOf(NAME_ATTRIBUTE) + NAME_ATTRIBUTE.length() + 2); partName = partName.substring(0, partName.indexOf("\"")); } } HttpPart httpPart = new HttpPart(partName, filename, IOUtils.toByteArray(part.getInputStream()), part.getContentType(), part.getSize()); Enumeration<Header> headers = part.getAllHeaders(); while (headers.hasMoreElements()) { Header header = headers.nextElement(); httpPart.addHeader(header.getName(), header.getValue()); } parts.add(httpPart); } } catch (MessagingException e) { throw new IOException(e); } return parts; }
From source file:org.mule.service.http.impl.service.server.grizzly.HttpParser.java
public static Collection<HttpPart> parseMultipartContent(InputStream content, String contentType) throws IOException { MimeMultipart mimeMultipart = null;/* w ww . ja v a2 s . c o m*/ List<HttpPart> parts = Lists.newArrayList(); try { mimeMultipart = new MimeMultipart(new ByteArrayDataSource(content, contentType)); } catch (MessagingException e) { throw new IOException(e); } try { int partCount = mimeMultipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart part = mimeMultipart.getBodyPart(i); String filename = part.getFileName(); String partName = filename; String[] contentDispositions = part.getHeader(CONTENT_DISPOSITION); if (contentDispositions != null) { String contentDisposition = contentDispositions[0]; if (contentDisposition.contains(NAME_ATTRIBUTE)) { partName = contentDisposition.substring( contentDisposition.indexOf(NAME_ATTRIBUTE) + NAME_ATTRIBUTE.length() + 2); partName = partName.substring(0, partName.indexOf("\"")); } } if (partName == null && mimeMultipart.getContentType().contains(MULTIPART_RELATED.toString())) { String[] contentIdHeader = part.getHeader(CONTENT_ID); if (contentIdHeader != null && contentIdHeader.length > 0) { partName = contentIdHeader[0]; } } HttpPart httpPart = new HttpPart(partName, filename, IOUtils.toByteArray(part.getInputStream()), part.getContentType(), part.getSize()); Enumeration<Header> headers = part.getAllHeaders(); while (headers.hasMoreElements()) { Header header = headers.nextElement(); httpPart.addHeader(header.getName(), header.getValue()); } parts.add(httpPart); } } catch (MessagingException e) { throw new IOException(e); } return parts; }
From source file:org.sakaiproject.nakamura.smtp.SakaiSmtpServer.java
private boolean isTextType(BodyPart part) throws MessagingException { return part.getSize() < MAX_PROPERTY_SIZE && part.getContentType().toLowerCase().startsWith("text/"); }