use of org.apache.cxf.jaxrs.client.WebClient in project tika by apache.
the class JoshuaNetworkTranslator method translate.
/**
* <p>Initially then check if the source language has been provided.
* If no source language (or a null value) has been provided then
* we make an attempt to guess the source using Tika's
* {@link org.apache.tika.langdetect.OptimaizeLangDetector}. If we
* are still unable to guess the language then we return the source
* text.</p>
*
* <p>We then process the input text into a new string consisting of
* sentences, one per line e.g. insert \n between the presence of '.'</p>
*
* @see org.apache.tika.language.translate.Translator#translate
* (java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException {
//create networkURI
if (!networkServer.endsWith("/")) {
networkURI = networkServer + "/" + targetLanguage;
} else {
networkURI = networkServer + targetLanguage;
}
if (!this.isAvailable())
return text;
//make an attempt to guess language if one is not provided.
if (sourceLanguage == null)
sourceLanguage = detectLanguage(text).getLanguage();
//process input text into sentences, one per line
// e.g. insert \n between the presence of '.'
StringBuilder sb = new StringBuilder(text);
int i = 0;
while ((i = sb.indexOf(".", i + 1)) != -1) {
sb.replace(i, i + 1, "\n");
}
String inputText = sb.toString();
WebClient client;
final List<Object> providers = new ArrayList<>();
JacksonJsonProvider jacksonJsonProvider = new JacksonJsonProvider();
providers.add(jacksonJsonProvider);
client = WebClient.create(networkURI, providers);
ObjectMapper requestMapper = new ObjectMapper();
ObjectNode jsonNode = requestMapper.createObjectNode();
jsonNode.put("inputLanguage", sourceLanguage);
jsonNode.put("inputText", inputText);
//make the reuest
Response response = client.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(jsonNode);
BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) response.getEntity(), UTF_8));
String line;
StringBuilder responseText = new StringBuilder();
while ((line = reader.readLine()) != null) {
responseText.append(line);
}
try {
ObjectMapper responseMapper = new ObjectMapper();
JsonNode jsonResp = responseMapper.readTree(responseText.toString());
if (jsonResp.findValuesAsText("outputText") != null) {
return jsonResp.findValuesAsText("outputText").get(0);
} else {
throw new TikaException(jsonResp.findValue("message").get(0).asText());
}
} catch (JsonParseException e) {
throw new TikaException("Error requesting translation from '" + sourceLanguage + "' to '" + targetLanguage + "', JSON response " + "from Joshua REST Server is not well formatted: " + responseText.toString());
}
}
use of org.apache.cxf.jaxrs.client.WebClient in project ddf by codice.
the class ConfluenceSourceTest method testCachedAvailableExpired.
@Test
public void testCachedAvailableExpired() throws Exception {
WebClient mockClient = mock(WebClient.class);
when(factory.getWebClient()).thenReturn(mockClient);
Response mockResponse = mock(Response.class);
when(mockClient.head()).thenReturn(mockResponse);
when(mockResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
confluence.setAvailabilityPollInterval(5);
assertThat(confluence.isAvailable(), is(true));
Thread.sleep(10);
assertThat(confluence.isAvailable(), is(false));
}
use of org.apache.cxf.jaxrs.client.WebClient in project ddf by codice.
the class ConfluenceSourceTest method testAvailibilityConnectionException.
@Test
public void testAvailibilityConnectionException() throws Exception {
WebClient mockClient = mock(WebClient.class);
when(factory.getWebClient()).thenReturn(mockClient);
when(mockClient.head()).thenThrow(new RuntimeException("Connection exception"));
assertThat(confluence.isAvailable(), is(false));
}
use of org.apache.cxf.jaxrs.client.WebClient in project ddf by codice.
the class ConfluenceSourceTest method testAvaliabilitySourceMonitor.
@Test
public void testAvaliabilitySourceMonitor() throws Exception {
SourceMonitor monitor = mock(SourceMonitor.class);
WebClient mockClient = mock(WebClient.class);
when(factory.getWebClient()).thenReturn(mockClient);
Response mockResponse = mock(Response.class);
when(mockClient.head()).thenReturn(mockResponse);
when(mockResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
assertThat(confluence.isAvailable(monitor), is(true));
verify(monitor).setAvailable();
Thread.sleep(10);
assertThat(confluence.isAvailable(monitor), is(false));
verify(monitor).setUnavailable();
}
use of org.apache.cxf.jaxrs.client.WebClient in project tika by apache.
the class Lingo24LangDetector method getAllLanguages.
/**
* Load the supported languages from the <a href="https://developer.lingo24.com/premium-machine-translation-api">Premium MT API</a>.
* Support is continually expanding.
* @return <code>Set<String></code> of supported languages.
*/
private Set<String> getAllLanguages() {
Set<String> languages = new HashSet<>();
if (!isAvailable) {
return languages;
}
WebClient _client = null;
try {
_client = WebClient.create(LINGO24_TRANSLATE_URL_BASE + LINGO24_SOURCELANG_ACTION);
Response response = _client.accept(MediaType.APPLICATION_JSON).query("user_key", userKey).get();
String json = response.readEntity(String.class);
JsonArray jsonArray = new JsonParser().parse(json).getAsJsonObject().get("source_langs").getAsJsonArray();
for (JsonElement jsonElement : jsonArray) {
languages.add(jsonElement.getAsJsonArray().get(0).getAsString());
}
} catch (Throwable e) {
LOG.warn("problem detecting", e);
} finally {
if (_client != null) {
_client.close();
}
}
return languages;
}
Aggregations