com.google.protobuf.ExtensionRegistry Java Examples

The following examples show how to use com.google.protobuf.ExtensionRegistry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleBidRequest.java    From bidder with Apache License 2.0 7 votes vote down vote up
/**
 * Given the input stream, parse and make the class.
 * 
 * @param in InputStream. The content of the POST
 * @throws Exception on protobuf, json or I/O errors.
 */
public GoogleBidRequest(InputStream in) throws Exception {
	int nRead;
	byte[] data = new byte[1024];
	ByteArrayOutputStream buffer = new ByteArrayOutputStream();
	while ((nRead = in.read(data, 0, data.length)) != -1) {
		buffer.write(data, 0, nRead);
	}

	data = buffer.toByteArray();
	if (data.length == 0) {
		notABidRequest = true;
		return;
	}

	ExtensionRegistry registry = ExtensionRegistry.newInstance();
	registry.add(com.google.doubleclick.AdxExt.imp);
	internal = com.google.openrtb.OpenRtb.BidRequest.parseFrom(data, registry);

	// internal = com.google.openrtb.OpenRtb.BidRequest.parseFrom(in);
	doInternal();
}
 
Example #2
Source File: ProtobufHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void merge(InputStream input, Charset charset, MediaType contentType,
		ExtensionRegistry extensionRegistry, Message.Builder builder)
		throws IOException, HttpMessageConversionException {

	if (contentType.isCompatibleWith(APPLICATION_JSON)) {
		this.jsonFormatter.merge(input, charset, extensionRegistry, builder);
	}
	else if (contentType.isCompatibleWith(APPLICATION_XML)) {
		this.xmlFormatter.merge(input, charset, extensionRegistry, builder);
	}
	else {
		throw new HttpMessageConversionException(
				"protobuf-java-format does not support parsing " + contentType);
	}
}
 
Example #3
Source File: Convert.java    From metastore with Apache License 2.0 6 votes vote down vote up
static Map<String, Descriptors.FileDescriptor> registerOptions(
    Map<String, Descriptors.FileDescriptor> fileDescriptorMap) {
  Map<String, DescriptorProtos.FileDescriptorProto> inMap = new HashMap<>();
  Map<String, Descriptors.FileDescriptor> outMap = new HashMap<>();

  fileDescriptorMap.forEach((name, fd) -> inMap.put(name, fd.toProto()));

  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  inMap
      .keySet()
      .forEach(fileName -> convertToFileDescriptorMap(fileName, null, inMap, outMap, registry));

  outMap.forEach(
      (k, fileDescriptor) ->
          Descriptors.FileDescriptor.internalUpdateFileDescriptor(fileDescriptor, registry));
  return outMap;
}
 
Example #4
Source File: TestExtensionRegistry.java    From jackson-datatype-protobuf with Apache License 2.0 6 votes vote down vote up
public static ExtensionRegistry getInstance() {
  ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
  Iterable<FieldDescriptor> extensionDescriptors = Iterables.concat(
      AllExtensions.getDescriptor().getExtensions(),
      RepeatedExtensions.getDescriptor().getExtensions()
  );

  for (FieldDescriptor extension : extensionDescriptors) {
    if (extension.getJavaType() == JavaType.MESSAGE) {
      extensionRegistry.add(extension, Nested.getDefaultInstance());
    } else {
      extensionRegistry.add(extension);
    }
  }

  return extensionRegistry;
}
 
Example #5
Source File: JsonFormat.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 */
public static void merge(CharSequence input,
                         ExtensionRegistry extensionRegistry,
                         Message.Builder builder, boolean selfType) throws ParseException {
    Tokenizer tokenizer = new Tokenizer(input);

    // Based on the state machine @ http://json.org/

    tokenizer.consume("{"); // Needs to happen when the object starts.
    while (!tokenizer.tryConsume("}")) { // Continue till the object is done
        mergeField(tokenizer, extensionRegistry, builder, selfType);
    }
    // Test to make sure the tokenizer has reached the end of the stream.
    if (!tokenizer.atEnd()) {
        throw tokenizer.parseException(
                "Expecting the end of the stream, but there seems to be more data!  "
                        + "Check the input for a valid JSON format.");
    }
}
 
Example #6
Source File: TestMmiReceiver.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
     * Set up the test environment.
     * 
     * @throws Exception
     *             set up failed
     */
    @Before
    public void setUp() throws Exception {
        receivingNode = new Node();
        publishingNode = new Node();
        final MmiReceiver receiver = new MmiReceiver("target1");
        receiver.addMMIEventListener(this);
        TypedSubscriber subscriber = new TypedSubscriber("test");
        subscriber.setReceiver(receiver);
        receivingNode.addSubscriber(subscriber);
        // TODO fix this in umundo
//        subscriber.registerType(LifeCycleEvents.LifeCycleEvent.class);

        ExtensionRegistry registry = ExtensionRegistry.newInstance();
        LifeCycleEvents.registerAllExtensions(registry);
        subscriber.setExtensionRegistry(registry);
        lock = new Object();
        receivedEvent = null;
    }
 
Example #7
Source File: JsonFormat.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 */
public void merge(CharSequence input,
                         ExtensionRegistry extensionRegistry,
                         Message.Builder builder) throws ParseException {
    Tokenizer tokenizer = new Tokenizer(input);

    // Based on the state machine @ http://json.org/

    tokenizer.consume("{"); // Needs to happen when the object starts.
    while (!tokenizer.tryConsume("}")) { // Continue till the object is done
        mergeField(tokenizer, extensionRegistry, builder);
    }
    // Test to make sure the tokenizer has reached the end of the stream.
    if (!tokenizer.atEnd()) {
        throw tokenizer.parseException("Expecting the end of the stream, but there seems to be more data!  Check the input for a valid JSON format.");
    }
}
 
Example #8
Source File: CodecConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void encoderDecoderOverrides() {
	Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
	Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
	ProtobufDecoder protobufDecoder = new ProtobufDecoder(ExtensionRegistry.newInstance());
	ProtobufEncoder protobufEncoder = new ProtobufEncoder();
	Jaxb2XmlEncoder jaxb2Encoder = new Jaxb2XmlEncoder();
	Jaxb2XmlDecoder jaxb2Decoder = new Jaxb2XmlDecoder();

	this.configurer.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
	this.configurer.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
	this.configurer.defaultCodecs().protobufDecoder(protobufDecoder);
	this.configurer.defaultCodecs().protobufEncoder(protobufEncoder);
	this.configurer.defaultCodecs().jaxb2Decoder(jaxb2Decoder);
	this.configurer.defaultCodecs().jaxb2Encoder(jaxb2Encoder);

	assertDecoderInstance(jacksonDecoder);
	assertDecoderInstance(protobufDecoder);
	assertDecoderInstance(jaxb2Decoder);
	assertEncoderInstance(jacksonEncoder);
	assertEncoderInstance(protobufEncoder);
	assertEncoderInstance(jaxb2Encoder);
}
 
Example #9
Source File: ProtobufHttpMessageConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void merge(InputStream input, Charset charset, MediaType contentType,
		ExtensionRegistry extensionRegistry, Message.Builder builder)
		throws IOException, HttpMessageConversionException {

	if (contentType.isCompatibleWith(APPLICATION_JSON)) {
		this.jsonFormatter.merge(input, charset, extensionRegistry, builder);
	}
	else if (contentType.isCompatibleWith(APPLICATION_XML)) {
		this.xmlFormatter.merge(input, charset, extensionRegistry, builder);
	}
	else {
		throw new HttpMessageConversionException(
				"protobuf-java-format does not support parsing " + contentType);
	}
}
 
Example #10
Source File: ProtoCoder.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Validate that all extensionHosts are able to be registered.
 *
 * @param moreExtensionHosts
 */
void validateExtensions(Iterable<Class<?>> moreExtensionHosts) {
  for (Class<?> extensionHost : moreExtensionHosts) {
    // Attempt to access the required method, to make sure it's present.
    try {
      Method registerAllExtensions =
          extensionHost.getDeclaredMethod("registerAllExtensions", ExtensionRegistry.class);
      checkArgument(
          Modifier.isStatic(registerAllExtensions.getModifiers()),
          "Method registerAllExtensions() must be static");
    } catch (NoSuchMethodException | SecurityException e) {
      throw new IllegalArgumentException(
          String.format("Unable to register extensions for %s", extensionHost.getCanonicalName()),
          e);
    }
  }
}
 
Example #11
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
private void handleValue(JsonParser parser,
                                ExtensionRegistry extensionRegistry,
                                Message.Builder builder,
                                FieldDescriptor field,
                                ExtensionRegistry.ExtensionInfo extension,
                                boolean unknown) throws IOException {

    Object value = null;
    if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
        value = handleObject(parser, extensionRegistry, builder, field, extension, unknown);
    } else {
        value = handlePrimitive(parser, field);
    }
    if (value != null) {
        if (field.isRepeated()) {
            builder.addRepeatedField(field, value);
        } else {
            builder.setField(field, value);
        }
    }
}
 
Example #12
Source File: JsonJacksonFormat.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 * @throws IOException
 */
public void merge(JsonParser parser,
						 ExtensionRegistry extensionRegistry,
                         Message.Builder builder) throws IOException {

    JsonToken token = parser.nextToken();
    if (token.equals(JsonToken.START_OBJECT)) {
    	token = parser.nextToken();
    }
    while (token != null && !token.equals(JsonToken.END_OBJECT)) {
    	mergeField(parser, extensionRegistry, builder);
    	token = parser.nextToken();
    }

    // Test to make sure the tokenizer has reached the end of the stream.
    if (parser.nextToken() != null) {
        throw new RuntimeException("Expecting the end of the stream, but there seems to be more data!  Check the input for a valid JSON format.");
    }
}
 
Example #13
Source File: ProtobufDataParser.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public ProtobufDataParser(
    ProtoConfigurableEntity.Context context,
    String messageId,
    Descriptors.Descriptor descriptor,
    Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap,
    ExtensionRegistry extensionRegistry,
    InputStream inputStream,
    String readerOffset,
    int maxObjectLength,
    boolean isDelimited
) throws IOException, Descriptors.DescriptorValidationException, DataParserException {
  this.context = context;
  this.inputStream = new OverrunInputStream(inputStream, maxObjectLength, true);
  this.messageId = messageId;
  this.messageTypeToExtensionMap = messageTypeToExtensionMap;
  this.extensionRegistry = extensionRegistry;
  this.descriptor = descriptor;
  this.builder = DynamicMessage.newBuilder(descriptor);
  this.isDelimited = isDelimited;

  // skip to the required location
  if (readerOffset != null && !readerOffset.isEmpty() && !readerOffset.equals("0")) {
    int offset = Integer.parseInt(readerOffset);
    this.inputStream.skip(offset);
  }
}
 
Example #14
Source File: ExtraActionUtils.java    From incubator-heron with Apache License 2.0 5 votes vote down vote up
public static ExtraActionInfo getExtraActionInfo(String extraActionFile) {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  ExtraActionsBase.registerAllExtensions(registry);

  try (InputStream stream = Files.newInputStream(Paths.get(extraActionFile))) {
    CodedInputStream coded = CodedInputStream.newInstance(stream);
    return ExtraActionInfo.parseFrom(coded, registry);
  } catch (IOException e) {
    throw new RuntimeException("ERROR: failed to deserialize extra action file "
      + extraActionFile + ": " + e.getMessage(), e);
  }
}
 
Example #15
Source File: XmlFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
 * Extensions will be recognized if they are registered in {@code extensionRegistry}.
 */
public void merge(CharSequence input,
                         ExtensionRegistry extensionRegistry,
                         Message.Builder builder) throws ParseException {
    Tokenizer tokenizer = new Tokenizer(input);

    // Need to first consume the outer object name element
    consumeOpeningElement(tokenizer);

    while (!tokenizer.tryConsume("</")) { // Continue till the object is done
        mergeField(tokenizer, extensionRegistry, builder);
    }

    consumeClosingElement(tokenizer);
}
 
Example #16
Source File: ProtoApk.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a copy of the current apk.
 *
 * @param destination Path to the new apk destination.
 * @param resourceFilter A filter for determining whether a given resource will be included in the
 *     copy.
 * @return The new ProtoApk.
 * @throws IOException when there are issues reading the apk.
 */
public ProtoApk copy(Path destination, BiPredicate<ResourceType, String> resourceFilter)
    throws IOException {

  final URI dstZipUri = URI.create("jar:" + destination.toUri());
  try (final ZipFile srcZip = new ZipFile(uri.getPath());
      final UniqueZipBuilder dstZip = UniqueZipBuilder.createFor(destination)) {
    final ResourceTable.Builder dstTableBuilder = ResourceTable.newBuilder();
    final ResourceTable resourceTable =
        ResourceTable.parseFrom(
            Files.newInputStream(apkFileSystem.getPath(RESOURCE_TABLE)),
            ExtensionRegistry.getEmptyRegistry());
    dstTableBuilder.setSourcePool(resourceTable.getSourcePool());
    for (Package pkg : resourceTable.getPackageList()) {
      Package dstPkg = copyPackage(resourceFilter, dstZip, pkg);
      dstTableBuilder.addPackage(dstPkg);
    }
    dstZip.addEntry(RESOURCE_TABLE, dstTableBuilder.build().toByteArray(), ZipEntry.DEFLATED);
    srcZip.stream()
        .filter(not(ZipEntry::isDirectory))
        .filter(entry -> !entry.getName().startsWith(RES_DIRECTORY + "/"))
        .filter(entry -> !entry.getName().equals(RESOURCE_TABLE))
        .forEach(
            entry -> {
              try {
                createDirectories(dstZip, apkFileSystem.getPath(entry.getName()).getParent());
                try (InputStream srcEntryInputStream = srcZip.getInputStream(entry)) {
                  byte[] content = ByteStreams.toByteArray(srcEntryInputStream);
                  dstZip.addEntry(entry, content);
                }
              } catch (IOException e) {
                throw new RuntimeException(e);
              }
            });
  }

  return readFrom(dstZipUri);
}
 
Example #17
Source File: JsonFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
private Object handleObject(Tokenizer tokenizer,
                                   ExtensionRegistry extensionRegistry,
                                   Message.Builder builder,
                                   FieldDescriptor field,
                                   ExtensionRegistry.ExtensionInfo extension,
                                   boolean unknown) throws ParseException {

    Message.Builder subBuilder;
    if (extension == null) {
        subBuilder = builder.newBuilderForField(field);
    } else {
        subBuilder = extension.defaultInstance.newBuilderForType();
    }

    if (unknown) {
        ByteString data = tokenizer.consumeByteString();
        try {
            subBuilder.mergeFrom(data);
            return subBuilder.build();
        } catch (InvalidProtocolBufferException e) {
            throw tokenizer.parseException("Failed to build " + field.getFullName() + " from " + data);
        }
    }

    tokenizer.consume("{");
    String endToken = "}";

    while (!tokenizer.tryConsume(endToken)) {
        if (tokenizer.atEnd()) {
            throw tokenizer.parseException("Expected \"" + endToken + "\".");
        }
        mergeField(tokenizer, extensionRegistry, subBuilder);
        if (tokenizer.tryConsume(",")) {
            // there are more fields in the object, so continue
            continue;
        }
    }

    return subBuilder.build();
}
 
Example #18
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMergeFrom() throws Exception {
  TypicalData input = TypicalData.newBuilder()
      .setMyInt(42)
      .setMyMessage(TypicalDataMessage.newBuilder().setMyMessageInt(43))
      .build();
  TypicalData output = TypicalData.newBuilder()
      .mergeFrom(input.toByteString(), ExtensionRegistry.getEmptyRegistry())
      .build();
  assertEquals(42, output.getMyInt());
  assertEquals(43, output.getMyMessage().getMyMessageInt());
}
 
Example #19
Source File: XmlJavaxFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
private void handleMissingField(String fieldName, XMLEventReader parser,
        XMLEvent event, ExtensionRegistry extensionRegistry, 
        UnknownFieldSet.Builder builder) throws XMLStreamException {
	
    // skip over the unknown fields, since we can't map them by id, then this message must not know about them.
    // We 'could' map them into the UnknownFieldSet, however none of the other formatters support this..
    // but in the future it would probably be useful for the case: Message A (v2) -> Message B (v1) -> Xml -> Message A (v2) 
    // this would require extra meta data in the xml to know the type of the unknown-field.
    
    
    if (event.isStartElement()) {
        /**
         * This loop will eat up everything inside "6"
         * So when this method is called, fieldName = 6, and event is set at index="11"
         * <unknown-field index="6">
         *      <unknown-field index="11">566667</unknown-field>
         *      <unknown-field index="15">
         *          <unknown-field index="16">566667</unknown-field>
         *      </unknown-field>
         * </unknown-field>
         */
        int depth = 1; // we start 1 level down, the value of "6"
        while (parser.hasNext()) {
            XMLEvent nextEvent = parser.nextEvent();
            if (nextEvent.isEndElement()) {
                depth--;
                if (depth <= 0 && parser.peek().isEndElement()) {
                    break;
                }
            } else if (nextEvent.isStartElement()) {
                depth++;
            }
        }
    } else if (event.isCharacters()) {
        // done, let it slide.
    }
}
 
Example #20
Source File: ResourcesZip.java    From bazel with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
ImmutableListMultimap<String, String> parseToolAttributes() throws IOException {
  return ToolAttributes.parseFrom(
          Files.readAllBytes(attributes), ExtensionRegistry.getEmptyRegistry())
      .getAttributesMap()
      .entrySet()
      .stream()
      .collect(
          ImmutableListMultimap.flatteningToImmutableListMultimap(
              Entry::getKey, e -> e.getValue().getValuesList().stream()));
}
 
Example #21
Source File: JavaPropsFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * Parse a text-format message from {@code input} and merge the contents
 * into {@code builder}.  Extensions will be recognized if they are
 * registered in {@code extensionRegistry}.
 */
public void merge(final CharSequence input,
                         final ExtensionRegistry extensionRegistry,
                         final Message.Builder builder)
                         throws ParseException {
  final Tokenizer tokenizer = new Tokenizer(input);
  final Map<String, Message> subMessages = new HashMap<String, Message>();

  while (!tokenizer.atEnd()) {
    mergeField(tokenizer, extensionRegistry, subMessages, builder);
  }
}
 
Example #22
Source File: JsonFormat.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Object handleObject(Tokenizer tokenizer,
                                   ExtensionRegistry extensionRegistry,
                                   Message.Builder builder,
                                   FieldDescriptor field,
                                   ExtensionRegistry.ExtensionInfo extension,
                                   boolean unknown, boolean selfType) throws ParseException {

    Message.Builder subBuilder;
    if (extension == null) {
        subBuilder = builder.newBuilderForField(field);
    } else {
        subBuilder = extension.defaultInstance.newBuilderForType();
    }

    if (unknown) {
        ByteString data = tokenizer.consumeByteString("", selfType);
        try {
            subBuilder.mergeFrom(data);
            return subBuilder.build();
        } catch (InvalidProtocolBufferException e) {
            throw tokenizer.parseException("Failed to build " + field.getFullName() + " from " + data);
        }
    }

    tokenizer.consume("{");
    String endToken = "}";

    while (!tokenizer.tryConsume(endToken)) {
        if (tokenizer.atEnd()) {
            throw tokenizer.parseException("Expected \"" + endToken + "\".");
        }
        mergeField(tokenizer, extensionRegistry, subBuilder, selfType);
        if (tokenizer.tryConsume(",")) {
            // there are more fields in the object, so continue
            continue;
        }
    }

    return subBuilder.build();
}
 
Example #23
Source File: MessagesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testParseReversedMessageSets() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  MessageFields.registerAllExtensions(registry);
  MessageData msg = MessageData.parseFrom(
      ALL_MESSAGES_WITH_MESSAGE_SETS_REVERSED_BYTES, registry);
  checkFields(msg);
}
 
Example #24
Source File: ProtobufUtilTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Helper used to test the recursive class traversal and print good error messages. */
private static Set<String> getRecursiveDescriptorFullNames(
    Class<? extends Message> clazz, ExtensionRegistry registry) {
  Set<String> result = new HashSet<>();
  for (GenericDescriptor d : getRecursiveDescriptorsForClass(clazz, registry)) {
    result.add(d.getFullName());
  }
  return result;
}
 
Example #25
Source File: ProtobufUtil.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively walks the given {@link Message} class and verifies that every field or message
 * linked in uses the Protocol Buffers proto2 syntax.
 */
static void checkProto2Syntax(Class<? extends Message> clazz, ExtensionRegistry registry) {
  for (GenericDescriptor d : getRecursiveDescriptorsForClass(clazz, registry)) {
    Syntax s = d.getFile().getSyntax();
    checkArgument(
        s == Syntax.PROTO2,
        "Message %s or one of its dependencies does not use proto2 syntax: %s in file %s",
        clazz.getName(),
        d.getFullName(),
        d.getFile().getName());
  }
}
 
Example #26
Source File: Pb2JsonUtils.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
public static void json2Pb(CharSequence input, Message.Builder builder) throws JsonFormat.ParseException {
    if (pbVersion == PbVersion.PROTO2) {
        pb2Converter.merge(input, ExtensionRegistry.getEmptyRegistry(), builder);
    } else {
        try {
            pb3ParseMethod.invoke(pb3Parser, input, builder);
        } catch (Exception ex) {
            log.warn("pb3 json2pb failed, ex:", ex);
        }
    }
}
 
Example #27
Source File: ProtobufJsonFormatHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.registryInitializer = mock(ExtensionRegistryInitializer.class);
	this.extensionRegistry = mock(ExtensionRegistry.class);
	this.converter = new ProtobufJsonFormatHttpMessageConverter(
			JsonFormat.parser(), JsonFormat.printer(), this.registryInitializer);
	this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
}
 
Example #28
Source File: ProtobufHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	this.registryInitializer = mock(ExtensionRegistryInitializer.class);
	this.extensionRegistry = mock(ExtensionRegistry.class);
	this.converter = new ProtobufHttpMessageConverter(this.registryInitializer);
	this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
}
 
Example #29
Source File: ProtobufHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void merge(InputStream input, Charset charset, MediaType contentType,
		ExtensionRegistry extensionRegistry, Message.Builder builder)
		throws IOException, HttpMessageConversionException {

	if (contentType.isCompatibleWith(APPLICATION_JSON)) {
		InputStreamReader reader = new InputStreamReader(input, charset);
		this.parser.merge(reader, builder);
	}
	else {
		throw new HttpMessageConversionException(
				"protobuf-java-util does not support parsing " + contentType);
	}
}
 
Example #30
Source File: ProtoTypeMapTest.java    From grpc-java-contrib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@BeforeClass
public static void buildProtoTypeMap() throws IOException {
    // Dump file generated during the maven generate-test-sources phase
    final String dumpPath = "target/generated-test-sources/protobuf/dump/descriptor_dump";

    byte[] generatorRequestBytes = ByteStreams.toByteArray(new FileInputStream(new File(dumpPath)));
    PluginProtos.CodeGeneratorRequest request = PluginProtos.CodeGeneratorRequest.parseFrom(
            generatorRequestBytes, ExtensionRegistry.newInstance());
    List<DescriptorProtos.FileDescriptorProto> fileProtos = request.getProtoFileList();
    protoTypeMap =  ProtoTypeMap.of(fileProtos);
}