JacksonXmlProviderCustomized.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.util.TimeZone;

  22. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  23. import com.fasterxml.jackson.databind.DeserializationFeature;
  24. import com.fasterxml.jackson.databind.SerializationFeature;
  25. import com.fasterxml.jackson.dataformat.xml.XmlMapper;
  26. import com.fasterxml.jackson.datatype.joda.JodaModule;

  27. /**
  28.  * JacksonXmlProvider
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class JacksonXmlProviderCustomized extends com.fasterxml.jackson.jaxrs.xml.JacksonXMLProvider {

  35.     private static boolean failOnMissingExternalTypeIdProperty = false;
  36.     public static boolean isFailOnMissingExternalTypeIdProperty() {
  37.         return JacksonXmlProviderCustomized.failOnMissingExternalTypeIdProperty;
  38.     }
  39.     public static void setFailOnMissingExternalTypeIdProperty(boolean failOnMissingExternalTypeIdProperty) {
  40.         JacksonXmlProviderCustomized.failOnMissingExternalTypeIdProperty = failOnMissingExternalTypeIdProperty;
  41.     }

  42.     public static XmlMapper getObjectMapper(boolean prettyPrint, TimeZone timeZone) {
  43.         XmlMapper mapper = new XmlMapper();
  44.         mapper.setTimeZone(timeZone);
  45.         mapper.registerModule(new JodaModule());
  46.         mapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
  47.                 WRITE_DATES_AS_TIMESTAMPS , false);
  48.         mapper.setSerializationInclusion(Include.NON_NULL);
  49.         mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
  50.         if(prettyPrint) {
  51.             mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
  52.         }
  53.         mapper.configure(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY, JacksonXmlProviderCustomized.failOnMissingExternalTypeIdProperty);
  54.         return mapper;
  55.     }
  56.    
  57.     public JacksonXmlProviderCustomized() {
  58.         super(getObjectMapper(false, TimeZone.getDefault()));
  59.     }
  60.     public JacksonXmlProviderCustomized(boolean prettyPrint) {
  61.         super(getObjectMapper(prettyPrint, TimeZone.getDefault()));
  62.     }

  63.     public JacksonXmlProviderCustomized(String timeZoneId) {
  64.         super(getObjectMapper(false, TimeZone.getTimeZone(timeZoneId)));
  65.     }
  66.     public JacksonXmlProviderCustomized(String timeZoneId, boolean prettyPrint) {
  67.         super(getObjectMapper(prettyPrint, TimeZone.getTimeZone(timeZoneId)));
  68.     }
  69.    
  70. }