Android Open Source - mobile-connector-sdk-android U B F Client






From Project

Back to project page mobile-connector-sdk-android.

License

The source code is released under:

Apache License

If you think the Android project mobile-connector-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

package com.silverpop.engage.network;
//w ww  .j  a  va2  s .  c o m
import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.silverpop.engage.config.EngageConfigManager;
import com.silverpop.engage.domain.EngageEvent;
import com.silverpop.engage.store.EngageLocalEventStore;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jeremydyer on 5/19/14.
 */
public class UBFClient
    extends EngageClient {

    private static final String TAG = EngageClient.class.getName();

    private static UBFClient ubfClient;
    private EngageLocalEventStore engageLocalEventStore = null;
    private int maxRetries = 3;

    private UBFClient(Context context, String clientId, String secret, String refreshToken, String host) {
        super(context, clientId, secret, refreshToken, host);

        maxRetries = EngageConfigManager.get(context).maxNumRetries();
        engageLocalEventStore = EngageLocalEventStore.get(context);

        try {
            if (!engageLocalEventStore.isConnected()) {
                engageLocalEventStore.open();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static UBFClient init(Context context, String clientId, String secret, String refreshToken, String host) {
        if (ubfClient == null) {
            ubfClient = new UBFClient(context, clientId, secret, refreshToken, host);
            ubfClient.authenticateClient(new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    ubfClient.postUBFEngageEvents(null, null);
                }
            }, new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Log.w(TAG, "UBFClient authentication failed");
                }
            });
        }
        return ubfClient;
    }

    public static UBFClient get(Context context) {
        if (ubfClient == null) {
            Log.e(TAG, UBFClient.class.getName() + " must be initialized before it can be retrieved");
            throw new RuntimeException(UBFClient.class.getName() + " must be initialized before it can be retrieved");
        }
        return ubfClient;
    }

    private String getUBFURL() {
        try {
            if (EngageConfigManager.get(mAppContext).secureConnection()) {
                return new URI("https", getHost(), "/rest/events/submission", null).toString();
            } else {
                return new URI("http", getHost(), "/rest/events/submission", null).toString();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    public void postUBFEngageEvents(Response.Listener<JSONObject> success,
                                    Response.ErrorListener error) {

        if (isAuthenticated()) {
            //Find the events that are ready to be sent to Engage.
            final EngageEvent[] unpostedEvents = engageLocalEventStore.findUnpostedEvents();

            if (unpostedEvents != null && unpostedEvents.length > 0) {
                //Create the JSONObject wrapped around the "events" tag engage expects.
                JSONObject events = new JSONObject();

                try {
                    JSONArray eventsArray = new JSONArray();
                    for (EngageEvent ee : unpostedEvents) {
                        eventsArray.put(new JSONObject(ee.getEventJson()));
                    }
                    events.put("events", eventsArray);

                    final JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST,
                            getUBFURL(), events,
                            new Response.Listener<JSONObject>() {
                                public void onResponse(JSONObject response) {
                                    //TODO: Add extra logic to make sure that the message was successful based on response payload
                                    //Update all of the EngageEvents to SUCCESSFULLY_POSTED
                                    for (EngageEvent ee : unpostedEvents) {
                                        ee.setEventStatus(EngageEvent.SUCCESSFULLY_POSTED);
                                        engageLocalEventStore.saveUBFEvent(ee);
                                    }
                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    //Update the failure count and then fail the status if the maximum has been reached.
                                    for (EngageEvent ee : unpostedEvents) {
                                        ee.setEventFailedPostCount(ee.getEventFailedPostCount() + 1);
                                        if (ee.getEventFailedPostCount() >= maxRetries) {
                                            ee.setEventStatus(EngageEvent.FAILED_POST);
                                        }
                                        engageLocalEventStore.saveUBFEvent(ee);
                                    }
                                }
                            }
                    )
                    {
                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            Map<String, String> params = new HashMap<String, String>();
                            String oauthToken = getOauthToken();
                            if (oauthToken != null) {
                                params.put("Authorization", "Bearer " + getOauthToken());
                            } else {
                                Log.w(TAG, "Client authentication has expired. Event post will fail " +
                                        "but will be reattempted after new authentication is complete.");
                            }

                            return params;
                        }
                    };
                    request(jsonRequest);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.d(TAG, "UBFClient is not yet authenticated. " +
                    "Events will be pushed when authentication is complete");
        }
    }
}




Java Source Code List

com.silverpop.engage.EngageApplication.java
com.silverpop.engage.UBFManager.java
com.silverpop.engage.XMLAPIManager.java
com.silverpop.engage.augmentation.UBFAugmentationServiceImpl.java
com.silverpop.engage.augmentation.UBFAugmentationService.java
com.silverpop.engage.augmentation.plugin.UBFAddressAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFCoordinatesAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFLocationNameAugmentationPlugin.java
com.silverpop.engage.config.EngageConfigManagerTests.java
com.silverpop.engage.config.EngageConfigManager.java
com.silverpop.engage.config.EngageConfigTest.java
com.silverpop.engage.config.EngageConfig.java
com.silverpop.engage.deeplinking.EngageDeepLinkManager.java
com.silverpop.engage.demo.engagetest.Application.java
com.silverpop.engage.demo.engagetest.EngageNotificationReceiver.java
com.silverpop.engage.demo.engagetest.MainActivity.java
com.silverpop.engage.demo.engagetest.PushReceiver.java
com.silverpop.engage.demo.engagetest.fragment.EngageConfigFragment.java
com.silverpop.engage.demo.engagetest.fragment.UBFAPIFragment.java
com.silverpop.engage.demo.engagetest.fragment.XMLAPIFragment.java
com.silverpop.engage.domain.EngageEvent.java
com.silverpop.engage.domain.JSONable.java
com.silverpop.engage.domain.UBFTests.java
com.silverpop.engage.domain.UBF.java
com.silverpop.engage.domain.XMLAPIEnum.java
com.silverpop.engage.domain.XMLAPITest.java
com.silverpop.engage.domain.XMLAPI.java
com.silverpop.engage.exception.XMLResponseParseException.java
com.silverpop.engage.location.manager.EngageLocationManager.java
com.silverpop.engage.location.manager.plugin.EngageLocationManagerDefault.java
com.silverpop.engage.location.receiver.EngageLocationReceiver.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverBase.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverGeocode.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverHardcodeTest.java
com.silverpop.engage.network.Credential.java
com.silverpop.engage.network.EngageClient.java
com.silverpop.engage.network.RequestCacheWrapper.java
com.silverpop.engage.network.UBFClient.java
com.silverpop.engage.network.XMLAPIClient.java
com.silverpop.engage.response.EngageResponseXMLTests.java
com.silverpop.engage.response.EngageResponseXML.java
com.silverpop.engage.response.XMLAPIResponseNode.java
com.silverpop.engage.store.EngageLocalEventStoreTest.java
com.silverpop.engage.store.EngageLocalEventStore.java
com.silverpop.engage.store.EngageSQLiteHelper.java
com.silverpop.engage.util.EngageExpirationParserTests.java
com.silverpop.engage.util.EngageExpirationParser.java
com.silverpop.engage.util.TimedAsyncTask.java