Search in sources :

Example 56 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class MailHelper method collectMailParts.

/**
 * Collects the body, attachment and inline attachment parts from the provided part.
 * <p>
 * A single collector can be null in order to collect only the relevant parts.
 *
 * @param part
 *          Part
 * @param bodyCollector
 *          Body collector (optional)
 * @param attachmentCollector
 *          Attachment collector (optional)
 * @param inlineAttachmentCollector
 *          Inline attachment collector (optional)
 */
public void collectMailParts(Part part, List<Part> bodyCollector, List<Part> attachmentCollector, List<Part> inlineAttachmentCollector) {
    if (part == null) {
        return;
    }
    try {
        String disp = part.getDisposition();
        if (disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT)) {
            if (attachmentCollector != null) {
                attachmentCollector.add(part);
            }
        } else {
            Object content = null;
            try {
                // NOSONAR
                checkValidCharset(part);
                // getContent might throw a MessagingException for legitimate parts (e.g. some images end up in a javax.imageio.IIOException for example).
                content = part.getContent();
            } catch (MessagingException | IOException e) {
                LOG.info("Unable to mime part content", e);
            }
            if (content instanceof Multipart) {
                Multipart multiPart = (Multipart) part.getContent();
                for (int i = 0; i < multiPart.getCount(); i++) {
                    collectMailParts(multiPart.getBodyPart(i), bodyCollector, attachmentCollector, inlineAttachmentCollector);
                }
            } else {
                if (part.isMimeType(CONTENT_TYPE_TEXT_PLAIN)) {
                    if (bodyCollector != null) {
                        bodyCollector.add(part);
                    }
                } else if (part.isMimeType(CONTENT_TYPE_TEXT_HTML)) {
                    if (bodyCollector != null) {
                        bodyCollector.add(part);
                    }
                } else if (part.isMimeType(CONTENT_TYPE_MESSAGE_RFC822) && part.getContent() instanceof MimeMessage) {
                    // its a MIME message in rfc822 format as attachment therefore we have to set the filename for the attachment correctly.
                    if (attachmentCollector != null) {
                        MimeMessage msg = (MimeMessage) part.getContent();
                        String filteredSubjectText = StringUtility.filterText(msg.getSubject(), "a-zA-Z0-9_-", "");
                        String fileName = (StringUtility.hasText(filteredSubjectText) ? filteredSubjectText : "originalMessage") + ".eml";
                        RFCWrapperPart wrapperPart = new RFCWrapperPart(part, fileName);
                        attachmentCollector.add(wrapperPart);
                    }
                } else if (disp != null && disp.equals(Part.INLINE)) {
                    if (inlineAttachmentCollector != null) {
                        inlineAttachmentCollector.add(part);
                    }
                } else {
                    String[] headerContentId = part.getHeader(CONTENT_ID_ID);
                    if (headerContentId != null && headerContentId.length > 0 && StringUtility.hasText(headerContentId[0]) && part.getContentType() != null && part.getContentType().startsWith(CONTENT_TYPE_IMAGE_PREFIX)) {
                        if (inlineAttachmentCollector != null) {
                            inlineAttachmentCollector.add(part);
                        }
                    } else if (part.getFileName() != null) /* assumption: file name = attachment (last resort) */
                    {
                        if (attachmentCollector != null) {
                            attachmentCollector.add(part);
                        }
                    } else {
                        LOG.debug("Unknown mail message part, headers: [{}]", part.getAllHeaders());
                    }
                }
            }
        }
    } catch (MessagingException | IOException e) {
        throw new ProcessingException("Unexpected: ", e);
    }
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) MimeMessage(javax.mail.internet.MimeMessage) IOException(java.io.IOException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 57 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class MailHelper method addResourcesAsAttachments.

/**
 * Adds the provided attachments to the existing mime message.
 * <p>
 * When working with {@link File}, use {@link #addAttachmentsToMimeMessage(MimeMessage, List)} instead.
 *
 * @param msg
 *          Mime message to attach files to
 * @param attachments
 *          List of attachments (binary resources).
 * @since 6.0
 */
public void addResourcesAsAttachments(MimeMessage msg, List<BinaryResource> attachments) {
    if (CollectionUtility.isEmpty(attachments)) {
        return;
    }
    try {
        Multipart multiPart = prepareMessageForAttachments(msg);
        for (BinaryResource attachment : attachments) {
            MimeBodyPart bodyPart = new MimeBodyPart();
            DataSource source = new BinaryResourceDataSource(attachment);
            bodyPart.setDataHandler(new DataHandler(source));
            bodyPart.setFileName(MimeUtility.encodeText(attachment.getFilename(), "UTF-8", null));
            multiPart.addBodyPart(bodyPart);
        }
        msg.saveChanges();
    } catch (MessagingException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    } catch (IOException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    }
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) MessagingException(javax.mail.MessagingException) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 58 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class AbstractHttpTransportManager method removeHttpTransport.

/**
 * Remove {@link HttpTransport}.
 */
protected synchronized void removeHttpTransport() {
    if (m_httpTransport == null) {
        return;
    }
    try {
        m_httpTransport.shutdown();
    } catch (IOException e) {
        throw new ProcessingException("Error during HttpTransport shut down.", e);
    } finally {
        m_active = false;
        m_httpTransport = null;
    }
}
Also used : IOException(java.io.IOException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 59 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class ApacheHttpRequest method execute.

@Override
public LowLevelHttpResponse execute() throws IOException {
    final StreamingContent streamingContent = getStreamingContent();
    if (streamingContent != null) {
        if (!(m_request instanceof HttpEntityEnclosingRequest)) {
            throw new ProcessingException("This request {} does not support content.", m_request);
        }
        AbstractHttpEntity entity = new AbstractHttpEntity() {

            @Override
            public void writeTo(OutputStream outstream) throws IOException {
                streamingContent.writeTo(outstream);
            }

            @Override
            public boolean isStreaming() {
                return true;
            }

            @Override
            public boolean isRepeatable() {
                if (streamingContent instanceof HttpContent) {
                    return ((HttpContent) streamingContent).retrySupported();
                }
                return false;
            }

            @Override
            public long getContentLength() {
                return ApacheHttpRequest.this.getContentLength();
            }

            @Override
            public InputStream getContent() throws IOException {
                throw new UnsupportedOperationException("Streaming entity cannot be represented as an input stream.");
            }
        };
        ((HttpEntityEnclosingRequest) m_request).setEntity(entity);
        entity.setContentEncoding(getContentEncoding());
        entity.setContentType(getContentType());
    }
    return createResponseInternal();
}
Also used : HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) OutputStream(java.io.OutputStream) StreamingContent(com.google.api.client.util.StreamingContent) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpContent(com.google.api.client.http.HttpContent) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 60 with ProcessingException

use of org.eclipse.scout.rt.platform.exception.ProcessingException in project scout.rt by eclipse.

the class SunSecurityProvider method createPasswordHash.

/**
 * If this method throws a NoSuchAlgorithmException, this may be because you are using an older Java version. In this
 * case it is safe to replace the algorithm with "PBKDF2WithHmacSHA1". SHA1 is still safe in the context of PBKDF2. To
 * do so replace this bean with your own implementation overwriting the method
 * {@link #getPasswordHashSecretKeyAlgorithm()}.
 */
@Override
public byte[] createPasswordHash(char[] password, byte[] salt, int iterations) {
    Assertions.assertGreater(Assertions.assertNotNull(password, "password must not be null.").length, 0, "empty password is not allowed.");
    Assertions.assertGreater(Assertions.assertNotNull(salt, "salt must not be null.").length, 0, "empty salt is not allowed.");
    Assertions.assertGreaterOrEqual(iterations, MIN_PASSWORD_HASH_ITERATIONS, "iterations must be > {}", MIN_PASSWORD_HASH_ITERATIONS);
    try {
        SecretKeyFactory skf = SecretKeyFactory.getInstance(getPasswordHashSecretKeyAlgorithm(), getCipherAlgorithmProvider());
        PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, 256);
        SecretKey key = skf.generateSecret(spec);
        byte[] res = key.getEncoded();
        return res;
    } catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchProviderException e) {
        throw new ProcessingException("Unable to create password hash.", e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException) SecretKeyFactory(javax.crypto.SecretKeyFactory) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Aggregations

ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)142 IOException (java.io.IOException)48 MessagingException (javax.mail.MessagingException)21 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)17 File (java.io.File)14 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)12 Folder (javax.mail.Folder)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 RemoteFile (org.eclipse.scout.rt.shared.services.common.file.RemoteFile)9 NoSuchProviderException (java.security.NoSuchProviderException)8 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)8 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 FileOutputStream (java.io.FileOutputStream)6 Message (javax.mail.Message)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStream (java.io.OutputStream)5 HashMap (java.util.HashMap)5