001/*******************************************************************************
002 * Copyright 2014 The MITRE Corporation
003 *   and the MIT Kerberos and Internet Trust Consortium
004 * 
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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/**
018 * 
019 */
020package org.mitre.jose;
021
022import javax.persistence.Basic;
023import javax.persistence.Embeddable;
024import javax.persistence.Transient;
025
026import com.google.common.base.Strings;
027import com.nimbusds.jose.JWEAlgorithm;
028
029/**
030 * 
031 * Wrapper class for Nimbus JOSE objects to fit into JPA
032 * 
033 * @author jricher
034 *
035 */
036@Embeddable
037public class JWEAlgorithmEmbed {
038
039        public static final JWEAlgorithmEmbed NONE = getForAlgorithmName("none");
040
041        private JWEAlgorithm algorithm;
042
043        public JWEAlgorithmEmbed() {
044
045        }
046
047        public JWEAlgorithmEmbed(JWEAlgorithm algorithm) {
048                this.algorithm = algorithm;
049        }
050
051        public static JWEAlgorithmEmbed getForAlgorithmName (String algorithmName) {
052                JWEAlgorithmEmbed ent = new JWEAlgorithmEmbed();
053                ent.setAlgorithmName(algorithmName);
054                if (ent.getAlgorithm() == null) {
055                        return null;
056                } else {
057                        return ent;
058                }
059        }
060
061        /**
062         * Get the name of this algorithm, return null if no algorithm set.
063         * @return
064         */
065        @Basic
066        public String getAlgorithmName() {
067                if (algorithm != null) {
068                        return algorithm.getName();
069                } else {
070                        return null;
071                }
072        }
073
074        /**
075         * Set the name of this algorithm.
076         * Calls JWEAlgorithm.parse()
077         * @param algorithmName
078         */
079        public void setAlgorithmName(String algorithmName) {
080                if (!Strings.isNullOrEmpty(algorithmName)) {
081                        algorithm = JWEAlgorithm.parse(algorithmName);
082                } else {
083                        algorithm = null;
084                }
085        }
086
087        /* (non-Javadoc)
088         * @see java.lang.Object#toString()
089         */
090        @Override
091        public String toString() {
092                return "JWEAlgorithmEmbed [algorithm=" + algorithm + "]";
093        }
094
095        /**
096         * @return the algorithm
097         */
098        @Transient
099        public JWEAlgorithm getAlgorithm() {
100                return algorithm;
101        }
102
103        /**
104         * @param algorithm the algorithm to set
105         */
106        public void setAlgorithm(JWEAlgorithm algorithm) {
107                this.algorithm = algorithm;
108        }
109
110}