You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2022/03/17 14:55:57 UTC

[tika] 01/01: TIKA-3695 -- initial commit. WIP do not merge.

This is an automated email from the ASF dual-hosted git repository.

tallison pushed a commit to branch TIKA-3695
in repository https://gitbox.apache.org/repos/asf/tika.git

commit 7c4545213bf3955f78ed9659257ca768f94989b3
Author: tallison <ta...@apache.org>
AuthorDate: Thu Mar 17 10:55:19 2022 -0400

    TIKA-3695 -- initial commit. WIP do not merge.
---
 .../java/org/apache/tika/metadata/Metadata.java    |  92 +++++++++++-
 .../apache/tika/metadata/MetadataWriteFilter.java  |  42 ++++++
 .../tika/metadata/MetadataWriteFilterFactory.java  |  21 +++
 .../apache/tika/metadata/StandardWriteFilter.java  | 161 +++++++++++++++++++++
 .../tika/metadata/StandardWriteFilterFactory.java  |  57 ++++++++
 .../apache/tika/metadata/TikaCoreProperties.java   |   4 +
 .../org/apache/tika/parser/AutoDetectParser.java   |   3 +
 .../apache/tika/parser/AutoDetectParserConfig.java |  11 ++
 .../org/apache/tika/config/TikaConfigTest.java     |  35 +++++
 .../resources/org/apache/tika/config/TIKA-3695.xml |  33 +++++
 10 files changed, 455 insertions(+), 4 deletions(-)

diff --git a/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java b/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java
index bfed8ef..6d1bc0b 100644
--- a/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java
+++ b/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java
@@ -44,6 +44,8 @@ public class Metadata
         implements CreativeCommons, Geographic, HttpHeaders, Message, ClimateForcast, TIFF,
         TikaMimeKeys, Serializable {
 
+    private static final MetadataWriteFilter ACCEPT_ALL = new AcceptAll();
+
     /**
      * Serial version UID
      */
@@ -58,6 +60,7 @@ public class Metadata
      */
     private Map<String, String[]> metadata = null;
 
+    private MetadataWriteFilter writeFilter = ACCEPT_ALL;
     /**
      * Constructs a new, empty metadata.
      */
@@ -132,6 +135,22 @@ public class Metadata
     }
 
     /**
+     * Sets the writeFilter that is called before {@link #set(String, String)}
+     * {@link #set(String, String[])}, {@link #add(String, String)},
+     * {@link #add(String, String[])}.  The default is {@link #ACCEPT_ALL}.
+     *
+     * This is intended for expert use only.  Some parsers rely on metadata
+     * during the parse, and if the metadata they need is excluded, they
+     * will not function properly.
+     *
+     * @param writeFilter
+     */
+    public void setWriteFilter(MetadataWriteFilter writeFilter) {
+        this.writeFilter = writeFilter;
+        this.writeFilter.filterExisting(metadata);
+    }
+
+    /**
      * Returns the value (if any) of the identified metadata property.
      *
      * @param property property definition
@@ -236,11 +255,35 @@ public class Metadata
      * @param value the metadata value.
      */
     public void add(final String name, final String value) {
+        if (!writeFilter.include(name, value)) {
+            return;
+        }
         String[] values = metadata.get(name);
         if (values == null) {
             set(name, value);
         } else {
-            metadata.put(name, appendedValues(values, value));
+            String filtered = writeFilter.filter(name, value, metadata);
+            if (filtered != null) {
+                metadata.put(name, appendedValues(values, filtered));
+            }
+        }
+    }
+
+    /**
+     * Add a metadata name/value mapping. Add the specified value to the list of
+     * values associated to the specified metadata name.
+     *
+     * @param name  the metadata name.
+     * @param newValues the metadata values
+     */
+    protected void add(final String name, final String[] newValues) {
+        String[] values = metadata.get(name);
+        if (values == null) {
+            set(name, newValues);
+        } else {
+            for (String val : newValues) {
+                add(name, val);
+            }
         }
     }
 
@@ -270,7 +313,7 @@ public class Metadata
                 set(property, value);
             } else {
                 if (property.isMultiValuePermitted()) {
-                    set(property, appendedValues(values, value));
+                    add(property.getName(), value);
                 } else {
                     throw new PropertyTypeException(
                             property.getName() + " : " + property.getPropertyType());
@@ -303,8 +346,28 @@ public class Metadata
      * @param value the metadata value, or <code>null</code>
      */
     public void set(String name, String value) {
+        if (! writeFilter.include(name, value)) {
+            return;
+        }
         if (value != null) {
-            metadata.put(name, new String[]{value});
+            metadata.remove(name);
+            String filtered = writeFilter.filter(name, value, metadata);
+            if (filtered != null) {
+                metadata.put(name, new String[]{filtered});
+            }
+        } else {
+            metadata.remove(name);
+        }
+    }
+
+    protected void set(String name, String[] values) {
+        //TODO: optimize this to not copy if all
+        //values are to be included "as is"
+        if (values != null) {
+            metadata.remove(name);
+            for (String v : values) {
+                add(name, v);
+            }
         } else {
             metadata.remove(name);
         }
@@ -352,7 +415,7 @@ public class Metadata
                 }
             }
         } else {
-            metadata.put(property.getName(), values);
+            set(property.getName(), values);
         }
     }
 
@@ -599,4 +662,25 @@ public class Metadata
         return buf.toString();
     }
 
+    /**
+     * NO-OP write filter that accepts everything without modification.
+     */
+    private static class AcceptAll implements MetadataWriteFilter, Serializable {
+
+        @Override
+        public void filterExisting(Map<String, String[]> data) {
+            return;
+        }
+
+        @Override
+        public boolean include(String field, String value) {
+            return true;
+        }
+
+        @Override
+        public String filter(String field, String value, Map<String, String[]> data) {
+            return value;
+        }
+    }
+
 }
diff --git a/tika-core/src/main/java/org/apache/tika/metadata/MetadataWriteFilter.java b/tika-core/src/main/java/org/apache/tika/metadata/MetadataWriteFilter.java
new file mode 100644
index 0000000..7918a63
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/metadata/MetadataWriteFilter.java
@@ -0,0 +1,42 @@
+/*
+ * 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.tika.metadata;
+
+import java.util.Map;
+
+public interface MetadataWriteFilter {
+
+    void filterExisting(Map<String, String[]> data);
+
+    boolean include(String field, String value);
+
+    /**
+     * Based on the field and value, this filter modifies the value
+     * to something that should be set or added to the Metadata object.
+     *
+     * If the value is <code>null</code>, no value is set or added.
+     *
+     * Status updates (e.g. write limit reached) can be added directly to the
+     * underlying metadata.
+     *
+     * @param field
+     * @param value
+     * @param data
+     * @return
+     */
+    String filter(String field, String value, Map<String, String[]> data);
+}
diff --git a/tika-core/src/main/java/org/apache/tika/metadata/MetadataWriteFilterFactory.java b/tika-core/src/main/java/org/apache/tika/metadata/MetadataWriteFilterFactory.java
new file mode 100644
index 0000000..9c9c0c1
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/metadata/MetadataWriteFilterFactory.java
@@ -0,0 +1,21 @@
+/*
+ * 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.tika.metadata;
+
+public interface MetadataWriteFilterFactory {
+    MetadataWriteFilter newInstance();
+}
diff --git a/tika-core/src/main/java/org/apache/tika/metadata/StandardWriteFilter.java b/tika-core/src/main/java/org/apache/tika/metadata/StandardWriteFilter.java
new file mode 100644
index 0000000..8bc47b6
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/metadata/StandardWriteFilter.java
@@ -0,0 +1,161 @@
+/*
+ * 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.tika.metadata;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CodingErrorAction;
+import java.nio.charset.StandardCharsets;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.tika.utils.StringUtils;
+
+/**
+ * This is to be used to limit the amount of metadata that a
+ * parser can add based on the {@link #maxEstimatedSize}. The
+ * maxEstimatedSize is measured in UTF-8 bytes.
+ *
+ * This can also be used to limit the fields that are stored
+ * in the metadata object at write-time with {@link #includeFields}.
+ *
+ * <b>NOTE:</b> Fields in {@link #ALWAYS_INCLUDE_FIELDS} are never
+ * always included, and their sizes are not included in the
+ * calculation of metadata size.
+ *
+ * <b>NOTE:</b> after the maxEstimatedSize has been hit, no
+ * further modifications to the metadata object will be allowed aside
+ * from adding/setting fields in the {@link #ALWAYS_INCLUDE_FIELDS}.
+ *
+ * <b>NOTE:</b> as with {@link Metadata}, this object is not thread safe.
+ */
+public class StandardWriteFilter implements MetadataWriteFilter, Serializable {
+
+    public static final Set<String> ALWAYS_INCLUDE_FIELDS = new HashSet<>();
+
+    static {
+        ALWAYS_INCLUDE_FIELDS.add(Metadata.CONTENT_LENGTH);
+        ALWAYS_INCLUDE_FIELDS.add(Metadata.CONTENT_TYPE);
+        ALWAYS_INCLUDE_FIELDS.add(TikaCoreProperties.CONTENT_TYPE_PARSER_OVERRIDE.getName());
+        ALWAYS_INCLUDE_FIELDS.add(TikaCoreProperties.CONTENT_TYPE_HINT.getName());
+        ALWAYS_INCLUDE_FIELDS.add(TikaCoreProperties.TIKA_CONTENT.getName());
+    }
+
+    private final boolean includeEmpty;
+    private final int maxEstimatedSize;
+
+    private final Set<String> includeFields;
+
+    //tracks the estimated size in utf8 bytes. Can be > maxEstimated size
+    int estimatedSize = 0;
+
+    /**
+     *
+     * @param maxEstimatedSize
+     * @param includeFields if null or empty, all fields are included; otherwise, which fields
+     *                      to add to the metadata object.
+     * @param includeEmpty if <code>true</code>, this will set or add an empty value to the
+     *                     metadata object.
+     */
+    public StandardWriteFilter(int maxEstimatedSize, Set<String> includeFields,
+                               boolean includeEmpty) {
+        if (maxEstimatedSize < 0) {
+            throw new IllegalArgumentException("max estimated size must be > 0");
+        }
+        this.maxEstimatedSize = maxEstimatedSize;
+        this.includeFields = includeFields;
+        this.includeEmpty = includeEmpty;
+    }
+
+    @Override
+    public void filterExisting(Map<String, String[]> data) {
+
+    }
+
+    @Override
+    public boolean include(String field, String value) {
+        return includeField(field) && includeValue(value);
+    }
+
+    @Override
+    public String filter(String field, String value, Map<String, String[]> data) {
+        if (ALWAYS_INCLUDE_FIELDS.contains(field)) {
+            return value;
+        }
+        if (estimatedSize > maxEstimatedSize) {
+            return null;
+        }
+        long length = value.getBytes(StandardCharsets.UTF_8).length;
+        String toWrite = value;
+        if (estimatedSize + length > maxEstimatedSize) {
+            toWrite = truncate(value);
+            data.put(TikaCoreProperties.METADATA_LIMIT_REACHED.getName(), new String[]{"true"});
+        }
+        //this will by default bump the estimated size over what was actually written
+        //we are currently only using this as an indicator of whether to even try to write more.
+        //this value is not necessarily accurate.
+        estimatedSize += length;
+        return toWrite;
+    }
+
+    private String truncate(String value) {
+        //correctly handle multibyte characters
+        int available = maxEstimatedSize - estimatedSize;
+        byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
+        ByteBuffer bb = ByteBuffer.wrap(bytes, 0, available);
+        CharBuffer cb = CharBuffer.allocate(available);
+        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
+        // Ignore an incomplete character
+        decoder.onMalformedInput(CodingErrorAction.IGNORE);
+        decoder.decode(bb, cb, true);
+        decoder.flush(cb);
+        return new String(cb.array(), 0, cb.position());
+    }
+
+
+    /**
+     * Tests for null or empty. Does not check for length
+     * @param value
+     * @return
+     */
+    private boolean includeValue(String value) {
+        if (includeEmpty) {
+            return true;
+        }
+        if (StringUtils.isBlank(value)) {
+            return false;
+        }
+        return true;
+    }
+
+    private boolean includeField(String name) {
+        if (ALWAYS_INCLUDE_FIELDS.contains(name)) {
+            return true;
+        }
+        if (includeFields == null) {
+            return true;
+        }
+        if (includeFields.contains(name)) {
+            return true;
+        }
+        return false;
+    }
+
+}
diff --git a/tika-core/src/main/java/org/apache/tika/metadata/StandardWriteFilterFactory.java b/tika-core/src/main/java/org/apache/tika/metadata/StandardWriteFilterFactory.java
new file mode 100644
index 0000000..43c623c
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/metadata/StandardWriteFilterFactory.java
@@ -0,0 +1,57 @@
+/*
+ * 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.tika.metadata;
+
+import java.util.Collections;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class StandardWriteFilterFactory implements MetadataWriteFilterFactory {
+
+    public static int DEFAULT_MAX_SIZE = 10 * 1024 * 1024;
+    private Set<String> includeFields = null;
+    private int maxEstimatedBytes = DEFAULT_MAX_SIZE;
+    private boolean includeEmpty = false;
+
+    public MetadataWriteFilter newInstance() {
+        return new StandardWriteFilter(maxEstimatedBytes, includeFields, includeEmpty);
+    }
+
+    public void setIncludeFields(Set<String> includeFields) {
+        Set<String> keys = ConcurrentHashMap.newKeySet(includeFields.size());
+        keys.addAll(includeFields);
+        this.includeFields = Collections.unmodifiableSet(keys);
+    }
+
+    public void setMaxEstimatedBytes(int maxEstimatedBytes) {
+        this.maxEstimatedBytes = maxEstimatedBytes;
+    }
+
+    public void setIncludeEmpty(boolean includeEmpty) {
+        this.includeEmpty = includeEmpty;
+    }
+
+    @Override
+    public String toString() {
+        return "WriteFilteringMetadataFactory{" + "includeFields=" + includeFields +
+                ", maxEstimatedBytes=" + maxEstimatedBytes + ", includeEmpty=" + includeEmpty + '}';
+    }
+
+    public int getMaxEstimatedBytes() {
+        return maxEstimatedBytes;
+    }
+}
diff --git a/tika-core/src/main/java/org/apache/tika/metadata/TikaCoreProperties.java b/tika-core/src/main/java/org/apache/tika/metadata/TikaCoreProperties.java
index f6025c3..ea08f75 100644
--- a/tika-core/src/main/java/org/apache/tika/metadata/TikaCoreProperties.java
+++ b/tika-core/src/main/java/org/apache/tika/metadata/TikaCoreProperties.java
@@ -81,6 +81,10 @@ public interface TikaCoreProperties {
      */
     Property TIKA_META_EXCEPTION_WARNING =
             Property.internalTextBag(TIKA_META_EXCEPTION_PREFIX + "warn");
+
+    Property METADATA_LIMIT_REACHED =
+            Property.internalBoolean(TIKA_META_EXCEPTION_PREFIX + "metadata_limit_reached");
+
     /**
      * Use this to store exceptions caught while trying to read the
      * stream of an embedded resource.  Do not use this if there is
diff --git a/tika-core/src/main/java/org/apache/tika/parser/AutoDetectParser.java b/tika-core/src/main/java/org/apache/tika/parser/AutoDetectParser.java
index 099536b..7b4157c 100644
--- a/tika-core/src/main/java/org/apache/tika/parser/AutoDetectParser.java
+++ b/tika-core/src/main/java/org/apache/tika/parser/AutoDetectParser.java
@@ -129,6 +129,9 @@ public class AutoDetectParser extends CompositeParser {
 
     public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
                       ParseContext context) throws IOException, SAXException, TikaException {
+        if (autoDetectParserConfig.getMetadataWriteFilterFactory() != null) {
+            metadata.setWriteFilter(autoDetectParserConfig.getMetadataWriteFilterFactory().newInstance());
+        }
         TemporaryResources tmp = new TemporaryResources();
         try {
             TikaInputStream tis = TikaInputStream.get(stream, tmp);
diff --git a/tika-core/src/main/java/org/apache/tika/parser/AutoDetectParserConfig.java b/tika-core/src/main/java/org/apache/tika/parser/AutoDetectParserConfig.java
index 32ae7c4..1115365 100644
--- a/tika-core/src/main/java/org/apache/tika/parser/AutoDetectParserConfig.java
+++ b/tika-core/src/main/java/org/apache/tika/parser/AutoDetectParserConfig.java
@@ -23,6 +23,7 @@ import org.w3c.dom.Element;
 
 import org.apache.tika.config.ConfigBase;
 import org.apache.tika.exception.TikaConfigException;
+import org.apache.tika.metadata.MetadataWriteFilterFactory;
 
 /**
  * This config object can be used to tune how conservative we want to be
@@ -69,6 +70,8 @@ public class AutoDetectParserConfig extends ConfigBase implements Serializable {
      */
     private Integer maximumPackageEntryDepth = null;
 
+    private MetadataWriteFilterFactory metadataWriteFilterFactory = null;
+
     /**
      *  Creates a SecureContentHandlerConfig using the passed in parameters.
      *
@@ -131,5 +134,13 @@ public class AutoDetectParserConfig extends ConfigBase implements Serializable {
     public void setMaximumPackageEntryDepth(int maximumPackageEntryDepth) {
         this.maximumPackageEntryDepth = maximumPackageEntryDepth;
     }
+
+    public MetadataWriteFilterFactory getMetadataWriteFilterFactory() {
+        return this.metadataWriteFilterFactory;
+    }
+
+    public void setMetadataWriteFilterFactory(MetadataWriteFilterFactory metadataWriteFilterFactory) {
+        this.metadataWriteFilterFactory = metadataWriteFilterFactory;
+    }
 }
 
diff --git a/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java b/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
index 5c8cbfd..2642c55 100644
--- a/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
+++ b/tika-core/src/test/java/org/apache/tika/config/TikaConfigTest.java
@@ -23,9 +23,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.List;
@@ -38,6 +40,9 @@ import org.apache.tika.ResourceLoggingClassLoader;
 import org.apache.tika.exception.TikaConfigException;
 import org.apache.tika.exception.TikaException;
 import org.apache.tika.metadata.Metadata;
+import org.apache.tika.metadata.MetadataWriteFilterFactory;
+import org.apache.tika.metadata.StandardWriteFilterFactory;
+import org.apache.tika.metadata.TikaCoreProperties;
 import org.apache.tika.mime.MediaType;
 import org.apache.tika.mime.MimeDetectionTest;
 import org.apache.tika.parser.AutoDetectParser;
@@ -46,6 +51,7 @@ import org.apache.tika.parser.CompositeParser;
 import org.apache.tika.parser.DefaultParser;
 import org.apache.tika.parser.EmptyParser;
 import org.apache.tika.parser.ErrorParser;
+import org.apache.tika.parser.ParseContext;
 import org.apache.tika.parser.Parser;
 import org.apache.tika.parser.ParserDecorator;
 import org.apache.tika.parser.mock.MockParser;
@@ -395,4 +401,33 @@ public class TikaConfigTest extends AbstractTikaConfigTest {
         assertNull(config.getMaximumDepth());
         assertNull(config.getMaximumPackageEntryDepth());
     }
+
+    @Test
+    public void testMetadataFactoryConfig() throws Exception {
+        TikaConfig tikaConfig =
+                new TikaConfig(TikaConfigTest.class.getResourceAsStream("TIKA-3695.xml"));
+        AutoDetectParserConfig config = tikaConfig.getAutoDetectParserConfig();
+        MetadataWriteFilterFactory factory = config.getMetadataWriteFilterFactory();
+        assertEquals(241, ((StandardWriteFilterFactory) factory).getMaxEstimatedBytes());
+        AutoDetectParser parser = new AutoDetectParser(tikaConfig);
+        String mock = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
+                "<mock>";
+        for (int i = 0; i < 10; i++) {
+            mock += "<metadata action=\"add\" name=\"dc:creator\">01234567890123456789</metadata>";
+        }
+        mock += "<write element=\"p\" times=\"30\"> hello </write>\n";
+        mock += "</mock>";
+        Metadata metadata = new Metadata();
+        List<Metadata> metadataList =
+                getRecursiveMetadata(new ByteArrayInputStream(mock.getBytes(StandardCharsets.UTF_8)),
+                parser, metadata, new ParseContext(), true);
+        assertEquals(1, metadataList.size());
+        metadata = metadataList.get(0);
+
+        String[] creators = metadata.getValues("dc:creator");
+        assertEquals(9, creators.length);
+        assertEquals("0123456", creators[8]);
+        assertContainsCount(" hello ", metadata.get(TikaCoreProperties.TIKA_CONTENT), 30);
+        assertEquals("true", metadata.get(TikaCoreProperties.METADATA_LIMIT_REACHED));
+    }
 }
diff --git a/tika-core/src/test/resources/org/apache/tika/config/TIKA-3695.xml b/tika-core/src/test/resources/org/apache/tika/config/TIKA-3695.xml
new file mode 100644
index 0000000..86ebe05
--- /dev/null
+++ b/tika-core/src/test/resources/org/apache/tika/config/TIKA-3695.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<properties>
+  <parsers>
+    <parser class="org.apache.tika.parser.DefaultParser"/>
+  </parsers>
+  <autoDetectParserConfig>
+    <params>
+      <spoolToDisk>12345</spoolToDisk>
+      <outputThreshold>6789</outputThreshold>
+    </params>
+    <metadataWriteFilterFactory class="org.apache.tika.metadata.StandardWriteFilterFactory">
+      <params>
+        <maxEstimatedBytes>241</maxEstimatedBytes>
+      </params>
+    </metadataWriteFilterFactory>
+  </autoDetectParserConfig>
+</properties>