diff --git a/changelog/unreleased/SOLR-7498-extract-null-stream-size.yml b/changelog/unreleased/SOLR-7498-extract-null-stream-size.yml new file mode 100644 index 000000000000..856363d468fc --- /dev/null +++ b/changelog/unreleased/SOLR-7498-extract-null-stream-size.yml @@ -0,0 +1,9 @@ +title: > + The extracting request handler no longer indexes the text "null" as stream_size when the size of + the uploaded content stream is unknown, which failed on numeric stream_size fields. +type: fixed +authors: + - name: Nick Shanin +links: + - name: SOLR-7498 + url: https://issues.apache.org/jira/browse/SOLR-7498 diff --git a/solr/modules/extraction/src/java/org/apache/solr/handler/extraction/ExtractionBackend.java b/solr/modules/extraction/src/java/org/apache/solr/handler/extraction/ExtractionBackend.java index 1b30dbb3385f..a170c77075f4 100644 --- a/solr/modules/extraction/src/java/org/apache/solr/handler/extraction/ExtractionBackend.java +++ b/solr/modules/extraction/src/java/org/apache/solr/handler/extraction/ExtractionBackend.java @@ -52,7 +52,10 @@ default ExtractionMetadata buildMetadataFromRequest(ExtractionRequest request) { md.add(ExtractingMetadataConstants.HTTP_HEADER_CONTENT_TYPE, request.contentType); md.add(ExtractingMetadataConstants.STREAM_NAME, request.streamName); md.add(ExtractingMetadataConstants.STREAM_SOURCE_INFO, request.streamSourceInfo); - md.add(ExtractingMetadataConstants.STREAM_SIZE, String.valueOf(request.streamSize)); + // the size is unknown (null) for streams that do not report one; do not add the text "null" + if (request.streamSize != null) { + md.add(ExtractingMetadataConstants.STREAM_SIZE, String.valueOf(request.streamSize)); + } md.add(ExtractingMetadataConstants.STREAM_CONTENT_TYPE, request.contentType); md.add(ExtractingMetadataConstants.HTTP_HEADER_CONTENT_ENCODING, request.charset); return md; diff --git a/solr/modules/extraction/src/test/org/apache/solr/handler/extraction/ExtractionBackendMetadataTest.java b/solr/modules/extraction/src/test/org/apache/solr/handler/extraction/ExtractionBackendMetadataTest.java new file mode 100644 index 000000000000..391d226c41e8 --- /dev/null +++ b/solr/modules/extraction/src/test/org/apache/solr/handler/extraction/ExtractionBackendMetadataTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.handler.extraction; + +import java.io.InputStream; +import org.apache.solr.SolrTestCase; +import org.junit.Test; +import org.xml.sax.helpers.DefaultHandler; + +/** SOLR-7498: an unknown stream size must not be reported as the text "null". */ +public class ExtractionBackendMetadataTest extends SolrTestCase { + + private static final ExtractionBackend BACKEND = + new ExtractionBackend() { + @Override + public ExtractionResult extract(InputStream inputStream, ExtractionRequest request) { + throw new UnsupportedOperationException(); + } + + @Override + public void extractWithSaxHandler( + InputStream inputStream, + ExtractionRequest request, + ExtractionMetadata md, + DefaultHandler saxContentHandler) { + throw new UnsupportedOperationException(); + } + + @Override + public String name() { + return "test"; + } + }; + + @Test + public void testUnknownStreamSizeIsNotAdded() { + ExtractionRequest request = ExtractionRequest.builder().streamName("doc.pdf").build(); + ExtractionMetadata md = BACKEND.buildMetadataFromRequest(request); + assertNull(md.getFirst(ExtractingMetadataConstants.STREAM_SIZE)); + assertEquals("doc.pdf", md.getFirst(ExtractingMetadataConstants.STREAM_NAME)); + } + + @Test + public void testKnownStreamSizeIsAdded() { + ExtractionRequest request = ExtractionRequest.builder().streamSize(1234L).build(); + ExtractionMetadata md = BACKEND.buildMetadataFromRequest(request); + assertEquals("1234", md.getFirst(ExtractingMetadataConstants.STREAM_SIZE)); + } +}