|
| 1 | +package io.avaje.http.client.moshi; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.UncheckedIOException; |
| 5 | +import java.lang.reflect.Type; |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.ConcurrentHashMap; |
| 8 | + |
| 9 | +import com.squareup.moshi.JsonAdapter; |
| 10 | +import com.squareup.moshi.Moshi; |
| 11 | +import com.squareup.moshi.Types; |
| 12 | + |
| 13 | +import io.avaje.http.client.BodyAdapter; |
| 14 | +import io.avaje.http.client.BodyContent; |
| 15 | +import io.avaje.http.client.BodyReader; |
| 16 | +import io.avaje.http.client.BodyWriter; |
| 17 | + |
| 18 | +/** |
| 19 | + * Moshi BodyAdapter to read and write beans as JSON. |
| 20 | + * |
| 21 | + * <pre>{@code |
| 22 | + * HttpClient.builder() |
| 23 | + * .baseUrl(baseUrl) |
| 24 | + * .bodyAdapter(new MoshiBodyAdapter()) |
| 25 | + * .build(); |
| 26 | + * |
| 27 | + * }</pre> |
| 28 | + */ |
| 29 | +public final class MoshiBodyAdapter implements BodyAdapter { |
| 30 | + |
| 31 | + private final Moshi moshi; |
| 32 | + private final ConcurrentHashMap<Type, BodyWriter<?>> beanWriterCache = new ConcurrentHashMap<>(); |
| 33 | + private final ConcurrentHashMap<Type, BodyReader<?>> beanReaderCache = new ConcurrentHashMap<>(); |
| 34 | + private final ConcurrentHashMap<Type, BodyReader<?>> listReaderCache = new ConcurrentHashMap<>(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Create passing the Moshi to use. |
| 38 | + */ |
| 39 | + public MoshiBodyAdapter(Moshi moshi) { |
| 40 | + this.moshi = moshi; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Create with a default Moshi that allows unknown properties. |
| 45 | + */ |
| 46 | + public MoshiBodyAdapter() { |
| 47 | + this(new Moshi.Builder().build()); |
| 48 | + } |
| 49 | + |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + @Override |
| 52 | + public <T> BodyWriter<T> beanWriter(Class<?> cls) { |
| 53 | + return (BodyWriter<T>) beanWriterCache.computeIfAbsent(cls, aClass -> new JWriter<>(moshi.adapter(cls))); |
| 54 | + } |
| 55 | + |
| 56 | + @SuppressWarnings("unchecked") |
| 57 | + @Override |
| 58 | + public <T> BodyWriter<T> beanWriter(Type type) { |
| 59 | + return (BodyWriter<T>) beanWriterCache.computeIfAbsent(type, aClass -> new JWriter<>(moshi.adapter(type))); |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + @Override |
| 64 | + public <T> BodyReader<T> beanReader(Class<T> cls) { |
| 65 | + return (BodyReader<T>) beanReaderCache.computeIfAbsent(cls, aClass -> new JReader<>(moshi.adapter(cls))); |
| 66 | + } |
| 67 | + |
| 68 | + @SuppressWarnings("unchecked") |
| 69 | + @Override |
| 70 | + public <T> BodyReader<T> beanReader(Type type) { |
| 71 | + return (BodyReader<T>) beanReaderCache.computeIfAbsent(type, aClass -> new JReader<>(moshi.adapter(type))); |
| 72 | + } |
| 73 | + |
| 74 | + @SuppressWarnings("unchecked") |
| 75 | + @Override |
| 76 | + public <T> BodyReader<List<T>> listReader(Type type) { |
| 77 | + return (BodyReader<List<T>>) |
| 78 | + listReaderCache.computeIfAbsent( |
| 79 | + type, |
| 80 | + aClass -> new JReader<>(moshi.adapter(Types.newParameterizedType(List.class, type)))); |
| 81 | + } |
| 82 | + |
| 83 | + @SuppressWarnings("unchecked") |
| 84 | + @Override |
| 85 | + public <T> BodyReader<List<T>> listReader(Class<T> cls) { |
| 86 | + return (BodyReader<List<T>>) |
| 87 | + listReaderCache.computeIfAbsent( |
| 88 | + cls, |
| 89 | + aClass -> new JReader<>(moshi.adapter(Types.newParameterizedType(List.class, cls)))); |
| 90 | + } |
| 91 | + |
| 92 | + private static class JReader<T> implements BodyReader<T> { |
| 93 | + |
| 94 | + private final JsonAdapter<T> reader; |
| 95 | + |
| 96 | + JReader(JsonAdapter<T> reader) { |
| 97 | + this.reader = reader; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public T readBody(String content) { |
| 102 | + try { |
| 103 | + return reader.fromJson(content); |
| 104 | + } catch (IOException e) { |
| 105 | + throw new UncheckedIOException(e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public T read(BodyContent bodyContent) { |
| 111 | + try { |
| 112 | + return reader.fromJson(bodyContent.contentAsUtf8()); |
| 113 | + } catch (IOException e) { |
| 114 | + throw new UncheckedIOException(e); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static class JWriter<T> implements BodyWriter<T> { |
| 120 | + |
| 121 | + private final JsonAdapter<T> writer; |
| 122 | + |
| 123 | + public JWriter(JsonAdapter<T> writer) { |
| 124 | + this.writer = writer; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public BodyContent write(T bean, String contentType) { |
| 129 | + // ignoring the requested contentType and always |
| 130 | + // writing the body as json content |
| 131 | + return write(bean); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public BodyContent write(T bean) { |
| 136 | + return BodyContent.of(writer.toJson(bean)); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments