|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +package com.salesforce.grpc.contrib; |
| 9 | + |
| 10 | +import com.google.common.reflect.TypeToken; |
| 11 | +import com.google.gson.Gson; |
| 12 | +import com.google.protobuf.GeneratedMessageV3; |
| 13 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 14 | +import io.grpc.Metadata; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.StringWriter; |
| 18 | +import java.io.Writer; |
| 19 | +import java.lang.reflect.InvocationTargetException; |
| 20 | +import java.lang.reflect.Method; |
| 21 | + |
| 22 | +/** |
| 23 | + * {@code MoreMetadata} provides additional utilities for working with gRPC {@code Metadata}. |
| 24 | + */ |
| 25 | +//CHECKSTYLE:OFF: MethodName |
| 26 | +public final class MoreMetadata { |
| 27 | + private MoreMetadata() { } |
| 28 | + |
| 29 | + /** |
| 30 | + * A metadata marshaller that encodes objects as JSON using the google-gson library. |
| 31 | + * |
| 32 | + * <p>All non-ascii characters are unicode escaped to comply with {@code AsciiMarshaller}'s character range |
| 33 | + * requirements. |
| 34 | + * |
| 35 | + * @param clazz the type to serialize |
| 36 | + * @param <T> |
| 37 | + */ |
| 38 | + public static final <T> Metadata.AsciiMarshaller<T> JSON_MARSHALLER(Class<T> clazz) { |
| 39 | + return new Metadata.AsciiMarshaller<T>() { |
| 40 | + TypeToken<T> typeToken = TypeToken.of(clazz); |
| 41 | + private Gson gson = new Gson(); |
| 42 | + |
| 43 | + @Override |
| 44 | + public String toAsciiString(T value) { |
| 45 | + try { |
| 46 | + try (StringWriter sw = new StringWriter()) { |
| 47 | + gson.toJson(value, typeToken.getType(), new UnicodeEscapingAsciiWriter(sw)); |
| 48 | + return sw.toString(); |
| 49 | + } |
| 50 | + } catch (IOException e) { |
| 51 | + throw new IllegalArgumentException(e); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public T parseAsciiString(String serialized) { |
| 57 | + return gson.fromJson(serialized, typeToken.getType()); |
| 58 | + } |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * See: https://github.com/google/gson/issues/388. |
| 64 | + */ |
| 65 | + private static final class UnicodeEscapingAsciiWriter extends Writer { |
| 66 | + private final Writer out; |
| 67 | + |
| 68 | + private UnicodeEscapingAsciiWriter(Writer out) { |
| 69 | + this.out = out; |
| 70 | + } |
| 71 | + |
| 72 | + @Override public void write(char[] buffer, int offset, int count) throws IOException { |
| 73 | + for (int i = 0; i < count; i++) { |
| 74 | + char c = buffer[i + offset]; |
| 75 | + if (c >= ' ' && c <= '~') { |
| 76 | + out.write(c); |
| 77 | + } else { |
| 78 | + out.write(String.format("\\u%04x", (int) c)); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override public void flush() throws IOException { |
| 84 | + out.flush(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override public void close() throws IOException { |
| 88 | + out.close(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * A metadata marshaller that encodes objects as protobuf according to their proto IDL specification. |
| 94 | + * |
| 95 | + * @param clazz the type to serialize |
| 96 | + * @param <T> |
| 97 | + */ |
| 98 | + public static <T extends GeneratedMessageV3> Metadata.BinaryMarshaller<T> PROTOBUF_MARSHALLER(Class<T> clazz) { |
| 99 | + try { |
| 100 | + Method defaultInstance = clazz.getMethod("getDefaultInstance"); |
| 101 | + GeneratedMessageV3 instance = (GeneratedMessageV3) defaultInstance.invoke(null); |
| 102 | + |
| 103 | + return new Metadata.BinaryMarshaller<T>() { |
| 104 | + @Override |
| 105 | + public byte[] toBytes(T value) { |
| 106 | + return value.toByteArray(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public T parseBytes(byte[] serialized) { |
| 111 | + try { |
| 112 | + return (T) instance.getParserForType().parseFrom(serialized); |
| 113 | + } catch (InvalidProtocolBufferException ipbe) { |
| 114 | + throw new IllegalArgumentException(ipbe); |
| 115 | + } |
| 116 | + } |
| 117 | + }; |
| 118 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) { |
| 119 | + throw new IllegalStateException(ex); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * A metadata marshaller that encodes boolean values. |
| 125 | + */ |
| 126 | + public static final Metadata.AsciiMarshaller<Boolean> BOOLEAN_MARSHALLER = new Metadata.AsciiMarshaller<Boolean>() { |
| 127 | + @Override |
| 128 | + public String toAsciiString(Boolean value) { |
| 129 | + return value.toString(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Boolean parseAsciiString(String serialized) { |
| 134 | + return Boolean.parseBoolean(serialized); |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + /** |
| 139 | + * A metadata marshaller that encodes integer-type values. |
| 140 | + */ |
| 141 | + public static final Metadata.AsciiMarshaller<Long> LONG_MARSHALLER = new Metadata.AsciiMarshaller<Long>() { |
| 142 | + @Override |
| 143 | + public String toAsciiString(Long value) { |
| 144 | + return value.toString(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public Long parseAsciiString(String serialized) { |
| 149 | + return Long.parseLong(serialized); |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + /** |
| 154 | + * A metadata marshaller that encodes floating-point-type values. |
| 155 | + */ |
| 156 | + public static final Metadata.AsciiMarshaller<Double> DOUBLE_MARSHALLER = new Metadata.AsciiMarshaller<Double>() { |
| 157 | + @Override |
| 158 | + public String toAsciiString(Double value) { |
| 159 | + return value.toString(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public Double parseAsciiString(String serialized) { |
| 164 | + return Double.parseDouble(serialized); |
| 165 | + } |
| 166 | + }; |
| 167 | +} |
0 commit comments