]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
70061cc3964fd6bc4b5f5beb84eb1b1e5d11446b
[tmp/jakarta-migration.git] /
1 package org.collectionspace.authentication.jackson2;
2
3 import java.io.IOException;
4 import java.util.Set;
5
6 import org.collectionspace.authentication.CSpaceTenant;
7 import org.collectionspace.authentication.CSpaceUser;
8 import org.springframework.security.core.GrantedAuthority;
9 import org.springframework.security.core.authority.SimpleGrantedAuthority;
10
11 import com.fasterxml.jackson.core.JsonParser;
12 import com.fasterxml.jackson.core.JsonProcessingException;
13 import com.fasterxml.jackson.core.type.TypeReference;
14 import com.fasterxml.jackson.databind.DeserializationContext;
15 import com.fasterxml.jackson.databind.JsonDeserializer;
16 import com.fasterxml.jackson.databind.JsonNode;
17 import com.fasterxml.jackson.databind.ObjectMapper;
18 import com.fasterxml.jackson.databind.node.MissingNode;
19
20 public class CSpaceUserDeserializer extends JsonDeserializer<CSpaceUser> {
21         private static final TypeReference<Set<SimpleGrantedAuthority>> SIMPLE_GRANTED_AUTHORITY_SET = new TypeReference<Set<SimpleGrantedAuthority>>() {
22         };
23
24   private static final TypeReference<Set<CSpaceTenant>> CSPACE_TENANT_SET = new TypeReference<Set<CSpaceTenant>>() {
25         };
26
27         @Override
28         public CSpaceUser deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
29                 ObjectMapper mapper = (ObjectMapper) parser.getCodec();
30                 JsonNode jsonNode = mapper.readTree(parser);
31
32                 Set<? extends GrantedAuthority> authorities = mapper.convertValue(jsonNode.get("authorities"), SIMPLE_GRANTED_AUTHORITY_SET);
33                 Set<CSpaceTenant> tenants = mapper.convertValue(jsonNode.get("tenants"), CSPACE_TENANT_SET);
34
35                 JsonNode passwordNode = readJsonNode(jsonNode, "password");
36                 String username = readJsonNode(jsonNode, "username").asText();
37                 String password = passwordNode.asText("");
38                 boolean requireSSO = readJsonNode(jsonNode, "requireSSO").asBoolean();
39                 String salt = readJsonNode(jsonNode, "salt").asText();
40
41                 CSpaceUser result = new CSpaceUser(username, password, salt, requireSSO, tenants,       authorities);
42
43                 if (passwordNode.asText(null) == null) {
44                         result.eraseCredentials();
45                 }
46
47                 return result;
48         }
49
50         private JsonNode readJsonNode(JsonNode jsonNode, String field) {
51                 return jsonNode.has(field) ? jsonNode.get(field) : MissingNode.getInstance();
52         }
53 }