|
38 | 38 | public class LanguageResource { |
39 | 39 | private static final Logger LOG = LoggerFactory.getLogger(LanguageResource.class); |
40 | 40 |
|
| 41 | + // TIKA-4510: handle @PUT and @POST separately to avoid nondeterministic failures |
41 | 42 | @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 | + |
42 | 50 | @POST |
43 | 51 | @Path("/stream") |
44 | 52 | @Consumes("*/*") |
45 | 53 | @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); |
54 | 56 | } |
55 | 57 |
|
56 | 58 | @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 | + |
57 | 66 | @POST |
58 | 67 | @Path("/string") |
59 | 68 | @Consumes("*/*") |
60 | 69 | @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 { |
62 | 80 | LanguageResult language = new OptimaizeLangDetector() |
63 | 81 | .loadModels() |
64 | 82 | .detect(string); |
65 | 83 | String detectedLang = language.getLanguage(); |
66 | 84 | LOG.info("Detecting language for incoming resource: [{}]", detectedLang); |
67 | 85 | return detectedLang; |
68 | 86 | } |
69 | | - |
70 | 87 | } |
0 commit comments