Android Open Source - RobolectricSample Authentication Gateway






From Project

Back to project page RobolectricSample.

License

The source code is released under:

MIT License

If you think the Android project RobolectricSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.pivotallabs.tracker;
/*  w w  w .  j  ava2s.  c  o  m*/
import android.content.Context;
import android.content.SharedPreferences;
import com.pivotallabs.Callbacks;
import com.pivotallabs.api.ApiGateway;
import com.pivotallabs.api.ApiResponse;
import com.pivotallabs.api.ApiResponseCallbacks;
import com.pivotallabs.util.Strings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AuthenticationGateway {
    static final String TRACKER_AUTH_PREF_KEY = "tracker-auth";
    private static final String GUID_KEY = "guid";
    public ApiGateway apiGateway;
    private SharedPreferences sharedPreferences;

    public AuthenticationGateway(ApiGateway apiGateway, Context context) {
        this.apiGateway = apiGateway;
        sharedPreferences = context.getSharedPreferences(TRACKER_AUTH_PREF_KEY, Context.MODE_PRIVATE);
    }

    public void signIn(String username, String password, Callbacks responseCallbacks) {
        TrackerAuthenticationRequest apiRequest = new TrackerAuthenticationRequest(username, password);
        ApiResponseCallbacks remoteCallbacks = new AuthenticationApiResponseCallbacks(responseCallbacks, sharedPreferences);
        apiGateway.makeRequest(apiRequest, remoteCallbacks);
    }

    public boolean isAuthenticated() {
        return !Strings.isEmptyOrWhitespace(getToken());
    }

    public void signOut() {
        sharedPreferences.edit().clear().commit();
    }

    public String getToken() {
        return sharedPreferences.getString(GUID_KEY, "");
    }

    private static class AuthenticationApiResponseCallbacks implements ApiResponseCallbacks {
        private Callbacks callbacks;
        private SharedPreferences sharedPreferences;

        public AuthenticationApiResponseCallbacks(Callbacks callbacks, SharedPreferences sharedPreferences) {
            this.callbacks = callbacks;
            this.sharedPreferences = sharedPreferences;
        }

        @Override
        public void onSuccess(ApiResponse response) {
            Matcher matcher = Pattern.compile("<guid>(.*?)</guid>").matcher(response.getResponseBody());
            matcher.find();
            sharedPreferences.edit().putString(GUID_KEY, matcher.group(1)).commit();
            callbacks.onSuccess();
        }

        @Override
        public void onFailure(ApiResponse response) {
            callbacks.onFailure();
        }

        @Override
        public void onComplete() {
            callbacks.onComplete();
        }
    }
}




Java Source Code List

com.pivotallabs.AuthenticationCallbacks.java
com.pivotallabs.Callbacks.java
com.pivotallabs.EmptyOnClickListener.java
com.pivotallabs.GenericAdapter.java
com.pivotallabs.HomeActivity.java
com.pivotallabs.MultiCallbacks.java
com.pivotallabs.NamesActivity.java
com.pivotallabs.NamesAdapter.java
com.pivotallabs.NotifyDataSetChangedCallbacks.java
com.pivotallabs.OnChangeListener.java
com.pivotallabs.StubTextWatcher.java
com.pivotallabs.ViewEnablingTextWatcher.java
com.pivotallabs.ViewVisibleWhileOutstandingCallbacks.java
com.pivotallabs.api.ApiGateway.java
com.pivotallabs.api.ApiRequest.java
com.pivotallabs.api.ApiResponseCallbacks.java
com.pivotallabs.api.ApiResponse.java
com.pivotallabs.api.CertificateIgnoringSSLSocketFactory.java
com.pivotallabs.api.Http.java
com.pivotallabs.api.TrustingTrustManager.java
com.pivotallabs.api.Xmls.java
com.pivotallabs.injected.Counter.java
com.pivotallabs.injected.InjectedActivity.java
com.pivotallabs.injected.RobolectricSampleModule.java
com.pivotallabs.injected.SampleGuiceApplication.java
com.pivotallabs.tracker.AuthenticationGateway.java
com.pivotallabs.tracker.RecentActivities.java
com.pivotallabs.tracker.RecentActivityActivity.java
com.pivotallabs.tracker.RecentActivityAdapter.java
com.pivotallabs.tracker.RecentActivityRequest.java
com.pivotallabs.tracker.RecentActivity.java
com.pivotallabs.tracker.SignInDialog.java
com.pivotallabs.tracker.TrackerAuthenticationRequest.java
com.pivotallabs.util.Pair.java
com.pivotallabs.util.Strings.java