1 /* 2 * Copyright 2008 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.openehealth.ipf.platform.camel.core.adapter; 17 18 import groovy.lang.Closure; 19 import groovy.transform.stc.ClosureParams; 20 import groovy.transform.stc.SimpleType; 21 import org.apache.camel.Exchange; 22 import org.apache.camel.Expression; 23 import org.apache.camel.Message; 24 import org.openehealth.ipf.platform.camel.core.closures.DelegatingExpression; 25 26 import static org.apache.camel.builder.Builder.body; 27 28 /** 29 * Abstract base class for classes that adapt <i>transform support library</i> 30 * interfaces to Apache Camel interfaces. 31 * 32 * @author Martin Krasser 33 */ 34 public abstract class AdapterSupport implements Adapter { 35 36 private Expression inputExpression; 37 private Expression paramsExpression; 38 39 /** 40 * Creates an adapter that by default takes input data from the body of the 41 * in-message. 42 * 43 * @see Exchange#getIn() 44 * @see Message#getBody() 45 */ 46 public AdapterSupport() { 47 inputExpression = body(); 48 } 49 50 /* (non-Javadoc) 51 * @see org.openehealth.ipf.platform.camel.core.adapter.Adapter#input(org.apache.camel.Expression) 52 */ 53 @Override 54 public Adapter input(Expression inputExpression) { 55 this.inputExpression = inputExpression; 56 return this; 57 } 58 59 /* (non-Javadoc) 60 * @see org.openehealth.ipf.platform.camel.core.adapter.Adapter#input(groovy.lang.Closure) 61 */ 62 @Override 63 public Adapter input(@ClosureParams(value = SimpleType.class, options = { "org.apache.camel.Expression"}) 64 Closure inputExpressionLogic) { 65 return input(new DelegatingExpression(inputExpressionLogic)); 66 } 67 68 /* (non-Javadoc) 69 * @see org.openehealth.ipf.platform.camel.core.adapter.Adapter#params(org.apache.camel.Expression) 70 */ 71 @Override 72 public Adapter params(Expression paramsExpression) { 73 this.paramsExpression = paramsExpression; 74 return this; 75 } 76 77 /* (non-Javadoc) 78 * @see org.openehealth.ipf.platform.camel.core.adapter.Adapter#params(groovy.lang.Closure) 79 */ 80 @Override 81 public Adapter params(@ClosureParams(value = SimpleType.class, options = { "org.apache.camel.Expression"}) 82 Closure paramsExpressionLogic) { 83 return params(new DelegatingExpression(paramsExpressionLogic)); 84 } 85 86 /* (non-Javadoc) 87 * @see org.openehealth.ipf.platform.camel.core.adapter.Adapter#staticParams(java.lang.Object[]) 88 */ 89 @Override 90 public Adapter staticParams(Object... params) { 91 paramsExpression = new StaticParams(params); 92 return this; 93 } 94 95 /** 96 * Applies the {@link Expression} set by {@link #input(Expression)} to 97 * obtain input data from the <code>exchange</code>. 98 * 99 * @param exchange 100 * message exchange. 101 * @return input data or <code>null</code> if the expression evaluates to 102 * <code>null</code> or the expression object is <code>null</code>. 103 */ 104 protected Object adaptInput(Exchange exchange) { 105 if (inputExpression == null) { 106 return null; 107 } 108 return inputExpression.evaluate(exchange, Object.class); 109 } 110 111 /** 112 * Applies the {@link Expression} set by {@link #params(Expression)} (or 113 * implicitly set by {@link #staticParams(Object...)}) to obtain input 114 * params from the <code>exchange</code>. 115 * 116 * @param exchange 117 * message exchange. 118 * @return input data or <code>null</code> if the expression evaluates to 119 * <code>null</code> or the expression object is <code>null</code>. 120 */ 121 protected Object adaptParams(Exchange exchange) { 122 if (paramsExpression == null) { 123 return null; 124 } 125 return paramsExpression.evaluate(exchange, Object.class); 126 } 127 128 }