From 5169bac90c03687bfd727d62ba4ab01aec9d87b1 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Tue, 16 Jan 2024 13:35:48 +0100 Subject: [PATCH] Fix REST error 500 when wrong field name is used Instead of producing a NPE, we now catch the problem (using the wrong field name `file` and return a proper error message. Closes #1748. --- .../test/integration/AbstractRestITCase.java | 2 +- .../elasticsearch/FsCrawlerRestIT.java | 27 +++++++++++++++++++ .../crawler/fs/rest/DocumentApi.java | 7 +++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/integration-tests/src/test/java/fr/pilato/elasticsearch/crawler/fs/test/integration/AbstractRestITCase.java b/integration-tests/src/test/java/fr/pilato/elasticsearch/crawler/fs/test/integration/AbstractRestITCase.java index 083d56844..b1172dfc7 100644 --- a/integration-tests/src/test/java/fr/pilato/elasticsearch/crawler/fs/test/integration/AbstractRestITCase.java +++ b/integration-tests/src/test/java/fr/pilato/elasticsearch/crawler/fs/test/integration/AbstractRestITCase.java @@ -213,7 +213,7 @@ protected interface HitChecker { void check(ESSearchHit hit); } - private static final Map debugOption = new HashMap<>(); + protected static final Map debugOption = new HashMap<>(); static { debugOption.put("debug", true); diff --git a/integration-tests/src/test/java/fr/pilato/elasticsearch/crawler/fs/test/integration/elasticsearch/FsCrawlerRestIT.java b/integration-tests/src/test/java/fr/pilato/elasticsearch/crawler/fs/test/integration/elasticsearch/FsCrawlerRestIT.java index e810eced5..4178bcfa1 100644 --- a/integration-tests/src/test/java/fr/pilato/elasticsearch/crawler/fs/test/integration/elasticsearch/FsCrawlerRestIT.java +++ b/integration-tests/src/test/java/fr/pilato/elasticsearch/crawler/fs/test/integration/elasticsearch/FsCrawlerRestIT.java @@ -32,12 +32,17 @@ import fr.pilato.elasticsearch.crawler.fs.settings.FsSettings; import fr.pilato.elasticsearch.crawler.fs.settings.Rest; import fr.pilato.elasticsearch.crawler.fs.test.integration.AbstractRestITCase; +import jakarta.ws.rs.core.MediaType; +import org.glassfish.jersey.media.multipart.FormDataMultiPart; +import org.glassfish.jersey.media.multipart.file.FileDataBodyPart; import org.junit.Test; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Predicate; @@ -280,4 +285,26 @@ public void testDocumentWithExternalTags() throws Exception { expectThrows(PathNotFoundException.class, () -> JsonPath.read(hit.getSource(), "$.external")); }); } + + @Test + public void testUploadUsingWrongFieldName() { + Path from = rootTmpDir.resolve("resources").resolve("documents").resolve("test.txt"); + if (Files.notExists(from)) { + staticLogger.error("file [{}] should exist before we start tests", from); + throw new RuntimeException(from + " doesn't seem to exist. Check your JUnit tests."); + } + + Map params = new HashMap<>(); + FileDataBodyPart filePart = new FileDataBodyPart("anotherfieldname", from.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE); + FormDataMultiPart mp = new FormDataMultiPart(); + mp.bodyPart(filePart); + if (staticLogger.isDebugEnabled()) { + staticLogger.debug("Rest response: {}", post(target, "/_document", mp, String.class, debugOption)); + } + + UploadResponse response = post(target, "/_document", mp, UploadResponse.class, params); + + assertThat(response.isOk(), is(false)); + assertThat(response.getMessage(), containsString("No file has been sent or you are not using [file] as the field name.")); + } } diff --git a/rest/src/main/java/fr/pilato/elasticsearch/crawler/fs/rest/DocumentApi.java b/rest/src/main/java/fr/pilato/elasticsearch/crawler/fs/rest/DocumentApi.java index 40a903c7a..bb7d25aef 100644 --- a/rest/src/main/java/fr/pilato/elasticsearch/crawler/fs/rest/DocumentApi.java +++ b/rest/src/main/java/fr/pilato/elasticsearch/crawler/fs/rest/DocumentApi.java @@ -164,6 +164,13 @@ private UploadResponse uploadToDocumentService( // Create the Doc object Doc doc = new Doc(); + if (d == null) { + UploadResponse response = new UploadResponse(); + response.setOk(false); + response.setMessage("No file has been sent or you are not using [file] as the field name."); + return response; + } + String filename = new String(d.getFileName().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); long filesize = d.getSize();