public byte[] URLtoByteArray(String location, boolean post) throws IOException {

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

    options.setLocation(location);
    options.setPost(post);

    return URLtoByteArray(options);
  }
  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);
      }
    }
  }
예제 #3
0
  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();
  }
예제 #4
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);
  }