Java 类org.apache.http.impl.client.ContentEncodingHttpClient 实例源码

项目:NoJPA    文件:LessismoreApiTranslateServiceImpl.java   
@Override
public String translate(String uniqueID, String attributeNameForDebug, String srcLang2Char, String destLang2Char, String originalText) throws Exception{

    long start = System.currentTimeMillis();
    if(originalText == null || srcLang2Char == null || destLang2Char == null){
        if(originalText != null) {
            log.warn("We will return null - because we was called with null, in translate[uniqueID("+ uniqueID +")srcLang2Char(" + srcLang2Char + "), destLang2Char(" + destLang2Char + "), originalText(" + originalText + ")]");
        }
        return null;
    }


    StringBuilder cache = new StringBuilder((int) (originalText.length() * 1.4));



    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("key", lessismoreAPIkey));
    qparams.add(new BasicNameValuePair("source", srcLang2Char));
    qparams.add(new BasicNameValuePair("target", destLang2Char));
    qparams.add(new BasicNameValuePair("q", originalText));

    HttpPost httpPost = null;
    if(gglTransUrl == null) {
        httpPost = new HttpPost("http://vsrvj71.less-is-more.dk/translate");
    } else {
        httpPost = new HttpPost(gglTransUrl);
    }
    httpPost.setHeader(new BasicHeader("X-HTTP-Method-Override", "GET"));

    httpPost.setEntity(new UrlEncodedFormEntity(qparams, "UTF-8"));

    HttpClient client = new ContentEncodingHttpClient();
    HttpResponse httpResponse = client.execute(httpPost);
    BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
    String line;
    while ((line = rd.readLine()) != null) {
        cache.append(line);
    }

    JsonNode jsonNode = new ObjectMapper().reader().readTree(cache.toString());

    long end = System.currentTimeMillis();
    String translatedText = jsonNode.get("translatedText").textValue();
    log.debug("Running translate: uniqueID("+ uniqueID +") TIME["+ (end - start) +"] src("+ srcLang2Char +") dest("+ destLang2Char +") originalText("+ originalText +") translated_text(" + translatedText +")");
    return translatedText;
}