Android Open Source - loopback-sdk-android Rest Adapter






From Project

Back to project page loopback-sdk-android.

License

The source code is released under:

MIT License

If you think the Android project loopback-sdk-android 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

// Copyright (c) 2013 StrongLoop. All rights reserved.
//from   w ww. ja va2s  . c  o  m
package com.strongloop.android.loopback;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * An extension to the vanilla
 * {@link com.strongloop.android.remoting.adapters.RestAdapter}
 * to make working with
 * {@link Model}s easier.
 */
public class RestAdapter
        extends com.strongloop.android.remoting.adapters.RestAdapter {

    public static final String SHARED_PREFERENCES_NAME =
            RestAdapter.class.getCanonicalName();
    public static final String PROPERTY_ACCESS_TOKEN = "accessToken";

    private final Context context;

    public RestAdapter(Context context, String url) {
        super(context, url);
        if (context == null) throw new NullPointerException("context must be not null");
        this.context = context;
        setAccessToken(loadAccessToken());
    }

    public void setAccessToken(String accessToken) {
        saveAccessToken(accessToken);
        getClient().addHeader("Authorization", accessToken);
    }

    public void clearAccessToken() {
        getClient().addHeader("Authorization", null);
    }

    public Context getApplicationContext() {
        return context;
    }

    /**
     * Creates a new {@link ModelRepository} representing the named model type.
     * @param name The model name.
     * @return A new repository instance.
     */
    public ModelRepository<Model> createRepository(String name) {
        return createRepository(name, null, null);
    }

    /**
     * Creates a new {@link ModelRepository} representing the named model type.
     * @param name The model name.
     * @param nameForRestUrl The model name to use in REST URL, usually the plural form of `name`.
     * @return A new repository instance.
     */
    public ModelRepository<Model> createRepository(String name, String nameForRestUrl) {
        return createRepository(name, nameForRestUrl, null);
    }

    /**
     * Creates a new {@link ModelRepository} representing the named model type.
     * @param name The model name.
     * @param nameForRestUrl The model name to use in REST URL, usually the plural form of `name`.
     * @param modelClass The model class. The class must have a public
     * no-argument constructor.
     * @return A new repository instance.
     */
    public <T extends Model> ModelRepository<T> createRepository(String name,
                                               String nameForRestUrl,
                                               Class<T> modelClass) {
        ModelRepository<T> repository = new ModelRepository<T>(name, nameForRestUrl, modelClass);
        attachModelRepository(repository);
        return repository;
    }

    /**
     * Creates a new {@link ModelRepository} from the given subclass.
     * @param repositoryClass A subclass of {@link ModelRepository} to use.
     * The class must have a public no-argument constructor.
     * @return A new repository instance.
     */
    public <U extends RestRepository> U createRepository(
            Class<U> repositoryClass) {
        U repository = null;
        try {
            repository = repositoryClass.newInstance();
            repository.setAdapter(this);
        }
        catch (Exception e) {
            IllegalArgumentException ex = new IllegalArgumentException();
            ex.initCause(e);
            throw ex;
        }
        attachModelRepository(repository);
        return repository;
    }


    private void attachModelRepository(RestRepository repository) {
        getContract().addItemsFromContract(repository.createContract());
        repository.setAdapter(this);
    }

    private void saveAccessToken(String accessToken) {
        final SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(PROPERTY_ACCESS_TOKEN, accessToken);
        editor.commit();
    }

    private String loadAccessToken() {
        return getSharedPreferences().getString(PROPERTY_ACCESS_TOKEN, null);
    }

    private SharedPreferences getSharedPreferences() {
        return context.getSharedPreferences(
                SHARED_PREFERENCES_NAME,
                Context.MODE_PRIVATE);
    }
}




Java Source Code List

com.strongloop.android.loopback.AccessTokenRepository.java
com.strongloop.android.loopback.AccessToken.java
com.strongloop.android.loopback.ContainerRepository.java
com.strongloop.android.loopback.Container.java
com.strongloop.android.loopback.FileRepository.java
com.strongloop.android.loopback.File.java
com.strongloop.android.loopback.LocalInstallation.java
com.strongloop.android.loopback.ModelRepository.java
com.strongloop.android.loopback.Model.java
com.strongloop.android.loopback.RestAdapter.java
com.strongloop.android.loopback.RestRepository.java
com.strongloop.android.loopback.UserRepository.java
com.strongloop.android.loopback.User.java
com.strongloop.android.loopback.callbacks.EmptyResponseParser.java
com.strongloop.android.loopback.callbacks.JsonArrayParser.java
com.strongloop.android.loopback.callbacks.JsonObjectParser.java
com.strongloop.android.loopback.callbacks.ListCallback.java
com.strongloop.android.loopback.callbacks.ObjectCallback.java
com.strongloop.android.loopback.callbacks.VoidCallback.java
com.strongloop.android.remoting.BeanUtil.java
com.strongloop.android.remoting.JsonUtil.java
com.strongloop.android.remoting.Repository.java
com.strongloop.android.remoting.Transient.java
com.strongloop.android.remoting.VirtualObject.java
com.strongloop.android.remoting.adapters.Adapter.java
com.strongloop.android.remoting.adapters.RestAdapter.java
com.strongloop.android.remoting.adapters.RestContractItem.java
com.strongloop.android.remoting.adapters.RestContract.java
com.strongloop.android.remoting.adapters.StreamParam.java