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);
}
}
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);
}
}
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;
}
}
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();
}
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);
}
}
Aggregations