use of java.io.ByteArrayInputStream in project camel by apache.
the class DefaultAhcBinding method onComplete.
@Override
public void onComplete(AhcEndpoint endpoint, Exchange exchange, String url, ByteArrayOutputStream os, int contentLength, int statusCode, String statusText) throws Exception {
// copy from output stream to input stream
os.flush();
os.close();
InputStream is = new ByteArrayInputStream(os.toByteArray());
String contentEncoding = exchange.getOut().getHeader(Exchange.CONTENT_ENCODING, String.class);
if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
is = GZIPHelper.uncompressGzip(contentEncoding, is);
}
// Honor the character encoding
String contentType = exchange.getOut().getHeader(Exchange.CONTENT_TYPE, String.class);
if (contentType != null) {
// find the charset and set it to the Exchange
AhcHelper.setCharsetFromContentType(contentType, exchange);
}
Object body = is;
// an exception can also be transffered as java object
if (contentType != null && contentType.equals(AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT)) {
if (endpoint.getComponent().isAllowJavaSerializedObject() || endpoint.isTransferException()) {
body = AhcHelper.deserializeJavaObjectFromStream(is);
}
}
if (!endpoint.isThrowExceptionOnFailure()) {
// if we do not use failed exception then populate response for all response codes
populateResponse(exchange, body, contentLength, statusCode);
} else {
if (statusCode >= 100 && statusCode < 300) {
// only populate response for OK response
populateResponse(exchange, body, contentLength, statusCode);
} else {
// operation failed so populate exception to throw
throw populateHttpOperationFailedException(endpoint, exchange, url, body, contentLength, statusCode, statusText);
}
}
}
use of java.io.ByteArrayInputStream in project camel by apache.
the class AhcBridgeEndpointTest method testBridgeEndpoint.
@Test
public void testBridgeEndpoint() throws Exception {
String response = template.requestBodyAndHeader("http://localhost:" + port1 + "/test/hello", new ByteArrayInputStream("This is a test".getBytes()), "Content-Type", "application/xml", String.class);
assertEquals("Get a wrong response", "/", response);
response = template.requestBody("http://localhost:" + port2 + "/hello/world", "hello", String.class);
assertEquals("Get a wrong response", "/hello/world", response);
try {
template.requestBody("http://localhost:" + port1 + "/hello/world", "hello", String.class);
fail("Expect exception here!");
} catch (Exception ex) {
assertTrue("We should get a RuntimeCamelException", ex instanceof RuntimeCamelException);
}
}
use of java.io.ByteArrayInputStream in project camel by apache.
the class ManagedCamelContext method dumpRoutesAsXml.
@Override
public String dumpRoutesAsXml(boolean resolvePlaceholders) throws Exception {
List<RouteDefinition> routes = context.getRouteDefinitions();
if (routes.isEmpty()) {
return null;
}
// use a routes definition to dump the routes
RoutesDefinition def = new RoutesDefinition();
def.setRoutes(routes);
String xml = ModelHelper.dumpModelAsXml(context, def);
// if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
if (resolvePlaceholders) {
final AtomicBoolean changed = new AtomicBoolean();
InputStream is = new ByteArrayInputStream(xml.getBytes());
Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {
@Override
public String transform(String text) {
try {
String after = getContext().resolvePropertyPlaceholders(text);
if (!changed.get()) {
changed.set(!text.equals(after));
}
return after;
} catch (Exception e) {
// ignore
return text;
}
}
});
// okay there were some property placeholder replaced so re-create the model
if (changed.get()) {
xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
RoutesDefinition copy = ModelHelper.createModelFromXml(context, xml, RoutesDefinition.class);
xml = ModelHelper.dumpModelAsXml(context, copy);
}
}
return xml;
}
use of java.io.ByteArrayInputStream in project camel by apache.
the class ManagedCamelContext method dumpRestsAsXml.
@Override
public String dumpRestsAsXml(boolean resolvePlaceholders) throws Exception {
List<RestDefinition> rests = context.getRestDefinitions();
if (rests.isEmpty()) {
return null;
}
// use a routes definition to dump the rests
RestsDefinition def = new RestsDefinition();
def.setRests(rests);
String xml = ModelHelper.dumpModelAsXml(context, def);
// if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
if (resolvePlaceholders) {
final AtomicBoolean changed = new AtomicBoolean();
InputStream is = new ByteArrayInputStream(xml.getBytes());
Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {
@Override
public String transform(String text) {
try {
String after = getContext().resolvePropertyPlaceholders(text);
if (!changed.get()) {
changed.set(!text.equals(after));
}
return after;
} catch (Exception e) {
// ignore
return text;
}
}
});
// okay there were some property placeholder replaced so re-create the model
if (changed.get()) {
xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
RestsDefinition copy = ModelHelper.createModelFromXml(context, xml, RestsDefinition.class);
xml = ModelHelper.dumpModelAsXml(context, copy);
}
}
return xml;
}
use of java.io.ByteArrayInputStream in project camel by apache.
the class ResourceHelper method resolveMandatoryResourceAsInputStream.
/**
* Resolves the mandatory resource.
* <p/>
* The resource uri can refer to the following systems to be loaded from
* <ul>
* <il>file:nameOfFile - to refer to the file system</il>
* <il>classpath:nameOfFile - to refer to the classpath (default)</il>
* <il>http:uri - to load the resource using HTTP</il>
* <il>ref:nameOfBean - to lookup the resource in the {@link org.apache.camel.spi.Registry}</il>
* <il>bean:nameOfBean.methodName - to lookup a bean in the {@link org.apache.camel.spi.Registry} and call the method</il>
* </ul>
* If no prefix has been given, then the resource is loaded from the classpath
* <p/>
* If possible recommended to use {@link #resolveMandatoryResourceAsUrl(org.apache.camel.spi.ClassResolver, String)}
*
* @param camelContext the Camel Context
* @param uri URI of the resource
* @return the resource as an {@link InputStream}. Remember to close this stream after usage.
* @throws java.io.IOException is thrown if the resource file could not be found or loaded as {@link InputStream}
*/
public static InputStream resolveMandatoryResourceAsInputStream(CamelContext camelContext, String uri) throws IOException {
if (uri.startsWith("ref:")) {
String ref = uri.substring(4);
String value = CamelContextHelper.mandatoryLookup(camelContext, ref, String.class);
return new ByteArrayInputStream(value.getBytes());
} else if (uri.startsWith("bean:")) {
String bean = uri.substring(5);
if (bean.contains(".")) {
String method = StringHelper.after(bean, ".");
bean = StringHelper.before(bean, ".") + "?method=" + method;
}
Exchange dummy = new DefaultExchange(camelContext);
Object out = camelContext.resolveLanguage("bean").createExpression(bean).evaluate(dummy, Object.class);
if (dummy.getException() != null) {
IOException io = new IOException("Cannot find resource: " + uri + " from calling the bean");
io.initCause(dummy.getException());
throw io;
}
if (out != null) {
InputStream is = camelContext.getTypeConverter().tryConvertTo(InputStream.class, dummy, out);
if (is == null) {
String text = camelContext.getTypeConverter().tryConvertTo(String.class, dummy, out);
if (text != null) {
return new ByteArrayInputStream(text.getBytes());
}
} else {
return is;
}
} else {
throw new IOException("Cannot find resource: " + uri + " from calling the bean");
}
}
InputStream is = resolveResourceAsInputStream(camelContext.getClassResolver(), uri);
if (is == null) {
String resolvedName = resolveUriPath(uri);
throw new FileNotFoundException("Cannot find resource: " + resolvedName + " in classpath for URI: " + uri);
} else {
return is;
}
}
Aggregations