JWKSetStringConverter.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *******************************************************************************/

  16. package org.mitre.oauth2.model.convert;

  17. import java.text.ParseException;

  18. import javax.persistence.AttributeConverter;
  19. import javax.persistence.Converter;

  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;

  22. import com.nimbusds.jose.jwk.JWKSet;

  23. /**
  24.  * @author jricher
  25.  *
  26.  */
  27. @Converter
  28. public class JWKSetStringConverter implements AttributeConverter<JWKSet, String> {

  29.     private static Logger logger = LoggerFactory.getLogger(JWKSetStringConverter.class);

  30.     @Override
  31.     public String convertToDatabaseColumn(JWKSet attribute) {
  32.         if (attribute != null) {
  33.             return attribute.toString();
  34.         } else {
  35.             return null;
  36.         }
  37.     }

  38.     /* (non-Javadoc)
  39.      * @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object)
  40.      */
  41.     @Override
  42.     public JWKSet convertToEntityAttribute(String dbData) {
  43.         if (dbData != null) {
  44.             try {
  45.                 JWKSet jwks = JWKSet.parse(dbData);
  46.                 return jwks;
  47.             } catch (ParseException e) {
  48.                 logger.error("Unable to parse JWK Set", e);
  49.                 return null;
  50.             }
  51.         } else {
  52.             return null;
  53.         }

  54.     }

  55. }