Search in sources :

Example 41 with DataFormat

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

the class DefaultCamelContextResolverTest method testDataformatWithBothNames.

@Test
public void testDataformatWithBothNames() throws Exception {
    DataFormat dataFormat = context.resolveDataFormat("red");
    assertNotNull("DataFormat not found in registry", dataFormat);
    assertTrue("Wrong instance type of the DataFormat", dataFormat instanceof SampleDataFormat);
    assertFalse("Here we should NOT have the fallback DataFormat", ((SampleDataFormat) dataFormat).isFallback());
}
Also used : DataFormat(org.apache.camel.spi.DataFormat) Test(org.junit.Test)

Example 42 with DataFormat

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

the class DataFormatProvider method getDataFormat.

private DataFormat getDataFormat(MediaType mt) {
    String type = JAXRSUtils.mediaTypeToString(mt);
    DataFormat format = formats.get(type);
    if (format != null) {
        return format;
    }
    int subtypeIndex = type.lastIndexOf('+');
    if (subtypeIndex != -1) {
        // example, application/json+v1, should still be handled by JSON
        // handler, etc
        format = formats.get(type.substring(0, subtypeIndex));
    }
    if (format == null && formats.containsKey(MediaType.WILDCARD)) {
        format = formats.get(MediaType.WILDCARD);
    }
    return format;
}
Also used : DataFormat(org.apache.camel.spi.DataFormat)

Example 43 with DataFormat

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

the class RestBindingDefinition method createRestBindingAdvice.

public RestBindingAdvice createRestBindingAdvice(RouteContext routeContext) throws Exception {
    CamelContext context = routeContext.getCamelContext();
    RestConfiguration config = context.getRestConfiguration(component, true);
    // these options can be overridden per rest verb
    String mode = config.getBindingMode().name();
    if (bindingMode != null) {
        mode = bindingMode.name();
    }
    boolean cors = config.isEnableCORS();
    if (enableCORS != null) {
        cors = enableCORS;
    }
    boolean skip = config.isSkipBindingOnErrorCode();
    if (skipBindingOnErrorCode != null) {
        skip = skipBindingOnErrorCode;
    }
    // cors headers
    Map<String, String> corsHeaders = config.getCorsHeaders();
    if (mode == null || "off".equals(mode)) {
        // binding mode is off, so create a off mode binding processor
        return new RestBindingAdvice(context, null, null, null, null, consumes, produces, mode, skip, cors, corsHeaders, defaultValues);
    }
    // setup json data format
    String name = config.getJsonDataFormat();
    if (name != null) {
        // must only be a name, not refer to an existing instance
        Object instance = context.getRegistry().lookupByName(name);
        if (instance != null) {
            throw new IllegalArgumentException("JsonDataFormat name: " + name + " must not be an existing bean instance from the registry");
        }
    } else {
        name = "json-jackson";
    }
    // this will create a new instance as the name was not already pre-created
    DataFormat json = context.resolveDataFormat(name);
    DataFormat outJson = context.resolveDataFormat(name);
    // is json binding required?
    if (mode.contains("json") && json == null) {
        throw new IllegalArgumentException("JSon DataFormat " + name + " not found.");
    }
    if (json != null) {
        Class<?> clazz = null;
        if (type != null) {
            String typeName = type.endsWith("[]") ? type.substring(0, type.length() - 2) : type;
            clazz = context.getClassResolver().resolveMandatoryClass(typeName);
        }
        if (clazz != null) {
            IntrospectionSupport.setProperty(context.getTypeConverter(), json, "unmarshalType", clazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), json, "useList", type.endsWith("[]"));
        }
        setAdditionalConfiguration(config, context, json, "json.in.");
        Class<?> outClazz = null;
        if (outType != null) {
            String typeName = outType.endsWith("[]") ? outType.substring(0, outType.length() - 2) : outType;
            outClazz = context.getClassResolver().resolveMandatoryClass(typeName);
        }
        if (outClazz != null) {
            IntrospectionSupport.setProperty(context.getTypeConverter(), outJson, "unmarshalType", outClazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), outJson, "useList", outType.endsWith("[]"));
        }
        setAdditionalConfiguration(config, context, outJson, "json.out.");
    }
    // setup xml data format
    name = config.getXmlDataFormat();
    if (name != null) {
        // must only be a name, not refer to an existing instance
        Object instance = context.getRegistry().lookupByName(name);
        if (instance != null) {
            throw new IllegalArgumentException("XmlDataFormat name: " + name + " must not be an existing bean instance from the registry");
        }
    } else {
        name = "jaxb";
    }
    // this will create a new instance as the name was not already pre-created
    DataFormat jaxb = context.resolveDataFormat(name);
    DataFormat outJaxb = context.resolveDataFormat(name);
    // is xml binding required?
    if (mode.contains("xml") && jaxb == null) {
        throw new IllegalArgumentException("XML DataFormat " + name + " not found.");
    }
    if (jaxb != null) {
        Class<?> clazz = null;
        if (type != null) {
            String typeName = type.endsWith("[]") ? type.substring(0, type.length() - 2) : type;
            clazz = context.getClassResolver().resolveMandatoryClass(typeName);
        }
        if (clazz != null) {
            JAXBContext jc = JAXBContext.newInstance(clazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), jaxb, "context", jc);
        }
        setAdditionalConfiguration(config, context, jaxb, "xml.in.");
        Class<?> outClazz = null;
        if (outType != null) {
            String typeName = outType.endsWith("[]") ? outType.substring(0, outType.length() - 2) : outType;
            outClazz = context.getClassResolver().resolveMandatoryClass(typeName);
        }
        if (outClazz != null) {
            JAXBContext jc = JAXBContext.newInstance(outClazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), outJaxb, "context", jc);
        } else if (clazz != null) {
            // fallback and use the context from the input
            JAXBContext jc = JAXBContext.newInstance(clazz);
            IntrospectionSupport.setProperty(context.getTypeConverter(), outJaxb, "context", jc);
        }
        setAdditionalConfiguration(config, context, outJaxb, "xml.out.");
    }
    return new RestBindingAdvice(context, json, jaxb, outJson, outJaxb, consumes, produces, mode, skip, cors, corsHeaders, defaultValues);
}
Also used : CamelContext(org.apache.camel.CamelContext) DataFormat(org.apache.camel.spi.DataFormat) RestConfiguration(org.apache.camel.spi.RestConfiguration) JAXBContext(javax.xml.bind.JAXBContext) RestBindingAdvice(org.apache.camel.processor.RestBindingAdvice)

Example 44 with DataFormat

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

the class DefaultCamelContextResolverTest method testNullLookupDataFormat.

@Test
public void testNullLookupDataFormat() throws Exception {
    DataFormat dataFormat = context.resolveDataFormat("xxxxxxxxx");
    assertNull("Non-existent DataFormat should be null", dataFormat);
}
Also used : DataFormat(org.apache.camel.spi.DataFormat) Test(org.junit.Test)

Example 45 with DataFormat

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

the class BeanIODataFormatSimpleTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // START SNIPPET: e1
            // setup beanio data format using the mapping file, loaded from the classpath
            DataFormat format = new BeanIODataFormat("org/apache/camel/dataformat/beanio/mappings.xml", "employeeFile");
            // a route which uses the bean io data format to format a CSV data
            // to java objects
            from("direct:unmarshal").unmarshal(format).split(body()).to("mock:beanio-unmarshal");
            // convert list of java objects back to flat format
            from("direct:marshal").marshal(format).to("mock:beanio-marshal");
        // END SNIPPET: e1
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) DataFormat(org.apache.camel.spi.DataFormat)

Aggregations

DataFormat (org.apache.camel.spi.DataFormat)45 RouteBuilder (org.apache.camel.builder.RouteBuilder)20 Exchange (org.apache.camel.Exchange)8 Processor (org.apache.camel.Processor)8 JaxbDataFormat (org.apache.camel.converter.jaxb.JaxbDataFormat)6 CamelContext (org.apache.camel.CamelContext)5 Test (org.junit.Test)5 JAXBContext (javax.xml.bind.JAXBContext)2 Rfc5425FrameDecoder (org.apache.camel.component.syslog.netty.Rfc5425FrameDecoder)2 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)2 JndiRegistry (org.apache.camel.impl.JndiRegistry)2 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)2 QRY_A19 (ca.uhn.hl7v2.model.v24.message.QRY_A19)1 QRD (ca.uhn.hl7v2.model.v24.segment.QRD)1 PipeParser (ca.uhn.hl7v2.parser.PipeParser)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Key (java.security.Key)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1