use of java.io.IOException in project camel by apache.
the class ExpressionMapper method convert.
@Override
public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
try {
if (currentExchange.get() == null) {
throw new IllegalStateException("Current exchange has not been set for ExpressionMapper");
}
Expression exp;
// Resolve the language being used for this expression and evaluate
Exchange exchange = currentExchange.get();
Language expLang = exchange.getContext().resolveLanguage(getLanguagePart());
String scheme = getSchemePart();
if (scheme != null && (scheme.equalsIgnoreCase("classpath") || scheme.equalsIgnoreCase("file") || scheme.equalsIgnoreCase("http"))) {
String path = getPathPart();
try {
exp = expLang.createExpression(resolveScript(scheme + ":" + path));
} catch (IOException e) {
throw new IllegalStateException("Expression script specified but not found", e);
}
} else {
exp = expLang.createExpression(getExpressionPart());
}
return exp.evaluate(exchange, destinationClass);
} finally {
done();
}
}
use of java.io.IOException in project camel by apache.
the class ElasticsearchActionRequestConverter method toSearchRequest.
@Converter
public static SearchRequest toSearchRequest(Object queryObject, Exchange exchange) {
SearchRequest searchRequest = new SearchRequest(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class)).types(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
String queryText = null;
if (queryObject instanceof Map<?, ?>) {
Map<String, Object> mapQuery = (Map<String, Object>) queryObject;
// Remove 'query' prefix from the query object for backward compatibility
if (mapQuery.containsKey(ElasticsearchConstants.ES_QUERY_DSL_PREFIX)) {
mapQuery = (Map<String, Object>) mapQuery.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
}
try {
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
queryText = contentBuilder.map(mapQuery).string();
} catch (IOException e) {
LOG.error(e.getMessage());
}
} else if (queryObject instanceof String) {
queryText = (String) queryObject;
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode jsonTextObject = mapper.readValue(queryText, JsonNode.class);
JsonNode parentJsonNode = jsonTextObject.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
if (parentJsonNode != null) {
queryText = parentJsonNode.toString();
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
} else {
// Cannot convert the queryObject into SearchRequest
return null;
}
searchSourceBuilder.query(QueryBuilders.wrapperQuery(queryText));
searchRequest.source(searchSourceBuilder);
return searchRequest;
}
use of java.io.IOException in project camel by apache.
the class ExecProducerTest method testInputLines.
@Test
@DirtiesContext
public void testInputLines() throws IOException {
// String must be convertible to InputStream
final String input = "line1" + LINE_SEPARATOR + "line2";
producerTemplate.send(new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(input);
}
});
assertEquals(input, IOUtils.toString(execCommandExecutorMock.lastCommandResult.getCommand().getInput()));
}
use of java.io.IOException in project camel by apache.
the class ExecProducerTest method testNullInBody.
@Test
@DirtiesContext
public void testNullInBody() throws IOException {
// Null body must also be supported
Exchange e = producerTemplate.send(new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(null);
}
});
ExecResult result = e.getIn().getBody(ExecResult.class);
assertNotNull(result);
assertNull(result.getCommand().getInput());
}
use of java.io.IOException in project camel by apache.
the class ExecProducerTest method testInputLinesNotConvertibleToInputStream.
@Test
@DirtiesContext
public void testInputLinesNotConvertibleToInputStream() throws IOException {
// String must be convertible to InputStream
final Integer notConvertibleToInputStreamBody = new Integer(1);
Exchange e = producerTemplate.send(new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(notConvertibleToInputStreamBody);
}
});
ExecResult result = e.getIn().getBody(ExecResult.class);
assertNotNull(result);
assertNull(result.getCommand().getInput());
}
Aggregations