Search in sources :

Example 16 with IOException

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();
    }
}
Also used : Exchange(org.apache.camel.Exchange) Language(org.apache.camel.spi.Language) Expression(org.apache.camel.Expression) IOException(java.io.IOException)

Example 17 with IOException

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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Converter(org.apache.camel.Converter)

Example 18 with IOException

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()));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) IOException(java.io.IOException) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 19 with IOException

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());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) IOException(java.io.IOException) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 20 with IOException

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());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) IOException(java.io.IOException) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Aggregations

IOException (java.io.IOException)41104 File (java.io.File)7663 InputStream (java.io.InputStream)4105 Test (org.junit.Test)3557 ArrayList (java.util.ArrayList)3023 FileInputStream (java.io.FileInputStream)2674 FileOutputStream (java.io.FileOutputStream)2358 FileNotFoundException (java.io.FileNotFoundException)2146 BufferedReader (java.io.BufferedReader)2028 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1761 InputStreamReader (java.io.InputStreamReader)1677 URL (java.net.URL)1608 HashMap (java.util.HashMap)1552 ByteArrayInputStream (java.io.ByteArrayInputStream)1534 OutputStream (java.io.OutputStream)1349 Path (org.apache.hadoop.fs.Path)1316 Map (java.util.Map)1212 List (java.util.List)1042 Properties (java.util.Properties)994 ServletException (javax.servlet.ServletException)916