use of java.io.ByteArrayInputStream in project camel by apache.
the class HL7DataFormatTest method testUnmarshalWithImplicitBig5Charset.
@Test
public void testUnmarshalWithImplicitBig5Charset() throws Exception {
String charset = "Big5";
MockEndpoint mock = getMockEndpoint("mock:unmarshalBig5");
mock.expectedMessageCount(1);
mock.message(0).body().isInstanceOf(Message.class);
mock.expectedHeaderReceived(HL7Constants.HL7_CHARSET, null);
mock.expectedHeaderReceived(Exchange.CHARSET_NAME, charset);
// Message without explicit encoding in MSH-18, but the unmarshaller "guesses"
// this time that it is Big5
byte[] body = createHL7AsString().getBytes(Charset.forName(charset));
template.sendBody("direct:unmarshalBig5", new ByteArrayInputStream(body));
assertMockEndpointsSatisfied();
Message msg = mock.getExchanges().get(0).getIn().getBody(Message.class);
assertEquals("2.4", msg.getVersion());
QRD qrd = (QRD) msg.get("QRD");
assertEquals("0101701234", qrd.getWhoSubjectFilter(0).getIDNumber().getValue());
}
use of java.io.ByteArrayInputStream in project camel by apache.
the class HL7DataFormatTest method testUnmarshalWithExplicitUTF16Charset.
@Test
public void testUnmarshalWithExplicitUTF16Charset() throws Exception {
String charset = "UTF-16";
MockEndpoint mock = getMockEndpoint("mock:unmarshal");
mock.expectedMessageCount(1);
mock.message(0).body().isInstanceOf(Message.class);
mock.expectedHeaderReceived(HL7Constants.HL7_CHARSET, HL7Charset.getHL7Charset(charset).getHL7CharsetName());
mock.expectedHeaderReceived(Exchange.CHARSET_NAME, charset);
// Message with explicit encoding in MSH-18
byte[] body = createHL7WithCharsetAsString(HL7Charset.UTF_16).getBytes(Charset.forName(charset));
template.sendBodyAndHeader("direct:unmarshal", new ByteArrayInputStream(body), Exchange.CHARSET_NAME, charset);
assertMockEndpointsSatisfied();
Message msg = mock.getExchanges().get(0).getIn().getBody(Message.class);
assertEquals("2.4", msg.getVersion());
QRD qrd = (QRD) msg.get("QRD");
assertEquals("0101701234", qrd.getWhoSubjectFilter(0).getIDNumber().getValue());
}
use of java.io.ByteArrayInputStream in project camel by apache.
the class HttpEntityConverter method asHttpEntity.
private static HttpEntity asHttpEntity(InputStream in, Exchange exchange) throws IOException {
InputStreamEntity entity;
if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
InputStream stream = GZIPHelper.compressGzip(contentEncoding, in);
entity = new InputStreamEntity(stream, stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1 : -1);
} else {
Message inMessage = exchange.getIn();
String length = inMessage.getHeader(Exchange.CONTENT_LENGTH, String.class);
if (ObjectHelper.isEmpty(length)) {
entity = new InputStreamEntity(in, -1);
} else {
entity = new InputStreamEntity(in, Long.parseLong(length));
}
}
if (exchange != null) {
String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
String contentType = ExchangeHelper.getContentType(exchange);
entity.setContentEncoding(contentEncoding);
entity.setContentType(contentType);
}
return entity;
}
use of java.io.ByteArrayInputStream in project camel by apache.
the class HttpEntityConverter method asHttpEntity.
private static HttpEntity asHttpEntity(byte[] data, Exchange exchange) throws Exception {
AbstractHttpEntity entity;
if (exchange != null && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
InputStream stream = GZIPHelper.compressGzip(contentEncoding, data);
entity = new InputStreamEntity(stream, stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1 : -1);
} else {
// create the Repeatable HttpEntity
entity = new ByteArrayEntity(data);
}
if (exchange != null) {
String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
String contentType = ExchangeHelper.getContentType(exchange);
entity.setContentEncoding(contentEncoding);
entity.setContentType(contentType);
}
return entity;
}
use of java.io.ByteArrayInputStream in project camel by apache.
the class HttpBodyTest method httpPostWithInputStreamBody.
@Test
public void httpPostWithInputStreamBody() throws Exception {
Exchange exchange = template.request(getProtocolString() + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/post", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(new ByteArrayInputStream(getBody().getBytes(charset)));
}
});
assertExchange(exchange);
}
Aggregations