/
TikaResourceTest.java
634 lines (548 loc) · 30.2 KB
/
TikaResourceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
* 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.server.standard;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.cxf.helpers.HttpHeaderHelper.CONTENT_ENCODING;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.cxf.attachment.AttachmentUtil;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.junit.jupiter.api.Test;
import org.apache.tika.TikaTest;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.OfficeOpenXMLExtended;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.metadata.serialization.JsonMetadata;
import org.apache.tika.parser.ocr.TesseractOCRParser;
import org.apache.tika.server.core.CXFTestBase;
import org.apache.tika.server.core.TikaServerParseExceptionMapper;
import org.apache.tika.server.core.config.DocumentSelectorConfig;
import org.apache.tika.server.core.config.PasswordProviderConfig;
import org.apache.tika.server.core.resource.TikaResource;
import org.apache.tika.server.core.writer.JSONMessageBodyWriter;
import org.apache.tika.server.standard.config.PDFServerConfig;
import org.apache.tika.server.standard.config.TesseractServerConfig;
public class TikaResourceTest extends CXFTestBase {
public static final String TEST_DOC = "test-documents/test.doc";
public static final String TEST_PASSWORD_PROTECTED = "test-documents/password.xls";
private static final String TEST_RECURSIVE_DOC = "test-documents/test_recursive_embedded.docx";
private static final String TEST_OOM = "mock/fake_oom.xml";
private static final String STREAM_CLOSED_FAULT = "java.io.IOException: Stream Closed";
private static final String TIKA_PATH = "/tika";
private static final String TIKA_POST_PATH = "/tika/form";
private static final int UNPROCESSEABLE = 422;
@Override
protected void setUpResources(JAXRSServerFactoryBean sf) {
sf.setResourceClasses(TikaResource.class);
sf.setResourceProvider(TikaResource.class,
new SingletonResourceProvider(new TikaResource()));
}
@Override
protected void setUpProviders(JAXRSServerFactoryBean sf) {
List<Object> providers = new ArrayList<>();
providers.add(new TikaServerParseExceptionMapper(false));
providers.add(new JSONMessageBodyWriter());
sf.setProviders(providers);
}
@Override
protected InputStream getTikaConfigInputStream() {
return getClass().getResourceAsStream("/config/tika-config-for-server-tests.xml");
}
@Test
public void testHelloWorld() throws Exception {
Response response =
WebClient.create(endPoint + TIKA_PATH).type("text/plain").accept("text/plain")
.get();
assertEquals(TikaResource.GREETING,
getStringFromInputStream((InputStream) response.getEntity()));
}
@Test
public void testSimpleWord() throws Exception {
Response response = WebClient.create(endPoint + TIKA_PATH).type("application/msword")
.accept("text/plain").put(ClassLoader.getSystemResourceAsStream(TEST_DOC));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("test"));
}
@Test
public void testWordGzipIn() throws Exception {
Response response = WebClient.create(endPoint + TIKA_PATH).type("application/msword")
.accept("text/plain").encoding("gzip")
.put(gzip(ClassLoader.getSystemResourceAsStream(TEST_DOC)));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("test"));
}
@Test
public void testLongGzipOut() throws Exception {
//if the output is long enough, jax-rs will compress it, otherwise it won't
//this output is long enough, and should be compressed
Response response =
WebClient.create(endPoint + TIKA_PATH).accept("text/plain").acceptEncoding("gzip")
.put(ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
assertTrue(response.getHeaders().containsKey(CONTENT_ENCODING));
assertEquals("gzip", response.getHeaderString(CONTENT_ENCODING));
String responseMsg = getStringFromInputStream(
new GzipCompressorInputStream((InputStream) response.getEntity()));
assertTrue(responseMsg.contains("Course of human"));
}
@Test
public void testShortGzipOut() throws Exception {
//if the output is long enough, jax-rs will compress it, otherwise it won't
//this output is short enough, and should not be compressed
Response response =
WebClient.create(endPoint + TIKA_PATH).accept("text/plain").acceptEncoding("gzip")
.put(ClassLoader.getSystemResourceAsStream(TEST_DOC));
assertFalse(response.getHeaders().containsKey(CONTENT_ENCODING));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("test"));
}
@Test
public void testTextMain() throws Exception {
//boilerpipe
Response response = WebClient.create(endPoint + TIKA_PATH + "/main").accept("text/plain")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testHTML.html"));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("Title : Test Indexation Html"));
assertFalse(responseMsg.contains("Indexation du fichier"));
}
@Test
public void testTextMainMultipart() throws Exception {
//boilerpipe
Attachment attachmentPart = new Attachment("myhtml", "text/html",
ClassLoader.getSystemResourceAsStream("test-documents/testHTML.html"));
Response response =
WebClient.create(endPoint + TIKA_PATH + "/form/main").type("multipart/form-data")
.accept("text/plain").post(attachmentPart);
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("Title : Test Indexation Html"));
assertFalse(responseMsg.contains("Indexation du fichier"));
}
@Test
public void testPasswordXLS() {
Response response = WebClient.create(endPoint + TIKA_PATH).type("application/vnd.ms-excel")
.accept("text/plain")
.put(ClassLoader.getSystemResourceAsStream("test-documents/password.xls"));
assertEquals(UNPROCESSEABLE, response.getStatus());
}
@Test
public void testSimpleWordHTML() throws Exception {
Response response = WebClient.create(endPoint + TIKA_PATH).type("application/msword")
.accept("text/html").put(ClassLoader.getSystemResourceAsStream(TEST_DOC));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("test"));
assertContains(
"<meta name=\"X-TIKA:digest:MD5\" content=\"f8be45c34e8919eedba48cc8d207fbf0\"/>",
responseMsg);
assertContains(
"<meta name=\"X-TIKA:digest:SHA1\" content=\"N4EBCE7EGTIGZWETEJ6WD3W4KN32TLPG\"/>",
responseMsg);
}
@Test
public void testPasswordXLSHTML() {
Response response = WebClient.create(endPoint + TIKA_PATH).type("application/vnd.ms-excel")
.accept("text/html")
.put(ClassLoader.getSystemResourceAsStream("test-documents/password.xls"));
assertEquals(UNPROCESSEABLE, response.getStatus());
}
@Test
public void testSimpleWordXML() throws Exception {
Response response =
WebClient.create(endPoint + TIKA_PATH).type("application/msword").accept("text/xml")
.put(ClassLoader.getSystemResourceAsStream(TEST_DOC));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("test"));
}
@Test
public void testPasswordXLSXML() {
Response response = WebClient.create(endPoint + TIKA_PATH).type("application/vnd.ms-excel")
.accept("text/xml")
.put(ClassLoader.getSystemResourceAsStream("test-documents/password.xls"));
assertEquals(UNPROCESSEABLE, response.getStatus());
}
@Test
public void testSimpleWordMultipartXML() throws Exception {
ClassLoader.getSystemResourceAsStream(TEST_DOC);
Attachment attachmentPart = new Attachment("myworddoc", "application/msword",
ClassLoader.getSystemResourceAsStream(TEST_DOC));
WebClient webClient = WebClient.create(endPoint + TIKA_PATH + "/form");
Response response =
webClient.type("multipart/form-data").accept("text/xml").post(attachmentPart);
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("test"));
assertContains(
"<meta name=\"X-TIKA:digest:MD5\" content=\"f8be45c34e8919eedba48cc8d207fbf0\"/>",
responseMsg);
}
@Test
public void testJAXBAndActivationDependency() {
//TIKA-2778
AttachmentUtil.getCommandMap();
}
@Test
public void testEmbedded() throws Exception {
//first try text
Response response = WebClient.create(endPoint + TIKA_PATH).accept("text/plain")
.put(ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("Course of human events"));
//now go for xml -- different call than text
response = WebClient.create(endPoint + TIKA_PATH).accept("text/xml")
.put(ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("Course of human events"));
assertContains(
"<meta name=\"X-TIKA:digest:MD5\" content=\"59f626e09a8c16ab6dbc2800c685f772\"/>",
responseMsg);
}
//TIKA-1845
@Test
public void testWMFInRTF() throws Exception {
Response response =
WebClient.create(endPoint + TIKA_PATH).type("application/rtf").accept("text/plain")
.put(ClassLoader.getSystemResourceAsStream(
"test-documents/testRTF_npeFromWMFInTikaServer.rtf"));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("Example text"));
}
//TIKA-2638 and TIKA-2816
@Test
public void testOCRLanguageConfig() throws Exception {
if (!new TesseractOCRParser().hasTesseract()) {
return;
}
Response response = WebClient.create(endPoint + TIKA_PATH).accept("text/plain")
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX + "OcrStrategy", "ocr_only")
.header(TesseractServerConfig.X_TIKA_OCR_HEADER_PREFIX + "Language", "eng+fra")
.header(TesseractServerConfig.X_TIKA_OCR_HEADER_PREFIX + "MinFileSizeToOcr", "10")
.header(TesseractServerConfig.X_TIKA_OCR_HEADER_PREFIX + "MaxFileSizeToOcr",
"1000000000")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertContains("Happy New Year 2003!", responseMsg);
}
//TIKA-2290
@Test
public void testPDFOCRConfig() throws Exception {
if (!new TesseractOCRParser().hasTesseract()) {
return;
}
Response response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX + "OcrStrategy", "no_ocr")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertEquals("", responseMsg.trim());
response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(TesseractServerConfig.X_TIKA_OCR_HEADER_PREFIX + "skipOcr", "true")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertEquals("", responseMsg.trim());
response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX + "OcrStrategy",
"ocr_only")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertContains("Happy New Year 2003!", responseMsg);
//now try a bad value
response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX + "OcrStrategy",
"non-sense-value")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
assertEquals(400, response.getStatus());
}
// TIKA-3320
@Test
public void testPDFLowerCaseOCRConfig() throws Exception {
if (!new TesseractOCRParser().hasTesseract()) {
return;
}
Response response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX.toLowerCase(Locale.ROOT) +
"ocrstrategy", "no_ocr")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertEquals("", responseMsg.trim());
response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(TesseractServerConfig.X_TIKA_OCR_HEADER_PREFIX.toLowerCase(
Locale.ROOT) + "skipocr", "true")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertEquals("", responseMsg.trim());
response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX.toLowerCase(Locale.ROOT) +
"ocrstrategy", "ocr_only")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertContains("Happy New Year 2003!", responseMsg);
//now try a bad value
response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX.toLowerCase(Locale.ROOT) +
"ocrstrategy", "non-sense-value")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
assertEquals(400, response.getStatus());
}
//TIKA-2669
@Test
public void testPDFConfig() throws Exception {
Response response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.put(ClassLoader.getSystemResourceAsStream(
"test-documents/testPDFTwoTextBoxes.pdf"));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
responseMsg = responseMsg.replaceAll("[\r\n ]+", " ").trim();
assertEquals(
"Left column line 1 Right column line 1 Left colu mn line 2 Right column line 2",
responseMsg);
response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX + "sortByPosition",
"false").put(ClassLoader.getSystemResourceAsStream(
"test-documents/testPDFTwoTextBoxes.pdf"));
responseMsg = getStringFromInputStream((InputStream) response.getEntity());
responseMsg = responseMsg.replaceAll("[\r\n ]+", " ").trim();
assertEquals(
"Left column line 1 Left column line 2 Right column line 1 Right column line 2",
responseMsg);
//make sure that default reverts to initial config option
response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.put(ClassLoader.getSystemResourceAsStream(
"test-documents/testPDFTwoTextBoxes.pdf"));
responseMsg = getStringFromInputStream((InputStream) response.getEntity());
responseMsg = responseMsg.replaceAll("[\r\n ]+", " ").trim();
assertEquals(
"Left column line 1 Right column line 1 Left colu mn line 2 Right column line 2",
responseMsg);
}
@Test
public void testExtractTextAcceptPlainText() throws Exception {
//TIKA-2384
Attachment attachmentPart = new Attachment("my-docx-file",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
ClassLoader.getSystemResourceAsStream("test-documents/2pic.docx"));
Response response =
WebClient.create(endPoint + TIKA_PATH + "/form").type("multipart/form-data")
.accept("text/plain").post(attachmentPart);
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertTrue(responseMsg.contains("P1040893.JPG"));
assertNotFound(STREAM_CLOSED_FAULT, responseMsg);
}
@Test
public void testDataIntegrityCheck() {
Response response;
try {
response = WebClient.create(endPoint + TIKA_PATH).type("application/pdf")
.accept("text/plain")
.header(TesseractServerConfig.X_TIKA_OCR_HEADER_PREFIX + "tesseractPath",
"C://tmp//hello.bat\u0000")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
assertEquals(400, response.getStatus());
} catch (ProcessingException e) {
//can't tell why this intermittently happens. :(
//started after the upgrade to 3.2.7
}
try {
response = WebClient.create(endPoint + TIKA_PATH).type("application/pdf")
.accept("text/plain")
.header(TesseractServerConfig.X_TIKA_OCR_HEADER_PREFIX + "tesseractPath",
"bogus path")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
assertEquals(400, response.getStatus());
} catch (ProcessingException e) {
//swallow
}
}
@Test
public void testTrustedMethodPrevention() {
Response response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(TesseractServerConfig.X_TIKA_OCR_HEADER_PREFIX +
"trustedPageSeparator", "\u0020")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
assertEquals(400, response.getStatus());
}
@Test
public void testFloatInHeader() {
Response response =
WebClient.create(endPoint + TIKA_PATH).type("application/pdf").accept("text/plain")
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX + "averageCharTolerance",
"2.0")
.put(ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"));
assertEquals(200, response.getStatus());
}
@Test
public void testUnicodePasswordProtectedSpaces() throws Exception {
//TIKA-2858
final String password = " ";
final String encoded =
new Base64().encodeAsString(password.getBytes(StandardCharsets.UTF_8));
Response response = WebClient.create(endPoint + TIKA_PATH).accept("text/plain")
.header(PasswordProviderConfig.PASSWORD_BASE64_UTF8, encoded)
.put(ClassLoader.getSystemResourceAsStream(
"test-documents/testPassword4Spaces.pdf"));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertContains("Just some text.", responseMsg);
}
@Test
public void testUnicodePasswordProtectedUnicode() throws Exception {
//TIKA-2858
final String password = " ! < > \" \\ \u20AC \u0153 \u00A4 \u0031\u2044\u0034 " +
"\u0031\u2044\u0032 \uD841\uDF0E \uD867\uDD98 \uD83D\uDE00 ";
final String encoded =
new Base64().encodeAsString(password.getBytes(StandardCharsets.UTF_8));
Response response = WebClient.create(endPoint + TIKA_PATH).accept("text/plain")
.header(PasswordProviderConfig.PASSWORD_BASE64_UTF8, encoded)
.put(ClassLoader.getSystemResourceAsStream(
"test-documents/testUnicodePassword.pdf"));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertContains("Just some text.", responseMsg);
}
// TIKA-3227
@Test
public void testSkipEmbedded() throws Exception {
Response response = WebClient.create(endPoint + TIKA_PATH).accept("text/plain")
.header(DocumentSelectorConfig.X_TIKA_SKIP_EMBEDDED_HEADER, "false")
.put(ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertContains("embed4.txt", responseMsg);
response = WebClient.create(endPoint + TIKA_PATH).accept("text/plain")
.header(DocumentSelectorConfig.X_TIKA_SKIP_EMBEDDED_HEADER, "true")
.put(ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertNotFound("embed4.txt", responseMsg);
}
// TIKA-3344
@Test
public void testPDFLowerCaseOCRConfigPOST() throws Exception {
if (!new TesseractOCRParser().hasTesseract()) {
return;
}
Response response = WebClient.create(endPoint + TIKA_POST_PATH).type("application/pdf")
.accept(MediaType.TEXT_PLAIN).type(MediaType.MULTIPART_FORM_DATA)
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX.toLowerCase(Locale.ROOT) +
"ocrstrategy", "no_ocr").post(testPDFLowerCaseOCRConfigPOSTBody());
String responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertEquals("", responseMsg.trim());
response = WebClient.create(endPoint + TIKA_POST_PATH).type("application/pdf")
.accept(MediaType.TEXT_PLAIN).type(MediaType.MULTIPART_FORM_DATA)
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX.toLowerCase(Locale.ROOT) +
"ocrstrategy", "ocr_only").post(testPDFLowerCaseOCRConfigPOSTBody());
responseMsg = getStringFromInputStream((InputStream) response.getEntity());
assertContains("Happy New Year 2003!", responseMsg);
//now try a bad value
response = WebClient.create(endPoint + TIKA_POST_PATH).type("application/pdf")
.accept(MediaType.TEXT_PLAIN).type(MediaType.MULTIPART_FORM_DATA)
.header(PDFServerConfig.X_TIKA_PDF_HEADER_PREFIX.toLowerCase(Locale.ROOT) +
"ocrstrategy", "non-sense-value").post(testPDFLowerCaseOCRConfigPOSTBody());
assertEquals(400, response.getStatus());
}
private MultipartBody testPDFLowerCaseOCRConfigPOSTBody() {
ContentDisposition cd =
new ContentDisposition("form-data; name=\"input\"; filename=\"testOCR.pdf\"");
Attachment att = new Attachment("upload",
ClassLoader.getSystemResourceAsStream("test-documents/testOCR.pdf"), cd);
return new MultipartBody(att);
}
@Test
public void testJson() throws Exception {
Response response =
WebClient.create(endPoint + TIKA_PATH + "/text").accept("application/json")
.put(ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
Metadata metadata = JsonMetadata.fromJson(
new InputStreamReader(((InputStream) response.getEntity()),
StandardCharsets.UTF_8));
assertContains("embed4.txt", metadata.get(TikaCoreProperties.TIKA_CONTENT));
assertContains("General Congress", metadata.get(TikaCoreProperties.TIKA_CONTENT));
assertNotFound("<p", metadata.get(TikaCoreProperties.TIKA_CONTENT));
assertEquals("Microsoft Office Word", metadata.get(OfficeOpenXMLExtended.APPLICATION));
//test that embedded parsers are appearing in full set of "parsed bys"
TikaTest.assertContains("org.apache.tika.parser.microsoft.EMFParser",
Arrays.asList(metadata.getValues(TikaCoreProperties.TIKA_PARSED_BY_FULL_SET)));
}
@Test
public void testJsonWriteLimitEmbedded() throws Exception {
Response response =
WebClient.create(endPoint + TIKA_PATH + "/html").accept("application/json")
.header("writeLimit", "500")
.put(ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
Metadata metadata = JsonMetadata.fromJson(
new InputStreamReader(((InputStream) response.getEntity()),
StandardCharsets.UTF_8));
assertContains("embed2a.txt", metadata.get(TikaCoreProperties.TIKA_CONTENT));
assertContains("When in the Course", metadata.get(TikaCoreProperties.TIKA_CONTENT));
assertNotFound("declare the causes", metadata.get(TikaCoreProperties.TIKA_CONTENT));
assertEquals("Microsoft Office Word", metadata.get(OfficeOpenXMLExtended.APPLICATION));
assertTrue(metadata.get(TikaCoreProperties.CONTAINER_EXCEPTION)
.startsWith("org.apache.tika.exception.WriteLimitReachedException"));
assertNotFound("embed4.txt", metadata.get(TikaCoreProperties.TIKA_CONTENT));
}
@Test
public void testJsonNoThrowWriteLimitEmbedded() throws Exception {
Response response =
WebClient.create(endPoint + TIKA_PATH + "/html").accept("application/json")
.header("writeLimit", "500").header("throwOnWriteLimitReached", "false")
.put(ClassLoader.getSystemResourceAsStream(TEST_RECURSIVE_DOC));
Metadata metadata = JsonMetadata.fromJson(
new InputStreamReader(((InputStream) response.getEntity()),
StandardCharsets.UTF_8));
String txt = metadata.get(TikaCoreProperties.TIKA_CONTENT);
assertContains("embed2a.txt", txt);
assertContains("When in the Course", txt);
assertNotFound("declare the causes", txt);
assertEquals("Microsoft Office Word", metadata.get(OfficeOpenXMLExtended.APPLICATION));
assertEquals("true", metadata.get(TikaCoreProperties.WRITE_LIMIT_REACHED));
assertContains("<div class=\"embedded\" id=\"embed4.txt",
metadata.get(TikaCoreProperties.TIKA_CONTENT));
}
@Test
public void testWriteLimitInPDF() throws Exception {
int writeLimit = 10;
Response response = WebClient.create(endPoint + TIKA_PATH).accept("application/json")
.header("writeLimit", Integer.toString(writeLimit))
.put(ClassLoader.getSystemResourceAsStream(
"test-documents/testPDFTwoTextBoxes.pdf"));
assertEquals(200, response.getStatus());
Reader reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
Metadata metadata = JsonMetadata.fromJson(reader);
assertEquals("true", metadata.get(TikaCoreProperties.WRITE_LIMIT_REACHED));
}
}