Skip to content

Commit 36a3d59

Browse files
authored
DATAES-746 - Add store converters to convert binary data.
Original PR: spring-projects#394
1 parent 05df3c3 commit 36a3d59

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/main/java/org/springframework/data/elasticsearch/core/convert/ElasticsearchCustomConversions.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.math.BigDecimal;
1919
import java.util.ArrayList;
20+
import java.util.Base64;
2021
import java.util.Collection;
2122
import java.util.Collections;
2223
import java.util.List;
@@ -48,6 +49,8 @@ public class ElasticsearchCustomConversions extends CustomConversions {
4849
converters.add(UUIDToStringConverter.INSTANCE);
4950
converters.add(BigDecimalToDoubleConverter.INSTANCE);
5051
converters.add(DoubleToBigDecimalConverter.INSTANCE);
52+
converters.add(ByteArrayToBase64Converter.INSTANCE);
53+
converters.add(Base64ToByteArrayConverter.INSTANCE);
5154

5255
STORE_CONVERTERS = Collections.unmodifiableList(converters);
5356
STORE_CONVERSIONS = StoreConversions.of(ElasticsearchSimpleTypes.HOLDER, STORE_CONVERTERS);
@@ -117,4 +120,36 @@ public Double convert(BigDecimal source) {
117120
return NumberUtils.convertNumberToTargetClass(source, Double.class);
118121
}
119122
}
123+
124+
/**
125+
* {@link Converter} to write a byte[] to a base64 encoded {@link String} value.
126+
*
127+
* @since 4.0
128+
*/
129+
@WritingConverter
130+
enum ByteArrayToBase64Converter implements Converter<byte[], String> {
131+
132+
INSTANCE,;
133+
134+
@Override
135+
public String convert(byte[] source) {
136+
return Base64.getEncoder().encodeToString(source);
137+
}
138+
}
139+
140+
/**
141+
* {@link Converter} to read a byte[] from a base64 encoded {@link String} value.
142+
*
143+
* @since 4.0
144+
*/
145+
@ReadingConverter
146+
enum Base64ToByteArrayConverter implements Converter<String, byte[]> {
147+
148+
INSTANCE;
149+
150+
@Override
151+
public byte[] convert(String source) {
152+
return Base64.getDecoder().decode(source);
153+
}
154+
}
120155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.core.convert;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions.Base64ToByteArrayConverter;
22+
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions.ByteArrayToBase64Converter;
23+
24+
/**
25+
* @author Peter-Josef Meisch
26+
*/
27+
class ElasticsearchCustomConversionsTest {
28+
29+
private byte[] bytes = new byte[] { 0x01, 0x02, 0x03, 0x04 };
30+
private String base64 = "AQIDBA==";
31+
32+
void shouldConvertFromByteArrayToBase64() {
33+
assertThat(ByteArrayToBase64Converter.INSTANCE.convert(bytes)).isEqualTo(base64);
34+
}
35+
36+
@Test
37+
void shouldConvertFromStringToBase64() {
38+
assertThat(Base64ToByteArrayConverter.INSTANCE.convert(base64)).isEqualTo(bytes);
39+
}
40+
}

0 commit comments

Comments
 (0)