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