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 */ 017package org.apache.camel.builder; 018 019import java.nio.charset.Charset; 020import java.util.Map; 021import java.util.zip.Deflater; 022 023import org.w3c.dom.Node; 024 025import org.apache.camel.model.DataFormatDefinition; 026import org.apache.camel.model.ProcessorDefinition; 027import org.apache.camel.model.dataformat.AvroDataFormat; 028import org.apache.camel.model.dataformat.Base64DataFormat; 029import org.apache.camel.model.dataformat.BeanioDataFormat; 030import org.apache.camel.model.dataformat.BindyDataFormat; 031import org.apache.camel.model.dataformat.BindyType; 032import org.apache.camel.model.dataformat.BoonDataFormat; 033import org.apache.camel.model.dataformat.CastorDataFormat; 034import org.apache.camel.model.dataformat.CsvDataFormat; 035import org.apache.camel.model.dataformat.CustomDataFormat; 036import org.apache.camel.model.dataformat.GzipDataFormat; 037import org.apache.camel.model.dataformat.HL7DataFormat; 038import org.apache.camel.model.dataformat.HessianDataFormat; 039import org.apache.camel.model.dataformat.IcalDataFormat; 040import org.apache.camel.model.dataformat.JacksonXMLDataFormat; 041import org.apache.camel.model.dataformat.JaxbDataFormat; 042import org.apache.camel.model.dataformat.JibxDataFormat; 043import org.apache.camel.model.dataformat.JsonDataFormat; 044import org.apache.camel.model.dataformat.JsonLibrary; 045import org.apache.camel.model.dataformat.LZFDataFormat; 046import org.apache.camel.model.dataformat.MimeMultipartDataFormat; 047import org.apache.camel.model.dataformat.PGPDataFormat; 048import org.apache.camel.model.dataformat.ProtobufDataFormat; 049import org.apache.camel.model.dataformat.RssDataFormat; 050import org.apache.camel.model.dataformat.SerializationDataFormat; 051import org.apache.camel.model.dataformat.SoapJaxbDataFormat; 052import org.apache.camel.model.dataformat.StringDataFormat; 053import org.apache.camel.model.dataformat.SyslogDataFormat; 054import org.apache.camel.model.dataformat.TidyMarkupDataFormat; 055import org.apache.camel.model.dataformat.XMLBeansDataFormat; 056import org.apache.camel.model.dataformat.XMLSecurityDataFormat; 057import org.apache.camel.model.dataformat.XStreamDataFormat; 058import org.apache.camel.model.dataformat.XmlJsonDataFormat; 059import org.apache.camel.model.dataformat.YAMLDataFormat; 060import org.apache.camel.model.dataformat.YAMLLibrary; 061import org.apache.camel.model.dataformat.ZipDataFormat; 062import org.apache.camel.model.dataformat.ZipFileDataFormat; 063import org.apache.camel.util.CollectionStringBuffer; 064import org.apache.camel.util.jsse.KeyStoreParameters; 065 066/** 067 * An expression for constructing the different possible {@link org.apache.camel.spi.DataFormat} 068 * options. 069 * 070 * @version 071 */ 072public class DataFormatClause<T extends ProcessorDefinition<?>> { 073 private final T processorType; 074 private final Operation operation; 075 076 /** 077 * {@link org.apache.camel.spi.DataFormat} operations. 078 */ 079 public enum Operation { 080 Marshal, Unmarshal 081 } 082 083 public DataFormatClause(T processorType, Operation operation) { 084 this.processorType = processorType; 085 this.operation = operation; 086 } 087 088 /** 089 * Uses the Avro data format 090 */ 091 public T avro() { 092 return dataFormat(new AvroDataFormat()); 093 } 094 095 public T avro(Object schema) { 096 AvroDataFormat dataFormat = new AvroDataFormat(); 097 dataFormat.setSchema(schema); 098 return dataFormat(dataFormat); 099 } 100 101 public T avro(String instanceClassName) { 102 return dataFormat(new AvroDataFormat(instanceClassName)); 103 } 104 105 /** 106 * Uses the base64 data format 107 */ 108 public T base64() { 109 Base64DataFormat dataFormat = new Base64DataFormat(); 110 return dataFormat(dataFormat); 111 } 112 113 /** 114 * Uses the base64 data format 115 */ 116 public T base64(int lineLength, String lineSeparator, boolean urlSafe) { 117 Base64DataFormat dataFormat = new Base64DataFormat(); 118 dataFormat.setLineLength(lineLength); 119 dataFormat.setLineSeparator(lineSeparator); 120 dataFormat.setUrlSafe(urlSafe); 121 return dataFormat(dataFormat); 122 } 123 124 /** 125 * Uses the beanio data format 126 */ 127 public T beanio(String mapping, String streamName) { 128 BeanioDataFormat dataFormat = new BeanioDataFormat(); 129 dataFormat.setMapping(mapping); 130 dataFormat.setStreamName(streamName); 131 return dataFormat(dataFormat); 132 } 133 134 /** 135 * Uses the beanio data format 136 */ 137 public T beanio(String mapping, String streamName, String encoding) { 138 BeanioDataFormat dataFormat = new BeanioDataFormat(); 139 dataFormat.setMapping(mapping); 140 dataFormat.setStreamName(streamName); 141 dataFormat.setEncoding(encoding); 142 return dataFormat(dataFormat); 143 } 144 145 /** 146 * Uses the beanio data format 147 */ 148 public T beanio(String mapping, String streamName, String encoding, 149 boolean ignoreUnidentifiedRecords, boolean ignoreUnexpectedRecords, boolean ignoreInvalidRecords) { 150 BeanioDataFormat dataFormat = new BeanioDataFormat(); 151 dataFormat.setMapping(mapping); 152 dataFormat.setStreamName(streamName); 153 dataFormat.setEncoding(encoding); 154 dataFormat.setIgnoreUnidentifiedRecords(ignoreUnidentifiedRecords); 155 dataFormat.setIgnoreUnexpectedRecords(ignoreUnexpectedRecords); 156 dataFormat.setIgnoreInvalidRecords(ignoreInvalidRecords); 157 return dataFormat(dataFormat); 158 } 159 160 /** 161 * Uses the beanio data format 162 */ 163 public T beanio(String mapping, String streamName, String encoding, String beanReaderErrorHandlerType) { 164 BeanioDataFormat dataFormat = new BeanioDataFormat(); 165 dataFormat.setMapping(mapping); 166 dataFormat.setStreamName(streamName); 167 dataFormat.setEncoding(encoding); 168 dataFormat.setBeanReaderErrorHandlerType(beanReaderErrorHandlerType); 169 return dataFormat(dataFormat); 170 } 171 172 /** 173 * Uses the Bindy data format 174 * 175 * @param type the type of bindy data format to use 176 * @param classType the POJO class type 177 */ 178 public T bindy(BindyType type, Class<?> classType) { 179 BindyDataFormat bindy = new BindyDataFormat(); 180 bindy.setType(type); 181 bindy.setClassType(classType); 182 return dataFormat(bindy); 183 } 184 185 /** 186 * Uses the Boon data format 187 * 188 * @param classType the POJO class type 189 */ 190 public T boon(Class<?> classType) { 191 BoonDataFormat boon = new BoonDataFormat(); 192 boon.setUnmarshalType(classType); 193 return dataFormat(boon); 194 } 195 196 /** 197 * Uses the CSV data format 198 */ 199 public T csv() { 200 return dataFormat(new CsvDataFormat()); 201 } 202 203 /** 204 * Uses the CSV data format for a huge file. 205 * Sequential access through an iterator. 206 */ 207 public T csvLazyLoad() { 208 return dataFormat(new CsvDataFormat(true)); 209 } 210 211 /** 212 * Uses the custom data format 213 */ 214 public T custom(String ref) { 215 return dataFormat(new CustomDataFormat(ref)); 216 } 217 218 /** 219 * Uses the Castor data format 220 */ 221 public T castor() { 222 return dataFormat(new CastorDataFormat()); 223 } 224 225 /** 226 * Uses the Castor data format 227 * 228 * @param mappingFile name of mapping file to locate in classpath 229 */ 230 public T castor(String mappingFile) { 231 CastorDataFormat castor = new CastorDataFormat(); 232 castor.setMappingFile(mappingFile); 233 return dataFormat(castor); 234 } 235 236 /** 237 * Uses the Castor data format 238 * 239 * @param mappingFile name of mapping file to locate in classpath 240 * @param validation whether validation is enabled or not 241 */ 242 public T castor(String mappingFile, boolean validation) { 243 CastorDataFormat castor = new CastorDataFormat(); 244 castor.setMappingFile(mappingFile); 245 castor.setValidation(validation); 246 return dataFormat(castor); 247 } 248 249 /** 250 * Uses the GZIP deflater data format 251 */ 252 public T gzip() { 253 GzipDataFormat gzdf = new GzipDataFormat(); 254 return dataFormat(gzdf); 255 } 256 257 /** 258 * Uses the Hessian data format 259 */ 260 public T hessian() { 261 return dataFormat(new HessianDataFormat()); 262 } 263 264 /** 265 * Uses the HL7 data format 266 */ 267 public T hl7() { 268 return dataFormat(new HL7DataFormat()); 269 } 270 271 /** 272 * Uses the HL7 data format 273 */ 274 public T hl7(boolean validate) { 275 HL7DataFormat hl7 = new HL7DataFormat(); 276 hl7.setValidate(validate); 277 return dataFormat(hl7); 278 } 279 280 /** 281 * Uses the HL7 data format 282 */ 283 public T hl7(Object parser) { 284 HL7DataFormat hl7 = new HL7DataFormat(); 285 hl7.setParser(parser); 286 return dataFormat(hl7); 287 } 288 289 /** 290 * Uses the iCal data format 291 */ 292 public T ical(boolean validating) { 293 IcalDataFormat ical = new IcalDataFormat(); 294 ical.setValidating(validating); 295 return dataFormat(ical); 296 } 297 298 /** 299 * Uses the LZF deflater data format 300 */ 301 public T lzf() { 302 LZFDataFormat lzfdf = new LZFDataFormat(); 303 return dataFormat(lzfdf); 304 } 305 306 /** 307 * Uses the MIME Multipart data format 308 */ 309 public T mimeMultipart() { 310 MimeMultipartDataFormat mm = new MimeMultipartDataFormat(); 311 return dataFormat(mm); 312 } 313 314 /** 315 * Uses the MIME Multipart data format 316 * 317 * @param multipartSubType Specifies the subtype of the MIME Multipart 318 */ 319 public T mimeMultipart(String multipartSubType) { 320 MimeMultipartDataFormat mm = new MimeMultipartDataFormat(); 321 mm.setMultipartSubType(multipartSubType); 322 return dataFormat(mm); 323 } 324 325 /** 326 * Uses the MIME Multipart data format 327 * 328 * @param multipartSubType the subtype of the MIME Multipart 329 * @param multipartWithoutAttachment defines whether a message without attachment is also marshaled 330 * into a MIME Multipart (with only one body part). 331 * @param headersInline define the MIME Multipart headers as part of the message body 332 * or as Camel headers 333 * @param binaryContent have binary encoding for binary content (true) or use Base-64 334 * encoding for binary content (false) 335 */ 336 public T mimeMultipart(String multipartSubType, boolean multipartWithoutAttachment, boolean headersInline, 337 boolean binaryContent) { 338 MimeMultipartDataFormat mm = new MimeMultipartDataFormat(); 339 mm.setMultipartSubType(multipartSubType); 340 mm.setMultipartWithoutAttachment(multipartWithoutAttachment); 341 mm.setHeadersInline(headersInline); 342 mm.setBinaryContent(binaryContent); 343 return dataFormat(mm); 344 } 345 346 /** 347 * Uses the MIME Multipart data format 348 * 349 * @param multipartSubType the subtype of the MIME Multipart 350 * @param multipartWithoutAttachment defines whether a message without attachment is also marshaled 351 * into a MIME Multipart (with only one body part). 352 * @param headersInline define the MIME Multipart headers as part of the message body 353 * or as Camel headers 354 * @param includeHeadeers if headersInline is set to true all camel headers matching this 355 * regex are also stored as MIME headers on the Multipart 356 * @param binaryContent have binary encoding for binary content (true) or use Base-64 357 * encoding for binary content (false) 358 */ 359 public T mimeMultipart(String multipartSubType, boolean multipartWithoutAttachment, boolean headersInline, 360 String includeHeaders, boolean binaryContent) { 361 MimeMultipartDataFormat mm = new MimeMultipartDataFormat(); 362 mm.setMultipartSubType(multipartSubType); 363 mm.setMultipartWithoutAttachment(multipartWithoutAttachment); 364 mm.setHeadersInline(headersInline); 365 mm.setIncludeHeaders(includeHeaders); 366 mm.setBinaryContent(binaryContent); 367 return dataFormat(mm); 368 } 369 370 /** 371 * Uses the MIME Multipart data format 372 * 373 * @param multipartWithoutAttachment defines whether a message without attachment is also marshaled 374 * into a MIME Multipart (with only one body part). 375 * @param headersInline define the MIME Multipart headers as part of the message body 376 * or as Camel headers 377 * @param binaryContent have binary encoding for binary content (true) or use Base-64 378 * encoding for binary content (false) 379 */ 380 public T mimeMultipart(boolean multipartWithoutAttachment, boolean headersInline, 381 boolean binaryContent) { 382 MimeMultipartDataFormat mm = new MimeMultipartDataFormat(); 383 mm.setMultipartWithoutAttachment(multipartWithoutAttachment); 384 mm.setHeadersInline(headersInline); 385 mm.setBinaryContent(binaryContent); 386 return dataFormat(mm); 387 } 388 389 /** 390 * Uses the PGP data format 391 */ 392 public T pgp(String keyFileName, String keyUserid) { 393 PGPDataFormat pgp = new PGPDataFormat(); 394 pgp.setKeyFileName(keyFileName); 395 pgp.setKeyUserid(keyUserid); 396 return dataFormat(pgp); 397 } 398 399 /** 400 * Uses the PGP data format 401 */ 402 public T pgp(String keyFileName, String keyUserid, String password) { 403 PGPDataFormat pgp = new PGPDataFormat(); 404 pgp.setKeyFileName(keyFileName); 405 pgp.setKeyUserid(keyUserid); 406 pgp.setPassword(password); 407 return dataFormat(pgp); 408 } 409 410 /** 411 * Uses the PGP data format 412 */ 413 public T pgp(String keyFileName, String keyUserid, String password, boolean armored, boolean integrity) { 414 PGPDataFormat pgp = new PGPDataFormat(); 415 pgp.setKeyFileName(keyFileName); 416 pgp.setKeyUserid(keyUserid); 417 pgp.setPassword(password); 418 pgp.setArmored(armored); 419 pgp.setIntegrity(integrity); 420 return dataFormat(pgp); 421 } 422 423 /** 424 * Uses the Jackson XML data format 425 */ 426 public T jacksonxml() { 427 return dataFormat(new JacksonXMLDataFormat()); 428 } 429 430 /** 431 * Uses the Jackson XML data format 432 * 433 * @param unmarshalType 434 * unmarshal type for xml jackson type 435 */ 436 public T jacksonxml(Class<?> unmarshalType) { 437 JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); 438 jacksonXMLDataFormat.setUnmarshalType(unmarshalType); 439 return dataFormat(jacksonXMLDataFormat); 440 } 441 442 /** 443 * Uses the Jackson XML data format 444 * 445 * @param unmarshalType 446 * unmarshal type for xml jackson type 447 * @param jsonView 448 * the view type for xml jackson type 449 */ 450 public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView) { 451 JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); 452 jacksonXMLDataFormat.setUnmarshalType(unmarshalType); 453 jacksonXMLDataFormat.setJsonView(jsonView); 454 return dataFormat(jacksonXMLDataFormat); 455 } 456 457 /** 458 * Uses the Jackson XML data format using the Jackson library turning pretty 459 * printing on or off 460 * 461 * @param prettyPrint 462 * turn pretty printing on or off 463 */ 464 public T jacksonxml(boolean prettyPrint) { 465 JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); 466 jacksonXMLDataFormat.setPrettyPrint(prettyPrint); 467 return dataFormat(jacksonXMLDataFormat); 468 } 469 470 /** 471 * Uses the Jackson XML data format 472 * 473 * @param unmarshalType 474 * unmarshal type for xml jackson type 475 * @param prettyPrint 476 * turn pretty printing on or off 477 */ 478 public T jacksonxml(Class<?> unmarshalType, boolean prettyPrint) { 479 JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); 480 jacksonXMLDataFormat.setUnmarshalType(unmarshalType); 481 jacksonXMLDataFormat.setPrettyPrint(prettyPrint); 482 return dataFormat(jacksonXMLDataFormat); 483 } 484 485 /** 486 * Uses the Jackson XML data format 487 * 488 * @param unmarshalType 489 * unmarshal type for xml jackson type 490 * @param jsonView 491 * the view type for xml jackson type 492 * @param prettyPrint 493 * turn pretty printing on or off 494 */ 495 public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView, boolean prettyPrint) { 496 JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); 497 jacksonXMLDataFormat.setUnmarshalType(unmarshalType); 498 jacksonXMLDataFormat.setJsonView(jsonView); 499 jacksonXMLDataFormat.setPrettyPrint(prettyPrint); 500 return dataFormat(jacksonXMLDataFormat); 501 } 502 503 /** 504 * Uses the Jackson XML data format 505 * 506 * @param unmarshalType 507 * unmarshal type for xml jackson type 508 * @param jsonView 509 * the view type for xml jackson type 510 * @param include 511 * include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc. 512 */ 513 public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView, String include) { 514 JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); 515 jacksonXMLDataFormat.setUnmarshalType(unmarshalType); 516 jacksonXMLDataFormat.setJsonView(jsonView); 517 jacksonXMLDataFormat.setInclude(include); 518 return dataFormat(jacksonXMLDataFormat); 519 } 520 521 /** 522 * Uses the Jackson XML data format 523 * 524 * @param unmarshalType 525 * unmarshal type for xml jackson type 526 * @param jsonView 527 * the view type for xml jackson type 528 * @param include 529 * include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc. 530 * @param prettyPrint 531 * turn pretty printing on or off 532 */ 533 public T jacksonxml(Class<?> unmarshalType, Class<?> jsonView, String include, boolean prettyPrint) { 534 JacksonXMLDataFormat jacksonXMLDataFormat = new JacksonXMLDataFormat(); 535 jacksonXMLDataFormat.setUnmarshalType(unmarshalType); 536 jacksonXMLDataFormat.setJsonView(jsonView); 537 jacksonXMLDataFormat.setInclude(include); 538 jacksonXMLDataFormat.setPrettyPrint(prettyPrint); 539 return dataFormat(jacksonXMLDataFormat); 540 } 541 542 /** 543 * Uses the JAXB data format 544 */ 545 public T jaxb() { 546 return dataFormat(new JaxbDataFormat()); 547 } 548 549 /** 550 * Uses the JAXB data format with context path 551 */ 552 public T jaxb(String contextPath) { 553 JaxbDataFormat dataFormat = new JaxbDataFormat(); 554 dataFormat.setContextPath(contextPath); 555 return dataFormat(dataFormat); 556 } 557 558 /** 559 * Uses the JAXB data format turning pretty printing on or off 560 */ 561 public T jaxb(boolean prettyPrint) { 562 return dataFormat(new JaxbDataFormat(prettyPrint)); 563 } 564 565 /** 566 * Uses the JiBX data format. 567 */ 568 public T jibx() { 569 return dataFormat(new JibxDataFormat()); 570 } 571 572 /** 573 * Uses the JiBX data format with unmarshall class. 574 */ 575 public T jibx(Class<?> unmarshallClass) { 576 return dataFormat(new JibxDataFormat(unmarshallClass)); 577 } 578 579 /** 580 * Uses the JSON data format using the XStream json library 581 */ 582 public T json() { 583 return dataFormat(new JsonDataFormat()); 584 } 585 586 /** 587 * Uses the JSON data format using the XStream json library turning pretty printing on or off 588 * 589 * @param prettyPrint turn pretty printing on or off 590 */ 591 public T json(boolean prettyPrint) { 592 JsonDataFormat json = new JsonDataFormat(); 593 json.setPrettyPrint(prettyPrint); 594 return dataFormat(json); 595 } 596 597 /** 598 * Uses the JSON data format 599 * 600 * @param library the json library to use 601 */ 602 public T json(JsonLibrary library) { 603 return dataFormat(new JsonDataFormat(library)); 604 } 605 606 /** 607 * Uses the JSON data format 608 * 609 * @param library the json library to use 610 * @param prettyPrint turn pretty printing on or off 611 */ 612 public T json(JsonLibrary library, boolean prettyPrint) { 613 JsonDataFormat json = new JsonDataFormat(library); 614 json.setPrettyPrint(prettyPrint); 615 return dataFormat(json); 616 } 617 618 /** 619 * Uses the JSON data format 620 * 621 * @param type the json type to use 622 * @param unmarshalType unmarshal type for json jackson type 623 */ 624 public T json(JsonLibrary type, Class<?> unmarshalType) { 625 JsonDataFormat json = new JsonDataFormat(type); 626 json.setUnmarshalType(unmarshalType); 627 return dataFormat(json); 628 } 629 630 /** 631 * Uses the JSON data format 632 * 633 * @param type the json type to use 634 * @param unmarshalType unmarshal type for json jackson type 635 * @param prettyPrint turn pretty printing on or off 636 */ 637 public T json(JsonLibrary type, Class<?> unmarshalType, boolean prettyPrint) { 638 JsonDataFormat json = new JsonDataFormat(type); 639 json.setUnmarshalType(unmarshalType); 640 json.setPrettyPrint(prettyPrint); 641 return dataFormat(json); 642 } 643 644 /** 645 * Uses the Jackson JSON data format 646 * 647 * @param unmarshalType unmarshal type for json jackson type 648 * @param jsonView the view type for json jackson type 649 */ 650 public T json(Class<?> unmarshalType, Class<?> jsonView) { 651 JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson); 652 json.setUnmarshalType(unmarshalType); 653 json.setJsonView(jsonView); 654 return dataFormat(json); 655 } 656 657 /** 658 * Uses the Jackson JSON data format 659 * 660 * @param unmarshalType unmarshal type for json jackson type 661 * @param jsonView the view type for json jackson type 662 * @param prettyPrint turn pretty printing on or off 663 */ 664 public T json(Class<?> unmarshalType, Class<?> jsonView, boolean prettyPrint) { 665 JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson); 666 json.setUnmarshalType(unmarshalType); 667 json.setJsonView(jsonView); 668 json.setPrettyPrint(prettyPrint); 669 return dataFormat(json); 670 } 671 672 /** 673 * Uses the Jackson JSON data format 674 * 675 * @param unmarshalType unmarshal type for json jackson type 676 * @param jsonView the view type for json jackson type 677 * @param include include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc. 678 */ 679 public T json(Class<?> unmarshalType, Class<?> jsonView, String include) { 680 JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson); 681 json.setUnmarshalType(unmarshalType); 682 json.setJsonView(jsonView); 683 json.setInclude(include); 684 return dataFormat(json); 685 } 686 687 /** 688 * Uses the Jackson JSON data format 689 * 690 * @param unmarshalType unmarshal type for json jackson type 691 * @param jsonView the view type for json jackson type 692 * @param include include such as <tt>ALWAYS</tt>, <tt>NON_NULL</tt>, etc. 693 * @param prettyPrint turn pretty printing on or off 694 */ 695 public T json(Class<?> unmarshalType, Class<?> jsonView, String include, boolean prettyPrint) { 696 JsonDataFormat json = new JsonDataFormat(JsonLibrary.Jackson); 697 json.setUnmarshalType(unmarshalType); 698 json.setJsonView(jsonView); 699 json.setInclude(include); 700 json.setPrettyPrint(prettyPrint); 701 return dataFormat(json); 702 } 703 704 /** 705 * Uses the protobuf data format 706 */ 707 public T protobuf() { 708 return dataFormat(new ProtobufDataFormat()); 709 } 710 711 public T protobuf(Object defaultInstance) { 712 ProtobufDataFormat dataFormat = new ProtobufDataFormat(); 713 dataFormat.setDefaultInstance(defaultInstance); 714 return dataFormat(dataFormat); 715 } 716 717 public T protobuf(String instanceClassName) { 718 return dataFormat(new ProtobufDataFormat(instanceClassName)); 719 } 720 721 /** 722 * Uses the RSS data format 723 */ 724 public T rss() { 725 return dataFormat(new RssDataFormat()); 726 } 727 728 /** 729 * Uses the Java Serialization data format 730 */ 731 public T serialization() { 732 return dataFormat(new SerializationDataFormat()); 733 } 734 735 /** 736 * Uses the Soap 1.1 JAXB data format 737 */ 738 public T soapjaxb() { 739 return dataFormat(new SoapJaxbDataFormat()); 740 } 741 742 /** 743 * Uses the Soap 1.1 JAXB data format 744 */ 745 public T soapjaxb(String contextPath) { 746 return dataFormat(new SoapJaxbDataFormat(contextPath)); 747 } 748 749 /** 750 * Uses the Soap 1.1 JAXB data format 751 */ 752 public T soapjaxb(String contextPath, String elementNameStrategyRef) { 753 return dataFormat(new SoapJaxbDataFormat(contextPath, elementNameStrategyRef)); 754 } 755 756 /** 757 * Uses the Soap 1.1 JAXB data format 758 */ 759 public T soapjaxb(String contextPath, Object elementNameStrategy) { 760 return dataFormat(new SoapJaxbDataFormat(contextPath, elementNameStrategy)); 761 } 762 763 /** 764 * Uses the Soap 1.2 JAXB data format 765 */ 766 public T soapjaxb12() { 767 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(); 768 soap.setVersion("1.2"); 769 return dataFormat(soap); 770 } 771 772 /** 773 * Uses the Soap 1.2 JAXB data format 774 */ 775 public T soapjaxb12(String contextPath) { 776 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath); 777 soap.setVersion("1.2"); 778 return dataFormat(soap); 779 } 780 781 /** 782 * Uses the Soap 1.2 JAXB data format 783 */ 784 public T soapjaxb12(String contextPath, String elementNameStrategyRef) { 785 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath, elementNameStrategyRef); 786 soap.setVersion("1.2"); 787 return dataFormat(soap); 788 } 789 790 /** 791 * Uses the Soap JAXB data format 792 */ 793 public T soapjaxb12(String contextPath, Object elementNameStrategy) { 794 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath, elementNameStrategy); 795 soap.setVersion("1.2"); 796 return dataFormat(soap); 797 } 798 799 /** 800 * Uses the String data format 801 */ 802 public T string() { 803 return string(null); 804 } 805 806 /** 807 * Uses the String data format supporting encoding using given charset 808 */ 809 public T string(String charset) { 810 StringDataFormat sdf = new StringDataFormat(); 811 sdf.setCharset(charset); 812 return dataFormat(sdf); 813 } 814 815 /** 816 * Uses the Syslog data format 817 */ 818 public T syslog() { 819 return dataFormat(new SyslogDataFormat()); 820 } 821 822 /** 823 * Return WellFormed HTML (an XML Document) either 824 * {@link java.lang.String} or {@link org.w3c.dom.Node} 825 */ 826 public T tidyMarkup(Class<?> dataObjectType) { 827 return dataFormat(new TidyMarkupDataFormat(dataObjectType)); 828 } 829 830 /** 831 * Return TidyMarkup in the default format 832 * as {@link org.w3c.dom.Node} 833 */ 834 public T tidyMarkup() { 835 return dataFormat(new TidyMarkupDataFormat(Node.class)); 836 } 837 838 /** 839 * Uses the XStream data format. 840 * <p/> 841 * Favor using {@link #xstream(String)} to pass in a permission 842 */ 843 public T xstream() { 844 return dataFormat(new XStreamDataFormat()); 845 } 846 847 /** 848 * Uses the xstream by setting the encoding or permission 849 * 850 * @param encodingOrPermission is either an encoding or permission syntax 851 */ 852 public T xstream(String encodingOrPermission) { 853 // is it an encoding? if not we assume its a permission 854 if (Charset.isSupported(encodingOrPermission)) { 855 return xstream(encodingOrPermission, (String) null); 856 } else { 857 return xstream(null, encodingOrPermission); 858 } 859 } 860 861 /** 862 * Uses the xstream by setting the encoding 863 */ 864 public T xstream(String encoding, String permission) { 865 XStreamDataFormat xdf = new XStreamDataFormat(); 866 xdf.setPermissions(permission); 867 xdf.setEncoding(encoding); 868 return dataFormat(xdf); 869 } 870 871 /** 872 * Uses the xstream by permitting the java type 873 * 874 * @param type the pojo xstream should use as allowed permission 875 */ 876 public T xstream(Class<?> type) { 877 return xstream(null, type); 878 } 879 880 /** 881 * Uses the xstream by permitting the java type 882 * 883 * @param encoding encoding to use 884 * @param type the pojo class(es) xstream should use as allowed permission 885 */ 886 public T xstream(String encoding, Class<?>... type) { 887 CollectionStringBuffer csb = new CollectionStringBuffer(","); 888 for (Class<?> clazz : type) { 889 csb.append("+"); 890 csb.append(clazz.getName()); 891 } 892 return xstream(encoding, csb.toString()); 893 } 894 895 /** 896 * Uses the YAML data format 897 * 898 * @param library the yaml library to use 899 */ 900 public T yaml(YAMLLibrary library) { 901 return dataFormat(new YAMLDataFormat(library)); 902 } 903 904 /** 905 * Uses the YAML data format 906 * 907 * @param type the yaml type to use 908 * @param type the type for json snakeyaml type 909 */ 910 public T yaml(YAMLLibrary library, Class<?> type) { 911 return dataFormat(new YAMLDataFormat(library, type)); 912 } 913 914 /** 915 * Uses the XML Security data format 916 */ 917 public T secureXML() { 918 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(); 919 return dataFormat(xsdf); 920 } 921 922 /** 923 * Uses the XML Security data format 924 */ 925 public T secureXML(String secureTag, boolean secureTagContents) { 926 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents); 927 return dataFormat(xsdf); 928 } 929 930 /** 931 * Uses the XML Security data format 932 */ 933 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents) { 934 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents); 935 return dataFormat(xsdf); 936 } 937 938 /** 939 * Uses the XML Security data format 940 */ 941 public T secureXML(String secureTag, boolean secureTagContents, String passPhrase) { 942 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase); 943 return dataFormat(xsdf); 944 } 945 946 /** 947 * Uses the XML Security data format 948 */ 949 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase) { 950 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, passPhrase); 951 return dataFormat(xsdf); 952 } 953 954 /** 955 * Uses the XML Security data format 956 */ 957 public T secureXML(String secureTag, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) { 958 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase, xmlCipherAlgorithm); 959 return dataFormat(xsdf); 960 } 961 962 963 /** 964 * Uses the XML Security data format 965 */ 966 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) { 967 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, passPhrase, xmlCipherAlgorithm); 968 return dataFormat(xsdf); 969 } 970 971 /** 972 * @deprecated Use {@link #secureXML(String, Map, boolean, String, String, String, String)} instead. 973 * Uses the XML Security data format 974 */ 975 @Deprecated 976 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 977 String keyCipherAlgorithm) { 978 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, keyCipherAlgorithm); 979 return dataFormat(xsdf); 980 } 981 982 /** 983 * Uses the XML Security data format 984 */ 985 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 986 String keyCipherAlgorithm, String keyOrTrustStoreParametersId) { 987 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 988 keyCipherAlgorithm, keyOrTrustStoreParametersId); 989 return dataFormat(xsdf); 990 } 991 992 /** 993 * Uses the XML Security data format 994 */ 995 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 996 String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) { 997 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 998 keyCipherAlgorithm, keyOrTrustStoreParametersId, keyPassword); 999 return dataFormat(xsdf); 1000 } 1001 1002 /** 1003 * Uses the XML Security data format 1004 */ 1005 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 1006 String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) { 1007 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 1008 keyCipherAlgorithm, keyOrTrustStoreParameters); 1009 return dataFormat(xsdf); 1010 } 1011 1012 /** 1013 * Uses the XML Security data format 1014 */ 1015 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm, 1016 String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) { 1017 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 1018 keyCipherAlgorithm, keyOrTrustStoreParameters, keyPassword); 1019 return dataFormat(xsdf); 1020 } 1021 1022 /** 1023 * Uses the XML Security data format 1024 */ 1025 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 1026 String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) { 1027 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 1028 keyCipherAlgorithm, keyOrTrustStoreParametersId); 1029 return dataFormat(xsdf); 1030 } 1031 1032 /** 1033 * Uses the XML Security data format 1034 */ 1035 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 1036 String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId, String keyPassword) { 1037 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 1038 keyCipherAlgorithm, keyOrTrustStoreParametersId, keyPassword); 1039 return dataFormat(xsdf); 1040 } 1041 1042 /** 1043 * Uses the XML Security data format 1044 */ 1045 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 1046 String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) { 1047 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 1048 keyCipherAlgorithm, keyOrTrustStoreParameters); 1049 return dataFormat(xsdf); 1050 } 1051 1052 /** 1053 * Uses the XML Security data format 1054 */ 1055 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 1056 String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword) { 1057 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 1058 keyCipherAlgorithm, keyOrTrustStoreParameters, keyPassword); 1059 return dataFormat(xsdf); 1060 } 1061 1062 /** 1063 * Uses the XML Security data format 1064 */ 1065 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias, 1066 String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters, String keyPassword, 1067 String digestAlgorithm) { 1068 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, 1069 keyCipherAlgorithm, keyOrTrustStoreParameters, keyPassword, digestAlgorithm); 1070 return dataFormat(xsdf); 1071 } 1072 1073 /** 1074 * Uses the xmlBeans data format 1075 */ 1076 public T xmlBeans() { 1077 return dataFormat(new XMLBeansDataFormat()); 1078 } 1079 1080 /** 1081 * Uses the xmljson dataformat, based on json-lib 1082 */ 1083 public T xmljson() { 1084 return dataFormat(new XmlJsonDataFormat()); 1085 } 1086 1087 /** 1088 * Uses the xmljson dataformat, based on json-lib, initializing custom options with a Map 1089 */ 1090 public T xmljson(Map<String, String> options) { 1091 return dataFormat(new XmlJsonDataFormat(options)); 1092 } 1093 1094 /** 1095 * Uses the ZIP deflater data format 1096 */ 1097 public T zip() { 1098 ZipDataFormat zdf = new ZipDataFormat(Deflater.DEFAULT_COMPRESSION); 1099 return dataFormat(zdf); 1100 } 1101 1102 /** 1103 * Uses the ZIP deflater data format 1104 */ 1105 public T zip(int compressionLevel) { 1106 ZipDataFormat zdf = new ZipDataFormat(compressionLevel); 1107 return dataFormat(zdf); 1108 } 1109 1110 /** 1111 * Uses the ZIP file data format 1112 */ 1113 public T zipFile() { 1114 ZipFileDataFormat zfdf = new ZipFileDataFormat(); 1115 return dataFormat(zfdf); 1116 } 1117 1118 @SuppressWarnings("unchecked") 1119 private T dataFormat(DataFormatDefinition dataFormatType) { 1120 switch (operation) { 1121 case Unmarshal: 1122 return (T) processorType.unmarshal(dataFormatType); 1123 case Marshal: 1124 return (T) processorType.marshal(dataFormatType); 1125 default: 1126 throw new IllegalArgumentException("Unknown DataFormat operation: " + operation); 1127 } 1128 } 1129}