コード例 #1
0
  /**
   * Parse the login page for the GALX id.
   *
   * @return GALX id
   * @throws GoogleAuthenticatorException
   */
  private String galx() throws GoogleAuthenticatorException {
    String galx = null;
    HttpGet get;
    try {
      DataConfiguration config = GoogleConfigurator.getConfiguration();

      Pattern pattern =
          Pattern.compile(config.getString("google.auth.reGalx"), Pattern.CASE_INSENSITIVE);
      get = new HttpGet(config.getString("google.auth.loginUrl"));

      HttpResponse response = _httpClient.execute(get);
      String html = GoogleUtils.toString(response.getEntity().getContent());
      // System.out.println("Page is : " + html);
      get.releaseConnection();
      Matcher matcher = pattern.matcher(html);
      if (matcher.find()) {
        galx = matcher.group(1);
      }

      if (galx == null) {
        throw new GoogleAuthenticatorException("Cannot parse GALX!");
      }
    } catch (ConfigurationException ex) {
      throw new GoogleAuthenticatorException(ex);
    } catch (ClientProtocolException ex) {
      throw new GoogleAuthenticatorException(ex);
    } catch (IOException ex) {
      throw new GoogleAuthenticatorException(ex);
    }

    // printCookies();

    return galx;
  }
コード例 #2
0
  /**
   * Login in Google.
   *
   * @param galx The GALX id
   * @return <code>true</code> if login was successful
   * @throws GoogleAuthenticatorException
   */
  private boolean login(String galx) throws GoogleAuthenticatorException {
    _isLoggedIn = false;

    try {
      DataConfiguration config = GoogleConfigurator.getConfiguration();

      HttpPost httpPost = new HttpPost(config.getString("google.auth.loginAuthenticate"));
      GoogleUtils.setupHttpRequestDefaults(httpPost);
      httpPost.setEntity(new UrlEncodedFormEntity(setupFormInputs(config, galx), HTTP.UTF_8));
      HttpResponse response = _httpClient.execute(httpPost);
      GoogleUtils.toString(response.getEntity().getContent());
      httpPost.releaseConnection();
    } catch (UnsupportedEncodingException ex) {
      throw new GoogleAuthenticatorException(ex);
    } catch (ClientProtocolException ex) {
      throw new GoogleAuthenticatorException(ex);
    } catch (IOException ex) {
      throw new GoogleAuthenticatorException(ex);
    } catch (ConfigurationException ex) {
      throw new GoogleAuthenticatorException(ex);
    }

    _isLoggedIn = true;
    return _isLoggedIn;
  }