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.client.console.rest;
20  
21  import com.fasterxml.jackson.core.JsonGenerator;
22  import com.fasterxml.jackson.core.JsonParser;
23  import com.fasterxml.jackson.core.JsonProcessingException;
24  import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
25  import com.fasterxml.jackson.databind.DeserializationContext;
26  import com.fasterxml.jackson.databind.JsonNode;
27  import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
28  import java.io.IOException;
29  import java.io.StringWriter;
30  import java.time.OffsetDateTime;
31  import java.time.format.DateTimeFormatter;
32  import java.time.format.DateTimeParseException;
33  import org.apache.syncope.common.lib.AMSession;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  public class AMSessionDeserializer extends StdDeserializer<AMSession> {
38  
39      private static final long serialVersionUID = 24527200564172L;
40  
41      private static final Logger LOG = LoggerFactory.getLogger(AMSessionDeserializer.class);
42  
43      public AMSessionDeserializer() {
44          this(null);
45      }
46  
47      public AMSessionDeserializer(final Class<?> vc) {
48          super(vc);
49      }
50  
51      @Override
52      public AMSession deserialize(final JsonParser jp, final DeserializationContext ctxt)
53              throws IOException, JsonProcessingException {
54  
55          JsonNode node = jp.getCodec().readTree(jp);
56  
57          AMSession waSession = new AMSession();
58  
59          if (node.has("authentication_date_formatted")) {
60              String authenticationDate = node.get("authentication_date_formatted").textValue();
61              try {
62                  waSession.setAuthenticationDate(
63                          OffsetDateTime.parse(authenticationDate, DateTimeFormatter.ISO_OFFSET_DATE_TIME));
64              } catch (DateTimeParseException e) {
65                  LOG.error("Unparsable date: {}", authenticationDate, e);
66              }
67          }
68  
69          if (node.has("authenticated_principal")) {
70              waSession.setPrincipal(node.get("authenticated_principal").textValue());
71          }
72  
73          if (node.has("ticket_granting_ticket")) {
74              waSession.setKey(node.get("ticket_granting_ticket").textValue());
75          }
76  
77          StringWriter writer = new StringWriter();
78          JsonGenerator jgen = jp.getCodec().getFactory().createGenerator(writer);
79          jgen.setPrettyPrinter(new DefaultPrettyPrinter());
80          jp.getCodec().writeTree(jgen, node);
81          waSession.setJson(writer.toString());
82  
83          return waSession;
84      }
85  }