|
1 | | -package org.rest.sec.client.marshall; |
2 | | - |
3 | | -import java.io.IOException; |
4 | | -import java.util.List; |
5 | | - |
6 | | -import org.rest.common.client.marshall.IMarshaller; |
7 | | -import org.rest.sec.model.Principal; |
8 | | -import org.rest.sec.model.Privilege; |
9 | | -import org.rest.sec.model.Role; |
10 | | -import org.rest.sec.model.dto.User; |
11 | | -import org.slf4j.Logger; |
12 | | -import org.slf4j.LoggerFactory; |
13 | | -import org.springframework.context.annotation.Profile; |
14 | | -import org.springframework.http.MediaType; |
15 | | -import org.springframework.stereotype.Component; |
16 | | - |
17 | | -import com.fasterxml.jackson.core.JsonParseException; |
18 | | -import com.fasterxml.jackson.core.type.TypeReference; |
19 | | -import com.fasterxml.jackson.databind.JsonMappingException; |
20 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
21 | | -import com.google.common.base.Preconditions; |
22 | | - |
23 | | -@Component("jacksonMarshaller") |
24 | | -@Profile("mime_json") |
25 | | -public final class JacksonMarshaller implements IMarshaller { |
26 | | - private final Logger logger = LoggerFactory.getLogger(JacksonMarshaller.class); |
27 | | - |
28 | | - private final ObjectMapper objectMapper; |
29 | | - |
30 | | - public JacksonMarshaller() { |
31 | | - super(); |
32 | | - |
33 | | - objectMapper = new ObjectMapper(); |
34 | | - } |
35 | | - |
36 | | - // API |
37 | | - |
38 | | - @Override |
39 | | - public final <T> String encode(final T entity) { |
40 | | - Preconditions.checkNotNull(entity); |
41 | | - String entityAsJSON = null; |
42 | | - try { |
43 | | - entityAsJSON = objectMapper.writeValueAsString(entity); |
44 | | - } catch (final JsonParseException parseEx) { |
45 | | - logger.error("", parseEx); |
46 | | - } catch (final JsonMappingException mappingEx) { |
47 | | - logger.error("", mappingEx); |
48 | | - } catch (final IOException ioEx) { |
49 | | - logger.error("", ioEx); |
50 | | - } |
51 | | - |
52 | | - return entityAsJSON; |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - public final <T> T decode(final String entityAsString, final Class<T> clazz) { |
57 | | - Preconditions.checkNotNull(entityAsString); |
58 | | - |
59 | | - T entity = null; |
60 | | - try { |
61 | | - entity = objectMapper.readValue(entityAsString, clazz); |
62 | | - } catch (final JsonParseException parseEx) { |
63 | | - logger.error("", parseEx); |
64 | | - } catch (final JsonMappingException mappingEx) { |
65 | | - logger.error("", mappingEx); |
66 | | - } catch (final IOException ioEx) { |
67 | | - logger.error("", ioEx); |
68 | | - } |
69 | | - |
70 | | - return entity; |
71 | | - } |
72 | | - |
73 | | - @SuppressWarnings("unchecked") |
74 | | - @Override |
75 | | - public final <T> List<T> decodeList(final String entitiesAsString, final Class<T> clazz) { |
76 | | - Preconditions.checkNotNull(entitiesAsString); |
77 | | - |
78 | | - List<T> entities = null; |
79 | | - try { |
80 | | - if (clazz.equals(Role.class)) { |
81 | | - entities = objectMapper.readValue(entitiesAsString, new TypeReference<List<Role>>() { |
82 | | - // ... |
83 | | - }); |
84 | | - } else if (clazz.equals(Privilege.class)) { |
85 | | - entities = objectMapper.readValue(entitiesAsString, new TypeReference<List<Privilege>>() { |
86 | | - // ... |
87 | | - }); |
88 | | - } else if (clazz.equals(User.class)) { |
89 | | - entities = objectMapper.readValue(entitiesAsString, new TypeReference<List<User>>() { |
90 | | - // ... |
91 | | - }); |
92 | | - } else if (clazz.equals(Principal.class)) { |
93 | | - entities = objectMapper.readValue(entitiesAsString, new TypeReference<List<Principal>>() { |
94 | | - // ... |
95 | | - }); |
96 | | - } else { |
97 | | - entities = objectMapper.readValue(entitiesAsString, List.class); |
98 | | - } |
99 | | - } catch (final JsonParseException parseEx) { |
100 | | - logger.error("", parseEx); |
101 | | - } catch (final JsonMappingException mappingEx) { |
102 | | - logger.error("", mappingEx); |
103 | | - } catch (final IOException ioEx) { |
104 | | - logger.error("", ioEx); |
105 | | - } |
106 | | - |
107 | | - return entities; |
108 | | - } |
109 | | - |
110 | | - @Override |
111 | | - public final String getMime() { |
112 | | - return MediaType.APPLICATION_JSON.toString(); |
113 | | - } |
114 | | - |
115 | | -} |
| 1 | +package org.rest.sec.client.marshall; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.rest.common.client.marshall.IMarshaller; |
| 7 | +import org.rest.sec.model.Principal; |
| 8 | +import org.rest.sec.model.Privilege; |
| 9 | +import org.rest.sec.model.Role; |
| 10 | +import org.rest.sec.model.dto.User; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.springframework.context.annotation.Profile; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | + |
| 17 | +import com.fasterxml.jackson.core.JsonParseException; |
| 18 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 19 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | + |
| 23 | +@Component |
| 24 | +@Profile("mime_json") |
| 25 | +public final class JacksonMarshaller implements IMarshaller { |
| 26 | + private final Logger logger = LoggerFactory.getLogger(JacksonMarshaller.class); |
| 27 | + |
| 28 | + private final ObjectMapper objectMapper; |
| 29 | + |
| 30 | + public JacksonMarshaller() { |
| 31 | + super(); |
| 32 | + |
| 33 | + objectMapper = new ObjectMapper(); |
| 34 | + } |
| 35 | + |
| 36 | + // API |
| 37 | + |
| 38 | + @Override |
| 39 | + public final <T> String encode(final T entity) { |
| 40 | + Preconditions.checkNotNull(entity); |
| 41 | + String entityAsJSON = null; |
| 42 | + try { |
| 43 | + entityAsJSON = objectMapper.writeValueAsString(entity); |
| 44 | + } catch (final JsonParseException parseEx) { |
| 45 | + logger.error("", parseEx); |
| 46 | + } catch (final JsonMappingException mappingEx) { |
| 47 | + logger.error("", mappingEx); |
| 48 | + } catch (final IOException ioEx) { |
| 49 | + logger.error("", ioEx); |
| 50 | + } |
| 51 | + |
| 52 | + return entityAsJSON; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public final <T> T decode(final String entityAsString, final Class<T> clazz) { |
| 57 | + Preconditions.checkNotNull(entityAsString); |
| 58 | + |
| 59 | + T entity = null; |
| 60 | + try { |
| 61 | + entity = objectMapper.readValue(entityAsString, clazz); |
| 62 | + } catch (final JsonParseException parseEx) { |
| 63 | + logger.error("", parseEx); |
| 64 | + } catch (final JsonMappingException mappingEx) { |
| 65 | + logger.error("", mappingEx); |
| 66 | + } catch (final IOException ioEx) { |
| 67 | + logger.error("", ioEx); |
| 68 | + } |
| 69 | + |
| 70 | + return entity; |
| 71 | + } |
| 72 | + |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + @Override |
| 75 | + public final <T> List<T> decodeList(final String entitiesAsString, final Class<T> clazz) { |
| 76 | + Preconditions.checkNotNull(entitiesAsString); |
| 77 | + |
| 78 | + List<T> entities = null; |
| 79 | + try { |
| 80 | + if (clazz.equals(Role.class)) { |
| 81 | + entities = objectMapper.readValue(entitiesAsString, new TypeReference<List<Role>>() { |
| 82 | + // ... |
| 83 | + }); |
| 84 | + } else if (clazz.equals(Privilege.class)) { |
| 85 | + entities = objectMapper.readValue(entitiesAsString, new TypeReference<List<Privilege>>() { |
| 86 | + // ... |
| 87 | + }); |
| 88 | + } else if (clazz.equals(User.class)) { |
| 89 | + entities = objectMapper.readValue(entitiesAsString, new TypeReference<List<User>>() { |
| 90 | + // ... |
| 91 | + }); |
| 92 | + } else if (clazz.equals(Principal.class)) { |
| 93 | + entities = objectMapper.readValue(entitiesAsString, new TypeReference<List<Principal>>() { |
| 94 | + // ... |
| 95 | + }); |
| 96 | + } else { |
| 97 | + entities = objectMapper.readValue(entitiesAsString, List.class); |
| 98 | + } |
| 99 | + } catch (final JsonParseException parseEx) { |
| 100 | + logger.error("", parseEx); |
| 101 | + } catch (final JsonMappingException mappingEx) { |
| 102 | + logger.error("", mappingEx); |
| 103 | + } catch (final IOException ioEx) { |
| 104 | + logger.error("", ioEx); |
| 105 | + } |
| 106 | + |
| 107 | + return entities; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public final String getMime() { |
| 112 | + return MediaType.APPLICATION_JSON.toString(); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments