Search in sources :

Example 56 with WebClient

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());
    }
}
Also used : TikaException(org.apache.tika.exception.TikaException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException) WebClient(org.apache.cxf.jaxrs.client.WebClient) Response(javax.ws.rs.core.Response) BufferedReader(java.io.BufferedReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 57 with WebClient

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));
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) Response(javax.ws.rs.core.Response) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 58 with WebClient

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));
}
Also used : WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 59 with WebClient

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();
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) Response(javax.ws.rs.core.Response) SourceMonitor(ddf.catalog.source.SourceMonitor) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 60 with WebClient

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;
}
Also used : Response(javax.ws.rs.core.Response) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) WebClient(org.apache.cxf.jaxrs.client.WebClient) HashSet(java.util.HashSet) JsonParser(com.google.gson.JsonParser)

Aggregations

WebClient (org.apache.cxf.jaxrs.client.WebClient)723 Test (org.junit.Test)400 Response (javax.ws.rs.core.Response)351 URL (java.net.URL)198 HashMap (java.util.HashMap)100 Book (org.apache.cxf.systest.jaxrs.security.Book)94 ArrayList (java.util.ArrayList)88 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)87 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)77 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)60 Bus (org.apache.cxf.Bus)48 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)46 Test (org.testng.annotations.Test)46 Form (javax.ws.rs.core.Form)44 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)42 JwtClaims (org.apache.cxf.rs.security.jose.jwt.JwtClaims)40 JwtAuthenticationClientFilter (org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationClientFilter)32 InputStream (java.io.InputStream)28 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)28 Document (org.w3c.dom.Document)27