Skip to content

Commit 871d2c0

Browse files
committed
TIKA-4809: Remove the writeLimit and throwOnWriteLimitReached headers
1 parent fe197f0 commit 871d2c0

8 files changed

Lines changed: 55 additions & 358 deletions

File tree

tika-server/tika-server-core/src/main/java/org/apache/tika/server/core/resource/RecursiveMetadataResource.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.apache.tika.server.core.resource;
1818

1919
import static org.apache.tika.server.core.resource.TikaResource.fillMetadata;
20-
import static org.apache.tika.server.core.resource.TikaResource.getWriteLimit;
2120
import static org.apache.tika.server.core.resource.TikaResource.setupContentHandlerFactory;
2221
import static org.apache.tika.server.core.resource.TikaResource.setupContentHandlerFactoryIfNeeded;
2322

@@ -74,8 +73,7 @@ public List<Metadata> parseMetadata(TikaInputStream tis, Metadata metadata,
7473
TikaResource.logRequest(LOG, "/rmeta", metadata);
7574

7675
// Set up handler factory in context using shared utility
77-
setupContentHandlerFactory(context, handlerConfig.type().toString(), handlerConfig.writeLimit(),
78-
handlerConfig.throwOnWriteLimitReached());
76+
setupContentHandlerFactory(context, handlerConfig.type().toString());
7977

8078
// Set up embedded limits if specified
8179
if (handlerConfig.maxEmbeddedCount() >= 0) {
@@ -96,8 +94,8 @@ static ServerHandlerConfig buildHandlerConfig(MultivaluedMap<String, String> htt
9694
} else if (httpHeaders.containsKey("maxEmbeddedCount")) {
9795
maxEmbeddedCount = Integer.parseInt(httpHeaders.getFirst("maxEmbeddedCount"));
9896
}
99-
return new ServerHandlerConfig(BasicContentHandlerFactory.parseHandlerType(handlerTypeName, DEFAULT_HANDLER_TYPE), parseMode,
100-
getWriteLimit(httpHeaders), maxEmbeddedCount, TikaResource.getThrowOnWriteLimitReached(httpHeaders));
97+
return new ServerHandlerConfig(BasicContentHandlerFactory.parseHandlerType(handlerTypeName, DEFAULT_HANDLER_TYPE),
98+
parseMode, maxEmbeddedCount);
10199
}
102100

103101
/**
@@ -169,8 +167,7 @@ public Response getMetadataWithConfig(
169167
private MetadataList parseMetadataWithContext(TikaInputStream tis, Metadata metadata, MultivaluedMap<String, String> httpHeaders,
170168
ServerHandlerConfig handlerConfig, ParseContext context) throws Exception {
171169
// Set up handler factory in context if not already set using shared utility
172-
setupContentHandlerFactoryIfNeeded(context, handlerConfig.type().toString(),
173-
handlerConfig.writeLimit(), handlerConfig.throwOnWriteLimitReached());
170+
setupContentHandlerFactoryIfNeeded(context, handlerConfig.type().toString());
174171

175172
// Filtering is done in child process, no need to filter again
176173
List<Metadata> metadataList = tikaResource.parseWithPipes(tis, metadata, context, ParseMode.RMETA);

tika-server/tika-server-core/src/main/java/org/apache/tika/server/core/resource/ServerHandlerConfig.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
public record ServerHandlerConfig(
3131
BasicContentHandlerFactory.HANDLER_TYPE type,
3232
ParseMode parseMode,
33-
int writeLimit,
34-
int maxEmbeddedCount,
35-
boolean throwOnWriteLimitReached
33+
int maxEmbeddedCount
3634
) {
3735
}

tika-server/tika-server-core/src/main/java/org/apache/tika/server/core/resource/TikaResource.java

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -407,65 +407,18 @@ public static void logRequest(Logger logger, String endpoint, Metadata metadata)
407407
}
408408
}
409409

410-
public static boolean getThrowOnWriteLimitReached(MultivaluedMap<String, String> httpHeaders) {
411-
if (httpHeaders.containsKey("throwOnWriteLimitReached")) {
412-
String val = httpHeaders.getFirst("throwOnWriteLimitReached");
413-
if ("true".equalsIgnoreCase(val)) {
414-
return true;
415-
} else if ("false".equalsIgnoreCase(val)) {
416-
return false;
417-
} else {
418-
throw new IllegalArgumentException("'throwOnWriteLimitReached' must be either 'true' or 'false'");
419-
}
420-
}
421-
// Default: throw on write limit reached
422-
return true;
423-
}
424-
425-
/**
426-
* Parses the writeLimit header value from HTTP headers.
427-
*
428-
* @param httpHeaders the HTTP headers
429-
* @return the write limit value, or -1 if not specified
430-
*/
431-
public static int getWriteLimit(MultivaluedMap<String, String> httpHeaders) {
432-
if (httpHeaders.containsKey("writeLimit")) {
433-
return Integer.parseInt(httpHeaders.getFirst("writeLimit"));
434-
}
435-
return -1;
436-
}
437-
438410
/**
439-
* Sets up the ContentHandlerFactory in the ParseContext based on handler type and HTTP headers.
440-
* This is a shared utility method used by both /tika and /rmeta endpoints.
411+
* Sets up the ContentHandlerFactory in the ParseContext, taking the write limits from
412+
* {@link org.apache.tika.config.OutputLimits} in the context.
441413
*
442414
* @param context the ParseContext to configure
443415
* @param handlerTypeName the handler type name (text, html, xml, ignore), may be null for default
444-
* @param httpHeaders the HTTP headers containing writeLimit and throwOnWriteLimitReached
445416
*/
446-
public static void setupContentHandlerFactory(ParseContext context, String handlerTypeName,
447-
MultivaluedMap<String, String> httpHeaders) {
448-
int writeLimit = getWriteLimit(httpHeaders);
449-
boolean throwOnWriteLimitReached = getThrowOnWriteLimitReached(httpHeaders);
450-
setupContentHandlerFactory(context, handlerTypeName, writeLimit, throwOnWriteLimitReached);
451-
}
452-
453-
/**
454-
* Sets up the ContentHandlerFactory in the ParseContext based on explicit parameters.
455-
* This overload is used when the values have already been parsed (e.g., from ServerHandlerConfig).
456-
*
457-
* @param context the ParseContext to configure
458-
* @param handlerTypeName the handler type name (text, html, xml, ignore), may be null for default
459-
* @param writeLimit the write limit, or -1 for unlimited
460-
* @param throwOnWriteLimitReached whether to throw when write limit is reached
461-
*/
462-
public static void setupContentHandlerFactory(ParseContext context, String handlerTypeName,
463-
int writeLimit, boolean throwOnWriteLimitReached) {
417+
public static void setupContentHandlerFactory(ParseContext context, String handlerTypeName) {
464418
BasicContentHandlerFactory.HANDLER_TYPE type = BasicContentHandlerFactory.parseHandlerType(
465419
handlerTypeName, DEFAULT_HANDLER_TYPE);
466-
ContentHandlerFactory factory = new BasicContentHandlerFactory(type, writeLimit,
467-
throwOnWriteLimitReached, context);
468-
context.set(ContentHandlerFactory.class, factory);
420+
context.set(ContentHandlerFactory.class,
421+
BasicContentHandlerFactory.newInstance(type, context));
469422
}
470423

471424
/**
@@ -474,28 +427,10 @@ public static void setupContentHandlerFactory(ParseContext context, String handl
474427
*
475428
* @param context the ParseContext to configure
476429
* @param handlerTypeName the handler type name
477-
* @param httpHeaders the HTTP headers
478-
*/
479-
public static void setupContentHandlerFactoryIfNeeded(ParseContext context, String handlerTypeName,
480-
MultivaluedMap<String, String> httpHeaders) {
481-
if (context.get(ContentHandlerFactory.class) == null) {
482-
setupContentHandlerFactory(context, handlerTypeName, httpHeaders);
483-
}
484-
}
485-
486-
/**
487-
* Sets up the ContentHandlerFactory in the ParseContext if not already set.
488-
* This overload is used when the values have already been parsed.
489-
*
490-
* @param context the ParseContext to configure
491-
* @param handlerTypeName the handler type name
492-
* @param writeLimit the write limit, or -1 for unlimited
493-
* @param throwOnWriteLimitReached whether to throw when write limit is reached
494430
*/
495-
public static void setupContentHandlerFactoryIfNeeded(ParseContext context, String handlerTypeName,
496-
int writeLimit, boolean throwOnWriteLimitReached) {
431+
public static void setupContentHandlerFactoryIfNeeded(ParseContext context, String handlerTypeName) {
497432
if (context.get(ContentHandlerFactory.class) == null) {
498-
setupContentHandlerFactory(context, handlerTypeName, writeLimit, throwOnWriteLimitReached);
433+
setupContentHandlerFactory(context, handlerTypeName);
499434
}
500435
}
501436

@@ -765,7 +700,7 @@ private Response produceRawOutput(TikaInputStream tis, Metadata metadata,
765700
String handlerTypeName) throws IOException {
766701
fillMetadata(null, metadata, httpHeaders);
767702
ParseContext context = createParseContext();
768-
setupContentHandlerFactory(context, handlerTypeName, httpHeaders);
703+
setupContentHandlerFactory(context, handlerTypeName);
769704
return produceRawOutputWithContext(tis, metadata, context, handlerTypeName);
770705
}
771706

@@ -781,7 +716,7 @@ private Response produceRawOutputWithContext(TikaInputStream tis, Metadata metad
781716
logRequest(LOG, "/tika", metadata);
782717

783718
// Ensure content handler factory is set (config may have set it)
784-
setupContentHandlerFactoryIfNeeded(context, handlerTypeName, -1, true);
719+
setupContentHandlerFactoryIfNeeded(context, handlerTypeName);
785720

786721
LOG.debug("produceRawOutput: handlerType={}, contentHandlerFactory={}",
787722
handlerTypeName, context.get(ContentHandlerFactory.class));
@@ -849,7 +784,7 @@ private Metadata produceJson(TikaInputStream tis, Metadata metadata,
849784
String handlerTypeName) throws IOException {
850785
fillMetadata(null, metadata, headers);
851786
ParseContext context = createParseContext();
852-
setupContentHandlerFactory(context, handlerTypeName, headers);
787+
setupContentHandlerFactory(context, handlerTypeName);
853788
return produceJsonWithContext(tis, metadata, context, handlerTypeName);
854789
}
855790

@@ -871,7 +806,7 @@ private Metadata produceJsonWithContext(TikaInputStream tis, Metadata metadata,
871806
logRequest(LOG, "/tika", metadata);
872807

873808
// Ensure content handler factory is set (config may have set it)
874-
setupContentHandlerFactoryIfNeeded(context, handlerTypeName, -1, true);
809+
setupContentHandlerFactoryIfNeeded(context, handlerTypeName);
875810

876811
List<Metadata> metadataList;
877812
try {

tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/RecursiveMetadataResourceTest.java

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -91,66 +91,5 @@ private String rmetaContent(String handlerSuffix) throws Exception {
9191
Reader reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
9292
return JsonMetadataList.fromJson(reader).get(0).get(TikaCoreProperties.TIKA_CONTENT);
9393
}
94-
/*
95-
@Test
96-
public void testWriteLimitInAll() throws Exception {
97-
//specify your file directory here
98-
Path testDocs = Paths.get("..../tika-parsers/src/test/resources/test-documents");
99-
for (File f : testDocs.toFile().listFiles()) {
100-
if (f.isDirectory()) {
101-
continue;
102-
}
103-
testWriteLimit(f);
104-
}
105-
}
106-
private void testWriteLimit(File f) throws Exception {
107-
Response response = WebClient.create(endPoint + META_PATH+"/text").accept(
108-
"application/json")
109-
.put(f);
110-
assertEquals(200, response.getStatus());
111-
Reader reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
112-
List<Metadata> metadataList = JsonMetadataList.fromJson(reader);
113-
int totalLen = 0;
114-
StringBuilder sb = new StringBuilder();
115-
for (Metadata m : metadataList) {
116-
String txt = m.get(AbstractRecursiveParserWrapperHandler.TIKA_CONTENT);
117-
sb.append(txt);
118-
totalLen += (txt == null) ? 0 : txt.length();
119-
}
120-
String fullText = sb.toString();
121-
Random r = new Random();
122-
for (int i = 0; i < 20; i++) {
123-
int writeLimit = r.nextInt(totalLen+100);
124-
response = WebClient.create(endPoint + META_PATH+"/text").accept(
125-
"application/json")
126-
.header("writeLimit", Integer.toString(writeLimit)).put(f);
127-
assertEquals(200, response.getStatus());
128-
reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
129-
List<Metadata> writeLimitMetadataList = JsonMetadataList.fromJson(reader);
130-
int len = 0;
131-
StringBuilder extracted = new StringBuilder();
132-
for (Metadata m : writeLimitMetadataList) {
133-
String txt = m.get(AbstractRecursiveParserWrapperHandler.TIKA_CONTENT);
134-
len += (txt == null) ? 0 : txt.length();
135-
extracted.append(txt);
136-
}
137-
if (totalLen > len) {
138-
boolean wlr = false;
139-
for (Metadata m : writeLimitMetadataList) {
140-
if ("true".equals(m.get(AbstractRecursiveParserWrapperHandler.WRITE_LIMIT_REACHED))) {
141-
wlr = true;
142-
}
143-
}
144-
assertTrue(f.getName() + ": writelimit: " + writeLimit + " len: "+len,
145-
len <= writeLimit);
146-
assertEquals(f.getName() +" writeLimit: " + writeLimit +
147-
" : fullLen:" + totalLen + " limitedLen: " +len,
148-
true, wlr);
149-
} else if (len > totalLen) {
150-
fail("len should never be > totalLen "+len + " : "+ totalLen);
151-
}
152-
}
153-
}
154-
*/
15594

15695
}

tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/TikaResourceTest.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,6 @@ public void testJsonNPE() throws Exception {
116116
assertNotFound("null pointer message", metadata.get(TikaCoreProperties.CONTAINER_EXCEPTION));
117117
}
118118

119-
@Test
120-
public void testJsonWriteLimit() throws Exception {
121-
Response response = WebClient
122-
.create(endPoint + TIKA_PATH + "/json")
123-
.header("writeLimit", "100")
124-
.put(ClassLoader.getSystemResourceAsStream(TEST_HELLO_WORLD_LONG));
125-
Metadata metadata = JsonMetadata.fromJson(new InputStreamReader(((InputStream) response.getEntity()), StandardCharsets.UTF_8));
126-
127-
assertEquals("Nikolai Lobachevsky", metadata.get("author"));
128-
assertEquals("application/mock+xml", metadata.get(Metadata.CONTENT_TYPE));
129-
assertContains("Hello world", metadata.get(TikaCoreProperties.TIKA_CONTENT));
130-
assertNotFound("dissolve", metadata.get(TikaCoreProperties.TIKA_CONTENT));
131-
assertTrue(metadata
132-
.get(TikaCoreProperties.CONTAINER_EXCEPTION)
133-
.startsWith("org.apache.tika.exception.WriteLimitReachedException"));
134-
assertEquals("true", metadata.get(TikaCoreProperties.WRITE_LIMIT_REACHED));
135-
}
136-
137119
@Test
138120
public void testJsonHandlerType() throws Exception {
139121
// Default /tika/json uses text handler
@@ -161,18 +143,6 @@ public void testJsonHandlerType() throws Exception {
161143
}
162144

163145
/*
164-
@Test
165-
public void testWriteLimitInAll() throws Exception {
166-
//specify your file directory here
167-
Path testDocs = Paths.get("..../tika-parsers/src/test/resources/test-documents");
168-
for (File f : testDocs.toFile().listFiles()) {
169-
if (f.isDirectory()) {
170-
continue;
171-
}
172-
testWriteLimit(f);
173-
}
174-
}
175-
176146
private void testWriteLimit(File f) throws Exception {
177147
Response response =
178148
WebClient.create(endPoint + TIKA_PATH + "/text").accept("application/json").put(f);

tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/benchmark/TikaServerBenchmark.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ private boolean verifyMockParserInUse() {
339339
.uri(URI.create(baseUrl + "/rmeta"))
340340
.header("Content-Type", "application/mock+xml")
341341
.header("Accept", "application/json")
342-
.header("writeLimit", "-1")
343342
.PUT(HttpRequest.BodyPublishers.ofString(testXml))
344343
.timeout(Duration.ofSeconds(10))
345344
.build();

0 commit comments

Comments
 (0)