List of usage examples for javax.mail.internet ContentType getParameter
public String getParameter(String name)
From source file:com.adaptris.core.mail.attachment.MimeMailCreator.java
private String getAttachmentFileName(MimeBodyPart p) throws Exception { String filename = null;/*from w w w . j a v a 2s . c o m*/ String[] hdr = p.getHeader("Content-Disposition"); if (hdr != null) { ContentDisposition cd = new ContentDisposition(hdr[0]); filename = cd.getParameter("filename"); } if (filename == null) { hdr = p.getHeader("Content-Type"); if (hdr != null) { ContentType ct = new ContentType(hdr[0]); filename = ct.getParameter("name"); } } if (filename == null) { filename = idGenerator.create(p); logR.warn("Could not determine filename for MimeBodyPart, assigning unique filename of {}", filename); } return filename; }
From source file:org.silverpeas.core.mail.extractor.EMLExtractor.java
private String getFileName(Part part) throws MessagingException { String fileName = part.getFileName(); if (!StringUtil.isDefined(fileName)) { try {/* ww w. j av a 2 s . c o m*/ ContentType type = new ContentType(part.getContentType()); fileName = type.getParameter("name"); } catch (ParseException e) { SilverLogger.getLogger(this).error(e); } } if (StringUtil.isDefined(fileName) && fileName.startsWith("=?") && fileName.endsWith("?=")) { try { fileName = MimeUtility.decodeText(part.getFileName()); } catch (UnsupportedEncodingException e) { SilverLogger.getLogger(this).error(e); } } return fileName; }
From source file:org.silverpeas.components.mailinglist.service.job.MailProcessor.java
protected String getFileName(Part part) throws MessagingException { String fileName = part.getFileName(); if (fileName == null) { try {//from w w w . j a v a 2s.c o m ContentType type = new ContentType(part.getContentType()); fileName = type.getParameter("name"); } catch (ParseException e) { logger.error(e.getMessage(), e); } } return fileName; }
From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java
protected String getFileName(Part part) throws MessagingException { String fileName = part.getFileName(); if (fileName == null) { try {/* w ww .jav a 2 s . com*/ ContentType type = new ContentType(part.getContentType()); fileName = type.getParameter("name"); } catch (ParseException e) { e.printStackTrace(); } } return fileName; }
From source file:com.adaptris.core.http.client.net.StandardHttpProducer.java
private String getContentEncoding(HttpURLConnection http) { if (http.getContentEncoding() != null) { return http.getContentEncoding(); }/*from www. j av a 2s .com*/ // Parse Content-Type header for encoding try { ContentType contentType = new ContentType(http.getContentType()); if (!isEmpty(contentType.getParameter(PARAM_CHARSET))) { return contentType.getParameter(PARAM_CHARSET); } } catch (ParseException e) { log.trace("Unable to parse Content-Type header \"{}\": {}", http.getContentType(), e.toString()); } return null; }
From source file:org.trancecode.xproc.step.UnEscapeMarkupStepProcessor.java
@Override protected void execute(final StepInput input, final StepOutput output) { final XdmNode sourceDocument = input.readNode(XProcPorts.SOURCE); final String namespaceOption = input.getOptionValue(XProcOptions.NAMESPACE, null); // Resolve namespace Steps.getUri(namespaceOption);/*from w w w. j a v a 2s. c om*/ final String contentTypeOption = input.getOptionValue(XProcOptions.CONTENT_TYPE, MediaTypes.MEDIA_XML); final String encodingOption = input.getOptionValue(XProcOptions.ENCODING, null); if (encodingOption != null && !Steps.ENCODING_BASE64.equals(encodingOption)) { throw XProcExceptions.xc0052(SaxonLocation.of(sourceDocument)); } final ContentType contentType = getContentType(contentTypeOption, input.getStep()); final String charsetOption = input.getOptionValue(XProcOptions.CHARSET, null); final String charset = (charsetOption == null) ? contentType.getParameter("charset") : charsetOption; if (Steps.ENCODING_BASE64.equals(encodingOption)) { if (charset == null) { throw XProcExceptions.xc0010(input.getStep().getNode()); } else { final SaxonBuilder builder = new SaxonBuilder( input.getPipelineContext().getProcessor().getUnderlyingConfiguration()); builder.startDocument(); final Iterable<XdmNode> childNodes = SaxonAxis.childElements(sourceDocument); for (final XdmNode aNode : childNodes) { if (XdmNodeKind.ELEMENT.equals(aNode.getNodeKind())) { final String unEscapeContent; try { unEscapeContent = getUnEscapeContent(aNode.getStringValue(), encodingOption, contentType, charset); } catch (IOException e) { throw XProcExceptions.xc0010(aNode); } builder.startElement(aNode.getNodeName(), aNode); for (final XdmNode attribute : SaxonAxis.attributes(aNode)) { LOG.trace("copy existing attribute: {}", attribute); builder.attribute(attribute.getNodeName(), attribute.getStringValue()); } if (MediaTypes.MEDIA_TYPE_HTML.equals(contentType.getBaseType())) { writeHtmlNodes(unEscapeContent, namespaceOption, input.getPipelineContext().getProcessor(), builder); } else { writeXmlNodes(unEscapeContent, namespaceOption, input.getPipelineContext().getProcessor(), builder); } builder.endElement(); } else { builder.nodes(aNode); } } builder.endDocument(); output.writeNodes(XProcPorts.RESULT, builder.getNode()); } } else { final SaxonProcessorDelegate escapeDelegate = new CopyingSaxonProcessorDelegate() { @Override public EnumSet<NextSteps> startElement(final XdmNode node, final SaxonBuilder builder) { builder.startElement(node.getNodeName(), node); return EnumSet.of(NextSteps.PROCESS_ATTRIBUTES, NextSteps.PROCESS_CHILDREN, NextSteps.START_CONTENT); } @Override public void text(final XdmNode node, final SaxonBuilder builder) { final String unEscapeContent; try { unEscapeContent = getUnEscapeContent(node.getStringValue(), encodingOption, contentType, charset); } catch (IOException e) { throw XProcExceptions.xc0010(node); } if (MediaTypes.MEDIA_TYPE_HTML.equals(contentType.getBaseType())) { writeHtmlNodes(unEscapeContent, namespaceOption, input.getPipelineContext().getProcessor(), builder); } else { writeXmlNodes(unEscapeContent, namespaceOption, input.getPipelineContext().getProcessor(), builder); } } }; final SaxonProcessor escapeProcessor = new SaxonProcessor(input.getPipelineContext().getProcessor(), escapeDelegate); final XdmNode result = escapeProcessor.apply(sourceDocument); output.writeNodes(XProcPorts.RESULT, result); } }
From source file:com.predic8.membrane.core.multipart.XOPReconstitutor.java
/** * @return reassembled SOAP message or null if message is not SOAP or not multipart *//* w ww . j ava 2 s .co m*/ public Message getReconstitutedMessage(Message message) throws ParseException, MalformedStreamException, IOException, EndOfStreamException, XMLStreamException, FactoryConfigurationError { ContentType contentType = message.getHeader().getContentTypeObject(); if (contentType == null || contentType.getPrimaryType() == null) return null; if (!contentType.getPrimaryType().equals("multipart") || !contentType.getSubType().equals("related")) return null; String type = contentType.getParameter("type"); if (!"application/xop+xml".equals(type)) return null; String start = contentType.getParameter("start"); if (start == null) return null; String boundary = contentType.getParameter("boundary"); if (boundary == null) return null; HashMap<String, Part> parts = split(message, boundary); Part startPart = parts.get(start); if (startPart == null) return null; ContentType innerContentType = new ContentType(startPart.getHeader().getContentType()); if (!innerContentType.getPrimaryType().equals("application") || !innerContentType.getSubType().equals("xop+xml")) return null; byte[] body = fillInXOPParts(startPart.getInputStream(), parts); Message m = new Message() { @Override protected void parseStartLine(InputStream in) throws IOException, EndOfStreamException { throw new RuntimeException("not implemented."); } @Override public String getStartLine() { throw new RuntimeException("not implemented."); } }; m.setBodyContent(body); String reconstitutedContentType = innerContentType.getParameter("type"); if (reconstitutedContentType != null) m.getHeader().add(Header.CONTENT_TYPE, reconstitutedContentType); return m; }
From source file:org.trancecode.xproc.step.HttpResponseHandler.java
private Iterable<XdmNode> constructMultipart(final ContentType contentMimeType, final String contentType, final BodypartResponseParser parser) throws IOException { final SaxonBuilder builder = new SaxonBuilder(processor.getUnderlyingConfiguration()); builder.startDocument();//ww w .j av a2 s . com builder.startElement(XProcXmlModel.Elements.MULTIPART); final String boundary = contentMimeType.getParameter("boundary"); builder.attribute(XProcXmlModel.Attributes.BOUNDARY, boundary); parser.setBoundary(boundary); builder.attribute(XProcXmlModel.Attributes.CONTENT_TYPE, contentType); final List<BodypartResponseParser.BodypartEntity> parts = parser.parseMultipart(); for (final BodypartResponseParser.BodypartEntity part : parts) { final String mimeType = part.getHeaderGroup().getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue(); final ContentType bodyCt = Steps.getContentType(mimeType, null); if (bodyCt.getSubType().contains("xml")) { builder.startElement(XProcXmlModel.Elements.BODY); builder.attribute(XProcXmlModel.Attributes.CONTENT_TYPE, mimeType); } final Iterable<XdmNode> body = constructBody(bodyCt, mimeType, part); if (body != null) { builder.nodes(body); } if (bodyCt.getSubType().contains("xml")) { builder.endElement(); } } builder.endDocument(); return ImmutableList.of(builder.getNode()); }
From source file:org.trancecode.xproc.step.RequestParser.java
private StringEntity parseBody(final XdmNode node, final Processor processor) { final XdmNode body = SaxonAxis.childElement(node, XProcXmlModel.Elements.BODY); if (body != null) { final String contentTypeAtt = body.getAttributeValue(XProcXmlModel.Attributes.CONTENT_TYPE); final String encoding = body.getAttributeValue(XProcXmlModel.Attributes.ENCODING); final ContentType contentType = Steps.getContentType(contentTypeAtt, body); final String contentString = getContentString(body, contentType, encoding, processor); try {//from w w w . j a v a 2 s.com return new StringEntity(contentString, contentType.toString(), Steps.getCharset(contentType.getParameter("charset")).toString()); } catch (final UnsupportedEncodingException e) { throw XProcExceptions.xc0020(body); } } return null; }
From source file:com.grameenfoundation.ictc.controllers.SaleforceIntegrationController.java
public static InputSource getInputSource(String ctype, InputStream in) throws Exception { // Creates ContentType ContentType contentType = null; try {//ww w . j a va 2 s . co m contentType = new ContentType(ctype) { }; } catch (Exception e) { System.out.println( "Unexpected Error occured while creating content-type object. Reason: " + e.getMessage()); throw new Exception(e.getMessage()); } // Checks primitive type String primaryType = contentType.getPrimaryType(); if (!"text".equals(primaryType) && !"application".equals(primaryType)) { System.out.println("Primary type received is " + primaryType + ". Only text or application primary type is expected"); throw new Exception(ctype); } // Checks sub type String subType = contentType.getSubType(); if (!"xml".equals(subType) && !subType.endsWith("+xml")) { System.out.println("sub type received is " + subType + ". Only xml sub type is expected"); throw new Exception(ctype); } // Gets charset parameter String charset = contentType.getParameter("charset"); if (charset == null) { // no charset // MIME type "text/*" omitted charset should be treated // as us-ascii if ("text".equals(contentType.getPrimaryType())) { charset = "us-ascii"; } } InputSource input; if (charset == null) { // application/xml omitted charset input = new InputSource(in); } else { // Creats a reader with java charset Reader reader = null; try { reader = new InputStreamReader(in, charset); } catch (UnsupportedEncodingException e) { System.out.println("UnsupportedEncodingException. Reason: " + e.getMessage()); throw new Exception(e.getMessage()); } input = new InputSource(reader); } return input; }