@Override
 public Urllib login() throws LoginException, BankException, IOException {
   HttpResponse httpResponse = null;
   try {
     LoginPackage lp = preLogin();
     httpResponse =
         urlopen.openAsHttpResponse(
             lp.getLoginTarget(),
             new StringEntity(
                 objectAsJson(new PersonalCodeRequest(getUsername(), getPassword())), HTTP.UTF_8),
             true);
     int responseCode = httpResponse.getStatusLine().getStatusCode();
     if (responseCode == 201) {
       return urlopen;
     } else if (responseCode == 401 || responseCode == 400) {
       throw new LoginException(res.getText(R.string.invalid_username_password).toString());
     } else if (responseCode == 503) {
       String errorMessage = null;
       try {
         ErrorResponse er =
             readJsonValue(httpResponse.getEntity().getContent(), ErrorResponse.class);
         StringBuilder sb = new StringBuilder();
         for (List<ErrorMessage> ems : er.getErrorMessages().values()) {
           for (ErrorMessage em : ems) {
             sb.append(Html.fromHtml(em.getMessage()).toString()).append("\n");
           }
         }
         errorMessage = sb.toString().trim();
       } catch (BankException e) {
         // Ignore json parse errors and show generic server error message
       }
       throw new BankException(
           TextUtils.isEmpty(errorMessage)
               ? context.getString(R.string.server_error_try_again)
               : errorMessage);
     } else {
       throw new BankException("");
     }
   } finally {
     if (httpResponse != null) {
       HttpEntity httpEntity = httpResponse.getEntity();
       if (httpEntity != null) {
         try {
           httpEntity.consumeContent();
         } catch (IOException e) {
           throw new BankException(e.getMessage(), e);
         }
       }
     }
   }
 }
 public Urllib login() throws LoginException, BankException {
   try {
     LoginPackage lp = preLogin();
     response = urlopen.open(lp.getLoginTarget(), lp.getPostData());
     if (response.contains("Felaktigt anv")) {
       throw new LoginException(res.getText(R.string.invalid_username_password).toString());
     }
   } catch (ClientProtocolException e) {
     throw new BankException(e.getMessage());
   } catch (IOException e) {
     e.printStackTrace();
     throw new BankException(e.getMessage());
   }
   return urlopen;
 }
Exemple #3
0
  public SessionPackage getSessionPackage(Context context) {
    String preloader = "Error...";
    try {
      preloader = IOUtils.toString(context.getResources().openRawResource(R.raw.loading));
    } catch (NotFoundException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    try {
      LoginPackage lp = preLogin();
      if (lp == null) {
        throw new BankException(
            "No automatic login for this bank. preLogin() is not implemented or has failed.");
      }
      // TODO: Skip the form submission. Login using Bank.login(...) and transfer cookies to
      // webview. The user is now logged in
      //      and can me directed to any page.
      String html =
          String.format(
              preloader,
              "function go(){document.getElementById('submitform').submit(); }", // Javascript
                                                                                 // function
              Helpers.renderForm(lp.getLoginTarget(), lp.getPostData())
                  + "<script type=\"text/javascript\">setTimeout('go()', 1000);</script>" // HTML
              );

      CookieStore cookies = urlopen.getHttpclient().getCookieStore();
      return new SessionPackage(html, cookies);
    } catch (ClientProtocolException e) {
      Log.e(TAG, e.getMessage());
    } catch (IOException e) {
      Log.e(TAG, e.getMessage());
    } catch (BankException e) {
      Log.e(TAG, e.getMessage());
    }
    String html =
        String.format(
            preloader,
            String.format(
                "function go(){window.location=\"%s\" }", this.URL), // Javascript function
            "<script type=\"text/javascript\">setTimeout('go()', 1000);</script>" // HTML
            );
    return new SessionPackage(html, null);
  }
Exemple #4
0
  @Override
  public Urllib login() throws LoginException, BankException {
    try {
      LoginPackage lp = preLogin();
      String response = urlopen.open(lp.getLoginTarget(), lp.getPostData());

      Matcher matcher = reLoginError.matcher(response);
      if (matcher.find()) {
        throw new LoginException(Html.fromHtml(matcher.group(1)).toString().trim());
      }
    } catch (ClientProtocolException e) {
      throw new BankException(e.getMessage());
    } catch (IOException e) {
      throw new BankException(e.getMessage());
    }
    return urlopen;
  }