use of javax.activation.DataHandler in project webservices-axiom by apache.
the class TestGetTextCharactersFromDataHandler method runTest.
@Override
protected void runTest() throws Throwable {
DataHandler dh = new DataHandler("test content", "text/plain; charset=utf-8");
OMText text = metaFactory.getOMFactory().createOMText(dh, true);
char[] chars = text.getTextCharacters();
byte[] decoded = Base64.decodeBase64(new String(chars));
assertEquals("test content", new String(decoded, "utf-8"));
}
use of javax.activation.DataHandler in project webservices-axiom by apache.
the class BufferUtilsTest method testByteArrayDataSourceBackedDataHandlerExceedLimit.
public void testByteArrayDataSourceBackedDataHandlerExceedLimit() throws Exception {
String str = "This is a test String";
byte[] b = str.getBytes();
ByteArrayDataSource bads = new ByteArrayDataSource(b, "text/plain");
DataHandler dh = new DataHandler(bads);
int unsupported = BufferUtils.doesDataHandlerExceedLimit(dh, 0);
assertEquals(-1, unsupported);
int doesExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 10);
assertEquals(1, doesExceed);
int doesNotExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 100);
assertEquals(0, doesNotExceed);
}
use of javax.activation.DataHandler in project webservices-axiom by apache.
the class BufferUtilsTest method testObjectBackedDataHandlerExceedLimit.
public void testObjectBackedDataHandlerExceedLimit() throws Exception {
String str = "This is a test String";
DataHandler dh = new DataHandler(str, "text/plain");
int unsupported = BufferUtils.doesDataHandlerExceedLimit(dh, 0);
assertEquals(-1, unsupported);
int doesExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 10);
assertEquals(1, doesExceed);
int doesNotExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 100);
assertEquals(0, doesNotExceed);
}
use of javax.activation.DataHandler in project webservices-axiom by apache.
the class TestMTOMForwardStreaming method runTest.
@Override
protected void runTest() throws Throwable {
DataSource ds1 = new TestDataSource('A', Runtime.getRuntime().maxMemory());
DataSource ds2 = new TestDataSource('B', Runtime.getRuntime().maxMemory());
// Programmatically create the original message
SOAPFactory factory = metaFactory.getSOAP12Factory();
final SOAPEnvelope orgEnvelope = factory.createSOAPEnvelope();
SOAPBody orgBody = factory.createSOAPBody(orgEnvelope);
OMElement orgBodyElement = factory.createOMElement("test", factory.createOMNamespace("urn:test", "p"), orgBody);
OMElement orgData1 = factory.createOMElement("data", null, orgBodyElement);
orgData1.addChild(factory.createOMText(new DataHandler(ds1), true));
OMElement orgData2 = factory.createOMElement("data", null, orgBodyElement);
orgData2.addChild(factory.createOMText(new DataHandler(ds2), true));
final OMOutputFormat format = new OMOutputFormat();
format.setDoOptimize(true);
format.setSOAP11(false);
final String contentType = format.getContentType();
final PipedOutputStream pipe1Out = new PipedOutputStream();
final PipedInputStream pipe1In = new PipedInputStream(pipe1Out);
// Create the producer thread (simulating the client sending the MTOM message)
Thread producerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
try {
orgEnvelope.serialize(pipe1Out, format);
} finally {
pipe1Out.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
producerThread.start();
final PipedOutputStream pipe2Out = new PipedOutputStream();
PipedInputStream pipe2In = new PipedInputStream(pipe2Out);
// Create the forwarder thread (simulating the mediation engine that forwards the message)
Thread forwarderThread = new Thread(new Runnable() {
@Override
public void run() {
try {
try {
MultipartBody mb = MultipartBody.builder().setInputStream(pipe1In).setContentType(contentType).build();
SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
// the element is built. Therefore we need two different test executions.
if (buildSOAPPart) {
envelope.build();
}
// Usage of serializeAndConsume should enable streaming
envelope.serializeAndConsume(pipe2Out, format);
} finally {
pipe2Out.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
forwarderThread.start();
try {
MultipartBody mb = MultipartBody.builder().setInputStream(pipe2In).setContentType(contentType).build();
SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, mb).getSOAPEnvelope();
OMElement bodyElement = envelope.getBody().getFirstElement();
Iterator<OMElement> it = bodyElement.getChildElements();
OMElement data1 = it.next();
OMElement data2 = it.next();
IOTestUtils.compareStreams(ds1.getInputStream(), ((PartDataHandler) ((OMText) data1.getFirstOMChild()).getDataHandler()).getPart().getInputStream(false));
IOTestUtils.compareStreams(ds2.getInputStream(), ((PartDataHandler) ((OMText) data2.getFirstOMChild()).getDataHandler()).getPart().getInputStream(false));
} finally {
pipe2In.close();
}
}
use of javax.activation.DataHandler in project webservices-axiom by apache.
the class AttachmentsTest method testTurkishLocale.
private void testTurkishLocale(String contentIDHeaderName) throws Exception {
Locale locale = Locale.getDefault();
Locale.setDefault(new Locale("tr", "TR"));
try {
MimeMessage message = new MimeMessage((Session) null);
MimeMultipart mp = new MimeMultipart("related");
MimeBodyPart bp1 = new MimeBodyPart();
bp1.setText("<root/>", "utf-8", "xml");
bp1.addHeader("Content-Transfer-Encoding", "binary");
mp.addBodyPart(bp1);
MimeBodyPart bp2 = new MimeBodyPart();
byte[] content = new byte[8192];
new Random().nextBytes(content);
bp2.setDataHandler(new DataHandler("Test", "text/plain"));
bp2.addHeader("Content-Transfer-Encoding", "binary");
bp2.addHeader(contentIDHeaderName, "part@apache.org");
mp.addBodyPart(bp2);
message.setContent(mp);
message.saveChanges();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mp.writeTo(baos);
String contentType = message.getContentType();
InputStream in = new ByteArrayInputStream(baos.toByteArray());
Attachments attachments = new Attachments(in, contentType);
assertNotNull(attachments.getDataHandler("part@apache.org"));
} finally {
Locale.setDefault(locale);
}
}
Aggregations