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.model.dataformat;
018
019import java.security.Key;
020import java.security.spec.AlgorithmParameterSpec;
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.apache.camel.CamelContext;
027import org.apache.camel.model.DataFormatDefinition;
028import org.apache.camel.spi.DataFormat;
029import org.apache.camel.spi.RouteContext;
030import org.apache.camel.util.CamelContextHelper;
031import org.apache.camel.util.ObjectHelper;
032
033@XmlRootElement(name = "crypto")
034@XmlAccessorType(XmlAccessType.FIELD)
035public class CryptoDataFormat extends DataFormatDefinition {
036    @XmlAttribute
037    private String algorithm;
038    @XmlAttribute
039    private String cryptoProvider;
040    @XmlAttribute
041    private String keyRef;
042    @XmlAttribute
043    private String initVectorRef;
044    @XmlAttribute
045    private String algorithmParameterRef;
046    @XmlAttribute
047    private Integer buffersize;
048    @XmlAttribute
049    private String macAlgorithm = "HmacSHA1";
050    @XmlAttribute
051    private Boolean shouldAppendHMAC;
052    @XmlAttribute
053    private Boolean inline;
054
055    public CryptoDataFormat() {
056        super("crypto");
057    }
058
059    @Override
060    protected DataFormat createDataFormat(RouteContext routeContext) {
061        DataFormat cryptoFormat = super.createDataFormat(routeContext);
062
063        if (ObjectHelper.isNotEmpty(keyRef)) {
064            Key key = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), keyRef, Key.class);
065            setProperty(routeContext.getCamelContext(), cryptoFormat, "key", key);
066        }
067        if (ObjectHelper.isNotEmpty(algorithmParameterRef)) {
068            AlgorithmParameterSpec spec = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(),
069                    algorithmParameterRef, AlgorithmParameterSpec.class);
070            setProperty(routeContext.getCamelContext(), cryptoFormat, "AlgorithmParameterSpec", spec);
071        }
072        if (ObjectHelper.isNotEmpty(initVectorRef)) {
073            byte[] iv = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), initVectorRef, byte[].class);
074            setProperty(routeContext.getCamelContext(), cryptoFormat, "InitializationVector", iv);
075        }
076        return cryptoFormat;
077    }
078
079    @Override
080    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
081        Boolean answer = ObjectHelper.toBoolean(shouldAppendHMAC);
082        if (answer != null && !answer) {
083            setProperty(camelContext, dataFormat, "shouldAppendHMAC", Boolean.FALSE);
084        } else {
085            setProperty(camelContext, dataFormat, "shouldAppendHMAC", Boolean.TRUE);
086        }
087        answer = ObjectHelper.toBoolean(inline);
088        if (answer != null && answer) {
089            setProperty(camelContext, dataFormat, "shouldInlineInitializationVector", Boolean.TRUE);
090        } else {
091            setProperty(camelContext, dataFormat, "shouldInlineInitializationVector", Boolean.FALSE);
092        }
093        if (algorithm != null) {
094            setProperty(camelContext, dataFormat, "algorithm", algorithm);
095        }
096        if (cryptoProvider != null) {
097            setProperty(camelContext, dataFormat, "cryptoProvider", cryptoProvider);
098        }
099        if (macAlgorithm != null) {
100            setProperty(camelContext, dataFormat, "macAlgorithm", macAlgorithm);
101        }
102        if (buffersize != null) {
103            setProperty(camelContext, dataFormat, "buffersize", buffersize);
104        }
105    }
106
107    public String getAlgorithm() {
108        return algorithm;
109    }
110
111    public void setAlgorithm(String algorithm) {
112        this.algorithm = algorithm;
113    }
114
115    public String getCryptoProvider() {
116        return cryptoProvider;
117    }
118
119    public void setCryptoProvider(String cryptoProvider) {
120        this.cryptoProvider = cryptoProvider;
121    }
122
123    public String getKeyRef() {
124        return keyRef;
125    }
126
127    public void setKeyRef(String keyRef) {
128        this.keyRef = keyRef;
129    }
130
131    public String getInitVectorRef() {
132        return initVectorRef;
133    }
134
135    public void setInitVectorRef(String initVectorRef) {
136        this.initVectorRef = initVectorRef;
137    }
138
139    public String getAlgorithmParameterRef() {
140        return algorithmParameterRef;
141    }
142
143    public void setAlgorithmParameterRef(String algorithmParameterRef) {
144        this.algorithmParameterRef = algorithmParameterRef;
145    }
146
147    public Integer getBuffersize() {
148        return buffersize;
149    }
150
151    public void setBuffersize(Integer buffersize) {
152        this.buffersize = buffersize;
153    }
154
155    public String getMacAlgorithm() {
156        return macAlgorithm;
157    }
158
159    public void setMacAlgorithm(String macAlgorithm) {
160        this.macAlgorithm = macAlgorithm;
161    }
162
163    public Boolean getShouldAppendHMAC() {
164        return shouldAppendHMAC;
165    }
166
167    public void setShouldAppendHMAC(Boolean shouldAppendHMAC) {
168        this.shouldAppendHMAC = shouldAppendHMAC;
169    }
170
171    public Boolean getInline() {
172        return inline;
173    }
174
175    public void setInline(Boolean inline) {
176        this.inline = inline;
177    }
178}