From e04193edd8a0a1a1e38617d1d65d8b2ed4da4e93 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Thu, 3 Nov 2016 17:58:05 +0900 Subject: [PATCH] Fix degrade for API access Signed-off-by: Hiroshi Miura --- .../omegat/textra/TextraApiClient.java | 26 ++++++++---------- .../omegat/textra/TextraOptions.java | 27 +++++++++++++++---- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/main/java/tokyo/northside/omegat/textra/TextraApiClient.java b/src/main/java/tokyo/northside/omegat/textra/TextraApiClient.java index c455ccd..a204953 100644 --- a/src/main/java/tokyo/northside/omegat/textra/TextraApiClient.java +++ b/src/main/java/tokyo/northside/omegat/textra/TextraApiClient.java @@ -41,20 +41,6 @@ public class TextraApiClient { private HttpPost httpPost; private RequestConfig requestConfig; - /** - * Constructor prepares httpClient object. - */ - public TextraApiClient() { - requestConfig = RequestConfig.custom() - .setConnectTimeout(CONNECTION_TIMEOUT) - .setSocketTimeout(SO_TIMEOUT) - .build(); - httpClient = HttpClientBuilder.create() - .setDefaultRequestConfig(requestConfig) - .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) - .build(); - } - /** * Try authenticate OAuth with given key/secret. * @param options connectivity options. @@ -67,6 +53,14 @@ public void authenticate(final TextraOptions options, final String text) { private void authenticate(final String url, final String apiUsername, final String apiKey, final String apiSecret, final String text) { + httpClient = HttpClientBuilder.create() + .setDefaultRequestConfig(requestConfig) + .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) + .build(); + requestConfig = RequestConfig.custom() + .setConnectTimeout(CONNECTION_TIMEOUT) + .setSocketTimeout(SO_TIMEOUT) + .build(); httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); OAuthConsumer consumer = new CommonsHttpOAuthConsumer(apiKey, apiSecret); @@ -76,8 +70,9 @@ private void authenticate(final String url, final String apiUsername, final Stri postParameters.add(new BasicNameValuePair("name", apiUsername)); postParameters.add(new BasicNameValuePair("type", "json")); postParameters.add(new BasicNameValuePair("text", text)); + try { - new UrlEncodedFormEntity(postParameters, "UTF-8"); + httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8")); } catch (UnsupportedEncodingException ex) { LOGGER.info("Encoding error."); } @@ -115,6 +110,7 @@ public String executeTranslation() { try (BufferedInputStream bis = new BufferedInputStream(respBodyStream)) { LOGGER.debug("Http response status: " + respStatus); String rsp = IOUtils.toString(bis); + LOGGER.info("response body: " + rsp); JSONObject jobj = new JSONObject(rsp); JSONObject resultset = jobj.getJSONObject("resultset"); result = resultset.getJSONObject("result").getString("text"); diff --git a/src/main/java/tokyo/northside/omegat/textra/TextraOptions.java b/src/main/java/tokyo/northside/omegat/textra/TextraOptions.java index 10f82bf..dc537d6 100644 --- a/src/main/java/tokyo/northside/omegat/textra/TextraOptions.java +++ b/src/main/java/tokyo/northside/omegat/textra/TextraOptions.java @@ -193,12 +193,12 @@ public String getApikey() { /** * Setter of Apikey. - * @param key to set. + * @param apiKey to set. * @return this object. */ @SuppressWarnings("HiddenField") - public TextraOptions setApikey(final String key) { - this.apikey = key; + public TextraOptions setApikey(final String apiKey) { + this.apikey = apiKey; return this; } @@ -271,16 +271,33 @@ public boolean isMode(final String name) { /** * Set language options. + *

+ * Format language string as like "zh-CN", "en", "ja" + * to be a proper case. + * Because OmegaT may give language in capital letter such as "EN". + *

* @param sLang source language. * @param tLang target language. * @return this object. */ public TextraOptions setLang(final String sLang, final String tLang) { - this.sourceLang = sLang; - this.targetLang = tLang; + this.sourceLang = formatLang(sLang); + this.targetLang = formatLang(tLang); return this; } + private String formatLang(final String lang) { + String result; + if (lang.contains("-")) { + int index = lang.indexOf("-"); + result = lang.substring(0, index).toLowerCase() + lang + .substring(index, lang.length() - index ).toUpperCase(); + } else { + result = lang.toLowerCase(); + } + return result; + } + /** * Get source language string. * @return source language.