View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.api.serialization;
20  
21  import com.fasterxml.jackson.core.Version;
22  import com.fasterxml.jackson.core.type.TypeReference;
23  import com.fasterxml.jackson.databind.SerializationFeature;
24  import com.fasterxml.jackson.databind.json.JsonMapper;
25  import com.fasterxml.jackson.databind.module.SimpleModule;
26  import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
27  import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
28  import org.identityconnectors.common.security.GuardedString;
29  import org.identityconnectors.framework.common.objects.Attribute;
30  import org.identityconnectors.framework.common.objects.AttributeDelta;
31  import org.identityconnectors.framework.common.objects.SyncToken;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Helper class for serialization and deserialization of configuration objects (POJOs) in JSON.
37   */
38  public final class POJOHelper {
39  
40      private static final Logger LOG = LoggerFactory.getLogger(POJOHelper.class);
41  
42      private static final JsonMapper MAPPER;
43  
44      static {
45          SimpleModule pojoModule = new SimpleModule("POJOModule", new Version(1, 0, 0, null, null, null));
46          pojoModule.addSerializer(GuardedString.class, new GuardedStringSerializer());
47          pojoModule.addSerializer(Attribute.class, new AttributeSerializer());
48          pojoModule.addSerializer(AttributeDelta.class, new AttributeDeltaSerializer());
49          pojoModule.addSerializer(SyncToken.class, new SyncTokenSerializer());
50          pojoModule.addDeserializer(GuardedString.class, new GuardedStringDeserializer());
51          pojoModule.addDeserializer(Attribute.class, new AttributeDeserializer());
52          pojoModule.addDeserializer(AttributeDelta.class, new AttributeDeltaDeserializer());
53          pojoModule.addDeserializer(SyncToken.class, new SyncTokenDeserializer());
54  
55          MAPPER = JsonMapper.builder().
56                  addModule(pojoModule).
57                  addModule(new JavaTimeModule()).
58                  addModule(new AfterburnerModule()).
59                  disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).
60                  build();
61      }
62  
63      public static String serialize(final Object object) {
64          String result = null;
65  
66          try {
67              result = MAPPER.writeValueAsString(object);
68          } catch (Exception e) {
69              LOG.error("During serialization", e);
70          }
71  
72          return result;
73      }
74  
75      public static <T extends Object> String serialize(final T object, final TypeReference<T> reference) {
76          String result = null;
77  
78          try {
79              result = MAPPER.writerFor(reference).writeValueAsString(object);
80          } catch (Exception e) {
81              LOG.error("During serialization", e);
82          }
83  
84          return result;
85      }
86  
87      public static <T extends Object> T deserialize(final String serialized, final Class<T> reference) {
88          T result = null;
89  
90          try {
91              result = MAPPER.readValue(serialized, reference);
92          } catch (Exception e) {
93              LOG.error("During deserialization", e);
94          }
95  
96          return result;
97      }
98  
99      public static <T extends Object> T deserialize(final String serialized, final TypeReference<T> reference) {
100         T result = null;
101 
102         try {
103             result = MAPPER.readValue(serialized, reference);
104         } catch (Exception e) {
105             LOG.error("During deserialization", e);
106         }
107 
108         return result;
109     }
110     
111     public static <T extends Object> T convertValue(final Object value, final Class<T> reference) {
112         T result = null;
113 
114         try {
115             result = MAPPER.convertValue(value, reference);
116         } catch (Exception e) {
117             LOG.error("During conversion", e);
118         }
119 
120         return result;
121     }
122 
123     private POJOHelper() {
124     }
125 }