Example usage for javax.activation DataSource getName

List of usage examples for javax.activation DataSource getName

Introduction

In this page you can find the example usage for javax.activation DataSource getName.

Prototype

public String getName();

Source Link

Document

Return the name of this object where the name of the object is dependant on the nature of the underlying objects.

Usage

From source file:com.googlecode.psiprobe.tools.Mailer.java

private static MimeBodyPart createAttachmentPart(DataSource attachment) throws MessagingException {
    MimeBodyPart attachmentPart = new MimeBodyPart();
    attachmentPart.setDataHandler(new DataHandler(attachment));
    attachmentPart.setDisposition(MimeBodyPart.ATTACHMENT);
    attachmentPart.setFileName(attachment.getName());
    return attachmentPart;
}

From source file:gmailclientfx.core.GmailClient.java

public static void sendMessage(String to, String subject, String body, List<String> attachments)
        throws Exception {
    // authenticate with gmail smtp server
    SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com", 587, EMAIL, ACCESS_TOKEN, true);

    // kreiraj MimeMessage objekt
    MimeMessage msg = new MimeMessage(OAuth2Authenticator.getSession());

    // dodaj headere
    msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
    msg.addHeader("format", "flowed");
    msg.addHeader("Content-Transfer-Encoding", "8bit");

    msg.setFrom(new InternetAddress(EMAIL));
    msg.setRecipients(javax.mail.Message.RecipientType.CC, InternetAddress.parse(to));
    msg.setSubject(subject, "UTF-8");
    msg.setReplyTo(InternetAddress.parse(EMAIL, false));

    // tijelo poruke
    BodyPart msgBodyPart = new MimeBodyPart();
    msgBodyPart.setText(body);//from ww  w .java  2s.c o  m

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(msgBodyPart);
    msg.setContent(multipart);

    // dodaj privitke
    if (attachments.size() > 0) {
        for (String attachment : attachments) {
            msgBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(attachment);
            msgBodyPart.setDataHandler(new DataHandler(source));
            msgBodyPart.setFileName(source.getName());
            multipart.addBodyPart(msgBodyPart);
        }
        msg.setContent(multipart);
    }
    smtpTransport.sendMessage(msg, InternetAddress.parse(to));

    Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
    alert.setTitle("Poruka poslana!");
    alert.setHeaderText(null);
    alert.setContentText("Email uspjeno poslan!");
    alert.showAndWait();
}

From source file:com.jaspersoft.jasperserver.rest.RESTUtils.java

public static void sendFile(DataSource ds, HttpServletResponse response) {
    response.setContentType(ds.getContentType());

    if (ds.getName() != null && ds.getName().length() > 0) {
        response.addHeader("Content-Disposition", "attachment; filename=" + ds.getName());
    }/*from   w  w  w  . j  av a  2 s  .  co  m*/

    OutputStream outputStream = null;
    BufferedInputStream bufferedInputStream = null;
    try {
        outputStream = response.getOutputStream();

        bufferedInputStream = new BufferedInputStream(ds.getInputStream());
        int readBytes = 0;

        while ((readBytes = bufferedInputStream.read()) != -1) {
            outputStream.write(readBytes);
        }

        if (log.isDebugEnabled()) {
            log.debug("finished sending bytes");
        }

    } catch (IOException ex) {
        log.error("Error serving a file: " + ex.getMessage(), ex);
    } finally {
        if (outputStream != null)
            try {
                outputStream.close();
            } catch (Exception ex) {
            }
        if (bufferedInputStream != null)
            try {
                bufferedInputStream.close();
            } catch (Exception ex) {
            }
    }
}

From source file:com.jaeksoft.searchlib.parser.EmlParser.java

@Override
protected void parseContent(StreamLimiter streamLimiter, LanguageEnum lang)
        throws IOException, SearchLibException {
    Session session = Session.getDefaultInstance(JAVAMAIL_PROPS);
    try {/*from w ww  .java2 s .c  o m*/

        MimeMessage mimeMessage = new MimeMessage(session, streamLimiter.getNewInputStream());
        MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse();

        ParserResultItem result = getNewParserResultItem();
        String from = mimeMessageParser.getFrom();
        if (from != null)
            result.addField(ParserFieldEnum.email_display_from, from.toString());
        for (Address address : mimeMessageParser.getTo())
            result.addField(ParserFieldEnum.email_display_to, address.toString());
        for (Address address : mimeMessageParser.getCc())
            result.addField(ParserFieldEnum.email_display_cc, address.toString());
        for (Address address : mimeMessageParser.getBcc())
            result.addField(ParserFieldEnum.email_display_bcc, address.toString());
        result.addField(ParserFieldEnum.subject, mimeMessageParser.getSubject());
        result.addField(ParserFieldEnum.htmlSource, mimeMessageParser.getHtmlContent());
        result.addField(ParserFieldEnum.content, mimeMessageParser.getPlainContent());
        result.addField(ParserFieldEnum.email_sent_date, mimeMessage.getSentDate());
        result.addField(ParserFieldEnum.email_received_date, mimeMessage.getReceivedDate());

        for (DataSource dataSource : mimeMessageParser.getAttachmentList()) {
            result.addField(ParserFieldEnum.email_attachment_name, dataSource.getName());
            result.addField(ParserFieldEnum.email_attachment_type, dataSource.getContentType());
            if (parserSelector == null)
                continue;
            Parser attachParser = parserSelector.parseStream(getSourceDocument(), dataSource.getName(),
                    dataSource.getContentType(), null, dataSource.getInputStream(), null, null, null);
            if (attachParser == null)
                continue;
            List<ParserResultItem> parserResults = attachParser.getParserResults();
            if (parserResults != null)
                for (ParserResultItem parserResult : parserResults)
                    result.addField(ParserFieldEnum.email_attachment_content, parserResult);
        }
        if (StringUtils.isEmpty(mimeMessageParser.getHtmlContent()))
            result.langDetection(10000, ParserFieldEnum.content);
        else
            result.langDetection(10000, ParserFieldEnum.htmlSource);
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:com.lin.umws.service.impl.UserServiceImpl.java

public void handlerUpload(DataHandler handler) {
    DataSource dataSource = handler.getDataSource();
    System.out.println("Content-Type: " + dataSource.getContentType());
    System.out.println("Name: " + dataSource.getName());

    try {/*from ww  w.j  a  v a 2s. com*/
        IOUtils.copy(dataSource.getInputStream(), System.out);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.qwazr.library.email.EmlParser.java

@Override
public void parseContent(final MultivaluedMap<String, String> parameters, final InputStream inputStream,
        final String extension, final String mimeType, final ParserResultBuilder resultBuilder)
        throws Exception {
    final Session session = Session.getDefaultInstance(JAVAMAIL_PROPS);

    resultBuilder.metas().set(MIME_TYPE, findMimeType(extension, mimeType, this::findMimeTypeUsingDefault));

    final MimeMessage mimeMessage = new MimeMessage(session, inputStream);
    final MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse();

    ParserFieldsBuilder document = resultBuilder.newDocument();
    final String from = mimeMessageParser.getFrom();
    if (from != null)
        document.add(FROM, from);/*from  w w w . ja  va2  s  .co m*/
    for (Address address : mimeMessageParser.getTo())
        document.add(RECIPIENT_TO, address.toString());
    for (Address address : mimeMessageParser.getCc())
        document.add(RECIPIENT_CC, address.toString());
    for (Address address : mimeMessageParser.getBcc())
        document.add(RECIPIENT_BCC, address.toString());
    document.add(SUBJECT, mimeMessageParser.getSubject());
    document.add(HTML_CONTENT, mimeMessageParser.getHtmlContent());
    document.add(PLAIN_CONTENT, mimeMessageParser.getPlainContent());
    document.add(SENT_DATE, mimeMessage.getSentDate());
    document.add(RECEIVED_DATE, mimeMessage.getReceivedDate());

    for (DataSource dataSource : mimeMessageParser.getAttachmentList()) {
        document.add(ATTACHMENT_NAME, dataSource.getName());
        document.add(ATTACHMENT_TYPE, dataSource.getContentType());
        // TODO Extract content from attachmend
        // if (parserSelector != null) {
        // Parser attachParser = parserSelector.parseStream(
        // getSourceDocument(), dataSource.getName(),
        // dataSource.getContentType(), null,
        // dataSource.getInputStream(), null, null, null);
        // if (attachParser != null) {
        // List<ParserResultItem> parserResults = attachParser
        // .getParserResults();
        // if (parserResults != null)
        // for (ParserResultItem parserResult : parserResults)
        // result.addField(
        // ParserFieldEnum.email_attachment_content,
        // parserResult);
        // }
        // }
    }
    if (StringUtils.isEmpty(mimeMessageParser.getHtmlContent()))
        document.add(LANG_DETECTION, languageDetection(document, PLAIN_CONTENT, 10000));
    else
        document.add(LANG_DETECTION, languageDetection(document, HTML_CONTENT, 10000));
}

From source file:com.opensearchserver.extractor.parser.Eml.java

@Override
protected void parseContent(InputStream inputStream, String extension, String mimeType) throws Exception {
    Session session = Session.getDefaultInstance(JAVAMAIL_PROPS);

    MimeMessage mimeMessage = new MimeMessage(session, inputStream);
    MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse();

    ParserDocument document = getNewParserDocument();
    String from = mimeMessageParser.getFrom();
    if (from != null)
        document.add(FROM, from.toString());
    for (Address address : mimeMessageParser.getTo())
        document.add(RECIPIENT_TO, address.toString());
    for (Address address : mimeMessageParser.getCc())
        document.add(RECIPIENT_CC, address.toString());
    for (Address address : mimeMessageParser.getBcc())
        document.add(RECIPIENT_BCC, address.toString());
    document.add(SUBJECT, mimeMessageParser.getSubject());
    document.add(HTML_CONTENT, mimeMessageParser.getHtmlContent());
    document.add(PLAIN_CONTENT, mimeMessageParser.getPlainContent());
    document.add(SENT_DATE, mimeMessage.getSentDate());
    document.add(RECEIVED_DATE, mimeMessage.getReceivedDate());

    for (DataSource dataSource : mimeMessageParser.getAttachmentList()) {
        document.add(ATTACHMENT_NAME, dataSource.getName());
        document.add(ATTACHMENT_TYPE, dataSource.getContentType());
        // TODO Extract content from attachmend
        // if (parserSelector != null) {
        // Parser attachParser = parserSelector.parseStream(
        // getSourceDocument(), dataSource.getName(),
        // dataSource.getContentType(), null,
        // dataSource.getInputStream(), null, null, null);
        // if (attachParser != null) {
        // List<ParserResultItem> parserResults = attachParser
        // .getParserResults();
        // if (parserResults != null)
        // for (ParserResultItem parserResult : parserResults)
        // result.addField(
        // ParserFieldEnum.email_attachment_content,
        // parserResult);
        // }/*from  ww  w .j  av  a 2 s.  co m*/
        // }
    }
    if (StringUtils.isEmpty(mimeMessageParser.getHtmlContent()))
        document.add(LANG_DETECTION, languageDetection(document, PLAIN_CONTENT, 10000));
    else
        document.add(LANG_DETECTION, languageDetection(document, HTML_CONTENT, 10000));
}

From source file:com.glaf.mail.MailSenderImpl.java

public void send(JavaMailSender javaMailSender, MailMessage mailMessage) throws Exception {
    if (StringUtils.isEmpty(mailMessage.getMessageId())) {
        mailMessage.setMessageId(UUID32.getUUID());
    }//w  ww. j av  a 2  s .co m

    mailHelper = new MxMailHelper();
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();

    MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);

    if (StringUtils.isNotEmpty(mailMessage.getFrom())) {
        messageHelper.setFrom(mailMessage.getFrom());
        mailFrom = mailMessage.getFrom();
    } else {
        if (StringUtils.isEmpty(mailFrom)) {
            mailFrom = MailProperties.getString("mail.mailFrom");
        }
        messageHelper.setFrom(mailFrom);
    }

    logger.debug("mailFrom:" + mailFrom);

    if (mailMessage.getTo() != null) {
        messageHelper.setTo(mailMessage.getTo());
    }

    if (mailMessage.getCc() != null) {
        messageHelper.setCc(mailMessage.getCc());
    }

    if (mailMessage.getBcc() != null) {
        messageHelper.setBcc(mailMessage.getBcc());
    }

    if (mailMessage.getReplyTo() != null) {
        messageHelper.setReplyTo(mailMessage.getReplyTo());
    }

    String mailSubject = mailMessage.getSubject();
    if (mailSubject == null) {
        mailSubject = "";
    }

    if (mailSubject != null) {
        // mailSubject = MimeUtility.encodeText(new
        // String(mailSubject.getBytes(), encoding), encoding, "B");
        mailSubject = MimeUtility.encodeWord(mailSubject);
    }

    mimeMessage.setSubject(mailSubject);

    Map<String, Object> dataMap = mailMessage.getDataMap();
    if (dataMap == null) {
        dataMap = new java.util.HashMap<String, Object>();
    }

    String serviceUrl = SystemConfig.getServiceUrl();

    logger.debug("mailSubject:" + mailSubject);
    logger.debug("serviceUrl:" + serviceUrl);

    if (serviceUrl != null) {
        String loginUrl = serviceUrl + "/mx/login";
        String mainUrl = serviceUrl + "/mx/main";
        logger.debug("loginUrl:" + loginUrl);
        dataMap.put("loginUrl", loginUrl);
        dataMap.put("mainUrl", mainUrl);
    }

    mailMessage.setDataMap(dataMap);

    if (StringUtils.isEmpty(mailMessage.getContent())) {
        Template template = TemplateContainer.getContainer().getTemplate(mailMessage.getTemplateId());
        if (template != null) {
            String templateType = template.getTemplateType();
            logger.debug("templateType:" + templateType);
            // logger.debug("content:" + template.getContent());
            if (StringUtils.equals(templateType, "eml")) {
                if (template.getContent() != null) {
                    Mail m = mailHelper.getMail(template.getContent().getBytes());
                    String content = m.getContent();
                    if (StringUtils.isNotEmpty(content)) {
                        template.setContent(content);
                        try {
                            Writer writer = new StringWriter();
                            TemplateUtils.evaluate(mailMessage.getTemplateId(), dataMap, writer);
                            String text = writer.toString();
                            writer.close();
                            writer = null;
                            mailMessage.setContent(text);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            throw new RuntimeException(ex);
                        }
                    }
                }
            } else {
                try {
                    String text = TemplateUtils.process(dataMap, template.getContent());
                    mailMessage.setContent(text);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    throw new RuntimeException(ex);
                }
            }
        }
    }

    if (StringUtils.isNotEmpty(mailMessage.getContent())) {
        String text = mailMessage.getContent();
        if (StringUtils.isNotEmpty(callbackUrl)) {
            String href = callbackUrl + "?messageId=" + mailMessage.getMessageId();
            text = mailHelper.embedCallbackScript(text, href);
            mailMessage.setContent(text);
            logger.debug(text);
            messageHelper.setText(text, true);
        }
        messageHelper.setText(text, true);
    }

    logger.debug("mail body:" + mailMessage.getContent());

    Collection<Object> files = mailMessage.getFiles();

    if (files != null && !files.isEmpty()) {
        Iterator<Object> iterator = files.iterator();
        while (iterator.hasNext()) {
            Object object = iterator.next();
            if (object instanceof java.io.File) {
                java.io.File file = (java.io.File) object;
                FileSystemResource resource = new FileSystemResource(file);
                String name = file.getName();
                name = MailTools.chineseStringToAscii(name);
                messageHelper.addAttachment(name, resource);
                logger.debug("add attachment:" + name);
            } else if (object instanceof DataSource) {
                DataSource dataSource = (DataSource) object;
                String name = dataSource.getName();
                name = MailTools.chineseStringToAscii(name);
                messageHelper.addAttachment(name, dataSource);
                logger.debug("add attachment:" + name);
            } else if (object instanceof DataFile) {
                DataFile dataFile = (DataFile) object;
                if (StringUtils.isNotEmpty(dataFile.getFilename())) {
                    String name = dataFile.getFilename();
                    name = MailTools.chineseStringToAscii(name);
                    InputStreamSource inputStreamSource = new MxMailInputSource(dataFile);
                    messageHelper.addAttachment(name, inputStreamSource);
                    logger.debug("add attachment:" + name);
                }
            }
        }
    }

    mimeMessage.setSentDate(new java.util.Date());

    javaMailSender.send(mimeMessage);

    logger.info("-----------------------------------------");
    logger.info("????");
    logger.info("-----------------------------------------");
}

From source file:com.jaeksoft.searchlib.crawler.mailbox.crawler.MailboxAbstractCrawler.java

final public void readMessage(IndexDocument crawlIndexDocument, IndexDocument parserIndexDocument,
        Folder folder, Message message, String id) throws Exception {

    crawlIndexDocument.addString(MailboxFieldEnum.message_id.name(), id);
    crawlIndexDocument.addString(MailboxFieldEnum.message_number.name(),
            Integer.toString(message.getMessageNumber()));
    if (message instanceof MimeMessage)
        crawlIndexDocument.addString(MailboxFieldEnum.content_id.name(),
                ((MimeMessage) message).getContentID());
    crawlIndexDocument.addString(MailboxFieldEnum.subject.name(), message.getSubject());
    putAddresses(crawlIndexDocument, message.getFrom(), MailboxFieldEnum.from_address.name(),
            MailboxFieldEnum.from_personal.name());
    putAddresses(crawlIndexDocument, message.getReplyTo(), MailboxFieldEnum.reply_to_address.name(),
            MailboxFieldEnum.reply_to_personal.name());
    putAddresses(crawlIndexDocument, message.getRecipients(RecipientType.TO),
            MailboxFieldEnum.recipient_to_address.name(), MailboxFieldEnum.recipient_to_personal.name());
    putAddresses(crawlIndexDocument, message.getRecipients(RecipientType.CC),
            MailboxFieldEnum.recipient_cc_address.name(), MailboxFieldEnum.recipient_cc_personal.name());
    putAddresses(crawlIndexDocument, message.getRecipients(RecipientType.BCC),
            MailboxFieldEnum.recipient_bcc_address.name(), MailboxFieldEnum.recipient_bcc_personal.name());
    Date dt = message.getSentDate();
    if (dt != null)
        crawlIndexDocument.addString(MailboxFieldEnum.send_date.name(), dt.toString());
    dt = message.getReceivedDate();//from w w  w.j a  v a2  s  .  co m
    if (dt != null)
        crawlIndexDocument.addString(MailboxFieldEnum.received_date.name(), dt.toString());
    if (message.isSet(Flag.ANSWERED))
        crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "ANSWERED");
    if (message.isSet(Flag.DELETED))
        crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "DELETED");
    if (message.isSet(Flag.DRAFT))
        crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "DRAFT");
    if (message.isSet(Flag.FLAGGED))
        crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "FLAGGED");
    if (message.isSet(Flag.SEEN))
        crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "SEEN");

    if (message instanceof MimeMessage) {
        MimeMessageParser mimeMessageParser = new MimeMessageParser((MimeMessage) message).parse();

        crawlIndexDocument.addString(MailboxFieldEnum.html_content.name(), mimeMessageParser.getHtmlContent());
        crawlIndexDocument.addString(MailboxFieldEnum.plain_content.name(),
                mimeMessageParser.getPlainContent());
        for (DataSource dataSource : mimeMessageParser.getAttachmentList()) {
            crawlIndexDocument.addString(MailboxFieldEnum.email_attachment_name.name(), dataSource.getName());
            crawlIndexDocument.addString(MailboxFieldEnum.email_attachment_type.name(),
                    dataSource.getContentType());
            if (parserSelector == null)
                continue;
            Parser attachParser = parserSelector.parseStream(null, dataSource.getName(),
                    dataSource.getContentType(), null, dataSource.getInputStream(), null, null, null);
            if (attachParser == null)
                continue;
            List<ParserResultItem> parserResults = attachParser.getParserResults();
            if (parserResults != null)
                for (ParserResultItem parserResult : parserResults)
                    crawlIndexDocument.addFieldIndexDocument(MailboxFieldEnum.email_attachment_content.name(),
                            parserResult.getParserDocument());
        }
    }
}

From source file:org.shredzone.cilla.service.impl.PictureServiceImpl.java

@Override
@CacheEvict(value = "processedImages", allEntries = true)
public void updatePicture(Picture picture, DataSource source) throws CillaServiceException {
    if (picture.getId() == 0) {
        throw new IllegalArgumentException("picture is not persisted");
    }//from   w  w w .j a  v  a  2s  .  com

    if (source != null) {
        try {
            Store store = picture.getImage();
            store.setContentType(source.getContentType());
            store.setName(source.getName());
            store.setLastModified(new Date());

            Dimension dim = imageProcessor.analyzeDimension(source);
            if (dim != null) {
                picture.setWidth(dim.width);
                picture.setHeight(dim.height);
            }

            ExifAnalyzer exif = imageProcessor.createExifAnalyzer(source);
            if (exif != null) {
                picture.setCreateDate(exif.getDateTime(picture.getCreateTimeZone()));
                picture.setExifData(exif.getExifData());
                picture.setLocation(exif.getGeolocation());
            } else {
                picture.setCreateDate(null);
                picture.setExifData(null);
                picture.setLocation(null);
            }

            ResourceDataSource ds = storeDao.access(store);
            FileCopyUtils.copy(source.getInputStream(), ds.getOutputStream());
        } catch (IOException ex) {
            throw new CillaServiceException("Could not read medium", ex);
        }
    }

    eventService.fireEvent(new Event<Picture>(EventType.GALLERY_PICTURE_UPDATE, picture));
}