Skip to content
This repository has been archived by the owner on Jul 29, 2023. It is now read-only.

Commit

Permalink
Fix degrade for API access
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Nov 3, 2016
1 parent 5ba28c8 commit e04193e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
26 changes: 11 additions & 15 deletions src/main/java/tokyo/northside/omegat/textra/TextraApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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.");
}
Expand Down Expand Up @@ -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");
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/tokyo/northside/omegat/textra/TextraOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -271,16 +271,33 @@ public boolean isMode(final String name) {

/**
* Set language options.
* <p>
* 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".
* </p>
* @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.
Expand Down

0 comments on commit e04193e

Please sign in to comment.