Skip to content

Commit 60ad692

Browse files
authored
fix(protobuf-core): re-introduce the load-by-file-path/url protobuf descriptors (#18770)
* fix(protobuf-core): re-introduce the load-by-file-path/url protobuf descriptors * fix: typo and refactor with context
1 parent 51876b5 commit 60ad692

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

extensions-core/protobuf-extensions/src/main/java/org/apache/druid/data/input/protobuf/FileBasedProtobufBytesDecoder.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import java.io.IOException;
2929
import java.io.InputStream;
30+
import java.net.MalformedURLException;
31+
import java.net.URL;
3032
import java.util.Objects;
3133

3234
public class FileBasedProtobufBytesDecoder extends DescriptorBasedProtobufBytesDecoder
@@ -56,20 +58,43 @@ public String getDescriptorFilePath()
5658
@Override
5759
protected DescriptorProtos.FileDescriptorSet loadFileDescriptorSet()
5860
{
59-
try (InputStream fin = this.getClass().getClassLoader().getResourceAsStream(descriptorFilePath)) {
61+
InputStream fin;
62+
DescriptorProtos.FileDescriptorSet descriptorSet;
63+
try {
64+
fin = this.getClass().getClassLoader().getResourceAsStream(descriptorFilePath);
6065
if (fin == null) {
61-
throw new ParseException(descriptorFilePath, "Descriptor not found in class path [%s]", descriptorFilePath);
66+
URL url;
67+
try {
68+
url = new URL(descriptorFilePath);
69+
}
70+
catch (MalformedURLException e) {
71+
throw new ParseException(
72+
descriptorFilePath,
73+
e,
74+
"Descriptor not found in class path or malformed URL: [%s]", descriptorFilePath
75+
);
76+
}
77+
try (InputStream urlIn = url.openConnection().getInputStream()) {
78+
if (urlIn == null) {
79+
throw new ParseException(
80+
descriptorFilePath,
81+
"Descriptor not found at URL: [%s]", descriptorFilePath
82+
);
83+
}
84+
descriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(urlIn);
85+
}
86+
} else {
87+
descriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(fin);
6288
}
6389

64-
final var descriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(fin);
6590
if (descriptorSet.getFileCount() == 0) {
6691
throw new ParseException(null, "No file descriptors found in the descriptor set");
6792
}
6893

6994
return descriptorSet;
7095
}
7196
catch (IOException e) {
72-
throw new ParseException(descriptorFilePath, e, "Failed to initialize descriptor");
97+
throw new ParseException(descriptorFilePath, e, "Failed to initialize descriptor at [%s]", descriptorFilePath);
7398
}
7499
}
75100

extensions-core/protobuf-extensions/src/test/java/org/apache/druid/data/input/protobuf/FileBasedProtobufBytesDecoderTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.druid.java.util.common.parsers.ParseException;
2626
import org.junit.jupiter.api.Test;
2727

28+
import java.io.File;
2829
import java.nio.ByteBuffer;
2930

3031
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -68,6 +69,16 @@ public void testMoreComplexProtoFile()
6869
assertEquals("prototest.ProtoNestedEvent", decoder.getDescriptor().getFullName());
6970
}
7071

72+
@Test
73+
public void testDescriptorUrl()
74+
{
75+
File descFile = new File("src/test/resources/proto_test_event.desc");
76+
String path = descFile.getAbsoluteFile().toString();
77+
78+
final var decoder = new FileBasedProtobufBytesDecoder("file://" + path, "ProtoTestEvent");
79+
assertEquals("prototest.ProtoTestEvent", decoder.getDescriptor().getFullName());
80+
}
81+
7182
@Test
7283
public void testParsingWithMoreComplexProtoFile() throws Exception
7384
{
@@ -123,7 +134,7 @@ public void testMalformedDescriptorUrl()
123134
);
124135

125136
assertEquals(
126-
"Descriptor not found in class path [file:/nonexist.desc]",
137+
"Failed to initialize descriptor at [file:/nonexist.desc]",
127138
ex.getMessage()
128139
);
129140
}

0 commit comments

Comments
 (0)