/**
   * Translate text
   *
   * @param text The text to be translated
   * @param target The target language code, for example "en"
   * @param source The source language code, for example "nl". Optional
   * @return
   * @throws Exception
   */
  public String translate(
      @Name("text") String text,
      @Name("target") String target,
      @Name("source") @Required(false) String source)
      throws Exception {
    String key = getState().get("key", String.class);
    if (key == null) {
      throw new Exception(
          "No valid API Key set. "
              + "Google Translate API is a paid service "
              + "and requires an API Key for billing.");
    }

    String url =
        TRANSLATE_API_URL
            + "?q="
            + URLEncoder.encode(text, "UTF-8")
            + "&key="
            + URLEncoder.encode(key, "UTF-8")
            + "&target="
            + URLEncoder.encode(target, "UTF-8");
    if (source != null) {
      url += "&source=" + URLEncoder.encode(source, "UTF-8");
    }
    String resp = HttpUtil.get(url);

    return resp;
  }
Exemple #2
0
  private ObjectNode exchangeCodeForAuthorization(final String code) throws IOException {
    final Map<String, String> params = new HashMap<String, String>();
    params.put("code", code);
    params.put("client_id", CLIENT_ID);
    params.put("client_secret", CLIENT_SECRET);
    params.put("redirect_uri", REDIRECT_URI);
    params.put("grant_type", "authorization_code");
    final String res = HttpUtil.postForm(OAUTH_URI + "/token", params);

    final ObjectNode json = MAPPER.readValue(res, ObjectNode.class);
    return json;
  }
Exemple #3
0
  private String createAuthorizationUrl() throws IOException {
    final Map<String, String> params = new HashMap<String, String>();
    params.put("scope", SCOPE);
    params.put("redirect_uri", REDIRECT_URI);
    params.put("response_type", "code");
    params.put("access_type", "offline");
    params.put("client_id", CLIENT_ID);
    params.put("approval_prompt", "force");
    final String url = HttpUtil.appendQueryParams(OAUTH_URI + "/auth", params);

    return url;
  }
  /**
   * Evaluate given expression For example expr="2.5 + 3 / sqrt(16)" will return "3.25"
   *
   * @param expr
   * @return result
   * @throws Exception
   */
  public String eval(@Name("expr") String expr) throws Exception {
    String url = CALC_API_URL + "?q=" + URLEncoder.encode(expr, "UTF-8");
    String resp = HttpUtil.get(url);

    // the field names in resp are not enclosed by quotes :(
    resp = resp.replaceAll("lhs:", "\"lhs\":");
    resp = resp.replaceAll("rhs:", "\"rhs\":");
    resp = resp.replaceAll("error:", "\"error\":");
    resp = resp.replaceAll("icc:", "\"icc\":");

    ObjectMapper mapper = JOM.getInstance();
    ObjectNode json = mapper.readValue(resp, ObjectNode.class);

    String error = json.get("error").asText();
    if (error != null && !error.isEmpty()) {
      throw new Exception(error);
    }

    String rhs = json.get("rhs").asText();
    return rhs;
  }
Exemple #5
0
 private void sendAuthorizationToAgent(
     final String agentUrl, final String agentMethod, final ObjectNode auth) throws IOException {
   final JSONRequest rpcRequest = new JSONRequest(agentMethod, auth);
   HttpUtil.post(agentUrl, rpcRequest.toString());
 }