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.util.HashMap;
020    import java.util.Map;
021    
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlAttribute;
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    
028    import org.apache.camel.model.DataFormatDefinition;
029    import org.apache.camel.spi.DataFormat;
030    import org.apache.camel.spi.NamespaceAware;
031    import org.apache.camel.util.jsse.KeyStoreParameters;
032    
033    /**
034     * Represents as XML Security Encrypter/Decrypter {@link DataFormat}
035     */
036    @XmlRootElement(name = "secureXML")
037    @XmlAccessorType(XmlAccessType.FIELD)
038    public class XMLSecurityDataFormat extends DataFormatDefinition implements NamespaceAware {
039    
040        private static final transient String TRIPLEDES = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc";
041    
042        @XmlAttribute
043        private String xmlCipherAlgorithm;
044        @XmlAttribute
045        private String passPhrase;
046        @XmlAttribute
047        private String secureTag;
048        @XmlAttribute
049        private Boolean secureTagContents;
050        @XmlAttribute
051        private String keyCipherAlgorithm;
052        @XmlAttribute
053        private String recipientKeyAlias;
054        @XmlAttribute
055        private String keyOrTrustStoreParametersId;
056        
057        @XmlTransient
058        private KeyStoreParameters keyOrTrustStoreParameters;
059        
060        @XmlTransient
061        private Map<String, String> namespaces;
062        
063        
064        public XMLSecurityDataFormat() {
065            super("secureXML");
066        }
067    
068        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents) {
069            this();
070            this.setSecureTag(secureTag);
071            this.setSecureTagContents(secureTagContents);
072        }
073        
074        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents) {
075            this();
076            this.setSecureTag(secureTag);
077            this.setSecureTagContents(secureTagContents);
078            this.setNamespaces(namespaces);
079        }
080    
081        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String passPhrase) {
082            this(secureTag, secureTagContents);
083            this.setPassPhrase(passPhrase);
084        }
085        
086        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, 
087                                     String passPhrase) {
088            this(secureTag, secureTagContents);
089            this.setPassPhrase(passPhrase);
090            this.setNamespaces(namespaces);
091        }
092        
093        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String passPhrase,
094                                     String xmlCipherAlgorithm) {
095            this(secureTag, secureTagContents, passPhrase);
096            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
097        }
098        
099        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String passPhrase,
100                                     String xmlCipherAlgorithm) {
101            this(secureTag, secureTagContents, passPhrase);
102            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
103            this.setNamespaces(namespaces);
104        }
105        
106        /**
107         * @deprecated  use {{@link #XMLSecurityDataFormat(String, boolean, String, String, String, String)} or 
108         *                  {{@link #XMLSecurityDataFormat(String, boolean, String, String, String, KeyStoreParameters)} instead
109         */
110        @Deprecated
111        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
112                String xmlCipherAlgorithm, String keyCipherAlgorithm) {
113            this(secureTag, secureTagContents);
114            this.setRecipientKeyAlias(recipientKeyAlias);
115            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
116            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
117        }
118    
119        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
120                                     String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
121            this(secureTag, secureTagContents);
122            this.setRecipientKeyAlias(recipientKeyAlias);
123            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
124            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
125            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
126        }
127        
128        public XMLSecurityDataFormat(String secureTag, boolean secureTagContents, String recipientKeyAlias,
129                String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
130            this(secureTag, secureTagContents);
131            this.setRecipientKeyAlias(recipientKeyAlias);
132            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
133            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
134            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
135        }
136        
137        /**
138         * @deprecated  use {{@link #XMLSecurityDataFormat(String, Map, boolean, String, String, String, String)} or 
139         *                  {{@link #XMLSecurityDataFormat(String, Map, boolean, String, String, String, KeyStoreParameters)} instead
140         */
141        @Deprecated
142        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
143                String xmlCipherAlgorithm, String keyCipherAlgorithm) {
144            this(secureTag, secureTagContents);
145            this.setRecipientKeyAlias(recipientKeyAlias);
146            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
147            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
148            this.setNamespaces(namespaces);
149        }
150        
151        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
152                String xmlCipherAlgorithm, String keyCipherAlgorithm, String keyOrTrustStoreParametersId) {
153            this(secureTag, secureTagContents);
154            this.setRecipientKeyAlias(recipientKeyAlias);
155            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
156            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
157            this.setNamespaces(namespaces);
158            this.setKeyOrTrustStoreParametersId(keyOrTrustStoreParametersId);
159        }
160    
161        public XMLSecurityDataFormat(String secureTag, Map<String, String> namespaces, boolean secureTagContents, String recipientKeyAlias,
162                String xmlCipherAlgorithm, String keyCipherAlgorithm, KeyStoreParameters keyOrTrustStoreParameters) {
163            this(secureTag, secureTagContents);
164            this.setRecipientKeyAlias(recipientKeyAlias);
165            this.setXmlCipherAlgorithm(xmlCipherAlgorithm);
166            this.setKeyCipherAlgorithm(keyCipherAlgorithm);
167            this.setNamespaces(namespaces);
168            this.setKeyOrTrustStoreParameters(keyOrTrustStoreParameters);
169        }
170    
171        @Override
172        protected void configureDataFormat(DataFormat dataFormat) {
173            if (getSecureTag() != null) {
174                setProperty(dataFormat, "secureTag", getSecureTag());
175            } else {
176                setProperty(dataFormat, "secureTag", "");
177            }
178    
179            setProperty(dataFormat, "secureTagContents", isSecureTagContents());
180    
181            if (passPhrase != null) {
182                setProperty(dataFormat, "passPhrase", getPassPhrase().getBytes());
183            } else {
184                setProperty(dataFormat, "passPhrase", "Just another 24 Byte key".getBytes());
185            }
186            if (getXmlCipherAlgorithm() != null) {
187                setProperty(dataFormat, "xmlCipherAlgorithm", getXmlCipherAlgorithm());
188            } else {
189                setProperty(dataFormat, "xmlCipherAlgorithm", TRIPLEDES);
190            }
191            if (getKeyCipherAlgorithm() != null) {
192                setProperty(dataFormat, "keyCipherAlgorithm", getKeyCipherAlgorithm());
193            }
194            if (getRecipientKeyAlias() != null) {
195                setProperty(dataFormat, "recipientKeyAlias", getRecipientKeyAlias());
196            }
197            if (getKeyOrTrustStoreParametersId() != null) {
198                setProperty(dataFormat, "keyOrTrustStoreParametersId", getKeyOrTrustStoreParametersId());
199            }
200            if (keyOrTrustStoreParameters != null) {
201                setProperty(dataFormat, "keyOrTrustStoreParameters", this.keyOrTrustStoreParameters);
202            }
203            if (namespaces != null) {
204                setProperty(dataFormat, "namespaces", this.namespaces);
205            }
206        }
207    
208        public String getXmlCipherAlgorithm() {
209            return xmlCipherAlgorithm;
210        }
211    
212        public void setXmlCipherAlgorithm(String xmlCipherAlgorithm) {
213            this.xmlCipherAlgorithm = xmlCipherAlgorithm;
214        }
215    
216        public String getPassPhrase() {
217            return passPhrase;
218        }
219    
220        public void setPassPhrase(String passPhrase) {
221            this.passPhrase = passPhrase;
222        }
223    
224        public String getSecureTag() {
225            return secureTag;
226        }
227    
228        public void setSecureTag(String secureTag) {
229            this.secureTag = secureTag;
230        }
231    
232        public Boolean getSecureTagContents() {
233            return secureTagContents;
234        }
235    
236        public void setSecureTagContents(Boolean secureTagContents) {
237            this.secureTagContents = secureTagContents;
238        }
239    
240        public boolean isSecureTagContents() {
241            return secureTagContents != null && secureTagContents;
242        }
243    
244        public void setKeyCipherAlgorithm(String keyCipherAlgorithm) {
245            this.keyCipherAlgorithm = keyCipherAlgorithm;
246        }
247    
248        public String getKeyCipherAlgorithm() {
249            return keyCipherAlgorithm;
250        }
251    
252        public void setRecipientKeyAlias(String recipientKeyAlias) {
253            this.recipientKeyAlias = recipientKeyAlias;
254        }
255    
256        public String getRecipientKeyAlias() {
257            return recipientKeyAlias;
258        }
259        
260        public void setKeyOrTrustStoreParametersId(String id) {
261            this.keyOrTrustStoreParametersId = id;
262        }
263        
264        public String getKeyOrTrustStoreParametersId() {
265            return this.keyOrTrustStoreParametersId;
266        }
267        
268        private void setKeyOrTrustStoreParameters(KeyStoreParameters keyOrTrustStoreParameters) {
269            this.keyOrTrustStoreParameters = keyOrTrustStoreParameters;
270            
271        }
272    
273        @Override
274        public void setNamespaces(Map<String, String> nspaces) {
275            if (this.namespaces == null) {
276                this.namespaces = new HashMap<String, String>();
277            }
278            this.namespaces.putAll(nspaces);
279        }
280        
281    }