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