ParameterConverterProvider.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.jaxrs;

  21. import java.lang.annotation.Annotation;
  22. import java.lang.reflect.Type;
  23. import java.util.Calendar;

  24. import javax.ws.rs.ext.ParamConverter;
  25. import javax.ws.rs.ext.ParamConverterProvider;

  26. import org.joda.time.DateTimeZone;
  27. import org.openspcoop2.utils.date.DateManager;

  28. /**
  29.  * ParameterConverterProvider
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class ParameterConverterProvider implements ParamConverterProvider {

  36.     private String timeZoneId = null;
  37.     public String getTimeZoneId() {
  38.         return this.timeZoneId;
  39.     }
  40.     public void setTimeZoneId(String timeZoneId) {
  41.         this.timeZoneId = timeZoneId;
  42.     }
  43.    
  44.     private DateTimeZone timeZone = null;
  45.     private DateTimeZone getDateTimeZone() {
  46.         if(this.timeZone==null) {
  47.             this.initTimeZone();
  48.         }
  49.         return this.timeZone;
  50.     }
  51.    
  52.     private synchronized void initTimeZone() {
  53.         if(this.timeZone == null) {
  54.             if(this.timeZoneId==null) {
  55.                 try {
  56.                     Calendar c = DateManager.getCalendar();
  57.                     this.setTimeZoneId(c.getTimeZone().getID());
  58.                 }catch(Exception e) {
  59.                     throw new RuntimeException(e.getMessage(),e);
  60.                 }
  61.             }
  62.             this.timeZone = DateTimeZone.forID(this.timeZoneId);
  63.         }
  64.     }
  65.    
  66.     @Override
  67.     public <T> ParamConverter<T> getConverter(Class<T> type, Type type1, Annotation[] antns) {
  68.         if (org.joda.time.DateTime.class.equals(type)) {
  69.             @SuppressWarnings("unchecked")
  70.             ParamConverter<T> paramConverter = (ParamConverter<T>) new JodaDateTimeConverter(this.getDateTimeZone());
  71.             return paramConverter;
  72.         }
  73.         else if (org.joda.time.LocalDate.class.equals(type)) {
  74.             @SuppressWarnings("unchecked")
  75.             ParamConverter<T> paramConverter = (ParamConverter<T>) new JodaLocalDateConverter(this.getDateTimeZone());
  76.             return paramConverter;
  77.         }
  78.         return null;
  79.     }

  80. }