Example usage for javax.activation DataHandler getContentType

List of usage examples for javax.activation DataHandler getContentType

Introduction

In this page you can find the example usage for javax.activation DataHandler getContentType.

Prototype

public String getContentType() 

Source Link

Document

Return the MIME type of this object as retrieved from the source object.

Usage

From source file:org.apache.axiom.om.impl.MIMEOutputUtils.java

/**
 * @deprecated This method is only useful in conjunction with
 *             {@link #writeBodyPart(OutputStream, MimeBodyPart, String)}, which is deprecated.
 *//*from ww w.j av  a2  s.  c  o  m*/
public static MimeBodyPart createMimeBodyPart(String contentID, DataHandler dataHandler,
        OMOutputFormat omOutputFormat) throws MessagingException {
    String contentType = dataHandler.getContentType();

    // Get the content-transfer-encoding
    String contentTransferEncoding = "binary";
    if (dataHandler instanceof ConfigurableDataHandler) {
        ConfigurableDataHandler configurableDataHandler = (ConfigurableDataHandler) dataHandler;
        contentTransferEncoding = configurableDataHandler.getTransferEncoding();
    }

    if (isDebugEnabled) {
        log.debug("Create MimeBodyPart");
        log.debug("  Content-ID = " + contentID);
        log.debug("  Content-Type = " + contentType);
        log.debug("  Content-Transfer-Encoding = " + contentTransferEncoding);
    }

    boolean useCTEBase64 = omOutputFormat != null && Boolean.TRUE
            .equals(omOutputFormat.getProperty(OMOutputFormat.USE_CTE_BASE64_FOR_NON_TEXTUAL_ATTACHMENTS));
    if (useCTEBase64) {
        if (!CommonUtils.isTextualPart(contentType) && "binary".equals(contentTransferEncoding)) {
            if (isDebugEnabled) {
                log.debug(
                        " changing Content-Transfer-Encoding from " + contentTransferEncoding + " to base-64");
            }
            contentTransferEncoding = "base64";
        }

    }

    // Now create the mimeBodyPart for the datahandler and add the appropriate content headers
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setDataHandler(dataHandler);
    mimeBodyPart.addHeader("Content-ID", "<" + contentID + ">");
    mimeBodyPart.addHeader("Content-Type", contentType);
    mimeBodyPart.addHeader("Content-Transfer-Encoding", contentTransferEncoding);
    return mimeBodyPart;
}

From source file:org.apache.axiom.om.impl.MIMEOutputUtils.java

/**
 * @deprecated Use {@link OMMultipartWriter} instead.
 *///from ww  w.j  a  v a  2 s.  com
public static void writeDataHandlerWithAttachmentsMessage(DataHandler rootDataHandler, final String contentType,
        OutputStream outputStream, Map attachments, OMOutputFormat format, Collection ids) {
    try {
        if (!rootDataHandler.getContentType().equals(contentType)) {
            rootDataHandler = new DataHandlerWrapper(rootDataHandler) {
                public String getContentType() {
                    return contentType;
                }
            };
        }

        OMMultipartWriter mpw = new OMMultipartWriter(outputStream, format);

        mpw.writePart(rootDataHandler, format.getRootContentId());

        Iterator idIterator = null;
        if (ids == null) {
            // If ids are not provided, use the attachment map
            // to get the keys
            idIterator = attachments.keySet().iterator();
        } else {
            // if ids are provided (normal case), iterate
            // over the ids so that the attachments are 
            // written in the same order as the id keys.
            idIterator = ids.iterator();
        }

        while (idIterator.hasNext()) {
            String key = (String) idIterator.next();
            mpw.writePart((DataHandler) attachments.get(key), key);
        }
        mpw.complete();
        outputStream.flush();
    } catch (IOException e) {
        throw new OMException("Error while writing to the OutputStream.", e);
    }
}

From source file:com.aimluck.eip.wiki.util.WikiFileUtils.java

public static List<FileuploadBean> getAttachmentFiles(Integer wikiId) {
    SelectQuery<EipTWikiFile> query = Database.query(EipTWikiFile.class);
    query.where(Operations.eq(EipTWikiFile.WIKI_ID_PROPERTY, wikiId));
    query.orderAscending(EipTWikiFile.UPDATE_DATE_PROPERTY);
    query.orderAscending(EipTWikiFile.FILE_PATH_PROPERTY);
    List<EipTWikiFile> result = query.fetchList();

    List<FileuploadBean> beanlist = new ArrayList<FileuploadBean>();
    for (EipTWikiFile file : result) {
        FileuploadBean bean = new FileuploadBean();
        bean.initField();/*from  w ww  .  java 2  s .c o m*/
        bean.setFileId(file.getFileId());
        bean.setFileName(file.getFileName());
        bean.setFlagNewFile(false);
        javax.activation.DataHandler hData = new javax.activation.DataHandler(
                new javax.activation.FileDataSource(file.getFileName()));
        if (hData != null) {
            bean.setContentType(hData.getContentType());
        }
        bean.setIsImage(FileuploadUtils.isImage(file.getFileName()));
        beanlist.add(bean);
    }
    return beanlist;
}

From source file:org.apache.axis.attachments.MimeUtils.java

/**
 * This routine will create a multipart object from the parts and the SOAP content.
 * @param env should be the text for the main root part.
 * @param parts contain a collection of the message parts.
 *
 * @return a new MimeMultipart object/*from   w  ww.  j  a v  a  2s . com*/
 *
 * @throws org.apache.axis.AxisFault
 */
public static javax.mail.internet.MimeMultipart createMP(String env, java.util.Collection parts, int sendType)
        throws org.apache.axis.AxisFault {

    javax.mail.internet.MimeMultipart multipart = null;

    try {
        String rootCID = SessionUtils.generateSessionId();

        if (sendType == Attachments.SEND_TYPE_MTOM) {
            multipart = new javax.mail.internet.MimeMultipart("related;type=\"application/xop+xml\"; start=\"<"
                    + rootCID + ">\"; start-info=\"text/xml; charset=utf-8\"");
        } else {
            multipart = new javax.mail.internet.MimeMultipart(
                    "related; type=\"text/xml\"; start=\"<" + rootCID + ">\"");
        }

        javax.mail.internet.MimeBodyPart messageBodyPart = new javax.mail.internet.MimeBodyPart();

        messageBodyPart.setText(env, "UTF-8");
        if (sendType == Attachments.SEND_TYPE_MTOM) {
            messageBodyPart.setHeader("Content-Type",
                    "application/xop+xml; charset=utf-8; type=\"text/xml; charset=utf-8\"");
        } else {
            messageBodyPart.setHeader("Content-Type", "text/xml; charset=UTF-8");
        }
        messageBodyPart.setHeader("Content-Id", "<" + rootCID + ">");
        messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
        multipart.addBodyPart(messageBodyPart);

        for (java.util.Iterator it = parts.iterator(); it.hasNext();) {
            org.apache.axis.Part part = (org.apache.axis.Part) it.next();
            javax.activation.DataHandler dh = org.apache.axis.attachments.AttachmentUtils
                    .getActivationDataHandler(part);
            String contentID = part.getContentId();

            messageBodyPart = new javax.mail.internet.MimeBodyPart();

            messageBodyPart.setDataHandler(dh);

            String contentType = part.getContentType();
            if ((contentType == null) || (contentType.trim().length() == 0)) {
                contentType = dh.getContentType();
            }
            if ((contentType == null) || (contentType.trim().length() == 0)) {
                contentType = "application/octet-stream";
            }

            messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, contentType);
            messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_ID, "<" + contentID + ">");
            messageBodyPart.setHeader(HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING, "binary"); // Safe and fastest for anything other than mail;

            for (java.util.Iterator i = part.getNonMatchingMimeHeaders(
                    new String[] { HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.HEADER_CONTENT_ID,
                            HTTPConstants.HEADER_CONTENT_TRANSFER_ENCODING }); i.hasNext();) {
                javax.xml.soap.MimeHeader header = (javax.xml.soap.MimeHeader) i.next();

                messageBodyPart.setHeader(header.getName(), header.getValue());
            }

            multipart.addBodyPart(messageBodyPart);
        }
    } catch (javax.mail.MessagingException e) {
        log.error(Messages.getMessage("javaxMailMessagingException00"), e);
    }

    return multipart;
}

From source file:fr.paris.lutece.portal.service.mail.MailUtil.java

/**
 * This Method convert a UrlAttachmentDataSource to a ByteArrayDataSource
 * and used MailAttachmentCacheService for caching resource.
 * @param urlAttachement {@link UrlAttachment}
 * @return a {@link ByteArrayDataSource}
 *//*  w  w w .j  a  v a 2  s  .co  m*/
private static ByteArrayDataSource convertUrlAttachmentDataSourceToByteArrayDataSource(
        UrlAttachment urlAttachement) {
    String strKey = MailAttachmentCacheService.getInstance().getKey(urlAttachement.getUrlData().toString());
    ByteArrayDataSource urlAttachmentDataSource = null;

    if (!MailAttachmentCacheService.getInstance().isCacheEnable()
            || (MailAttachmentCacheService.getInstance().getFromCache(strKey) == null)) {
        DataHandler handler = new DataHandler(urlAttachement.getUrlData());
        ByteArrayOutputStream bo = null;
        InputStream input = null;
        String strType = null;

        try {
            Object o = handler.getContent();
            strType = handler.getContentType();

            if (o != null) {
                if (o instanceof InputStream) {
                    input = (InputStream) o;
                    bo = new ByteArrayOutputStream();

                    int read;
                    byte[] tab = new byte[CONSTANTE_FILE_ATTACHMET_BUFFER];

                    do {
                        read = input.read(tab);

                        if (read > 0) {
                            bo.write(tab, 0, read);
                        }
                    } while (read > 0);
                }
            }
        } catch (IOException e) {
            // Document is ignored
            AppLogService.info(urlAttachement.getContentLocation() + MSG_ATTACHMENT_NOT_FOUND);
        } finally {
            //closed inputstream and outputstream 
            try {
                if (input != null) {
                    input.close();
                }

                if (bo != null) {
                    bo.close();
                    urlAttachmentDataSource = new ByteArrayDataSource(bo.toByteArray(), strType);
                }
            } catch (IOException e) {
                AppLogService.error(e);
            }
        }

        if (MailAttachmentCacheService.getInstance().isCacheEnable()) {
            //add resource in cache
            MailAttachmentCacheService.getInstance().putInCache(strKey, urlAttachmentDataSource);
        }
    } else {
        //used the resource store in cache
        urlAttachmentDataSource = (ByteArrayDataSource) MailAttachmentCacheService.getInstance()
                .getFromCache(strKey);
    }

    return urlAttachmentDataSource;
}

From source file:org.openehealth.ipf.commons.ihe.ws.cxf.NonReadingAttachmentMarshaller.java

@Override
public String addSwaRefAttachment(DataHandler data) {
    return attachmentDescription(data.getName(), null, data.getContentType());
}

From source file:org.mule.module.http.functional.requester.HttpRequestInboundAttachmentsTestCase.java

private void assertAttachment(MuleMessage message, String attachmentName, String attachmentContents,
        String contentType) throws IOException {
    assertTrue(message.getInboundAttachmentNames().contains(attachmentName));

    DataHandler handler = message.getInboundAttachment(attachmentName);
    assertThat(handler.getContentType(), equalTo(contentType));

    assertThat(IOUtils.toString(handler.getInputStream()), equalTo(attachmentContents));

}

From source file:org.openehealth.ipf.commons.ihe.ws.cxf.NonReadingAttachmentMarshaller.java

@Override
public String addMtomAttachment(DataHandler data, String elementNamespace, String elementLocalName) {
    return attachmentDescription(data.getName(), null, data.getContentType());
}

From source file:org.wso2.carbon.attachment.mgt.ui.fileupload.AttachmentUploadClient.java

public void addUploadedFileItem(FileItemData fileItemData) throws AttachmentMgtException, RemoteException {
    DataHandler handler = fileItemData.getDataHandler();
    TAttachment attachment = new TAttachment();
    attachment.setName(handler.getName());
    attachment.setContentType(handler.getContentType());

    attachment.setCreatedBy("DummyUser"); //TODO: Remove this hard-coded value
    attachment.setContent(fileItemData.getDataHandler());
    String attachmentID = stub.add(attachment);
    log.info("Attachment was uploaded with id:" + attachmentID);
}

From source file:org.wso2.carbon.humantask.ui.fileupload.AttachmentUploadClient.java

/**
 * Upload the attachment and return the attachment id
 * @param fileItemData wrapper for the attachment
 * @return attachment id for the uploaded attachment
 * @throws AttachmentMgtException If an error occurred in the back-end component
 * @throws RemoteException if an error during the communication
 *//*  w  ww .j a  v a  2  s.  c om*/
public String addUploadedFileItem(FileItemData fileItemData)
        throws AttachmentMgtException, RemoteException, ExceptionException {
    DataHandler handler = fileItemData.getDataHandler();
    TAttachment attachment = new TAttachment();
    attachment.setName(handler.getName());
    attachment.setContentType(handler.getContentType());
    attachment.setCreatedBy(getUserName());

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    attachment.setCreatedTime(calendar);
    attachment.setContent(handler);
    String attachmentID = stub.add(attachment);
    log.info("Attachment was uploaded with id:" + attachmentID);
    return attachmentID;
}