@Override public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!this.isAvailable) return text; Response response = client.accept(MediaType.APPLICATION_JSON) .query("key", apiKey).query("source", sourceLanguage) .query("target", targetLanguage).query("q", text).get(); BufferedReader reader = new BufferedReader(new InputStreamReader( (InputStream) response.getEntity(), UTF_8)); String line = null; StringBuffer responseText = new StringBuffer(); while ((line = reader.readLine()) != null) { responseText.append(line); } ObjectMapper mapper = new ObjectMapper(); JsonNode jsonResp = mapper.readTree(responseText.toString()); return jsonResp.findValuesAsText("translatedText").get(0); }
@Override public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException { if (!this.isAvailable) return text; Response response = client.accept(MediaType.APPLICATION_JSON) .query("user_key", userKey).query("source", sourceLanguage) .query("target", targetLanguage).query("q", text).get(); BufferedReader reader = new BufferedReader(new InputStreamReader( (InputStream) response.getEntity(), UTF_8)); String line = null; StringBuffer responseText = new StringBuffer(); while ((line = reader.readLine()) != null) { responseText.append(line); } ObjectMapper mapper = new ObjectMapper(); JsonNode jsonResp = mapper.readTree(responseText.toString()); if (jsonResp.findValuesAsText("errors").isEmpty()) { return jsonResp.findValuesAsText("translation").get(0); } else { throw new TikaException(jsonResp.findValue("errors").get(0).asText()); } }
/** * 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; }
.accept("application/json").query("handler", "ignore") .post(attachmentPart); reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
private static boolean checkReplica(String address) { try { Response r = WebClient.create(address).query("_wadl").get(); return r.getStatus() == 200; } catch (Exception ex) { return false; } }
@Test public void testThatValidationConstraintsAreViolatedWhenBookNameIsNotSet() { final Response r = createWebClient("/bookstore/book").query("id", "124").get(); assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus()); }
@Test public void testThatNoValidationConstraintsAreViolated() { final Response r = createWebClient("/bookstore/books").query("page", "2").get(); assertEquals(Status.OK.getStatusCode(), r.getStatus()); }
@Test public void testThatValidationConstraintsAreViolatedWhenBookDoesNotExist() { final Response r = createWebClient("/bookstore/book").query("id", "3333").get(); assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), r.getStatus()); }
@Test public void testThatNoValidationConstraintsAreViolatedWhenBookIdIsSet() { final Response r = createWebClient("/bookstore/book").query("id", "123").get(); assertEquals(Status.OK.getStatusCode(), r.getStatus()); }
@Test public void testThatMinValidationFails() { final Response r = createWebClient("/bookstore/books").query("page", "0").get(); assertEquals(Status.BAD_REQUEST.getStatusCode(), r.getStatus()); }
@Test public void testQueryParameter() { Response r = createWebClient(getBasePath() + "/books/param", "text/plain").query("fieldValue", "queryParam").get(); assertEquals("queryParam", r.readEntity(String.class)); assertEquals(Response.Status.OK.getStatusCode(), r.getStatus()); r = createWebClient(getBasePath() + "/books/param2", "text/plain").query("fieldValue2", "queryParam").get(); assertEquals("queryParam", r.readEntity(String.class)); assertEquals(Response.Status.OK.getStatusCode(), r.getStatus()); }
private void doTestGetBookWithResourceContext(String address) throws Exception { WebClient wc = WebClient.create(address); wc.accept("application/xml"); wc.query("bookid", "12345"); wc.query("bookname", "bookcontext"); Book2 book = wc.get(Book2.class); assertEquals(12345L, book.getId()); assertEquals("bookcontext", book.getName()); }
@Test public void testGetBookJsonp() throws Exception { String url = "http://localhost:" + PORT + "/the/jsonp/books/123"; WebClient client = WebClient.create(url); client.accept("application/json, application/x-javascript"); client.query("_jsonp", "callback"); Response r = client.get(); assertEquals("application/x-javascript", r.getMetadata().getFirst("Content-Type")); assertEquals("callback({\"Book\":{\"id\":123,\"name\":\"CXF in Action\"}});", IOUtils.readStringFromStream((InputStream)r.getEntity())); }
@Test public void testSearchBook123WithWebClient() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/books/search"; WebClient client = WebClient.create(address); Book b = client.query("_s", "name==CXF*;id=ge=123;id=lt=124").get(Book.class); assertEquals(b.getId(), 123L); }
@Test public void testSearchBook123WithWebClient() throws Exception { String address = "http://localhost:" + PORT + "/bookstore/books/search"; WebClient client = WebClient.create(address); Book b = client.query("$filter", "name eq 'CXF*' and id ge 123 and id lt 124").get(Book.class); assertEquals(b.getId(), 123L); } }
@Test public void testGetBookWithNameInQuery() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/name-in-query"; WebClient wc = WebClient.create(endpointAddress); String name = "Many spaces"; wc.query("name", name); Book b = wc.get(Book.class); assertEquals(name, b.getName()); }
@Test public void testGetGenericBook() throws Exception { String baseAddress = "http://localhost:" + PORT + "/the/thebooks8/books"; WebClient wc = WebClient.create(baseAddress); Long id = wc.type("application/xml").accept("text/plain").post(new Book("CXF", 1L), Long.class); assertEquals(Long.valueOf(1), id); Book book = wc.replaceHeader("Accept", "application/xml").query("id", 1L).get(Book.class); assertEquals("CXF", book.getName()); }
@Test public void testGetBookXSLTXml() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/the/thebooks5/bookstore/books/xslt"; WebClient wc = WebClient.create(endpointAddress); wc.accept("application/xml").path(666).matrix("name2", 2).query("name", "Action - "); Book b = wc.get(Book.class); assertEquals(666, b.getId()); assertEquals("CXF in Action - 2", b.getName()); }