/** * Registers a protobuf file * @param fileName The name of the file. * @param contents The contents of the file. * @param dynamicEntityClass The dynamic entity class. * the types in the protobuf. * @throws IOException in case the registration fails. */ void registerSchema(String fileName, String contents, Class<?> dynamicEntityClass) throws IOException { getSerializationContext().registerProtoFiles(FileDescriptorSource.fromString(fileName, contents)); getSerializationContext().getFileDescriptors().entrySet().stream() .filter(p -> p.getKey().equals(fileName)) .flatMap(fd -> fd.getValue().getMessageTypes().stream()) .map(AnnotatedDescriptorImpl::getFullName) .forEach(t -> classByType.put(t, dynamicEntityClass)); }
/** * Registers a protobuf file * @param fileName The name of the file. * @param contents The contents of the file. * @param dynamicEntityClass The dynamic entity class. * the types in the protobuf. * @throws IOException in case the registration fails. */ void registerSchema(String fileName, String contents, Class<?> dynamicEntityClass) throws IOException { getSerializationContext().registerProtoFiles(FileDescriptorSource.fromString(fileName, contents)); getSerializationContext().getFileDescriptors().entrySet().stream() .filter(p -> p.getKey().equals(fileName)) .flatMap(fd -> fd.getValue().getMessageTypes().stream()) .map(AnnotatedDescriptorImpl::getFullName) .forEach(t -> classByType.put(t, dynamicEntityClass)); }
private FileDescriptor parseSchema() throws DescriptorParserException, IOException { FileDescriptorSource fileDescriptorSource = FileDescriptorSource.fromString( schemaName, protoSchema ); Configuration config = Configuration.builder().build(); SerializationContext serCtx = ProtobufUtil.newSerializationContext( config ); serCtx.registerProtoFiles( fileDescriptorSource ); return serCtx.getFileDescriptors().get( schemaName ); } }
@Test(expectedExceptions = DescriptorParserException.class, expectedExceptionsMessageRegExp = "java.lang.IllegalStateException: Annotation 'IndexedField' cannot be used together with 'Field' on field test.User.age") public void testIndexedFieldAndField() throws Exception { String testProto = "package test;\n" + "/* @Indexed */ message User {\n" + " /* @IndexedField(index=false, store=false) @Field(store=Store.NO, index=Index.NO) */ " + " required int32 age = 1;\n" + "}"; SerializationContext serCtx = ProtobufMetadataManagerImpl.getSerializationContext(cacheManager); serCtx.registerProtoFiles(FileDescriptorSource.fromString("test.proto", testProto)); }
@Test(expectedExceptions = DescriptorParserException.class, expectedExceptionsMessageRegExp = "java.lang.IllegalStateException: Annotation 'IndexedField' cannot be used together with 'Analyzer' on field test.User2.name") public void testIndexedFieldAndAnalyzer() throws Exception { String testProto = "package test;\n" + "/* @Indexed */" + "message User2 {\n" + " /* @IndexedField(index=false, store=false) @Analyzer(definition=\"standard\") */" + " required string name = 1;\n" + "}"; SerializationContext serCtx = ProtobufMetadataManagerImpl.getSerializationContext(cacheManager); serCtx.registerProtoFiles(FileDescriptorSource.fromString("test1.proto", testProto)); }
@Test(expectedExceptions = DescriptorParserException.class, expectedExceptionsMessageRegExp = "java.lang.IllegalStateException: Annotation 'IndexedField' cannot be used together with 'SortableField' on field test.User1.age") public void testIndexedFieldAndSortableField() throws Exception { String testProto = "package test;\n" + "/* @Indexed */ message User1 {\n" + " /* @IndexedField(index=false, store=false) @SortableField */ " + " required int32 age = 1;\n" + "}"; SerializationContext serCtx = ProtobufMetadataManagerImpl.getSerializationContext(cacheManager); serCtx.registerProtoFiles(FileDescriptorSource.fromString("test1.proto", testProto)); }
@Test(expectedExceptions = DescriptorParserException.class, expectedExceptionsMessageRegExp = "java.lang.IllegalStateException: Cannot specify an analyzer for field test.User3.name unless the field is analyzed.") public void testAnalyzerForNotAnalyzedField() throws Exception { String testProto = "package test;\n" + "/* @Indexed */ message User3 {\n" + " /* @Field(store=Store.NO, index=Index.YES, analyze=Analyze.NO, analyzer=@Analyzer(definition=\"standard\")) */" + " required string name = 1;\n" + "}"; SerializationContext serCtx = ProtobufMetadataManagerImpl.getSerializationContext(cacheManager); serCtx.registerProtoFiles(FileDescriptorSource.fromString("test2.proto", testProto)); } }
serializationContext.registerProtoFiles(FileDescriptorSource.fromString(fileName, protoFile));
@BeforeClass(alwaysRun = true) public void setUp() throws IOException { dataSrc = "{\"_type\":\"Person\", \"name\":\"joe\", \"address\":{\"_type\":\"Address\", \"street\":\"\", \"city\":\"London\", \"zip\":\"0\"}}"; SerializationContext serCtx = ProtobufUtil.newSerializationContext(); serCtx.registerProtoFiles(FileDescriptorSource.fromString("person_definition.proto", PROTO_DEFINITIONS)); transcoder = new ProtostreamJsonTranscoder(serCtx); supportedMediaTypes = transcoder.getSupportedMediaTypes(); }
public void registerProtobufFile(ProtobufResource protobuf) throws TranslatorException { try { if (protobuf != null) { // client side this.ctx.registerProtoFiles(FileDescriptorSource.fromString(protobuf.getIdentifier(), protobuf.getContents())); // server side RemoteCache<String, String> metadataCache = this.cacheManager .getCache(ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME); if (metadataCache != null) { metadataCache.put(protobuf.getIdentifier(), protobuf.getContents()); String errors = metadataCache.get(ProtobufMetadataManagerConstants.ERRORS_KEY_SUFFIX); // ispn removes leading '/' in a string in the results String protoSchemaIdent = (protobuf.getIdentifier().startsWith("/")) ? protobuf.getIdentifier().substring(1) : protobuf.getIdentifier(); if (errors != null && isProtoSchemaInErrors(protoSchemaIdent, errors)) { throw new TranslatorException(InfinispanManagedConnectionFactory.UTIL.getString("proto_error", errors)); } } } else { throw new TranslatorException(InfinispanManagedConnectionFactory.UTIL.getString("no_protobuf")); } } catch(Throwable t) { throw new TranslatorException(t); } }
serCtx.registerProtoFiles(fromString("uuid.proto", "message unique_id { required string uuid=1; }")); serCtx.registerMarshaller(new UUIDMarshaller());
serCtx.registerProtoFiles(fromString("uuid.proto", "message unique_id { required string uuid=1; }")); serCtx.registerMarshaller(new UUIDMarshaller());
public void testProtobufFieldIndexingMetadata() throws Exception { SerializationContext serCtx = ProtobufMetadataManagerImpl.getSerializationContext(cacheManager); serCtx.registerProtoFiles(FileDescriptorSource.fromString("user_definition.proto", PROTO_DEFINITIONS)); ProtobufFieldIndexingMetadata userIndexedFieldProvider = new ProtobufFieldIndexingMetadata(serCtx.getMessageDescriptor("User")); ProtobufFieldIndexingMetadata addressIndexedFieldProvider = new ProtobufFieldIndexingMetadata(serCtx.getMessageDescriptor("User.Address")); // try top level attributes assertTrue(userIndexedFieldProvider.isIndexed(new String[]{"name"})); assertFalse(userIndexedFieldProvider.isIndexed(new String[]{"surname"})); assertTrue(addressIndexedFieldProvider.isIndexed(new String[]{"street"})); assertFalse(addressIndexedFieldProvider.isIndexed(new String[]{"postCode"})); // try nested attributes assertTrue(userIndexedFieldProvider.isIndexed(new String[]{"indexedAddresses", "street"})); assertFalse(userIndexedFieldProvider.isIndexed(new String[]{"indexedAddresses", "postCode"})); assertFalse(userIndexedFieldProvider.isIndexed(new String[]{"unindexedAddresses", "street"})); assertFalse(userIndexedFieldProvider.isIndexed(new String[]{"unindexedAddresses", "postCode"})); }