1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }