Search in sources :

Example 11 with DataType

use of org.apache.camel.spi.DataType in project camel by apache.

the class ContractAdvice method applyTransformerChain.

private boolean applyTransformerChain(Message message, DataType from, DataType to) throws Exception {
    CamelContext context = message.getExchange().getContext();
    Transformer fromTransformer = context.resolveTransformer(from.getModel());
    Transformer toTransformer = context.resolveTransformer(to.getModel());
    if (fromTransformer != null && toTransformer != null) {
        LOG.debug("Applying transformer 1/2: from='{}', to='{}', transformer='{}'", from, to, fromTransformer);
        fromTransformer.transform(message, from, new DataType(Object.class));
        LOG.debug("Applying transformer 2/2: from='{}', to='{}', transformer='{}'", from, to, toTransformer);
        toTransformer.transform(message, new DataType(Object.class), to);
        return true;
    }
    return false;
}
Also used : CamelContext(org.apache.camel.CamelContext) Transformer(org.apache.camel.spi.Transformer) DataType(org.apache.camel.spi.DataType)

Example 12 with DataType

use of org.apache.camel.spi.DataType in project camel by apache.

the class ContractAdvice method before.

@Override
public Object before(Exchange exchange) throws Exception {
    if (!(exchange.getIn() instanceof DataTypeAware)) {
        return null;
    }
    DataTypeAware target = (DataTypeAware) exchange.getIn();
    DataType from = target.getDataType();
    DataType to = contract.getInputType();
    if (to != null) {
        if (!to.equals(from)) {
            LOG.debug("Looking for transformer for INPUT: from='{}', to='{}'", from, to);
            doTransform(exchange.getIn(), from, to);
            target.setDataType(to);
        }
        if (contract.isValidateInput()) {
            doValidate(exchange.getIn(), to);
        }
    }
    return null;
}
Also used : DataTypeAware(org.apache.camel.spi.DataTypeAware) DataType(org.apache.camel.spi.DataType)

Example 13 with DataType

use of org.apache.camel.spi.DataType in project camel by apache.

the class TransformerBuilderTest method testEndpointTransformer.

public void testEndpointTransformer() throws Exception {
    CamelContext ctx = new DefaultCamelContext();
    RouteBuilder builder = new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            transformer().fromType("json:foo").toType("xml:bar").withUri("direct:transformer");
            from("direct:transformer").log("test");
        }
    };
    ctx.addRoutes(builder);
    ctx.start();
    Transformer transformer = ctx.resolveTransformer(new DataType("json:foo"), new DataType("xml:bar"));
    assertNotNull(transformer);
    assertEquals(ProcessorTransformer.class, transformer.getClass());
    ProcessorTransformer pt = (ProcessorTransformer) transformer;
    Field f = ProcessorTransformer.class.getDeclaredField("processor");
    f.setAccessible(true);
    Object processor = f.get(pt);
    assertEquals(SendProcessor.class, processor.getClass());
    SendProcessor sp = (SendProcessor) processor;
    assertEquals("direct://transformer", sp.getEndpoint().getEndpointUri());
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) Field(java.lang.reflect.Field) ProcessorTransformer(org.apache.camel.impl.transformer.ProcessorTransformer) Transformer(org.apache.camel.spi.Transformer) DataFormatTransformer(org.apache.camel.impl.transformer.DataFormatTransformer) DataType(org.apache.camel.spi.DataType) SendProcessor(org.apache.camel.processor.SendProcessor) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProcessorTransformer(org.apache.camel.impl.transformer.ProcessorTransformer)

Example 14 with DataType

use of org.apache.camel.spi.DataType in project camel by apache.

the class DefaultTransformerRegistry method resolveTransformer.

@Override
public Transformer resolveTransformer(TransformerKey key) {
    if (ObjectHelper.isEmpty(key.getScheme()) && key.getTo() == null) {
        return null;
    }
    // try exact match
    Transformer answer = get(aliasMap.containsKey(key) ? aliasMap.get(key) : key);
    if (answer != null || ObjectHelper.isNotEmpty(key.getScheme())) {
        return answer;
    }
    // try wildcard match for next - add an alias if matched
    TransformerKey alias = null;
    if (key.getFrom() != null && ObjectHelper.isNotEmpty(key.getFrom().getName())) {
        alias = new TransformerKey(new DataType(key.getFrom().getModel()), key.getTo());
        answer = get(alias);
    }
    if (answer == null && ObjectHelper.isNotEmpty(key.getTo().getName())) {
        alias = new TransformerKey(key.getFrom(), new DataType(key.getTo().getModel()));
        answer = get(alias);
    }
    if (answer == null && key.getFrom() != null && ObjectHelper.isNotEmpty(key.getFrom().getName()) && ObjectHelper.isNotEmpty(key.getTo().getName())) {
        alias = new TransformerKey(new DataType(key.getFrom().getModel()), new DataType(key.getTo().getModel()));
        answer = get(alias);
    }
    if (answer == null && key.getFrom() != null) {
        alias = new TransformerKey(key.getFrom().getModel());
        answer = get(alias);
    }
    if (answer == null) {
        alias = new TransformerKey(key.getTo().getModel());
        answer = get(alias);
    }
    if (answer != null) {
        aliasMap.put(key, alias);
    }
    return answer;
}
Also used : Transformer(org.apache.camel.spi.Transformer) TransformerKey(org.apache.camel.impl.transformer.TransformerKey) DataType(org.apache.camel.spi.DataType)

Example 15 with DataType

use of org.apache.camel.spi.DataType in project camel by apache.

the class RestBindingAdvice method marshal.

private void marshal(Exchange exchange, Map<String, Object> state) {
    // only marshal if there was no exception
    if (exchange.getException() != null) {
        return;
    }
    if (skipBindingOnErrorCode) {
        Integer code = exchange.hasOut() ? exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class) : exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
        // if there is a custom http error code then skip binding
        if (code != null && code >= 300) {
            return;
        }
    }
    boolean isXml = false;
    boolean isJson = false;
    // accept takes precedence
    String accept = (String) state.get(STATE_KEY_ACCEPT);
    if (accept != null) {
        isXml = accept.toLowerCase(Locale.ENGLISH).contains("xml");
        isJson = accept.toLowerCase(Locale.ENGLISH).contains("json");
    }
    // fallback to content type if still undecided
    if (!isXml && !isJson) {
        String contentType = ExchangeHelper.getContentType(exchange);
        if (contentType != null) {
            isXml = contentType.toLowerCase(Locale.ENGLISH).contains("xml");
            isJson = contentType.toLowerCase(Locale.ENGLISH).contains("json");
        }
    }
    // that information in the consumes
    if (!isXml && !isJson) {
        isXml = produces != null && produces.toLowerCase(Locale.ENGLISH).contains("xml");
        isJson = produces != null && produces.toLowerCase(Locale.ENGLISH).contains("json");
    }
    // only allow xml/json if the binding mode allows that (when off we still want to know if its xml or json)
    if (bindingMode != null) {
        isXml &= bindingMode.equals("off") || bindingMode.equals("auto") || bindingMode.contains("xml");
        isJson &= bindingMode.equals("off") || bindingMode.equals("auto") || bindingMode.contains("json");
        // if we do not yet know if its xml or json, then use the binding mode to know the mode
        if (!isJson && !isXml) {
            isXml = bindingMode.equals("auto") || bindingMode.contains("xml");
            isJson = bindingMode.equals("auto") || bindingMode.contains("json");
        }
    }
    // in case we have not yet been able to determine if xml or json, then use the same as in the unmarshaller
    if (isXml && isJson) {
        isXml = state.get(STATE_KEY_DO_MARSHAL).equals(STATE_XML);
        isJson = !isXml;
    }
    // need to prepare exchange first
    ExchangeHelper.prepareOutToIn(exchange);
    // ensure there is a content type header (even if binding is off)
    ensureHeaderContentType(produces, isXml, isJson, exchange);
    if (bindingMode == null || "off".equals(bindingMode)) {
        // binding is off, so no message body binding
        return;
    }
    // is there any marshaller at all
    if (jsonMarshal == null && xmlMarshal == null) {
        return;
    }
    // is the body empty
    if ((exchange.hasOut() && exchange.getOut().getBody() == null) || (!exchange.hasOut() && exchange.getIn().getBody() == null)) {
        return;
    }
    String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
    // need to lower-case so the contains check below can match if using upper case
    contentType = contentType.toLowerCase(Locale.US);
    try {
        // favor json over xml
        if (isJson && jsonMarshal != null) {
            // only marshal if its json content type
            if (contentType.contains("json")) {
                jsonMarshal.process(exchange);
                setOutputDataType(exchange, new DataType("json"));
            }
        } else if (isXml && xmlMarshal != null) {
            // only marshal if its xml content type
            if (contentType.contains("xml")) {
                xmlMarshal.process(exchange);
                setOutputDataType(exchange, new DataType("xml"));
            }
        } else {
            // we could not bind
            if (bindingMode.equals("auto")) {
            // okay for auto we do not mind if we could not bind
            } else {
                if (bindingMode.contains("xml")) {
                    exchange.setException(new BindingException("Cannot bind to xml as message body is not xml compatible", exchange));
                } else {
                    exchange.setException(new BindingException("Cannot bind to json as message body is not json compatible", exchange));
                }
            }
        }
    } catch (Throwable e) {
        exchange.setException(e);
    }
}
Also used : DataType(org.apache.camel.spi.DataType) BindingException(org.apache.camel.processor.binding.BindingException)

Aggregations

DataType (org.apache.camel.spi.DataType)15 DataTypeAware (org.apache.camel.spi.DataTypeAware)8 Exchange (org.apache.camel.Exchange)5 Transformer (org.apache.camel.spi.Transformer)5 CamelContext (org.apache.camel.CamelContext)4 Processor (org.apache.camel.Processor)3 Order (org.apache.camel.example.transformer.demo.Order)3 Test (org.junit.Test)3 Field (java.lang.reflect.Field)2 CompositeData (javax.management.openmbean.CompositeData)2 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)2 CompositeType (javax.management.openmbean.CompositeType)2 TabularData (javax.management.openmbean.TabularData)2 TabularDataSupport (javax.management.openmbean.TabularDataSupport)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 OrderResponse (org.apache.camel.example.transformer.demo.OrderResponse)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 DataFormatTransformer (org.apache.camel.impl.transformer.DataFormatTransformer)2 ProcessorTransformer (org.apache.camel.impl.transformer.ProcessorTransformer)2 BindingException (org.apache.camel.processor.binding.BindingException)2