Example usage for org.apache.http.auth AuthenticationException AuthenticationException

List of usage examples for org.apache.http.auth AuthenticationException AuthenticationException

Introduction

In this page you can find the example usage for org.apache.http.auth AuthenticationException AuthenticationException.

Prototype

public AuthenticationException() 

Source Link

Document

Creates a new AuthenticationException with a null detail message.

Usage

From source file:org.klnusbaum.udj.network.ServerConnection.java

public static AuthResult authenticate(String username, String password)
        throws AuthenticationException, IOException, APIVersionException, JSONException {
    URI AUTH_URI = null;//from   w w w  .  j a v  a 2 s .c  om
    try {
        AUTH_URI = new URI(NETWORK_PROTOCOL, null, SERVER_HOST, SERVER_PORT, "/udj/0_6/auth", null, null);
    } catch (URISyntaxException e) {
        //TODO should never get here but I should do something if it does.
    }
    final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(PARAM_USERNAME, username));
    params.add(new BasicNameValuePair(PARAM_PASSWORD, password));
    HttpEntity entity = null;
    entity = new UrlEncodedFormEntity(params);
    final HttpPost post = new HttpPost(AUTH_URI);
    post.addHeader(entity.getContentType());
    post.setEntity(entity);
    final HttpResponse resp = getHttpClient().execute(post);
    Log.d(TAG, "Auth Status code was " + resp.getStatusLine().getStatusCode());
    final String response = EntityUtils.toString(resp.getEntity());
    Log.d(TAG, "Auth Response was " + response);
    if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_IMPLEMENTED) {
        throw new APIVersionException();
    } else if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        throw new AuthenticationException();
    } else {
        JSONObject authResponse = new JSONObject(response);
        return new AuthResult(authResponse.getString("ticket_hash"), authResponse.getString("user_id"));
    }
}

From source file:org.klnusbaum.udj.network.ServerConnection.java

private static void basicResponseErrorCheck(HttpResponse resp, String response)
        throws AuthenticationException, IOException {
    if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        throw new AuthenticationException();
    } else if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_BAD_REQUEST
            || resp.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND
            || resp.getStatusLine().getStatusCode() >= 500) {
        //TODO this should just be "General server error"
        Log.e(TAG, "Basic Response Error Check got an error code: " + resp.getStatusLine().getStatusCode());
        throw new IOException(response);
    }//from   www  . j  ava 2s.  co m
}

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

@SuppressLint("SimpleDateFormat")
public List<RawContact> getContacts(Account account)
        throws JSONException, ParseException, IOException, AuthenticationException {

    final ArrayList<RawContact> serverList = new ArrayList<RawContact>();
    ContactsSync app = ContactsSync.getInstance();
    int pictureSize = app.getPictureSize();
    String pic_size = null;//from   w  w  w  .ja  v a  2 s . c om
    boolean album_picture = false;

    if (app.getSyncType() == ContactsSync.SyncType.LEGACY) {
        switch (pictureSize) {
        case RawContact.IMAGE_SIZES.SMALL_SQUARE:
            pic_size = "pic_square";
            break;
        case RawContact.IMAGE_SIZES.SMALL:
            pic_size = "pic_small";
            break;
        case RawContact.IMAGE_SIZES.NORMAL:
            pic_size = "pic";
            break;
        case RawContact.IMAGE_SIZES.SQUARE:
        case RawContact.IMAGE_SIZES.BIG_SQUARE:
        case RawContact.IMAGE_SIZES.HUGE_SQUARE:
        case RawContact.IMAGE_SIZES.MAX:
        case RawContact.IMAGE_SIZES.MAX_SQUARE:
            album_picture = true;
        case RawContact.IMAGE_SIZES.BIG:
            pic_size = "pic_big";
            break;
        }
    } else {
        pic_size = "pic";
        album_picture = false;
    }

    String fields = "uid, first_name, last_name, " + pic_size;

    boolean more = true;
    int limit;
    int offset = 0;
    while (more) {
        more = false;
        Bundle params = new Bundle();

        if (album_picture) {
            limit = 20;
            String query1 = "SELECT " + fields
                    + " FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) LIMIT " + limit
                    + " OFFSET " + offset;
            String query2 = "SELECT owner, src_big, modified FROM photo WHERE pid IN (SELECT cover_pid FROM album WHERE owner IN (SELECT uid FROM #query1) AND type = 'profile')";
            params.putString("method", "fql.multiquery");
            params.putString("queries", "{\"query1\":\"" + query1 + "\", \"query2\":\"" + query2 + "\"}");
        } else {
            limit = 1000;
            String query = "SELECT " + fields
                    + " FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) LIMIT " + limit
                    + " OFFSET " + offset;
            params.putString("method", "fql.query");
            params.putString("query", query);
        }

        params.putInt("timeout", app.getConnectionTimeout() * 1000);
        Request request = Request.newRestRequest(mSession, "fql.query", params, HttpMethod.GET);
        Response response = request.executeAndWait();

        if (response == null) {
            throw new IOException();
        }
        if (response.getGraphObjectList() == null) {
            if (response.getError() != null) {
                if (response.getError().getErrorCode() == 190) {
                    throw new AuthenticationException();
                } else {
                    throw new ParseException(response.getError().getErrorMessage());
                }
            } else {
                throw new ParseException();
            }
        }

        try {
            JSONArray serverContacts;
            HashMap<String, JSONObject> serverImages = new HashMap<String, JSONObject>();
            if (album_picture) {
                JSONArray result = response.getGraphObjectList().getInnerJSONArray();
                serverContacts = result.getJSONObject(0).getJSONArray("fql_result_set");
                JSONArray images = result.getJSONObject(1).getJSONArray("fql_result_set");
                JSONObject image;
                for (int j = 0; j < images.length(); j++) {
                    image = images.getJSONObject(j);
                    serverImages.put(image.getString("owner"), image);
                }
            } else {
                serverContacts = response.getGraphObjectList().getInnerJSONArray();
            }

            JSONObject contact;
            for (int i = 0; i < serverContacts.length(); i++) {
                contact = serverContacts.getJSONObject(i);
                contact.put("picture", !contact.isNull(pic_size) ? contact.getString(pic_size) : null);
                if (album_picture && serverImages.containsKey(contact.getString("uid"))) {
                    contact.put("picture", serverImages.get(contact.getString("uid")).getString("src_big"));
                }
                RawContact rawContact = RawContact.valueOf(contact);
                if (rawContact != null) {
                    serverList.add(rawContact);
                }
            }

            if (serverContacts.length() > limit / 2) {
                offset += limit;
                more = true;
            }
        } catch (FacebookException e) {
            throw new ParseException(e.getMessage());
        } catch (JSONException e) {
            throw new ParseException(e.getMessage());
        }
    }

    return serverList;
}

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

public ContactPhoto getContactPhotoHD(RawContact contact, int width, int height)
        throws IOException, AuthenticationException, JSONException {

    Bundle params = new Bundle();
    ContactsSync app = ContactsSync.getInstance();
    params.putInt("width", width);
    params.putInt("height", height);
    params.putBoolean("redirect", false);
    params.putInt("timeout", app.getConnectionTimeout() * 1000);
    Request request = new Request(mSession, contact.getUid() + "/picture", params, HttpMethod.GET);
    Response response = request.executeAndWait();

    if (response == null) {
        throw new IOException();
    }// w w w .  j a v a 2s . c om
    if (response.getGraphObject() == null) {
        if (response.getError() != null) {
            if (response.getError().getErrorCode() == 190) {
                throw new AuthenticationException();
            } else {
                throw new ParseException(response.getError().getErrorMessage());
            }
        } else {
            throw new ParseException();
        }
    }

    Log.d("FacebookGetPhoto", "response: " + response.getGraphObject().getInnerJSONObject().toString());
    String image = response.getGraphObject().getInnerJSONObject().getJSONObject("data").getString("url");

    return new ContactPhoto(contact, image, 0);
}

From source file:ro.weednet.contactssync.syncadapter.SyncAdapter.java

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {/*from   ww w  . jav a2 s.c  o  m*/
    String authtoken = null;
    try {
        //   ContactManager.setAccountContactsVisibility(getContext(), account, true);
        ContactsSync app = ContactsSync.getInstance();

        if (app.getSyncWifiOnly() && !app.wifiConnected()) {
            throw new OperationCanceledException("not on wifi");
        }

        authtoken = mAccountManager.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE,
                NOTIFY_AUTH_FAILURE);
        if (authtoken == null) {
            throw new AuthenticationException();
        }

        final long groupId = ContactManager.ensureGroupExists(mContext, account);
        final Uri rawContactsUri = RawContacts.CONTENT_URI.buildUpon()
                .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
                .appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type).build();

        List<RawContact> localContacts = ContactManager.getLocalContacts(mContext, rawContactsUri);

        if (app.getFullSync()) {
            ContactManager.deleteContacts(mContext, localContacts);
            localContacts.clear();
            app.clearFullSync();
        }

        NetworkUtilities nu = new NetworkUtilities(authtoken, mContext);
        List<RawContact> rawContacts = nu.getContacts(account);

        List<RawContact> syncedContacts = ContactManager.updateContacts(mContext, account, rawContacts, groupId,
                app.getJoinById(), app.getSyncAllContacts());

        ContactManager.deleteMissingContacts(mContext, localContacts, syncedContacts);

        if (app.getJoinById()) {
            ContactManager.addJoins(mContext, account, rawContacts);
        }

        if (app.getSyncType() == ContactsSync.SyncType.HARD) {
            localContacts = ContactManager.getLocalContacts(mContext, rawContactsUri);
            ContactManager.updateContactDetails(mContext, localContacts, nu);
        } else if (app.getSyncType() == ContactsSync.SyncType.MEDIUM) {
            List<RawContact> starredContacts = ContactManager.getStarredContacts(mContext, rawContactsUri);
            ContactManager.updateContactDetails(mContext, starredContacts, nu);
        }

        NotificationManager mNotificationManager = (NotificationManager) getContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancelAll();

    } catch (final AuthenticatorException e) {
        Log.e(TAG, "AuthenticatorException", e);
        syncResult.stats.numParseExceptions++;
        showNotificationError("Error connecting to facebook");
    } catch (final OperationCanceledException e) {
        Log.e(TAG, "OperationCanceledExcetpion", e);
    } catch (final IOException e) {
        Log.e(TAG, "IOException", e);
        syncResult.stats.numIoExceptions++;
        showNotificationError("Error connecting to facebook");
    } catch (final AuthenticationException e) {
        Log.e(TAG, "AuthenticationException", e);
        syncResult.stats.numAuthExceptions++;
        if (authtoken != null) {
            mAccountManager.invalidateAuthToken(account.type, authtoken);
        }
        try {
            authtoken = mAccountManager.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE,
                    NOTIFY_AUTH_FAILURE);
        } catch (OperationCanceledException e1) {
            Log.e(TAG, "OperationCanceledExcetpion", e);
        } catch (AuthenticatorException e1) {
            Log.e(TAG, "AuthenticatorException", e);
            syncResult.stats.numParseExceptions++;
        } catch (IOException e1) {
            Log.e(TAG, "IOException", e);
            syncResult.stats.numIoExceptions++;
        }
    } catch (final ParseException e) {
        Log.e(TAG, "ParseException", e);
        syncResult.stats.numParseExceptions++;
        showNotificationError("Error parsing the information from facebook");
    } catch (final JSONException e) {
        Log.e(TAG, "JSONException", e);
        syncResult.stats.numParseExceptions++;
        showNotificationError("Error parsing the information from facebook");
    } catch (final Exception e) {
        Log.e(TAG, "Unknown exception", e);
    }
}