public byte[] URLtoByteArray(String location) throws IOException {
    Http.Options options = new Http.Options();

    options.setLocation(location);

    return URLtoByteArray(options);
  }
 public byte[] URLtoByteArray(Http.Options options) throws IOException {
   return URLtoByteArray(
       options.getLocation(),
       options.getMethod(),
       options.getHeaders(),
       options.getCookies(),
       options.getAuth(),
       options.getBody(),
       options.getFileParts(),
       options.getParts(),
       options.getResponse(),
       options.isFollowRedirects());
 }
Esempio n. 3
0
  protected String doTranslate(String fromLanguageId, String toLanguageId, String fromText)
      throws Exception {

    fromLanguageId = getMicrosoftLanguageId(fromLanguageId);
    toLanguageId = getMicrosoftLanguageId(toLanguageId);

    Http.Options options = new Http.Options();

    StringBundler sb = new StringBundler(7);

    sb.append("http://api.microsofttranslator.com/v2/Http.svc/Translate?");
    sb.append("text=");
    sb.append(HttpUtil.encodeURL(fromText));
    sb.append("&from=");
    sb.append(fromLanguageId);
    sb.append("&to=");
    sb.append(toLanguageId);

    options.setLocation(sb.toString());

    String accessToken = _microsoftTranslatorAuthenticator.getAccessToken();

    if (Validator.isNull(accessToken)) {
      throw new MicrosoftTranslatorException(_microsoftTranslatorAuthenticator.getError());
    }

    options.addHeader("Authorization", "Bearer " + accessToken);

    String text = HttpUtil.URLtoString(options);

    int x = text.indexOf(">") + 1;
    int y = text.indexOf("</string>", x);

    if ((x == -1) || (y == -1)) {
      x = text.indexOf("Message: ");
      y = text.indexOf("<", x);

      if ((x > -1) && (y > -1)) {
        text = text.substring(x, y);
      }

      throw new MicrosoftTranslatorException(text);
    }

    String toText = text.substring(x, y);

    toText = toText.trim();

    return StringUtil.replace(toText, CharPool.NEW_LINE, CharPool.SPACE);
  }
  protected void doInit() {
    if (Validator.isNull(_clientId)) {
      _clientId = PropsValues.MICROSOFT_TRANSLATOR_CLIENT_ID;
      _clientSecret = PropsValues.MICROSOFT_TRANSLATOR_CLIENT_SECRET;
    }

    try {
      Http.Options options = new Http.Options();

      StringBundler sb = new StringBundler(5);

      sb.append("grant_type=client_credentials&client_id=");
      sb.append(HttpUtil.encodeURL(_clientId));
      sb.append("&client_secret=");
      sb.append(HttpUtil.encodeURL(_clientSecret));
      sb.append("&scope=http://api.microsofttranslator.com");

      options.setBody(
          sb.toString(), ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED, StringPool.UTF8);

      options.setLocation(_URL);

      options.setPost(true);

      String jsonString = HttpUtil.URLtoString(options);

      JSONObject jsonObject = JSONFactoryUtil.createJSONObject(jsonString);

      _error = jsonObject.getString("error_description");

      if (_error != null) {
        if (_log.isInfoEnabled()) {
          _log.info("Unable to initialize access token: " + _error);
        }
      }

      _accessToken = jsonObject.getString("access_token");

      if (_accessToken != null) {
        _log.info("Access token " + _accessToken);
      }

      _initTime = System.currentTimeMillis();
    } catch (Exception e) {
      if (_log.isInfoEnabled()) {
        _log.info("Unable to initialize authentication token", e);
      }
    }
  }
  protected void doInit() throws Exception {
    if (_companyId > 0) {
      Company company = CompanyLocalServiceUtil.getCompany(_companyId);

      _domain = company.getMx();
      _userName = PrefsPropsUtil.getString(_companyId, PropsKeys.GOOGLE_APPS_USERNAME);
      _password = PrefsPropsUtil.getString(_companyId, PropsKeys.GOOGLE_APPS_PASSWORD);
    }

    Http.Options options = new Http.Options();

    options.addPart("accountType", "HOSTED");

    String emailAddress = _userName;

    if (!emailAddress.contains(StringPool.AT)) {
      emailAddress = _userName.concat(StringPool.AT).concat(_domain);
    }

    options.addPart("Email", emailAddress);

    options.addPart("Passwd", _password);
    options.addPart("service", "apps");
    options.setLocation("https://www.google.com/accounts/ClientLogin");
    options.setPost(true);

    String content = HttpUtil.URLtoString(options);

    Properties properties = PropertiesUtil.load(content);

    _error = properties.getProperty("Error");

    if (_error != null) {
      _log.info("Unable to initialize authentication token: " + _error);
    }

    _authenticationToken = properties.getProperty("Auth");

    if (_authenticationToken != null) {
      _log.info("Authentication token " + _authenticationToken);
    }

    _initTime = System.currentTimeMillis();
  }
Esempio n. 6
0
  protected Response doExecuteMethod(String url, String methodName, Object[] arguments)
      throws Exception {

    if (_log.isDebugEnabled()) {
      StringBundler sb = new StringBundler();

      sb.append("XML-RPC invoking ");
      sb.append(methodName);
      sb.append(" ");

      if (arguments != null) {
        for (int i = 0; i < arguments.length; i++) {
          sb.append(arguments[i]);

          if (i < (arguments.length - 1)) {
            sb.append(", ");
          }
        }
      }

      _log.debug(sb.toString());
    }

    String requestXML = XmlRpcParser.buildMethod(methodName, arguments);

    Http.Options options = new Http.Options();

    if (_HTTP_HEADER_VERSION_VERBOSITY_DEFAULT) {
    } else if (_HTTP_HEADER_VERSION_VERBOSITY_PARTIAL) {
      options.addHeader(HttpHeaders.USER_AGENT, ReleaseInfo.getName());
    } else {
      options.addHeader(HttpHeaders.USER_AGENT, ReleaseInfo.getServerInfo());
    }

    options.setBody(requestXML, ContentTypes.TEXT_XML, StringPool.UTF8);
    options.setLocation(url);
    options.setPost(true);

    String responseXML = HttpUtil.URLtoString(options);

    return XmlRpcParser.parseResponse(responseXML);
  }