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.EncryptionMethod; 028 029/** 030 * @author jricher 031 * 032 */ 033@Embeddable 034public class JWEEncryptionMethodEmbed { 035 036 public static final JWEEncryptionMethodEmbed NONE = getForAlgorithmName("none"); 037 038 private EncryptionMethod algorithm; 039 040 public JWEEncryptionMethodEmbed() { 041 042 } 043 044 public JWEEncryptionMethodEmbed(EncryptionMethod algorithm) { 045 this.algorithm = algorithm; 046 } 047 048 public static JWEEncryptionMethodEmbed getForAlgorithmName (String algorithmName) { 049 JWEEncryptionMethodEmbed ent = new JWEEncryptionMethodEmbed(); 050 ent.setAlgorithmName(algorithmName); 051 if (ent.getAlgorithm() == null) { 052 return null; 053 } else { 054 return ent; 055 } 056 } 057 058 /** 059 * Get the name of this algorithm, return null if no algorithm set. 060 * @return 061 */ 062 @Basic 063 public String getAlgorithmName() { 064 if (algorithm != null) { 065 return algorithm.getName(); 066 } else { 067 return null; 068 } 069 } 070 071 /** 072 * Set the name of this algorithm. 073 * Calls EncryptionMethod.parse() 074 * @param algorithmName 075 */ 076 public void setAlgorithmName(String algorithmName) { 077 if (!Strings.isNullOrEmpty(algorithmName)) { 078 algorithm = EncryptionMethod.parse(algorithmName); 079 } else { 080 algorithm = null; 081 } 082 } 083 084 /* (non-Javadoc) 085 * @see java.lang.Object#toString() 086 */ 087 @Override 088 public String toString() { 089 return "JWEEncryptionMethodEmbed [algorithm=" + algorithm + "]"; 090 } 091 092 /** 093 * @return the algorithm 094 */ 095 @Transient 096 public EncryptionMethod getAlgorithm() { 097 return algorithm; 098 } 099 100 /** 101 * @param algorithm the algorithm to set 102 */ 103 public void setAlgorithm(EncryptionMethod algorithm) { 104 this.algorithm = algorithm; 105 } 106 107 108}