001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.builder; 018 019 import java.util.Map; 020 import java.util.zip.Deflater; 021 022 import org.w3c.dom.Node; 023 024 025 import org.apache.camel.model.DataFormatDefinition; 026 import org.apache.camel.model.ProcessorDefinition; 027 import org.apache.camel.model.dataformat.AvroDataFormat; 028 import org.apache.camel.model.dataformat.BindyDataFormat; 029 import org.apache.camel.model.dataformat.BindyType; 030 import org.apache.camel.model.dataformat.C24IOContentType; 031 import org.apache.camel.model.dataformat.C24IODataFormat; 032 import org.apache.camel.model.dataformat.CastorDataFormat; 033 import org.apache.camel.model.dataformat.CsvDataFormat; 034 import org.apache.camel.model.dataformat.CustomDataFormat; 035 import org.apache.camel.model.dataformat.GzipDataFormat; 036 import org.apache.camel.model.dataformat.HL7DataFormat; 037 import org.apache.camel.model.dataformat.JaxbDataFormat; 038 import org.apache.camel.model.dataformat.JibxDataFormat; 039 import org.apache.camel.model.dataformat.JsonDataFormat; 040 import org.apache.camel.model.dataformat.JsonLibrary; 041 import org.apache.camel.model.dataformat.PGPDataFormat; 042 import org.apache.camel.model.dataformat.ProtobufDataFormat; 043 import org.apache.camel.model.dataformat.RssDataFormat; 044 import org.apache.camel.model.dataformat.SerializationDataFormat; 045 import org.apache.camel.model.dataformat.SoapJaxbDataFormat; 046 import org.apache.camel.model.dataformat.StringDataFormat; 047 import org.apache.camel.model.dataformat.SyslogDataFormat; 048 import org.apache.camel.model.dataformat.TidyMarkupDataFormat; 049 import org.apache.camel.model.dataformat.XMLBeansDataFormat; 050 import org.apache.camel.model.dataformat.XMLSecurityDataFormat; 051 import org.apache.camel.model.dataformat.XStreamDataFormat; 052 import org.apache.camel.model.dataformat.XmlJsonDataFormat; 053 import org.apache.camel.model.dataformat.ZipDataFormat; 054 import org.apache.camel.util.jsse.KeyStoreParameters; 055 056 057 /** 058 * An expression for constructing the different possible {@link org.apache.camel.spi.DataFormat} 059 * options. 060 * 061 * @version 062 */ 063 public class DataFormatClause<T extends ProcessorDefinition<?>> { 064 private final T processorType; 065 private final Operation operation; 066 067 /** 068 * {@link org.apache.camel.spi.DataFormat} operations. 069 */ 070 public enum Operation { 071 Marshal, Unmarshal 072 } 073 074 public DataFormatClause(T processorType, Operation operation) { 075 this.processorType = processorType; 076 this.operation = operation; 077 } 078 079 080 /** 081 * Uses the Avro data format 082 */ 083 public T avro() { 084 return dataFormat(new AvroDataFormat()); 085 } 086 087 public T avro(Object schema) { 088 AvroDataFormat dataFormat = new AvroDataFormat(); 089 dataFormat.setSchema(schema); 090 return dataFormat(dataFormat); 091 } 092 093 public T avro(String instanceClassName) { 094 return dataFormat(new AvroDataFormat(instanceClassName)); 095 } 096 097 /** 098 * Uses the Bindy data format 099 * 100 * @param type the type of bindy data format to use 101 * @param packages packages to scan for Bindy annotated POJO classes 102 */ 103 public T bindy(BindyType type, String... packages) { 104 BindyDataFormat bindy = new BindyDataFormat(); 105 bindy.setType(type); 106 bindy.setPackages(packages); 107 return dataFormat(bindy); 108 } 109 110 /** 111 * Uses the Bindy data format 112 * 113 * @param type the type of bindy data format to use 114 * @param classType the POJO class type 115 */ 116 public T bindy(BindyType type, Class<?> classType) { 117 BindyDataFormat bindy = new BindyDataFormat(); 118 bindy.setType(type); 119 bindy.setClassType(classType); 120 return dataFormat(bindy); 121 } 122 123 /** 124 * Uses the 125 * <a href="http://fabric.fusesource.org/documentation/camel/c24io.html">C24IO</a> 126 * data format for dealing with lots of different message formats such as SWIFT etc. 127 */ 128 public T c24io() { 129 return dataFormat(new C24IODataFormat()); 130 } 131 132 /** 133 * Uses the 134 * <a href="http://fabric.fusesource.org/documentation/camel/c24io.html">C24IO</a> 135 * data format with the specified type of ComplexDataObject 136 * for marshalling and unmarshalling messages using the dataObject's default Source and Sink. 137 */ 138 public T c24io(Class<?> dataObjectType) { 139 return dataFormat(new C24IODataFormat(dataObjectType)); 140 } 141 142 143 /** 144 * Uses the 145 * <a href="http://fabric.fusesource.org/documentation/camel/c24io.html">C24IO</a> 146 * data format with the specified type of ComplexDataObject 147 * for marshalling and unmarshalling messages using the dataObject's default Source and Sink. 148 */ 149 public T c24io(Class<?> elementType, C24IOContentType contentType) { 150 return dataFormat(new C24IODataFormat(elementType, contentType)); 151 } 152 153 /** 154 * Uses the 155 * <a href="http://fabric.fusesource.org/documentation/camel/c24io.html">C24IO</a> 156 * data format with the specified content type 157 * for marshalling and unmarshalling messages 158 */ 159 public T c24io(C24IOContentType contentType) { 160 return dataFormat(new C24IODataFormat(contentType)); 161 } 162 163 /** 164 * Uses the CSV data format 165 */ 166 public T csv() { 167 return dataFormat(new CsvDataFormat()); 168 } 169 170 /** 171 * Uses the custom data format 172 */ 173 public T custom(String ref) { 174 return dataFormat(new CustomDataFormat(ref)); 175 } 176 177 /** 178 * Uses the Castor data format 179 */ 180 public T castor() { 181 return dataFormat(new CastorDataFormat()); 182 } 183 184 /** 185 * Uses the Castor data format 186 * 187 * @param mappingFile name of mapping file to locate in classpath 188 */ 189 public T castor(String mappingFile) { 190 CastorDataFormat castor = new CastorDataFormat(); 191 castor.setMappingFile(mappingFile); 192 return dataFormat(castor); 193 } 194 195 /** 196 * Uses the Castor data format 197 * 198 * @param mappingFile name of mapping file to locate in classpath 199 * @param validation whether validation is enabled or not 200 */ 201 public T castor(String mappingFile, boolean validation) { 202 CastorDataFormat castor = new CastorDataFormat(); 203 castor.setMappingFile(mappingFile); 204 castor.setValidation(validation); 205 return dataFormat(castor); 206 } 207 208 /** 209 * Uses the GZIP deflater data format 210 */ 211 public T gzip() { 212 GzipDataFormat gzdf = new GzipDataFormat(); 213 return dataFormat(gzdf); 214 } 215 216 /** 217 * Uses the HL7 data format 218 */ 219 public T hl7() { 220 return dataFormat(new HL7DataFormat()); 221 } 222 223 /** 224 * Uses the HL7 data format 225 */ 226 public T hl7(boolean validate) { 227 HL7DataFormat hl7 = new HL7DataFormat(); 228 hl7.setValidate(validate); 229 return dataFormat(hl7); 230 } 231 232 /** 233 * Uses the PGP data format 234 */ 235 public T pgp(String keyFileName, String keyUserid) { 236 PGPDataFormat pgp = new PGPDataFormat(); 237 pgp.setKeyFileName(keyFileName); 238 pgp.setKeyUserid(keyUserid); 239 return dataFormat(pgp); 240 } 241 242 /** 243 * Uses the PGP data format 244 */ 245 public T pgp(String keyFileName, String keyUserid, String password) { 246 PGPDataFormat pgp = new PGPDataFormat(); 247 pgp.setKeyFileName(keyFileName); 248 pgp.setKeyUserid(keyUserid); 249 pgp.setPassword(password); 250 return dataFormat(pgp); 251 } 252 253 /** 254 * Uses the PGP data format 255 */ 256 public T pgp(String keyFileName, String keyUserid, String password, boolean armored, boolean integrity) { 257 PGPDataFormat pgp = new PGPDataFormat(); 258 pgp.setKeyFileName(keyFileName); 259 pgp.setKeyUserid(keyUserid); 260 pgp.setPassword(password); 261 pgp.setArmored(armored); 262 pgp.setIntegrity(integrity); 263 return dataFormat(pgp); 264 } 265 266 /** 267 * Uses the JAXB data format 268 */ 269 public T jaxb() { 270 return dataFormat(new JaxbDataFormat()); 271 } 272 273 /** 274 * Uses the JAXB data format with context path 275 */ 276 public T jaxb(String contextPath) { 277 JaxbDataFormat dataFormat = new JaxbDataFormat(); 278 dataFormat.setContextPath(contextPath); 279 return dataFormat(dataFormat); 280 } 281 282 /** 283 * Uses the JAXB data format turning pretty printing on or off 284 */ 285 public T jaxb(boolean prettyPrint) { 286 return dataFormat(new JaxbDataFormat(prettyPrint)); 287 } 288 289 /** 290 * Uses the JiBX data format. 291 */ 292 public T jibx() { 293 return dataFormat(new JibxDataFormat()); 294 } 295 296 /** 297 * Uses the JiBX data format with unmarshall class. 298 */ 299 public T jibx(Class<?> unmarshallClass) { 300 return dataFormat(new JibxDataFormat(unmarshallClass)); 301 } 302 303 /** 304 * Uses the JSON data format using the XStream json library 305 */ 306 public T json() { 307 return dataFormat(new JsonDataFormat()); 308 } 309 310 /** 311 * Uses the JSON data format 312 * 313 * @param library the json library to use 314 */ 315 public T json(JsonLibrary library) { 316 return dataFormat(new JsonDataFormat(library)); 317 } 318 319 /** 320 * Uses the JSON data format 321 * 322 * @param type the json type to use 323 * @param unmarshalType unmarshal type for json jackson type 324 */ 325 public T json(JsonLibrary type, Class<?> unmarshalType) { 326 JsonDataFormat json = new JsonDataFormat(type); 327 json.setUnmarshalType(unmarshalType); 328 return dataFormat(json); 329 } 330 331 /** 332 * Uses the protobuf data format 333 */ 334 public T protobuf() { 335 return dataFormat(new ProtobufDataFormat()); 336 } 337 338 public T protobuf(Object defaultInstance) { 339 ProtobufDataFormat dataFormat = new ProtobufDataFormat(); 340 dataFormat.setDefaultInstance(defaultInstance); 341 return dataFormat(dataFormat); 342 } 343 344 public T protobuf(String instanceClassName) { 345 return dataFormat(new ProtobufDataFormat(instanceClassName)); 346 } 347 348 /** 349 * Uses the RSS data format 350 */ 351 public T rss() { 352 return dataFormat(new RssDataFormat()); 353 } 354 355 /** 356 * Uses the Java Serialization data format 357 */ 358 public T serialization() { 359 return dataFormat(new SerializationDataFormat()); 360 } 361 362 /** 363 * Uses the Soap 1.1 JAXB data format 364 */ 365 public T soapjaxb() { 366 return dataFormat(new SoapJaxbDataFormat()); 367 } 368 369 /** 370 * Uses the Soap 1.1 JAXB data format 371 */ 372 public T soapjaxb(String contextPath) { 373 return dataFormat(new SoapJaxbDataFormat(contextPath)); 374 } 375 376 /** 377 * Uses the Soap 1.1 JAXB data format 378 */ 379 public T soapjaxb(String contextPath, String elementNameStrategyRef) { 380 return dataFormat(new SoapJaxbDataFormat(contextPath, elementNameStrategyRef)); 381 } 382 383 /** 384 * Uses the Soap 1.1 JAXB data format 385 */ 386 public T soapjaxb(String contextPath, Object elementNameStrategy) { 387 return dataFormat(new SoapJaxbDataFormat(contextPath, elementNameStrategy)); 388 } 389 390 /** 391 * Uses the Soap 1.2 JAXB data format 392 */ 393 public T soapjaxb12() { 394 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(); 395 soap.setVersion("1.2"); 396 return dataFormat(soap); 397 } 398 399 /** 400 * Uses the Soap 1.2 JAXB data format 401 */ 402 public T soapjaxb12(String contextPath) { 403 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath); 404 soap.setVersion("1.2"); 405 return dataFormat(soap); 406 } 407 408 /** 409 * Uses the Soap 1.2 JAXB data format 410 */ 411 public T soapjaxb12(String contextPath, String elementNameStrategyRef) { 412 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath, elementNameStrategyRef); 413 soap.setVersion("1.2"); 414 return dataFormat(soap); 415 } 416 417 /** 418 * Uses the Soap JAXB data format 419 */ 420 public T soapjaxb12(String contextPath, Object elementNameStrategy) { 421 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath, elementNameStrategy); 422 soap.setVersion("1.2"); 423 return dataFormat(soap); 424 } 425 426 /** 427 * Uses the String data format 428 */ 429 public T string() { 430 return string(null); 431 } 432 433 /** 434 * Uses the String data format supporting encoding using given charset 435 */ 436 public T string(String charset) { 437 StringDataFormat sdf = new StringDataFormat(); 438 sdf.setCharset(charset); 439 return dataFormat(sdf); 440 } 441 442 /** 443 * Uses the Syslog data format 444 */ 445 public T syslog() { 446 return dataFormat(new SyslogDataFormat()); 447 } 448 449 /** 450 * Return WellFormed HTML (an XML Document) either 451 * {@link java.lang.String} or {@link org.w3c.dom.Node} 452 */ 453 public T tidyMarkup(Class<?> dataObjectType) { 454 return dataFormat(new TidyMarkupDataFormat(dataObjectType)); 455 } 456 457 /** 458 * Return TidyMarkup in the default format 459 * as {@link org.w3c.dom.Node} 460 */ 461 public T tidyMarkup() { 462 return dataFormat(new TidyMarkupDataFormat(Node.class)); 463 } 464 465 /** 466 * Uses the XStream data format 467 */ 468 public T xstream() { 469 return dataFormat(new XStreamDataFormat()); 470 } 471 472 /** 473 * Uses the xstream by setting the encoding 474 */ 475 public T xstream(String encoding) { 476 return dataFormat(new XStreamDataFormat(encoding)); 477 } 478 479 /** 480 * Uses the XML Security data format 481 */ 482 public T secureXML() { 483 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(); 484 return dataFormat(xsdf); 485 } 486 487 /** 488 * Uses the XML Security data format 489 */ 490 public T secureXML(String secureTag, boolean secureTagContents) { 491 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents); 492 return dataFormat(xsdf); 493 } 494 495 /** 496 * Uses the XML Security data format 497 */ 498 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents) { 499 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents); 500 return dataFormat(xsdf); 501 } 502 503 /** 504 * Uses the XML Security data format 505 */ 506 public T secureXML(String secureTag, boolean secureTagContents, String passPhrase) { 507 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase); 508 return dataFormat(xsdf); 509 } 510 511 /** 512 * Uses the XML Security data format 513 */ 514 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase) { 515 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, passPhrase); 516 return dataFormat(xsdf); 517 } 518 519 /** 520 * Uses the XML Security data format 521 */ 522 public T secureXML(String secureTag, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) { 523 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase, xmlCipherAlgorithm); 524 return dataFormat(xsdf); 525 } 526 527 528 /** 529 * Uses the XML Security data format 530 */ 531 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) { 532 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, passPhrase, xmlCipherAlgorithm); 533 return dataFormat(xsdf); 534 } 535 536 /** 537 * @deprectaed Use {@link #secureXML(String, Map, boolean, String, String, String, String) instead. 538 * Uses the XML Security data format 539 */ 540 @Deprecated 541 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 542 String keyCipherAlgorithm) { 543 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, keyCipherAlgorithm); 544 return dataFormat(xsdf); 545 } 546 547 /** 548 * Uses the XML Security data format 549 */ 550 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 551 String keyCipherAlgorithm, String keyOrTrustStoreParametersId) { 552 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 553 keyCipherAlgorithm, keyOrTrustStoreParametersId); 554 return dataFormat(xsdf); 555 } 556 557 /** 558 * Uses the XML Security data format 559 */ 560 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 561 String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) { 562 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 563 keyCipherAlgorithm, keyOrTrustStoreParametersId, keyPassword); 564 return dataFormat(xsdf); 565 } 566 567 /** 568 * Uses the XML Security data format 569 */ 570 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 571 String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) { 572 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 573 keyCipherAlgorithm, keyOrTrustStoreParameters); 574 return dataFormat(xsdf); 575 } 576 577 /** 578 * Uses the XML Security data format 579 */ 580 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 581 String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) { 582 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 583 keyCipherAlgorithm, keyOrTrustStoreParameters, keyPassword); 584 return dataFormat(xsdf); 585 } 586 587 /** 588 * Uses the XML Security data format 589 */ 590 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 591 String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) { 592 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 593 keyCipherAlgorithm, keyOrTrustStoreParametersId); 594 return dataFormat(xsdf); 595 } 596 597 /** 598 * Uses the XML Security data format 599 */ 600 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 601 String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) { 602 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 603 keyCipherAlgorithm, keyOrTrustStoreParametersId, keyPassword); 604 return dataFormat(xsdf); 605 } 606 607 /** 608 * Uses the XML Security data format 609 */ 610 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 611 String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) { 612 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 613 keyCipherAlgorithm, keyOrTrustStoreParameters); 614 return dataFormat(xsdf); 615 } 616 617 /** 618 * Uses the XML Security data format 619 */ 620 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 621 String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) { 622 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 623 keyCipherAlgorithm, keyOrTrustStoreParameters, keyPassword); 624 return dataFormat(xsdf); 625 } 626 627 /** 628 * Uses the xmlBeans data format 629 */ 630 public T xmlBeans() { 631 return dataFormat(new XMLBeansDataFormat()); 632 } 633 634 /** 635 * Uses the xmljson dataformat, based on json-lib 636 */ 637 public T xmljson() { 638 return dataFormat(new XmlJsonDataFormat()); 639 } 640 641 /** 642 * Uses the xmljson dataformat, based on json-lib, initializing custom options with a Map 643 */ 644 public T xmljson(Map<String, String> options) { 645 return dataFormat(new XmlJsonDataFormat(options)); 646 } 647 648 /** 649 * Uses the ZIP deflater data format 650 */ 651 public T zip() { 652 ZipDataFormat zdf = new ZipDataFormat(Deflater.DEFAULT_COMPRESSION); 653 return dataFormat(zdf); 654 } 655 656 /** 657 * Uses the ZIP deflater data format 658 */ 659 public T zip(int compressionLevel) { 660 ZipDataFormat zdf = new ZipDataFormat(compressionLevel); 661 return dataFormat(zdf); 662 } 663 664 @SuppressWarnings("unchecked") 665 private T dataFormat(DataFormatDefinition dataFormatType) { 666 switch (operation) { 667 case Unmarshal: 668 return (T) processorType.unmarshal(dataFormatType); 669 case Marshal: 670 return (T) processorType.marshal(dataFormatType); 671 default: 672 throw new IllegalArgumentException("Unknown DataFormat operation: " + operation); 673 } 674 } 675 }