use of javax.activation.DataSource in project scout.rt by eclipse.
the class MailHelperTest method testDataSourceWithoutFileExtension.
@Test
public void testDataSourceWithoutFileExtension() throws IOException, MessagingException {
final byte[] sampleData = new byte[] { 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
final String fileName = "test.file";
DataSource ds = BEANS.get(MailHelper.class).createDataSource(new ByteArrayInputStream(sampleData), fileName, null);
assertNotNull(ds);
assertEquals(fileName, ds.getName());
assertTrue(ds instanceof ByteArrayDataSource);
ByteArrayDataSource bds = (ByteArrayDataSource) ds;
assertEquals("application/octet-stream", bds.getContentType());
try (InputStream in = bds.getInputStream()) {
byte[] data = IOUtility.readBytes(in);
assertArrayEquals(sampleData, data);
}
new MailMessage().withBodyPlainText("test").withAttachment(new MailAttachment(ds));
MimeMessage message = BEANS.get(MailHelper.class).createMimeMessage(new MailMessage().withBodyPlainText("test"));
message.writeTo(new ByteArrayOutputStream());
}
use of javax.activation.DataSource in project scout.rt by eclipse.
the class MailHelper method addAttachmentsToMimeMessage.
/**
* Adds the provided attachments to the existing mime message.
* <p>
* When working with {@link BinaryResource}, use {@link #addResourcesAsAttachments(MimeMessage, List)} instead.
*
* @param msg
* Mime message to attach files to
* @param attachments
* List of attachments (files).
* @since 4.1
*/
public void addAttachmentsToMimeMessage(MimeMessage msg, List<File> attachments) {
if (CollectionUtility.isEmpty(attachments)) {
return;
}
try {
Multipart multiPart = prepareMessageForAttachments(msg);
for (File attachment : attachments) {
MimeBodyPart bodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName(MimeUtility.encodeText(attachment.getName(), "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 javax.activation.DataSource 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 javax.activation.DataSource in project jbossws-cxf by jbossws.
the class JBWS2259TestCase method testCall.
@Test
@RunAsClient
public void testCall() throws Exception {
URL wsdlURL = new URL(baseURL + "?wsdl");
QName serviceName = new QName("http://ws.jboss.org/jbws2259", "EndpointService");
Service service = Service.create(wsdlURL, serviceName);
Endpoint port = service.getPort(Endpoint.class);
BindingProvider bindingProvider = (BindingProvider) port;
SOAPBinding soapBinding = (SOAPBinding) bindingProvider.getBinding();
soapBinding.setMTOMEnabled(true);
File sharkFile = getResourceFile("jaxws/jbws2259/attach.jpeg");
DataSource ds = new FileDataSource(sharkFile);
DataHandler handler = new DataHandler(ds);
String expectedContentType = "image/jpeg";
Photo p = new Photo();
p.setCaption("JBWS2259 Smile :-)");
p.setExpectedContentType(expectedContentType);
p.setImage(handler);
Photo reponse = port.echo(p);
DataHandler dhResponse = reponse.getImage();
String contentType = dhResponse.getContentType();
assertEquals("content-type", expectedContentType, contentType);
}
use of javax.activation.DataSource in project mule by mulesoft.
the class TransformerUtils method checkTransformerReturnClass.
/**
* Checks whether a given value is a valid output for a transformer.
*
* @param transformer the transformer used to validate
* @param value the output value
* @throws TransformerException if the output value is of a unexpected type.
*/
public static void checkTransformerReturnClass(Transformer transformer, Object value) throws TransformerException {
if (value == null && (transformer instanceof AbstractTransformer && ((AbstractTransformer) transformer).isAllowNullReturn())) {
return;
}
if (transformer.getReturnDataType() != null) {
DataTypeParamsBuilder dtBuilder = DataType.builder().fromObject(value);
if (!(value instanceof DataHandler) && !(value instanceof DataSource)) {
// To avoid getting an error because the DataType was constructed with a default mediaType
dtBuilder = dtBuilder.mediaType(transformer.getReturnDataType().getMediaType());
}
DataType dt = dtBuilder.build();
if (!transformer.getReturnDataType().isCompatibleWith(dt)) {
throw new TransformerException(transformUnexpectedType(dt, transformer.getReturnDataType()), transformer);
}
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("The transformed value is of expected type. Type is: " + ClassUtils.getSimpleName(value.getClass()));
}
}
Aggregations