Example usage for android.accounts NetworkErrorException NetworkErrorException

List of usage examples for android.accounts NetworkErrorException NetworkErrorException

Introduction

In this page you can find the example usage for android.accounts NetworkErrorException NetworkErrorException.

Prototype

public NetworkErrorException(Throwable cause) 

Source Link

Usage

From source file:org.smilec.smile.bu.AbstractBaseManager.java

protected static void checkConnection(Context context) throws NetworkErrorException {

    boolean isConnected = DeviceUtil.isConnected(context);

    if (!isConnected) {
        throw new NetworkErrorException("Connection unavailable");
    }//w ww  .  j av a  2  s . c  om

}

From source file:ro.weednet.contactssync.client.NetworkUtilities.java

/**
 * Connects to the Sync test server, authenticates the provided
 * username and password.//  www  .j a  v a 2  s.  c om
 * 
 * @param username
 *            The server account username
 * @param password
 *            The server account password
 * @return String The authentication token returned by the server (or null)
 * @throws NetworkErrorException 
 */
public boolean checkAccessToken() throws NetworkErrorException {
    //TODO: try to re-use timeout values (or remove preferences options
    //   params.putInt("timeout", ContactsSync.getInstance().getConnectionTimeout() * 1000);

    if (!mSession.isOpened()) {
        return false;
    }

    try {
        Request request = new Request(mSession, "me/permissions");
        Response response = request.executeAndWait();

        if (response.getError() != null) {
            if (response.getError().getErrorCode() == 190) {
                return false;
            } else {
                throw new NetworkErrorException(response.getError().getErrorMessage());
            }
        }

        JSONObject json = response.getGraphObject().getInnerJSONObject();
        JSONObject permissions = json.getJSONArray("data").getJSONObject(0);

        for (int i = 0; i < Authenticator.REQUIRED_PERMISSIONS.length; i++) {
            if (permissions.isNull(Authenticator.REQUIRED_PERMISSIONS[i])
                    || permissions.getInt(Authenticator.REQUIRED_PERMISSIONS[i]) == 0) {
                return false;
            }
        }
    } catch (FacebookException e) {
        throw new NetworkErrorException(e.getMessage());
    } catch (JSONException e) {
        throw new NetworkErrorException(e.getMessage());
    }

    return true;
}

From source file:eu.masconsult.bgbanking.accounts.AccountAuthenticator.java

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
        Bundle options) throws NetworkErrorException {
    Log.v(TAG, "getAuthToken(account: " + account + ", authTokenType: " + authTokenType + ")");

    Bank bank = Bank.fromAccountType(context, account.type);
    if (bank == null) {
        throw new IllegalArgumentException("unsupported account type " + account.type);
    }// w  ww . j  a  va  2  s  . c  o m
    if (!Constants.getAuthorityType(context).equals(authTokenType)) {
        throw new IllegalArgumentException("unsupported authTOkenType " + authTokenType);
    }

    final Intent intent = new Intent(context, LoginActivity.class);
    intent.putExtra(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    intent.putExtra(KEY_ACCOUNT_NAME, account.name);
    intent.putExtra(KEY_ACCOUNT_TYPE, account.type);

    String password = accountManager.getPassword(account);
    try {
        String authToken = bank.getClient().authenticate(account.name, password);
        Log.v(TAG, "obtained auth token " + authToken);

        if (authToken == null) {
            throw new AuthenticationException("no authToken");
        }

        // store the new auth token and return it
        accountManager.setAuthToken(account, authTokenType, authToken);
        intent.putExtra(KEY_AUTHTOKEN, authToken);
        return intent.getExtras();
    } catch (ParseException e) {
        Log.w(TAG, "ParseException", e);
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_ERROR_CODE, 1);
        bundle.putString(KEY_ERROR_MESSAGE, e.getMessage());
        return bundle;
    } catch (IOException e) {
        Log.w(TAG, "IOException", e);
        throw new NetworkErrorException(e);
    } catch (CaptchaException e) {
        Log.w(TAG, "CaptchaException", e);
        // We need human to verify captcha
        final Bundle bundle = new Bundle();
        bundle.putParcelable(AccountManager.KEY_INTENT, intent);
        intent.putExtra(KEY_CAPTCHA_URI, e.getCaptchaUri());
        return bundle;
    } catch (AuthenticationException e) {
        Log.w(TAG, "AuthenticationException", e);
        // we need new credentials
        final Bundle bundle = new Bundle();
        bundle.putParcelable(AccountManager.KEY_INTENT, intent);
        return bundle;
    }
}

From source file:org.alfresco.mobile.android.platform.SessionManager.java

public void createSession(AlfrescoAccount currentAccount) {
    // Check Connectivity
    if (!ConnectivityUtils.hasInternetAvailable(appContext)) {
        EventBusManager.getInstance().post(new LoadAccountErrorEvent(null, currentAccount,
                new NetworkErrorException("Not Online"),
                AlfrescoExceptionHelper.getMessageId(appContext, new NetworkErrorException("Not Online"))));
        return;/*from   w  w  w.  j a va2s  .  c o m*/
    }

    if (hasSession(currentAccount.getId())) {
        removeAccount(currentAccount.getId());
        saveAccount(currentAccount);
    }

    Builder request = new Builder();
    Operator.with(appContext, currentAccount).load(request);
}

From source file:luki.x.net.XNetEngine.java

private InputStream getHttpInputStream(HttpClient httpClient, HttpGet httpGet) throws Exception {
    HttpResponse httpResponse = httpClient.execute(httpGet);
    int statusCode = httpResponse.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        throw new NetworkErrorException("statusCode != HttpStatus.SC_OK");
    }//from w w  w  .j av  a2s  .c om
    InputStream in = httpResponse.getEntity().getContent();
    return in;
}

From source file:com.ntsync.android.sync.client.NetworkUtilities.java

private static byte[] sendAuthStep2(Context context, BigInteger clientM, String accountName, BigInteger srpA)
        throws UnsupportedEncodingException, ServerException, NetworkErrorException {
    final HttpPost post = new HttpPost(AUTH2_URI);
    List<BasicNameValuePair> values = new ArrayList<BasicNameValuePair>();
    values.add(new BasicNameValuePair("m",
            Base64.encodeToString(BigIntegers.asUnsignedByteArray(clientM), Base64.DEFAULT)));
    values.add(new BasicNameValuePair("srpA",
            Base64.encodeToString(BigIntegers.asUnsignedByteArray(srpA), Base64.DEFAULT)));

    HttpEntity postEntity = new UrlEncodedFormEntity(values, SyncDataHelper.DEFAULT_CHARSET_NAME);
    post.setHeader(postEntity.getContentEncoding());
    post.setEntity(postEntity);// ww  w. j a  va2 s. com

    try {
        final HttpResponse resp = getHttpClient(context).execute(post, createHttpContext(accountName, null));
        return getResponse(resp);
    } catch (IOException ex) {
        throw new NetworkErrorException(ex);
    }
}

From source file:com.ntsync.android.sync.client.NetworkUtilities.java

private static byte[] sendAuthStep1(Context context, String username)
        throws UnsupportedEncodingException, ServerException, NetworkErrorException {
    final HttpPost post = new HttpPost(AUTH1_URI);
    List<BasicNameValuePair> values = new ArrayList<BasicNameValuePair>();
    values.add(new BasicNameValuePair("name", username));

    HttpEntity postEntity = new UrlEncodedFormEntity(values, SyncDataHelper.DEFAULT_CHARSET_NAME);
    post.setHeader(postEntity.getContentEncoding());
    // Explicit set no session
    post.setEntity(postEntity);//from  w  w  w  .j  av a  2 s  .  c o  m
    try {
        final HttpResponse resp = getHttpClient(context).execute(post, createHttpContext(username, null));
        return getResponse(resp);
    } catch (IOException ex) {
        throw new NetworkErrorException(ex);
    }
}

From source file:com.ntsync.android.sync.client.NetworkUtilities.java

public static Pair<UserRegistrationState, String> registrateUser(Context context, String email,
        byte[] srpPassword, byte[] pwdSalt, String name) throws ServerException, NetworkErrorException {

    String username;//from w  w  w  .  j ava  2 s  .c o m
    UserRegistrationState state;
    int retryCount = 5;
    boolean retry;
    do {
        retry = false;
        state = null;
        try {
            // create new Username
            username = createUsername(context, email);
            if (username == null) {
                return new Pair<UserRegistrationState, String>(UserRegistrationState.EMAIL_INVALID, null);
            }
            Pair<byte[], BigInteger> verif = SRP6Helper.createUserVerification(username, srpPassword);

            final HttpPost post = new HttpPost(REGISTRATION_URI);
            List<BasicNameValuePair> values = new ArrayList<BasicNameValuePair>();
            values.add(new BasicNameValuePair("username", username));
            values.add(new BasicNameValuePair("email", email));
            values.add(new BasicNameValuePair("verif",
                    Base64.encodeToString(BigIntegers.asUnsignedByteArray(verif.right), Base64.DEFAULT)));
            values.add(new BasicNameValuePair("srpSalt", Base64.encodeToString(verif.left, Base64.DEFAULT)));
            values.add(new BasicNameValuePair("pwdSalt", Base64.encodeToString(pwdSalt, Base64.DEFAULT)));
            values.add(new BasicNameValuePair("personname", name));
            values.add(new BasicNameValuePair("locale", Locale.getDefault().toString()));

            HttpEntity postEntity = new UrlEncodedFormEntity(values, SyncDataHelper.DEFAULT_CHARSET_NAME);
            post.setHeader(postEntity.getContentEncoding());
            post.setEntity(postEntity);

            byte[] content;
            final HttpResponse resp = getHttpClient(context).execute(post, createHttpContext(username, null));
            content = getResponse(resp);

            if (content != null && content.length > 0) {
                String respCode = new String(content, SyncDataHelper.DEFAULT_CHARSET_NAME);
                state = UserRegistrationState.fromErrorVal(respCode);
            }
            if (state == null) {
                throw new ServerException("No valid response");
            }
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        } catch (IOException ex) {
            throw new NetworkErrorException(ex);
        }
        if (state == UserRegistrationState.USERNAME_IN_USE) {
            // Create a new username when alredy in use (concurrent catch of
            // username)
            retry = true;
        }
        retryCount--;
    } while (retry && retryCount > 0);

    return new Pair<UserRegistrationState, String>(state, username);
}

From source file:com.ntsync.android.sync.client.NetworkUtilities.java

private static String createUsername(Context context, String email)
        throws ServerException, NetworkErrorException, UnsupportedEncodingException {
    final HttpPost post = new HttpPost(CREATEUSERNAME_URI);
    if (Log.isLoggable(TAG, Log.INFO)) {
        Log.i(TAG, "createUsername with URI: " + post.getURI());
    }/*from w  ww.  j  a  v a 2  s.c o  m*/

    List<BasicNameValuePair> values = new ArrayList<BasicNameValuePair>();
    values.add(new BasicNameValuePair("email", email));

    HttpEntity postEntity = new UrlEncodedFormEntity(values, SyncDataHelper.DEFAULT_CHARSET_NAME);
    post.setHeader(postEntity.getContentEncoding());
    post.setEntity(postEntity);

    String username = null;
    byte[] content;
    try {
        final HttpResponse resp = getHttpClient(context).execute(post, createHttpContext(null, null));
        content = getResponse(resp);
    } catch (IOException ex) {
        throw new NetworkErrorException(ex);
    }
    if (content != null && content.length > 0) {
        username = new String(content, SyncDataHelper.DEFAULT_CHARSET_NAME);
    }
    return username;
}