Ejemplo n.º 1
0
  private <T, U extends RestClientRootUrl & RestClientHeaders & RestClientSupport>
      RestCallResult doFireRequestWithPersistentAuth(Request<T> request, Response<T> response) {
    RestCallResult result = RestCallResult.OTHER_ERROR;
    U restClient = request.getRestClient();

    try {
      Bundle bundle = accountPropertiesManager.getAuthToken().getResult();
      if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
        String authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);

        if (TextUtils.isEmpty(authToken)) {
          messageHelper.showError(
              ErrorType.REST_MAJOR, context.getString(R.string.rest_token_missing));
        } else {
          prepareAuthToken(restClient, version, authToken);
          T restResponse = request.fire();
          result = RestCallResult.SUCCESS;
          if (response != null) {
            response.onResponse(restResponse);
          }
        }
      } else if (bundle.containsKey(AccountManager.KEY_INTENT)) {
        Intent accountAuthenticatorResponse = bundle.getParcelable(AccountManager.KEY_INTENT);
        Intent editConnectionIntent = new Intent(Broadcasts.NO_CONNECTION_SPECIFIED);
        editConnectionIntent.putExtra(AccountManager.KEY_INTENT, accountAuthenticatorResponse);
        context.sendBroadcast(editConnectionIntent);

        result = RestCallResult.CONNECTION_ERROR;
      }
    } catch (ResourceAccessException | AuthenticatorException e) {
      messageHelper.showError(ErrorType.REST_MINOR, e);
      result = RestCallResult.CONNECTION_ERROR;
    } catch (HttpClientErrorException e) {
      String msg = e.getMessage();
      switch (e.getStatusCode()) {
        case UNAUTHORIZED:
          // ok, session id is not valid anymore - invalidate it
          accountPropertiesManager.setAuthToken(null);
          result = RestCallResult.AUTH_ERROR;
          break;
        case NOT_FOUND:
          messageHelper.showError(
              ErrorType.REST_MAJOR,
              context.getString(R.string.rest_url_missing, msg, restClient.getRootUrl()));
          break;
        default:
          messageHelper.showError(ErrorType.REST_MAJOR, messageHelper.createMessage(e));
          break;
      }
    } catch (Exception e) {
      messageHelper.showError(ErrorType.REST_MAJOR, e);
    }

    if (result == RestCallResult.SUCCESS) {
      messageHelper.resetMinorErrors();
    }

    return result;
  }
Ejemplo n.º 2
0
  private String getConnectionDetails() {
    String token = propertiesManager.peekAuthToken();
    if (token == null) {
      token = context.getString(R.string.rest_error_detail_token_missing);
    }

    StringBuilder certificate = new StringBuilder();
    String apiUrl = propertiesManager.getApiUrl();
    if (apiUrl != null) {
      try {
        URL url = new URL(apiUrl);
        CertHandlingStrategy certHandlingStrategy = propertiesManager.getCertHandlingStrategy();

        if (url.getProtocol().equalsIgnoreCase("https")) {
          certificate
              .append("\n")
              .append(
                  context.getString(
                      R.string.rest_error_detail_certificate_strategy,
                      certHandlingStrategy.toString()));
        }
        if (certHandlingStrategy == CertHandlingStrategy.TRUST_CUSTOM) {
          boolean hasCert = propertiesManager.getCertificateChain().length > 0;
          certificate
              .append("\n\t")
              .append(
                  context.getString(
                      hasCert
                          ? R.string.rest_error_detail_certificate_stored
                          : R.string.rest_error_detail_certificate_missing));
        }
      } catch (MalformedURLException e) {
        apiUrl = context.getString(R.string.rest_error_detail_malformed_url, e.getMessage());
      }
    } else {
      apiUrl = context.getString(R.string.rest_error_detail_missing_url);
    }
    return context.getString(
        R.string.rest_error_details,
        apiUrl,
        propertiesManager.getUsername(),
        token,
        certificate.toString());
  }
Ejemplo n.º 3
0
 @AfterInject
 public void init() {
   accountPropertiesManager.notifyAndRegisterListener(
       AccountProperty.VERSION,
       new PropertyChangedListener<Version>() {
         @Override
         public void onPropertyChange(Version property) {
           version = property;
         }
       });
 }