List of usage examples for javax.mail.internet ContentType ContentType
public ContentType(String s) throws ParseException
From source file:com.cubusmail.mail.text.MessageTextUtil.java
/** * Reads the string out of part's input stream. On first try the input * stream retrieved by <code>javax.mail.Part.getInputStream()</code> is * used. If an I/O error occurs (<code>java.io.IOException</code>) then the * next try is with part's raw input stream. If everything fails an empty * string is returned./* ww w . ja va 2 s . c om*/ * * @param p * - the <code>javax.mail.Part</code> object * @param ct * - the part's content type * @return the string read from part's input stream or the empty string "" * if everything failed * @throws MessagingException * - if an error occurs in part's getter methods */ public static String readPart(final Part p) throws MessagingException { String contentType = p.getContentType(); ContentType type = new ContentType(contentType); /* * Use specified charset if available else use default one */ String charset = type.getParameter("charset"); if (null == charset || charset.equalsIgnoreCase(CubusConstants.US_ASCII)) { charset = CubusConstants.DEFAULT_CHARSET; } try { return readStream(p.getInputStream(), charset); } catch (final IOException e) { /* * Try to get data from raw input stream */ final InputStream inStream; if (p instanceof MimeBodyPart) { final MimeBodyPart mpb = (MimeBodyPart) p; inStream = mpb.getRawInputStream(); } else if (p instanceof MimeMessage) { final MimeMessage mm = (MimeMessage) p; inStream = mm.getRawInputStream(); } else { inStream = null; } if (inStream == null) { /* * Neither a MimeBodyPart nor a MimeMessage */ return ""; } try { return readStream(inStream, charset); } catch (final IOException e1) { log.error(e1.getLocalizedMessage(), e1); return e1.getLocalizedMessage(); // return STR_EMPTY; } finally { try { inStream.close(); } catch (final IOException e1) { log.error(e1.getLocalizedMessage(), e1); } } } }
From source file:com.example.app.support.AppUtil.java
@NotNull private static String _getExtensionWithFallback(String fileName, String contentType) { String ext = StringFactory.getExtension(fileName); if (ext.isEmpty() && !ContentTypes.Application.octet_stream.toString().equals(contentType)) { try {/*from ww w. j a v a 2 s. com*/ ext = new ContentType(contentType).getSubType().toLowerCase(); switch (ext) { case "jpeg": ext = "jpg"; break; case "tiff": ext = "tif"; break; case "svg+xml": ext = "svg"; break; case "x-portable-anymap": ext = "pnm"; break; case "x-portable-bitmap": ext = "pbm"; break; case "x-portable-graymap": ext = "pgm"; break; case "x-portable-pixmap": ext = "ppm"; break; default: break; } } catch (ParseException e) { _logger.error("Unable to parse content type: " + contentType, e); } } return ext; }
From source file:org.trancecode.xproc.step.RequestParser.java
private FormBodyPart getContentBody(final XdmNode node, final Processor processor) { final String contentTypeAtt = node.getAttributeValue(XProcXmlModel.Attributes.CONTENT_TYPE); final String encoding = node.getAttributeValue(XProcXmlModel.Attributes.ENCODING); final ContentType contentType = Steps.getContentType(contentTypeAtt, node); final String contentString = getContentString(node, contentType, encoding, processor); final StringBody body; try {//from w w w . ja v a2 s.c o m body = new StringBody(contentString, contentType.toString(), Steps.getCharset(contentType.getParameter("charset"))); } catch (final UnsupportedEncodingException e) { throw XProcExceptions.xc0020(node); } final String id = node.getAttributeValue(XProcXmlModel.Attributes.ID); final String description = node.getAttributeValue(XProcXmlModel.Attributes.DESCRIPTION); final String disposition = node.getAttributeValue(XProcXmlModel.Attributes.DISPOSITION); final FormBodyPart bodyPart = new FormBodyPart("body", body) { @Override protected void generateContentDisp(final ContentBody body) { if (disposition != null) { addField(MIME.CONTENT_DISPOSITION, disposition); } } @Override protected void generateTransferEncoding(final ContentBody body) { if (encoding != null) { addField(MIME.CONTENT_TRANSFER_ENC, encoding); } } @Override protected void generateContentType(final ContentBody body) { final StringBuilder buffer = new StringBuilder(); buffer.append(body.getMimeType()); if (body.getCharset() != null) { try { final String testCharset = new ContentType(body.getMimeType()).getParameter("charset"); if (testCharset != null) { final Charset charset = Charset.forName(testCharset); if (!StringUtils.equalsIgnoreCase(charset.displayName(), body.getCharset())) { buffer.append("; charset=").append(body.getCharset().toLowerCase()); } } else { buffer.append("; charset=utf-8"); } } catch (final ParseException | IllegalCharsetNameException e) { throw XProcExceptions.xc0020(node); } } addField(MIME.CONTENT_TYPE, buffer.toString()); } }; if (id != null) { bodyPart.addField("Content-ID", id); } if (description != null) { bodyPart.addField("Content-Description", description); } return bodyPart; }
From source file:immf.SendMailBridge.java
private void parseBodypart(SenderMail sendMail, BodyPart bp, String subtype, String parentSubtype) throws IOException { boolean limiterr = false; String badfile = null;//from w ww . j av a 2 s . co m try { String contentType = bp.getContentType().toLowerCase(); log.info("Bodypart ContentType:" + contentType); log.info("subtype:" + subtype); if (contentType.startsWith("multipart/")) { parseMultipart(sendMail, (Multipart) bp.getContent(), getSubtype(contentType), subtype); } else if (sendMail.getPlainTextContent() == null && contentType.startsWith("text/plain")) { // ???plain/text? String content = (String) bp.getContent(); log.info("set Content text [" + content + "]"); String charset = (new ContentType(contentType)).getParameter("charset"); content = this.charConv.convert(content, charset); log.debug(" conv " + content); sendMail.setPlainTextContent(content); } else if (sendMail.getHtmlContent() == null && contentType.startsWith("text/html") && (subtype.equalsIgnoreCase("alternative") || subtype.equalsIgnoreCase("related"))) { String content = (String) bp.getContent(); log.info("set Content html [" + content + "]"); String charset = (new ContentType(contentType)).getParameter("charset"); content = this.charConv.convert(content, charset); log.debug(" conv " + content); // html???? sendMail.setHtmlContent(content); } else if (contentType.startsWith("text/html")) { // subtype?mixed?? String content = (String) bp.getContent(); if (sendMail.getHtmlContent() == null) { // text/plain???????? text/html????????? log.info("set Content html [" + content + "]"); String charset = (new ContentType(contentType)).getParameter("charset"); content = this.charConv.convert(content, charset); log.debug(" conv " + content); // html???? sendMail.setHtmlContent(content); } else { log.info("Discarding duplicate content [" + content + "]"); } } else { log.debug("attach"); // ???? if (subtype.equalsIgnoreCase("related")) { // SenderAttachment file = new SenderAttachment(); String fname = uniqId(); String fname2 = Util.getFileName(bp); // iPhone?gifpng?????????????ContentType(?png???gif?) if (getSubtype(contentType).equalsIgnoreCase("png")) { file.setContentType("image/gif"); fname = fname + ".gif"; fname2 = getBasename(fname2) + ".gif"; file.setData(inputstream2bytes(Util.png2gif(bp.getInputStream()))); } else { file.setContentType(contentType); fname = fname + "." + getSubtype(contentType); file.setData(inputstream2bytes(bp.getInputStream())); } file.setInline(true); boolean inline = !this.forcePlainText & sendMail.checkAttachmentCapability(file); if (!inline) { file.setInline(false); if (!sendMail.checkAttachmentCapability(file)) { limiterr = true; badfile = fname2; throw new Exception("Attachments: size limit or file count limit exceeds!"); } } if (inline) { file.setFilename(fname); file.setContentId(bp.getHeader("Content-Id")[0]); log.info("Inline Attachment " + file.loggingString() + ", Hash:" + file.getHash()); } else { file.setFilename(fname2); log.info("Attachment " + file.loggingString()); } sendMail.addAttachmentFileIdList(file); } else { // ? SenderAttachment file = new SenderAttachment(); file.setInline(false); file.setContentType(contentType); String fname = Util.getFileName(bp); if (getSubtype(contentType).equalsIgnoreCase("png")) { file.setContentType("image/gif"); file.setFilename(getBasename(fname) + ".gif"); file.setData(inputstream2bytes(Util.png2gif(bp.getInputStream()))); } else { file.setFilename(fname); file.setData(inputstream2bytes(bp.getInputStream())); } if (!sendMail.checkAttachmentCapability(file)) { limiterr = true; badfile = file.getFilename(); throw new Exception("Attachments: size limit or file count limit exceeds!"); } sendMail.addAttachmentFileIdList(file); log.info("Attachment " + file.loggingString()); } } } catch (Exception e) { log.error("parse bodypart error.", e); if (limiterr) { sendMail.addPlainTextContent("\n[(" + badfile + ")]"); if (sendMail.getHtmlContent() != null) { sendMail.addHtmlContent("[(" + badfile + ")]<br>"); } } else { throw new IOException("BodyPart error." + e.getMessage(), e); } } }
From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java
public Attach getAttach(Part part, String baseStorePath) throws MessagingException, IOException { MimeBodyPart mimePart = (MimeBodyPart) part; Attach attach = new Attach(); if (StringUtils.isEmpty(mimePart.getFileName())) { attach.setFileName("UNKNOWN"); } else {//from w ww . ja v a 2s.c om String fileName = mimePart.getFileName(); String encoded = System.getProperty("mail.mime.encodefilename"); if (Boolean.parseBoolean(encoded)) { fileName = MimeUtility.decodeText(fileName); } attach.setFileName(fileName); } ContentType type = new ContentType(mimePart.getContentType()); attach.setMimeType(type.getBaseType()); InputStream inputStream = mimePart.getDataHandler().getInputStream(); attach.buildMd5Checksum(inputStream); attach.buildPath(mimePart, baseStorePath); return attach; }
From source file:com.example.app.support.service.AppUtil.java
@NotNull private static String _getExtensionWithFallback(String fileName, String contentType) { String ext = StringFactory.getExtension(fileName); if (ext.isEmpty() && !Objects.equals(ContentTypes.Application.octet_stream.toString(), contentType)) { try {/* www. j a va 2 s . c o m*/ ext = new ContentType(contentType).getSubType().toLowerCase(); switch (ext) { case "jpeg": ext = "jpg"; break; case "tiff": ext = "tif"; break; case "svg+xml": ext = "svg"; break; case "x-portable-anymap": ext = "pnm"; break; case "x-portable-bitmap": ext = "pbm"; break; case "x-portable-graymap": ext = "pgm"; break; case "x-portable-pixmap": ext = "ppm"; break; default: break; } } catch (ParseException e) { _logger.error("Unable to parse content type: " + contentType, e); } } return ext; }
From source file:org.aksw.resparql.IMyHandler.java
@Override public boolean handle(HttpExchange x) throws Exception { Map<String, ContentType> accepts = MyHandler.getPreferredFormats(x.getRequestHeaders()); if (accepts.isEmpty()) return false; //Map.Entry<String, ContentType> type = accepts.entrySet().iterator().next(); RegexInvocationContainer ric = accepts.containsValue(new ContentType("text/html")) ? pageRIC : dataRIC; String targetURL = (String) ric.invoke(x.getRequestURI().toString()); if (targetURL == null) { return false; }//w ww. j av a2 s . c o m MyHandler.sendRedirect(x, targetURL); return true; }
From source file:com.example.app.support.service.AppUtil.java
/** * Get the text/html content type//from w w w. j av a 2 s.c o m * * @return content type */ public static ContentType getHtmlContentType() { try { return new ContentType("text/html"); } catch (ParseException e) { _logger.error("Unable to create html content type", e); throw new RuntimeException(e); } }