Skip to content

Commit 0f49ffe

Browse files
LucaDaiLucaD
authored andcommitted
TIKA-4510: Fix nondeterministic failures in LanguageResourceTest (#2360)
Co-authored-by: LucaD <lucadai.dev@gmail.com>
1 parent 208c50c commit 0f49ffe

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,50 @@
3838
public class LanguageResource {
3939
private static final Logger LOG = LoggerFactory.getLogger(LanguageResource.class);
4040

41+
// TIKA-4510: handle @PUT and @POST separately to avoid nondeterministic failures
4142
@PUT
43+
@Path("/stream")
44+
@Consumes("*/*")
45+
@Produces("text/plain")
46+
public String detectPutStream(final InputStream is) throws IOException {
47+
return detectStream(is);
48+
}
49+
4250
@POST
4351
@Path("/stream")
4452
@Consumes("*/*")
4553
@Produces("text/plain")
46-
public String detect(final InputStream is) throws IOException {
47-
String fileTxt = IOUtils.toString(is, UTF_8);
48-
LanguageResult language = new OptimaizeLangDetector()
49-
.loadModels()
50-
.detect(fileTxt);
51-
String detectedLang = language.getLanguage();
52-
LOG.info("Detecting language for incoming resource: [{}]", detectedLang);
53-
return detectedLang;
54+
public String detectPostStream(final InputStream is) throws IOException {
55+
return detectStream(is);
5456
}
5557

5658
@PUT
59+
@Path("/string")
60+
@Consumes("*/*")
61+
@Produces("text/plain")
62+
public String detectPutString(final String string) throws IOException {
63+
return detectString(string);
64+
}
65+
5766
@POST
5867
@Path("/string")
5968
@Consumes("*/*")
6069
@Produces("text/plain")
61-
public String detect(final String string) throws IOException {
70+
public String detectPostString(final String string) throws IOException {
71+
return detectString(string);
72+
}
73+
74+
private String detectStream(InputStream is) throws IOException {
75+
String fileTxt = IOUtils.toString(is, UTF_8);
76+
return detectString(fileTxt);
77+
}
78+
79+
private String detectString(String string) throws IOException {
6280
LanguageResult language = new OptimaizeLangDetector()
6381
.loadModels()
6482
.detect(string);
6583
String detectedLang = language.getLanguage();
6684
LOG.info("Detecting language for incoming resource: [{}]", detectedLang);
6785
return detectedLang;
6886
}
69-
7087
}

0 commit comments

Comments
 (0)