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.reifier.dataformat;
018
019import java.security.Key;
020import java.security.spec.AlgorithmParameterSpec;
021
022import org.apache.camel.CamelContext;
023import org.apache.camel.model.DataFormatDefinition;
024import org.apache.camel.model.dataformat.CryptoDataFormat;
025import org.apache.camel.spi.DataFormat;
026import org.apache.camel.support.CamelContextHelper;
027import org.apache.camel.util.ObjectHelper;
028
029public class CryptoDataFormatReifier extends DataFormatReifier<CryptoDataFormat> {
030
031    public CryptoDataFormatReifier(DataFormatDefinition definition) {
032        super((CryptoDataFormat)definition);
033    }
034
035    @Override
036    protected DataFormat doCreateDataFormat(CamelContext camelContext) {
037        DataFormat cryptoFormat = super.doCreateDataFormat(camelContext);
038
039        if (ObjectHelper.isNotEmpty(definition.getKeyRef())) {
040            Key key = CamelContextHelper.mandatoryLookup(camelContext, definition.getKeyRef(), Key.class);
041            setProperty(camelContext, cryptoFormat, "key", key);
042        }
043        if (ObjectHelper.isNotEmpty(definition.getAlgorithmParameterRef())) {
044            AlgorithmParameterSpec spec = CamelContextHelper.mandatoryLookup(camelContext, definition.getAlgorithmParameterRef(), AlgorithmParameterSpec.class);
045            setProperty(camelContext, cryptoFormat, "AlgorithmParameterSpec", spec);
046        }
047        if (ObjectHelper.isNotEmpty(definition.getInitVectorRef())) {
048            byte[] iv = CamelContextHelper.mandatoryLookup(camelContext, definition.getInitVectorRef(), byte[].class);
049            setProperty(camelContext, cryptoFormat, "InitializationVector", iv);
050        }
051        return cryptoFormat;
052    }
053
054    @Override
055    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
056        Boolean answer = ObjectHelper.toBoolean(definition.getShouldAppendHMAC());
057        if (answer != null && !answer) {
058            setProperty(camelContext, dataFormat, "shouldAppendHMAC", Boolean.FALSE);
059        } else {
060            setProperty(camelContext, dataFormat, "shouldAppendHMAC", Boolean.TRUE);
061        }
062        answer = ObjectHelper.toBoolean(definition.getInline());
063        if (answer != null && answer) {
064            setProperty(camelContext, dataFormat, "shouldInlineInitializationVector", Boolean.TRUE);
065        } else {
066            setProperty(camelContext, dataFormat, "shouldInlineInitializationVector", Boolean.FALSE);
067        }
068        if (definition.getAlgorithm() != null) {
069            setProperty(camelContext, dataFormat, "algorithm", definition.getAlgorithm());
070        }
071        if (definition.getCryptoProvider() != null) {
072            setProperty(camelContext, dataFormat, "cryptoProvider", definition.getCryptoProvider());
073        }
074        if (definition.getMacAlgorithm() != null) {
075            setProperty(camelContext, dataFormat, "macAlgorithm", definition.getMacAlgorithm());
076        }
077        if (definition.getBuffersize() != null) {
078            setProperty(camelContext, dataFormat, "buffersize", definition.getBuffersize());
079        }
080    }
081
082}