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.JsonParser;
22  import com.fasterxml.jackson.databind.JsonDeserializer;
23  import com.fasterxml.jackson.databind.JsonNode;
24  import com.fasterxml.jackson.databind.node.ObjectNode;
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.Base64;
28  import java.util.List;
29  import org.apache.commons.lang3.StringUtils;
30  import org.identityconnectors.common.security.GuardedString;
31  
32  public abstract class AbstractValueDeserializer<T extends Object> extends JsonDeserializer<T> {
33  
34      protected List<Object> doDeserialize(final JsonNode value, final JsonParser jp) throws IOException {
35          List<Object> values = new ArrayList<>();
36  
37          for (JsonNode node : value) {
38              if (node.isNull()) {
39                  values.add(null);
40              } else if (node.isObject()) {
41                  values.add(((ObjectNode) node).traverse(jp.getCodec()).readValueAs(GuardedString.class));
42              } else if (node.isBoolean()) {
43                  values.add(node.asBoolean());
44              } else if (node.isDouble()) {
45                  values.add(node.asDouble());
46              } else if (node.isLong()) {
47                  values.add(node.asLong());
48              } else if (node.isInt()) {
49                  values.add(node.asInt());
50              } else {
51                  String text = node.asText();
52                  if (text.startsWith(AbstractValueSerializer.BYTE_ARRAY_PREFIX)
53                          && text.endsWith(AbstractValueSerializer.BYTE_ARRAY_SUFFIX)) {
54  
55                      values.add(Base64.getDecoder().decode(StringUtils.substringBetween(
56                              text,
57                              AbstractValueSerializer.BYTE_ARRAY_PREFIX,
58                              AbstractValueSerializer.BYTE_ARRAY_SUFFIX)));
59                  } else {
60                      values.add(text);
61                  }
62              }
63          }
64  
65          return values;
66      }
67  }