|
| 1 | +[[YAMLDataFormat-YAML]] |
| 2 | +YAML |
| 3 | +~~~~ |
| 4 | + |
| 5 | +YAML is a link:data-format.html[Data Format] to marshal and unmarshal |
| 6 | +Java objects to and from http://www.yaml.org/[YAML]. |
| 7 | + |
| 8 | +For YAML to object marshalling, Camel provides integration with three |
| 9 | +popular YAML libraries: |
| 10 | + |
| 11 | +* The http://www.snakeyaml.org/[SnakeYAML] library |
| 12 | +
|
| 13 | +Every library requires adding the special camel component (see |
| 14 | +"Dependency..." paragraphs further down). By default Camel uses the |
| 15 | +SnakeYAML library. |
| 16 | + |
| 17 | +[[YAML-Options]] |
| 18 | +YAML Options |
| 19 | +^^^^^^^^^^^^ |
| 20 | + |
| 21 | +// dataformat options: START |
| 22 | +The YAML SnakeYAML dataformat supports 10 options which are listed below. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +{% raw %} |
| 27 | +[width="100%",cols="2s,1m,1m,6",options="header"] |
| 28 | +|======================================================================= |
| 29 | +| Name | Default | Java Type | Description |
| 30 | +| library | SnakeYAML | YAMLLibrary | Which yaml library to use such. Is by default SnakeYAML |
| 31 | +| unmarshalTypeName | | String | Class name of the java type to use when unarmshalling |
| 32 | +| constructor | | String | BaseConstructor to construct incoming documents. |
| 33 | +| representer | | String | Representer to emit outgoing objects. |
| 34 | +| dumperOptions | | String | DumperOptions to configure outgoing objects. |
| 35 | +| resolver | | String | Resolver to detect implicit type |
| 36 | +| useApplicationContextClassLoader | true | Boolean | Use ApplicationContextClassLoader as custom ClassLoader |
| 37 | +| prettyFlow | false | Boolean | Force the emitter to produce a pretty YAML document when using the flow style. |
| 38 | +| allowAnyType | false | Boolean | Allow any class to be un-marshaled |
| 39 | +| typeFilter | | List | Set the types SnakeYAML is allowed to un-marshall |
| 40 | +|======================================================================= |
| 41 | +{% endraw %} |
| 42 | +// dataformat options: END |
| 43 | + |
| 44 | +WARNING: SnakeYAML can load any class from YAML definition which may lead to security breach so by default, SnakeYAML DataForma restrict the object it can load to standard Java objects like List or Long. If you want to load custom POJOs you need to add theirs type to SnakeYAML DataFormat type filter list. If your source is trusted, you can set the property allowAnyType to true so SnakeYAML DataForma won't perform any filter on the types. |
| 45 | + |
| 46 | +[[YAMLDataFormat-UsingYAMLdataformatwiththeSnakeYAMLlibrary]] |
| 47 | +Using YAML data format with the SnakeYAML library |
| 48 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 49 | + |
| 50 | +- Turn Object messages into yaml then send to MQSeries |
| 51 | ++ |
| 52 | +[source,java] |
| 53 | +------------------------------------------------------------ |
| 54 | +from("activemq:My.Queue") |
| 55 | + .marshal().yaml() |
| 56 | + .to("mqseries:Another.Queue"); |
| 57 | +------------------------------------------------------------ |
| 58 | ++ |
| 59 | +[source,java] |
| 60 | +------------------------------------------------------------ |
| 61 | +from("activemq:My.Queue") |
| 62 | + .marshal().yaml(YAMLLibrary.SnakeYAML) |
| 63 | + .to("mqseries:Another.Queue"); |
| 64 | +------------------------------------------------------------ |
| 65 | +
|
| 66 | +- Restrict classes to be loaded from YAML |
| 67 | ++ |
| 68 | +[source,java] |
| 69 | +------------------------------------------------------------ |
| 70 | +// Creat a SnakeYAMLDataFormat instance |
| 71 | +SnakeYAMLDataFormat yaml = new SnakeYAMLDataFormat(); |
| 72 | +
|
| 73 | +// Restrict classes to be loaded from YAML |
| 74 | +yaml.addTypeFilters(TypeFilters.types(MyPojo.class, MyOtherPojo.class)); |
| 75 | + |
| 76 | +from("activemq:My.Queue") |
| 77 | + .unmarshal(yaml) |
| 78 | + .to("mqseries:Another.Queue"); |
| 79 | +------------------------------------------------------------ |
| 80 | + |
| 81 | +[[YAMLDataFormat-UsingYAMLinSpringDSL]] |
| 82 | +Using YAML in Spring DSL |
| 83 | +^^^^^^^^^^^^^^^^^^^^^^^^ |
| 84 | + |
| 85 | +When using link:data-format.html[Data Format] in Spring DSL you need to |
| 86 | +declare the data formats first. This is done in the *DataFormats* XML |
| 87 | +tag. |
| 88 | + |
| 89 | +[source,xml] |
| 90 | +-------------------------------------------------------------------------------- |
| 91 | +<dataFormats> |
| 92 | + <!-- |
| 93 | + here we define a YAML data format with the id snake and that it should use |
| 94 | + the TestPojo as the class type when doing unmarshal. The unmarshalTypeName |
| 95 | + is optional |
| 96 | + --> |
| 97 | + <yaml |
| 98 | + id="snake" |
| 99 | + library="SnakeYAML" |
| 100 | + unmarshalTypeName="org.apache.camel.component.yaml.model.TestPojo"/> |
| 101 | +
|
| 102 | + <!-- |
| 103 | + here we define a YAML data format with the id snake-safe which restricts the |
| 104 | + classes to be loaded from YAML to TestPojo and those belonging to package |
| 105 | + com.mycompany |
| 106 | + --> |
| 107 | + <yaml id="snake-safe"> |
| 108 | + <typeFilter value="org.apache.camel.component.yaml.model.TestPojo"/> |
| 109 | + <typeFilter value="com.mycompany\..*" type="regexp"/> |
| 110 | + </yaml> |
| 111 | +</dataFormats> |
| 112 | +-------------------------------------------------------------------------------- |
| 113 | + |
| 114 | +And then you can refer to those ids in the route: |
| 115 | + |
| 116 | +[source,xml] |
| 117 | +------------------------------------- |
| 118 | + <route> |
| 119 | + <from uri="direct:unmarshal"/> |
| 120 | + <unmarshal> |
| 121 | + <custom ref="snake"/> |
| 122 | + </unmarshal> |
| 123 | + <to uri="mock:unmarshal"/> |
| 124 | + </route> |
| 125 | + <route> |
| 126 | + <from uri="direct:unmarshal-safe"/> |
| 127 | + <unmarshal> |
| 128 | + <custom ref="snake-safe"/> |
| 129 | + </unmarshal> |
| 130 | + <to uri="mock:unmarshal-safe"/> |
| 131 | + </route> |
| 132 | +------------------------------------- |
| 133 | + |
| 134 | + |
| 135 | +[[YAMLDataFormat-DependenciesforSnakeYAML]] |
| 136 | +Dependencies for SnakeYAML |
| 137 | +^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 138 | + |
| 139 | +To use YAML in your camel routes you need to add the a dependency |
| 140 | +on *camel-snakeyaml* which implements this data format. |
| 141 | + |
| 142 | +If you use maven you could just add the following to your pom.xml, |
| 143 | +substituting the version number for the latest & greatest release |
| 144 | +(see link:download.html[the download page for the latest versions]). |
| 145 | + |
| 146 | +[source,xml] |
| 147 | +------------------------------------------ |
| 148 | +<dependency> |
| 149 | + <groupId>org.apache.camel</groupId> |
| 150 | + <artifactId>camel-snakeyaml</artifactId> |
| 151 | + <version>${camel-version}</version> |
| 152 | +</dependency> |
| 153 | +------------------------------------------ |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
0 commit comments