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.CastorDataFormat;
031 import org.apache.camel.model.dataformat.CsvDataFormat;
032 import org.apache.camel.model.dataformat.CustomDataFormat;
033 import org.apache.camel.model.dataformat.GzipDataFormat;
034 import org.apache.camel.model.dataformat.HL7DataFormat;
035 import org.apache.camel.model.dataformat.JaxbDataFormat;
036 import org.apache.camel.model.dataformat.JibxDataFormat;
037 import org.apache.camel.model.dataformat.JsonDataFormat;
038 import org.apache.camel.model.dataformat.JsonLibrary;
039 import org.apache.camel.model.dataformat.PGPDataFormat;
040 import org.apache.camel.model.dataformat.ProtobufDataFormat;
041 import org.apache.camel.model.dataformat.RssDataFormat;
042 import org.apache.camel.model.dataformat.SerializationDataFormat;
043 import org.apache.camel.model.dataformat.SoapJaxbDataFormat;
044 import org.apache.camel.model.dataformat.StringDataFormat;
045 import org.apache.camel.model.dataformat.SyslogDataFormat;
046 import org.apache.camel.model.dataformat.TidyMarkupDataFormat;
047 import org.apache.camel.model.dataformat.XMLBeansDataFormat;
048 import org.apache.camel.model.dataformat.XMLSecurityDataFormat;
049 import org.apache.camel.model.dataformat.XStreamDataFormat;
050 import org.apache.camel.model.dataformat.XmlJsonDataFormat;
051 import org.apache.camel.model.dataformat.ZipDataFormat;
052 import org.apache.camel.util.jsse.KeyStoreParameters;
053
054
055 /**
056 * An expression for constructing the different possible {@link org.apache.camel.spi.DataFormat}
057 * options.
058 *
059 * @version
060 */
061 public class DataFormatClause<T extends ProcessorDefinition<?>> {
062 private final T processorType;
063 private final Operation operation;
064
065 /**
066 * {@link org.apache.camel.spi.DataFormat} operations.
067 */
068 public enum Operation {
069 Marshal, Unmarshal
070 }
071
072 public DataFormatClause(T processorType, Operation operation) {
073 this.processorType = processorType;
074 this.operation = operation;
075 }
076
077
078 /**
079 * Uses the Avro data format
080 */
081 public T avro() {
082 return dataFormat(new AvroDataFormat());
083 }
084
085 public T avro(Object schema) {
086 AvroDataFormat dataFormat = new AvroDataFormat();
087 dataFormat.setSchema(schema);
088 return dataFormat(dataFormat);
089 }
090
091 public T avro(String instanceClassName) {
092 return dataFormat(new AvroDataFormat(instanceClassName));
093 }
094
095 /**
096 * Uses the Bindy data format
097 *
098 * @param type the type of bindy data format to use
099 * @param packages packages to scan for Bindy annotated POJO classes
100 */
101 public T bindy(BindyType type, String... packages) {
102 BindyDataFormat bindy = new BindyDataFormat();
103 bindy.setType(type);
104 bindy.setPackages(packages);
105 return dataFormat(bindy);
106 }
107
108 /**
109 * Uses the Bindy data format
110 *
111 * @param type the type of bindy data format to use
112 * @param classType the POJO class type
113 */
114 public T bindy(BindyType type, Class<?> classType) {
115 BindyDataFormat bindy = new BindyDataFormat();
116 bindy.setType(type);
117 bindy.setClassType(classType);
118 return dataFormat(bindy);
119 }
120
121 /**
122 * Uses the CSV data format
123 */
124 public T csv() {
125 return dataFormat(new CsvDataFormat());
126 }
127
128 /**
129 * Uses the custom data format
130 */
131 public T custom(String ref) {
132 return dataFormat(new CustomDataFormat(ref));
133 }
134
135 /**
136 * Uses the Castor data format
137 */
138 public T castor() {
139 return dataFormat(new CastorDataFormat());
140 }
141
142 /**
143 * Uses the Castor data format
144 *
145 * @param mappingFile name of mapping file to locate in classpath
146 */
147 public T castor(String mappingFile) {
148 CastorDataFormat castor = new CastorDataFormat();
149 castor.setMappingFile(mappingFile);
150 return dataFormat(castor);
151 }
152
153 /**
154 * Uses the Castor data format
155 *
156 * @param mappingFile name of mapping file to locate in classpath
157 * @param validation whether validation is enabled or not
158 */
159 public T castor(String mappingFile, boolean validation) {
160 CastorDataFormat castor = new CastorDataFormat();
161 castor.setMappingFile(mappingFile);
162 castor.setValidation(validation);
163 return dataFormat(castor);
164 }
165
166 /**
167 * Uses the GZIP deflater data format
168 */
169 public T gzip() {
170 GzipDataFormat gzdf = new GzipDataFormat();
171 return dataFormat(gzdf);
172 }
173
174 /**
175 * Uses the HL7 data format
176 */
177 public T hl7() {
178 return dataFormat(new HL7DataFormat());
179 }
180
181 /**
182 * Uses the HL7 data format
183 */
184 public T hl7(boolean validate) {
185 HL7DataFormat hl7 = new HL7DataFormat();
186 hl7.setValidate(validate);
187 return dataFormat(hl7);
188 }
189
190 /**
191 * Uses the PGP data format
192 */
193 public T pgp(String keyFileName, String keyUserid) {
194 PGPDataFormat pgp = new PGPDataFormat();
195 pgp.setKeyFileName(keyFileName);
196 pgp.setKeyUserid(keyUserid);
197 return dataFormat(pgp);
198 }
199
200 /**
201 * Uses the PGP data format
202 */
203 public T pgp(String keyFileName, String keyUserid, String password) {
204 PGPDataFormat pgp = new PGPDataFormat();
205 pgp.setKeyFileName(keyFileName);
206 pgp.setKeyUserid(keyUserid);
207 pgp.setPassword(password);
208 return dataFormat(pgp);
209 }
210
211 /**
212 * Uses the PGP data format
213 */
214 public T pgp(String keyFileName, String keyUserid, String password, boolean armored, boolean integrity) {
215 PGPDataFormat pgp = new PGPDataFormat();
216 pgp.setKeyFileName(keyFileName);
217 pgp.setKeyUserid(keyUserid);
218 pgp.setPassword(password);
219 pgp.setArmored(armored);
220 pgp.setIntegrity(integrity);
221 return dataFormat(pgp);
222 }
223
224 /**
225 * Uses the JAXB data format
226 */
227 public T jaxb() {
228 return dataFormat(new JaxbDataFormat());
229 }
230
231 /**
232 * Uses the JAXB data format with context path
233 */
234 public T jaxb(String contextPath) {
235 JaxbDataFormat dataFormat = new JaxbDataFormat();
236 dataFormat.setContextPath(contextPath);
237 return dataFormat(dataFormat);
238 }
239
240 /**
241 * Uses the JAXB data format turning pretty printing on or off
242 */
243 public T jaxb(boolean prettyPrint) {
244 return dataFormat(new JaxbDataFormat(prettyPrint));
245 }
246
247 /**
248 * Uses the JiBX data format.
249 */
250 public T jibx() {
251 return dataFormat(new JibxDataFormat());
252 }
253
254 /**
255 * Uses the JiBX data format with unmarshall class.
256 */
257 public T jibx(Class<?> unmarshallClass) {
258 return dataFormat(new JibxDataFormat(unmarshallClass));
259 }
260
261 /**
262 * Uses the JSON data format using the XStream json library
263 */
264 public T json() {
265 return dataFormat(new JsonDataFormat());
266 }
267
268 /**
269 * Uses the JSON data format
270 *
271 * @param library the json library to use
272 */
273 public T json(JsonLibrary library) {
274 return dataFormat(new JsonDataFormat(library));
275 }
276
277 /**
278 * Uses the JSON data format
279 *
280 * @param type the json type to use
281 * @param unmarshalType unmarshal type for json jackson type
282 */
283 public T json(JsonLibrary type, Class<?> unmarshalType) {
284 JsonDataFormat json = new JsonDataFormat(type);
285 json.setUnmarshalType(unmarshalType);
286 return dataFormat(json);
287 }
288
289 /**
290 * Uses the protobuf data format
291 */
292 public T protobuf() {
293 return dataFormat(new ProtobufDataFormat());
294 }
295
296 public T protobuf(Object defaultInstance) {
297 ProtobufDataFormat dataFormat = new ProtobufDataFormat();
298 dataFormat.setDefaultInstance(defaultInstance);
299 return dataFormat(dataFormat);
300 }
301
302 public T protobuf(String instanceClassName) {
303 return dataFormat(new ProtobufDataFormat(instanceClassName));
304 }
305
306 /**
307 * Uses the RSS data format
308 */
309 public T rss() {
310 return dataFormat(new RssDataFormat());
311 }
312
313 /**
314 * Uses the Java Serialization data format
315 */
316 public T serialization() {
317 return dataFormat(new SerializationDataFormat());
318 }
319
320 /**
321 * Uses the Soap 1.1 JAXB data format
322 */
323 public T soapjaxb() {
324 return dataFormat(new SoapJaxbDataFormat());
325 }
326
327 /**
328 * Uses the Soap 1.1 JAXB data format
329 */
330 public T soapjaxb(String contextPath) {
331 return dataFormat(new SoapJaxbDataFormat(contextPath));
332 }
333
334 /**
335 * Uses the Soap 1.1 JAXB data format
336 */
337 public T soapjaxb(String contextPath, String elementNameStrategyRef) {
338 return dataFormat(new SoapJaxbDataFormat(contextPath, elementNameStrategyRef));
339 }
340
341 /**
342 * Uses the Soap 1.1 JAXB data format
343 */
344 public T soapjaxb(String contextPath, Object elementNameStrategy) {
345 return dataFormat(new SoapJaxbDataFormat(contextPath, elementNameStrategy));
346 }
347
348 /**
349 * Uses the Soap 1.2 JAXB data format
350 */
351 public T soapjaxb12() {
352 SoapJaxbDataFormat soap = new SoapJaxbDataFormat();
353 soap.setVersion("1.2");
354 return dataFormat(soap);
355 }
356
357 /**
358 * Uses the Soap 1.2 JAXB data format
359 */
360 public T soapjaxb12(String contextPath) {
361 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath);
362 soap.setVersion("1.2");
363 return dataFormat(soap);
364 }
365
366 /**
367 * Uses the Soap 1.2 JAXB data format
368 */
369 public T soapjaxb12(String contextPath, String elementNameStrategyRef) {
370 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath, elementNameStrategyRef);
371 soap.setVersion("1.2");
372 return dataFormat(soap);
373 }
374
375 /**
376 * Uses the Soap JAXB data format
377 */
378 public T soapjaxb12(String contextPath, Object elementNameStrategy) {
379 SoapJaxbDataFormat soap = new SoapJaxbDataFormat(contextPath, elementNameStrategy);
380 soap.setVersion("1.2");
381 return dataFormat(soap);
382 }
383
384 /**
385 * Uses the String data format
386 */
387 public T string() {
388 return string(null);
389 }
390
391 /**
392 * Uses the String data format supporting encoding using given charset
393 */
394 public T string(String charset) {
395 StringDataFormat sdf = new StringDataFormat();
396 sdf.setCharset(charset);
397 return dataFormat(sdf);
398 }
399
400 /**
401 * Uses the Syslog data format
402 */
403 public T syslog() {
404 return dataFormat(new SyslogDataFormat());
405 }
406
407 /**
408 * Return WellFormed HTML (an XML Document) either
409 * {@link java.lang.String} or {@link org.w3c.dom.Node}
410 */
411 public T tidyMarkup(Class<?> dataObjectType) {
412 return dataFormat(new TidyMarkupDataFormat(dataObjectType));
413 }
414
415 /**
416 * Return TidyMarkup in the default format
417 * as {@link org.w3c.dom.Node}
418 */
419 public T tidyMarkup() {
420 return dataFormat(new TidyMarkupDataFormat(Node.class));
421 }
422
423 /**
424 * Uses the XStream data format
425 */
426 public T xstream() {
427 return dataFormat(new XStreamDataFormat());
428 }
429
430 /**
431 * Uses the xstream by setting the encoding
432 */
433 public T xstream(String encoding) {
434 return dataFormat(new XStreamDataFormat(encoding));
435 }
436
437 /**
438 * Uses the XML Security data format
439 */
440 public T secureXML() {
441 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat();
442 return dataFormat(xsdf);
443 }
444
445 /**
446 * Uses the XML Security data format
447 */
448 public T secureXML(String secureTag, boolean secureTagContents) {
449 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents);
450 return dataFormat(xsdf);
451 }
452
453 /**
454 * Uses the XML Security data format
455 */
456 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents) {
457 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents);
458 return dataFormat(xsdf);
459 }
460
461 /**
462 * Uses the XML Security data format
463 */
464 public T secureXML(String secureTag, boolean secureTagContents, String passPhrase) {
465 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase);
466 return dataFormat(xsdf);
467 }
468
469 /**
470 * Uses the XML Security data format
471 */
472 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase) {
473 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, passPhrase);
474 return dataFormat(xsdf);
475 }
476
477 /**
478 * Uses the XML Security data format
479 */
480 public T secureXML(String secureTag, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) {
481 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, passPhrase, xmlCipherAlgorithm);
482 return dataFormat(xsdf);
483 }
484
485
486 /**
487 * Uses the XML Security data format
488 */
489 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase, String xmlCipherAlgorithm) {
490 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, passPhrase, xmlCipherAlgorithm);
491 return dataFormat(xsdf);
492 }
493
494 /**
495 * @deprectaed Use {@link #secureXML(String, Map, boolean, String, String, String, String) instead.
496 * Uses the XML Security data format
497 */
498 @Deprecated
499 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm,
500 String keyCipherAlgorithm) {
501 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm, keyCipherAlgorithm);
502 return dataFormat(xsdf);
503 }
504
505 /**
506 * Uses the XML Security data format
507 */
508 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm,
509 String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
510 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm,
511 keyCipherAlgorithm, keyOrTrustStoreParametersId);
512 return dataFormat(xsdf);
513 }
514
515 /**
516 * Uses the XML Security data format
517 */
518 public T secureXML(String secureTag, boolean secureTagContents, String recipientKeyAlias, String xmlCipherAlgorithm,
519 String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
520 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm,
521 keyCipherAlgorithm, keyOrTrustStoreParameters);
522 return dataFormat(xsdf);
523 }
524
525 /**
526 * Uses the XML Security data format
527 */
528 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
529 String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
530 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm,
531 keyCipherAlgorithm, keyOrTrustStoreParametersId);
532 return dataFormat(xsdf);
533 }
534
535 /**
536 * Uses the XML Security data format
537 */
538 public T secureXML(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
539 String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
540 XMLSecurityDataFormat xsdf = new XMLSecurityDataFormat(secureTag, namespaces, secureTagContents, recipientKeyAlias, xmlCipherAlgorithm,
541 keyCipherAlgorithm, keyOrTrustStoreParameters);
542 return dataFormat(xsdf);
543 }
544
545 /**
546 * Uses the xmlBeans data format
547 */
548 public T xmlBeans() {
549 return dataFormat(new XMLBeansDataFormat());
550 }
551
552 /**
553 * Uses the xmljson dataformat, based on json-lib
554 */
555 public T xmljson() {
556 return dataFormat(new XmlJsonDataFormat());
557 }
558
559 /**
560 * Uses the xmljson dataformat, based on json-lib, initializing custom options with a Map
561 */
562 public T xmljson(Map<String, String> options) {
563 return dataFormat(new XmlJsonDataFormat(options));
564 }
565
566 /**
567 * Uses the ZIP deflater data format
568 */
569 public T zip() {
570 ZipDataFormat zdf = new ZipDataFormat(Deflater.DEFAULT_COMPRESSION);
571 return dataFormat(zdf);
572 }
573
574 /**
575 * Uses the ZIP deflater data format
576 */
577 public T zip(int compressionLevel) {
578 ZipDataFormat zdf = new ZipDataFormat(compressionLevel);
579 return dataFormat(zdf);
580 }
581
582 @SuppressWarnings("unchecked")
583 private T dataFormat(DataFormatDefinition dataFormatType) {
584 switch (operation) {
585 case Unmarshal:
586 return (T) processorType.unmarshal(dataFormatType);
587 case Marshal:
588 return (T) processorType.marshal(dataFormatType);
589 default:
590 throw new IllegalArgumentException("Unknown DataFormat operation: " + operation);
591 }
592 }
593 }