Java Code Examples for com.google.protobuf.ExtensionRegistry#newInstance()

The following examples show how to use com.google.protobuf.ExtensionRegistry#newInstance() . 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: ProtobufParser.java    From product-microgateway with Apache License 2.0 6 votes vote down vote up
/**
 * Compile the protobuf and generate descriptor file.
 *
 * @param protoPath      protobuf file path
 * @param descriptorPath descriptor file path
 * @return {@link DescriptorProtos.FileDescriptorSet} object
 */
private static DescriptorProtos.FileDescriptorProto generateRootFileDescriptor(String protoPath,
                                                                              String descriptorPath) {
    String command = new ProtocCommandBuilder
            (protoPath, resolveProtoFolderPath(protoPath), descriptorPath).build();
    generateDescriptor(command);
    File initialFile = new File(descriptorPath);
    try (InputStream targetStream = new FileInputStream(initialFile)) {
        ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
        //to register all custom extensions in order to parse properly
        ExtensionHolder.registerAllExtensions(extensionRegistry);
        DescriptorProtos.FileDescriptorSet set = DescriptorProtos.FileDescriptorSet.parseFrom(targetStream,
                extensionRegistry);
        logger.debug("Descriptor file is parsed successfully. file:" , descriptorPath);
        if (set.getFileList().size() > 0) {
            return set.getFile(0);
        }
    } catch (IOException e) {
        throw new CLIInternalException("Error reading generated descriptor file '" + descriptorPath + "'.", e);
    }
    return null;
}
 
Example 3
Source File: Convert.java    From metastore with Apache License 2.0 5 votes vote down vote up
private static Map<String, Descriptors.FileDescriptor> convertToFileDescriptorMap(
    Map<String, DescriptorProtos.FileDescriptorProto> inMap) {
  Map<String, Descriptors.FileDescriptor> outMap = new HashMap<>();
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  inMap
      .keySet()
      .forEach(fileName -> convertToFileDescriptorMap(fileName, null, inMap, outMap, registry));
  return outMap;
}
 
Example 4
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testExtensionRegistry() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  Typical.registerAllExtensions(registry);

  Descriptor descriptor = TypicalData.Builder.getDescriptor();
  FieldDescriptor fieldDescriptor = descriptor.findFieldByNumber(1);
  assertFalse(fieldDescriptor.isExtension());

  ExtensionRegistry.ExtensionInfo extensionInfo =
      registry.findExtensionByNumber(descriptor, 1000);
  assertNotNull(extensionInfo);

  FieldDescriptor extensionFieldDescriptor = extensionInfo.descriptor;
  assertNotNull(extensionFieldDescriptor);
  assertEquals(1000, extensionFieldDescriptor.getNumber());
  assertTrue(extensionFieldDescriptor.isExtension());

  Message message = extensionInfo.defaultInstance;
  assertTrue(message instanceof TypicalDataMessage);

  TypicalDataMessage data = ((TypicalDataMessage.Builder) message.toBuilder())
      .setMyMessageInt(100)
      .build();
  assertEquals(100, data.getMyMessageInt());

  // Primitive extension
  extensionInfo = registry.findExtensionByNumber(descriptor, 1001);
  assertNotNull(extensionInfo);

  extensionFieldDescriptor = extensionInfo.descriptor;
  assertNotNull(extensionFieldDescriptor);
  assertEquals(1001, extensionFieldDescriptor.getNumber());
  assertTrue(extensionFieldDescriptor.isExtension());
  assertNull(extensionInfo.defaultInstance);
}
 
Example 5
Source File: MapsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMergeFromInputStream() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  ByteArrayInputStream in = new ByteArrayInputStream(ALL_MAPS_BYTES);
  MapMsg.Builder builder = MapMsg.newBuilder().mergeFrom(in, registry);
  MapMsg msg = builder.build();
  checkFields(builder);
  checkFields(msg);
}
 
Example 6
Source File: ToolProtoUtil.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Get the standard extension registry to use for processing service config. By default, registers
 * extensions from {@code google/api/annotations.proto} (and related proto files).
 */
public static ExtensionRegistry getStandardPlatformExtensions() {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  AnnotationsProto.registerAllExtensions(registry);

  return registry;
}
 
Example 7
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testExtensionRegistryGetUnmodifiable() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  ExtensionRegistry registry2 = registry.getUnmodifiable();
  registry.add(Typical.myPrimitiveExtension);
  // Extension added to registry should be visible in registry2.
  Descriptor descriptor = TypicalData.getDescriptor();
  ExtensionRegistry.ExtensionInfo extensionInfo =
      registry2.findExtensionByNumber(descriptor, 1001);
  assertNotNull(extensionInfo);

  ExtensionRegistryLite registryLite = ExtensionRegistryLite.newInstance();
  ExtensionRegistryLite registryLite2 = registryLite.getUnmodifiable();
  assertNotNull(registryLite2);
}
 
Example 8
Source File: OneofTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMergeFromInputStream(OneofGroupCase groupCase) throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  ByteArrayInputStream in = new ByteArrayInputStream(getBytes(groupCase));
  OneofMsg.Builder builder = OneofMsg.newBuilder().mergeFrom(in, registry);
  checkFields(builder, groupCase);
  checkFields(builder.build(), groupCase);
}
 
Example 9
Source File: PersisterHelper.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static ExtensionRegistry buildRegistry(MarshallerReaderContext context, ProcessMarshaller processMarshaller ) {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    if( processMarshaller != null ) {
        context.parameterObject = registry;
        processMarshaller.init( context );
    }
    return registry;
}
 
Example 10
Source File: StringsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMergeFromInputStream() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  StringFields.registerAllExtensions(registry);
  ByteArrayInputStream in = new ByteArrayInputStream(ALL_STRINGS_BYTES);
  StringMsg.Builder builder = StringMsg.newBuilder().mergeFrom(in, registry);
  StringMsg msg = builder.build();
  checkFields(builder);
  checkFields(msg);
}
 
Example 11
Source File: UmundoETLProtocolAdapter.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
     * {@inheritDoc}
     */
    @Override
    public void start() throws IOException {
        receiver = new MmiReceiver(sourceUrl);

        node = new Node();
        subscriber = new TypedSubscriber(channel);
        subscriber.setReceiver(receiver);
        node.addSubscriber(subscriber);

        // TODO fix this in umundo
//        subscriber.registerType(LifeCycleEvents.LifeCycleEvent.class);
        registry = ExtensionRegistry.newInstance();
        LifeCycleEvents.registerAllExtensions(registry);
        subscriber.setExtensionRegistry(registry);

        publisher = new TypedPublisher(channel);
        node.addPublisher(publisher);

        // Ad all listeners that have been added before the receiver was
        // created
        synchronized (cachedListeners) {
            for (MMIEventListener listener : cachedListeners) {
                receiver.addMMIEventListener(listener);
            }
        }
        LOGGER.info("receiving MMI events via channel '" + channel
                + "' to '" + sourceUrl + "'");
    }
 
Example 12
Source File: OneofTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testParseFromByteArray() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  OneofMsg msg = OneofMsg.parseFrom(ONEOF_STRING_BYTES, registry);
  checkFields(msg, OneofGroupCase.ONEOF_STRING);
  msg = OneofMsg.parseFrom(ONEOF_INT_BYTES, registry);
  checkFields(msg, OneofGroupCase.ONEOF_INT);
  msg = OneofMsg.parseFrom(ONEOF_MESSAGE_BYTES, registry);
  checkFields(msg, OneofGroupCase.ONEOF_MESSAGE);
}
 
Example 13
Source File: ProtoDomain.java    From metastore with Apache License 2.0 5 votes vote down vote up
private void crosswire() {
  HashMap<String, DescriptorProtos.FileDescriptorProto> map = new HashMap<>();
  fileDescriptorSet.getFileList().stream()
      .filter(fdp -> !fdp.getName().startsWith("google/protobuf"))
      .forEach(fdp -> map.put(fdp.getName(), fdp));

  Map<String, Descriptors.FileDescriptor> outMap = new HashMap<>();
  ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
  map.forEach(
      (fileName, proto) -> convertToFileDescriptorMap(fileName, map, outMap, extensionRegistry));
  fileDescriptorMap = outMap;

  indexOptionsByNumber();
  indexDescriptorByName();
}
 
Example 14
Source File: EnumsTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testParseFromByteArray() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  EnumFields.registerAllExtensions(registry);
  EnumMsg msg = EnumMsg.parseFrom(ALL_ENUMS_BYTES, registry);
  checkFields(msg);
}
 
Example 15
Source File: MapsTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@AutoreleasePool
public void testParseFromByteArray() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  MapMsg msg = MapMsg.parseFrom(ALL_MAPS_BYTES, registry);
  checkFields(msg);
}
 
Example 16
Source File: PrimitivesTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testParseFromByteArray() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  Primitives.registerAllExtensions(registry);
  PrimitiveFields msg = PrimitiveFields.parseFrom(ALL_PRIMITIVES_BYTES, registry);
  checkFields(msg);
}
 
Example 17
Source File: KieModuleCacheHelper.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public static ExtensionRegistry buildRegistry() {
    ExtensionRegistry registry = ExtensionRegistry.newInstance();
    return registry;
}
 
Example 18
Source File: ProtobufTestUtil.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public static void checkProtobufDataExtensions(byte[] bytes) throws IOException {
  ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
  ExtensionsProto.registerAllExtensions(extensionRegistry);

  ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
  EmployeeProto.Employee.Builder builder = EmployeeProto.Employee.newBuilder();
  EmployeeProto.Employee employee;
  for(int i = 0; i < 10; i++) {
    builder.mergeDelimitedFrom(bIn, extensionRegistry);
    employee = builder.build();
    PersonProto.Person person;

    if( i % 2 == 0) {
      Assert.assertEquals("SF", employee.getExtension(ExtensionsProto.stringField));
      Assert.assertEquals(true, employee.getExtension(ExtensionsProto.boolField));
      Assert.assertEquals(43243, (int)employee.getExtension(ExtensionsProto.intField));
      Assert.assertEquals(2343254354L, (Object) employee.getExtension(ExtensionsProto.longField));
      Assert.assertEquals(3534.234, (Object) employee.getExtension(ExtensionsProto.doubleField));
      Assert.assertEquals(343.34f, (Object) employee.getExtension(ExtensionsProto.floatField));
      Assert.assertEquals(ByteString.copyFromUtf8("NewYork"), employee.getExtension(ExtensionsProto.bytesField));

      EngineerProto.Engineer engineer = employee.getEngineer();
      Assert.assertNotNull(engineer);
      Assert.assertEquals("South SF", engineer.getExtension(ExtensionsProto.EngineerExtension.factoryAddress));

      person = engineer.getPerson();
    } else {
      Assert.assertEquals("SF", employee.getExtension(ExtensionsProto.stringField));
      Assert.assertEquals(true, employee.getExtension(ExtensionsProto.boolField));
      Assert.assertEquals(4375, (int)employee.getExtension(ExtensionsProto.intField));
      Assert.assertEquals(2343254354L, (Object) employee.getExtension(ExtensionsProto.longField));
      Assert.assertEquals(23423.4234, (Object) employee.getExtension(ExtensionsProto.doubleField));
      Assert.assertEquals(22.22f, (Object) employee.getExtension(ExtensionsProto.floatField));
      Assert.assertEquals(ByteString.copyFromUtf8("SanFrancisco"), employee.getExtension(ExtensionsProto.bytesField));

      ExecutiveProto.Executive exec = employee.getExec();
      Assert.assertNotNull(exec);
      Assert.assertEquals("SOMA", exec.getExtension(ExtensionsProto.ExecutiveExtension.officeAddress));

      person = exec.getPerson();
    }

    Assert.assertNotNull(person);
    Assert.assertEquals(
        "SJ",
        person.getExtension(ExtensionsProto.PersonExtension.NestedPersonExtension.residenceAddress)
    );
    builder.clear();
  }
}
 
Example 19
Source File: ExtensionRegistryFactory.java    From micronaut-grpc with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs the extension registry.
 * @return The extension registry
 */
@Singleton
protected ExtensionRegistry extensionRegistry() {
    return ExtensionRegistry.newInstance();
}
 
Example 20
Source File: MessagesTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testMergeExistingMessageFields() throws Exception {
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  MessageFields.registerAllExtensions(registry);

  MessageData toMerge = MessageData.newBuilder()
      .setMsgF(SubMsg.newBuilder().setUintF(123).build())
      .setGroupF(GroupF.newBuilder().setUintF(234).build())
      .addMsgR(SubMsg.newBuilder().setUintF(345).build())
      .addGroupR(GroupR.newBuilder().setUintF(456).build())
      .setExtension(MessageFields.msgFe, SubMsg.newBuilder().setUintF(567).build())
      .setExtension(MessageFields.groupFe, GroupFe.newBuilder().setUintF(678).build())
      .addExtension(MessageFields.msgRe, SubMsg.newBuilder().setUintF(789).build())
      .addExtension(MessageFields.groupRe, GroupRe.newBuilder().setUintF(890).build())
      .build();
  byte[] toMergeBytes = toMerge.toByteArray();

  // Save the singular fields so we can verify that they aren't modified by merging.
  SubMsg field1 = SubMsg.newBuilder().setIntF(321).build();
  GroupF field2 = GroupF.newBuilder().setIntF(432).build();
  SubMsg field3 = SubMsg.newBuilder().setIntF(765).build();
  GroupFe field4 = GroupFe.newBuilder().setIntF(876).build();
  MessageData.Builder builder = MessageData.newBuilder()
      .setMsgF(field1)
      .setGroupF(field2)
      .addMsgR(SubMsg.newBuilder().setIntF(543).build())
      .addGroupR(GroupR.newBuilder().setIntF(654).build())
      .setExtension(MessageFields.msgFe, field3)
      .setExtension(MessageFields.groupFe, field4)
      .addExtension(MessageFields.msgRe, SubMsg.newBuilder().setIntF(987).build())
      .addExtension(MessageFields.groupRe, GroupRe.newBuilder().setIntF(98).build());

  MessageData.Builder builder1 = builder.build().toBuilder();
  builder1.mergeFrom(toMerge);
  checkMergedFields(builder1);

  MessageData.Builder builder2 = builder.build().toBuilder();
  builder2.mergeFrom(toMergeBytes, registry);
  // TODO(kstanger): This is a bug in the native ObjC runtime. It fails to
  // merge message type extension fields when reading from data. Instead it
  // just overwrites the existing message field.
  //checkMergedFields(builder2);

  assertFalse(field1.hasUintF());
  assertFalse(field2.hasUintF());
  assertFalse(field3.hasUintF());
  assertFalse(field4.hasUintF());
}