com.fullcontact.rpc.jersey.RequestParser Maven / Gradle / Ivy
The newest version!
package com.fullcontact.rpc.jersey;
import com.fullcontact.rpc.jersey.util.ProtobufDescriptorJavaUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.protobuf.DescriptorProtos;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import com.google.protobuf.UnsafeByteOperations;
import io.grpc.Metadata;
import io.grpc.stub.AbstractStub;
import io.grpc.stub.MetadataUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;
/**
* Utility used by resource template to parse parameters
*
* @author Michael Rose (xorlev)
*/
public class RequestParser {
public static void parseQueryParams(UriInfo uriInfo,
V.Builder builder,
DescriptorProtos.FieldDescriptorProto... pathParams)
throws InvalidProtocolBufferException {
parseQueryParams(uriInfo, builder, ImmutableList.copyOf(pathParams));
}
public static void parseQueryParams(UriInfo uriInfo,
V.Builder builder,
List pathParams)
throws InvalidProtocolBufferException {
Set pathDescriptors = Sets.newHashSet(pathParams);
for (String queryParam : uriInfo.getQueryParameters().keySet()) {
ImmutableList descriptors =
ProtobufDescriptorJavaUtil.fieldPath(builder.getDescriptorForType(), queryParam);
if (!descriptors.isEmpty()) {
Descriptors.FieldDescriptor field = Iterables.getLast(descriptors);
if (!pathDescriptors.contains(field.toProto())) {
setFieldSafely(builder, queryParam, uriInfo.getQueryParameters().get(queryParam));
}
}
}
}
public static > T parseHeaders(HttpHeaders headers, T stub) {
return stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(parseHeaders(headers)));
}
public static Metadata parseHeaders(HttpHeaders headers) {
Metadata newHeaders = new Metadata();
headers.getRequestHeaders().forEach((k, v) -> {
newHeaders.put(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER), v.get(0));
}
);
return newHeaders;
}
public static void setFieldSafely(Message.Builder builder, String path, String value)
throws InvalidProtocolBufferException {
setFieldSafely(builder, path, ImmutableList.of(value));
}
public static void setFieldSafely(Message.Builder builder, String path, List value)
throws InvalidProtocolBufferException {
Descriptors.Descriptor descriptor = builder.getDescriptorForType();
ImmutableList fieldDescriptors =
ProtobufDescriptorJavaUtil.fieldPath(descriptor, path);
Message.Builder fieldBuilder = builder;
for (Descriptors.FieldDescriptor fieldDescriptor : fieldDescriptors) {
if (fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) {
fieldBuilder = fieldBuilder.getFieldBuilder(fieldDescriptor);
}
}
if (fieldDescriptors.isEmpty()) {
throw new IllegalArgumentException("Path " + path + " doesn't exist from root: "
+ builder.getDescriptorForType().getName());
}
setFieldSafely(fieldBuilder, fieldDescriptors.get(fieldDescriptors.size() - 1), value);
}
public static void setFieldSafely(Message.Builder builder, Descriptors.FieldDescriptor fd, List value)
throws InvalidProtocolBufferException {
Object valueToSet = getValueFor(fd, value);
builder.setField(fd, valueToSet);
}
private static Object getValueFor(FieldDescriptor fd, List value) throws InvalidProtocolBufferException {
Object result;
if (!fd.isRepeated()) {
if (value.size() != 1) {
throw new InvalidProtocolBufferException("Unable to map " + fd + " to value: " + value);
}
result = getUnaryValueFor(fd, value.get(0));
} else {
List