|
1 | 1 | /*
|
2 |
| - * Copyright 2014 the original author or authors. |
| 2 | + * Copyright 2014-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 | package org.springframework.data.elasticsearch.core;
|
17 | 17 |
|
18 | 18 | import java.io.IOException;
|
| 19 | +import java.util.ArrayList; |
19 | 20 | import java.util.List;
|
20 | 21 |
|
21 |
| -import com.fasterxml.jackson.annotation.JsonIgnore; |
22 |
| -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
23 |
| -import com.fasterxml.jackson.annotation.JsonProperty; |
24 |
| -import com.fasterxml.jackson.core.*; |
25 |
| -import com.fasterxml.jackson.databind.*; |
26 |
| -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
27 |
| -import com.fasterxml.jackson.databind.module.SimpleModule; |
28 |
| -import org.springframework.data.elasticsearch.annotations.Document; |
| 22 | +import org.springframework.data.annotation.ReadOnlyProperty; |
29 | 23 | import org.springframework.data.elasticsearch.core.geo.CustomGeoModule;
|
30 |
| -import org.springframework.data.geo.*; |
31 |
| -import java.util.List; |
| 24 | +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; |
| 25 | +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; |
| 26 | +import org.springframework.data.mapping.context.MappingContext; |
| 27 | +import org.springframework.util.Assert; |
32 | 28 |
|
33 |
| -import com.fasterxml.jackson.annotation.JsonIgnore; |
34 |
| -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
35 |
| -import com.fasterxml.jackson.annotation.JsonProperty; |
36 |
| -import com.fasterxml.jackson.core.Version; |
37 |
| -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 29 | +import com.fasterxml.jackson.databind.BeanDescription; |
| 30 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 31 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 32 | +import com.fasterxml.jackson.databind.SerializationConfig; |
38 | 33 | import com.fasterxml.jackson.databind.module.SimpleModule;
|
39 |
| - |
| 34 | +import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; |
| 35 | +import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; |
40 | 36 |
|
41 | 37 | /**
|
42 |
| - * DocumentMapper using jackson |
| 38 | + * EntityMapper based on a Jackson {@link ObjectMapper}. |
43 | 39 | *
|
44 | 40 | * @author Artur Konczak
|
45 | 41 | * @author Petar Tahchiev
|
| 42 | + * @author Oliver Gierke |
46 | 43 | */
|
47 | 44 | public class DefaultEntityMapper implements EntityMapper {
|
48 | 45 |
|
49 | 46 | private ObjectMapper objectMapper;
|
50 | 47 |
|
51 |
| - public DefaultEntityMapper() { |
| 48 | + /** |
| 49 | + * Creates a new {@link DefaultEntityMapper} using the given {@link MappingContext}. |
| 50 | + * |
| 51 | + * @param context must not be {@literal null}. |
| 52 | + */ |
| 53 | + public DefaultEntityMapper( |
| 54 | + MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> context) { |
| 55 | + |
| 56 | + Assert.notNull(context, "MappingContext must not be null!"); |
| 57 | + |
52 | 58 | objectMapper = new ObjectMapper();
|
| 59 | + |
| 60 | + objectMapper.registerModule(new SpringDataElasticsearchModule(context)); |
| 61 | + objectMapper.registerModule(new CustomGeoModule()); |
| 62 | + |
53 | 63 | objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
54 | 64 | objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
55 |
| - objectMapper.registerModule(new CustomGeoModule()); |
56 | 65 | }
|
57 | 66 |
|
| 67 | + /* |
| 68 | + * (non-Javadoc) |
| 69 | + * @see org.springframework.data.elasticsearch.core.EntityMapper#mapToString(java.lang.Object) |
| 70 | + */ |
58 | 71 | @Override
|
59 | 72 | public String mapToString(Object object) throws IOException {
|
60 | 73 | return objectMapper.writeValueAsString(object);
|
61 | 74 | }
|
62 | 75 |
|
| 76 | + /* |
| 77 | + * (non-Javadoc) |
| 78 | + * @see org.springframework.data.elasticsearch.core.EntityMapper#mapToObject(java.lang.String, java.lang.Class) |
| 79 | + */ |
63 | 80 | @Override
|
64 | 81 | public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
|
65 | 82 | return objectMapper.readValue(source, clazz);
|
66 | 83 | }
|
| 84 | + |
| 85 | + /** |
| 86 | + * A simple Jackson module to register the {@link SpringDataSerializerModifier}. |
| 87 | + * |
| 88 | + * @author Oliver Gierke |
| 89 | + * @since 3.1 |
| 90 | + */ |
| 91 | + private static class SpringDataElasticsearchModule extends SimpleModule { |
| 92 | + |
| 93 | + private static final long serialVersionUID = -9168968092458058966L; |
| 94 | + |
| 95 | + /** |
| 96 | + * Creates a new {@link SpringDataElasticsearchModule} using the given {@link MappingContext}. |
| 97 | + * |
| 98 | + * @param context must not be {@literal null}. |
| 99 | + */ |
| 100 | + public SpringDataElasticsearchModule( |
| 101 | + MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> context) { |
| 102 | + |
| 103 | + Assert.notNull(context, "MappingContext must not be null!"); |
| 104 | + |
| 105 | + setSerializerModifier(new SpringDataSerializerModifier(context)); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * A {@link BeanSerializerModifier} that will drop properties annotated with {@link ReadOnlyProperty} for |
| 110 | + * serialization. |
| 111 | + * |
| 112 | + * @author Oliver Gierke |
| 113 | + * @since 3.1 |
| 114 | + */ |
| 115 | + private static class SpringDataSerializerModifier extends BeanSerializerModifier { |
| 116 | + |
| 117 | + private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> context; |
| 118 | + |
| 119 | + public SpringDataSerializerModifier( |
| 120 | + MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> context) { |
| 121 | + |
| 122 | + Assert.notNull(context, "MappingContext must not be null!"); |
| 123 | + |
| 124 | + this.context = context; |
| 125 | + } |
| 126 | + |
| 127 | + /* |
| 128 | + * (non-Javadoc) |
| 129 | + * @see com.fasterxml.jackson.databind.ser.BeanSerializerModifier#changeProperties(com.fasterxml.jackson.databind.SerializationConfig, com.fasterxml.jackson.databind.BeanDescription, java.util.List) |
| 130 | + */ |
| 131 | + @Override |
| 132 | + public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription description, |
| 133 | + List<BeanPropertyWriter> properties) { |
| 134 | + |
| 135 | + Class<?> type = description.getBeanClass(); |
| 136 | + ElasticsearchPersistentEntity<?> entity = context.getPersistentEntity(type); |
| 137 | + |
| 138 | + if (entity == null) { |
| 139 | + return super.changeProperties(config, description, properties); |
| 140 | + } |
| 141 | + |
| 142 | + List<BeanPropertyWriter> result = new ArrayList<>(properties.size()); |
| 143 | + |
| 144 | + for (BeanPropertyWriter beanPropertyWriter : properties) { |
| 145 | + |
| 146 | + ElasticsearchPersistentProperty property = entity.getPersistentProperty(beanPropertyWriter.getName()); |
| 147 | + |
| 148 | + if (property != null && property.isWritable()) { |
| 149 | + result.add(beanPropertyWriter); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return result; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
67 | 157 | }
|
0 commit comments