use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.
the class LZFDataFormat method unmarshal.
@Override
public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
InputStream compressedInput = null;
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
try {
compressedInput = new LZFInputStream(inputStream);
IOHelper.copy(compressedInput, osb);
return osb.build();
} finally {
// must close all input streams
IOHelper.close(osb, compressedInput, inputStream);
}
}
use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.
the class ZipFileDataFormat method unmarshal.
@Override
public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
if (usingIterator) {
ZipIterator zipIterator = new ZipIterator(exchange.getIn());
zipIterator.setAllowEmptyDirectory(allowEmptyDirectory);
return zipIterator;
} else {
ZipInputStream zis = new ZipInputStream(inputStream);
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
try {
ZipEntry entry = zis.getNextEntry();
if (entry != null) {
exchange.getOut().setHeader(FILE_NAME, entry.getName());
IOHelper.copy(zis, osb);
}
entry = zis.getNextEntry();
if (entry != null) {
throw new IllegalStateException("Zip file has more than 1 entry.");
}
return osb.build();
} finally {
IOHelper.close(zis, osb);
}
}
}
use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.
the class JettyContentExchange9 method send.
public void send(HttpClient client) throws IOException {
org.eclipse.jetty.client.api.Request.Listener listener = new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
onRequestComplete();
}
@Override
public void onFailure(Request request, Throwable failure) {
onConnectionFailed(failure);
}
};
InputStreamResponseListener responseListener = new InputStreamResponseListener() {
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
byte[] buffer = new byte[content.limit()];
content.get(buffer);
try {
osb.write(buffer);
callback.succeeded();
} catch (IOException e) {
callback.failed(e);
}
}
@Override
public void onComplete(Result result) {
if (result.isFailed()) {
doTaskCompleted(result.getFailure());
} else {
try {
Object content = osb.build();
if (content instanceof byte[]) {
onResponseComplete(result, (byte[]) content);
} else {
StreamCache cos = (StreamCache) content;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
cos.writeTo(baos);
onResponseComplete(result, baos.toByteArray());
}
} catch (IOException e) {
doTaskCompleted(e);
}
}
}
};
request.followRedirects(supportRedirect).listener(listener).send(responseListener);
}
use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.
the class DataFormatTransformer method transform.
/**
* Perform data transformation with specified from/to type using DataFormat.
* @param message message to apply transformation
* @param from 'from' data type
* @param to 'to' data type
*/
@Override
public void transform(Message message, DataType from, DataType to) throws Exception {
Exchange exchange = message.getExchange();
CamelContext context = exchange.getContext();
// Unmarshaling into Java Object
if ((to == null || to.isJavaType()) && (from.equals(getFrom()) || from.getModel().equals(getModel()))) {
DataFormat dataFormat = getDataFormat(exchange);
LOG.debug("Unmarshaling with '{}'", dataFormat);
Object answer = dataFormat.unmarshal(exchange, message.getBody(InputStream.class));
if (to != null && to.getName() != null) {
Class<?> toClass = context.getClassResolver().resolveClass(to.getName());
if (!toClass.isAssignableFrom(answer.getClass())) {
LOG.debug("Converting to '{}'", toClass.getName());
answer = context.getTypeConverter().mandatoryConvertTo(toClass, answer);
}
}
message.setBody(answer);
// Marshaling from Java Object
} else if ((from == null || from.isJavaType()) && (to.equals(getTo()) || to.getModel().equals(getModel()))) {
Object input = message.getBody();
if (from != null && from.getName() != null) {
Class<?> fromClass = context.getClassResolver().resolveClass(from.getName());
if (!fromClass.isAssignableFrom(input.getClass())) {
LOG.debug("Converting to '{}'", fromClass.getName());
input = context.getTypeConverter().mandatoryConvertTo(fromClass, input);
}
}
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
DataFormat dataFormat = getDataFormat(exchange);
LOG.debug("Marshaling with '{}'", dataFormat);
dataFormat.marshal(exchange, message.getBody(), osb);
message.setBody(osb.build());
} else {
throw new IllegalArgumentException("Unsupported transformation: from='" + from + ", to='" + to + "'");
}
}
use of org.apache.camel.converter.stream.OutputStreamBuilder in project camel by apache.
the class GzipDataFormat method unmarshal.
public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
GZIPInputStream unzipInput = null;
OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
try {
unzipInput = new GZIPInputStream(inputStream);
IOHelper.copy(unzipInput, osb);
return osb.build();
} finally {
// must close all input streams
IOHelper.close(osb, unzipInput, inputStream);
}
}
Aggregations