Skip to content

Commit

Permalink
Add IdentityItem to IdentityMap (#8)
Browse files Browse the repository at this point in the history
* Add IdentityItem

* Add tests for identity item

* add convince overloaded constructor

* Throw IllegalArgumentException if id null and add test

* Add override for hashCode

* Update access levels and update API signatures in IdentityItem

* Clean up merge

* Fix java doc

* fix java doc

* invert expression

* Deep copy on getIdentityItemsForNamespace

* Invert params

* Add throws to javadoc

* move throws doc to bottom of comment

* use @link for javadoc

* Rename IdentityEdge event type to EdgeIdentity

* Add final

* Add test for equals

* Fix assertion

* Add import
  • Loading branch information
nporter-adbe authored Mar 12, 2021
1 parent 8ffe573 commit 7eefc91
Show file tree
Hide file tree
Showing 16 changed files with 406 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

package com.adobe.marketing.mobile.identityedge;

/**
* Represents the authentication state for an {@link IdentityItem}
*/
public enum AuthenticationState {
AMBIGUOUS("ambiguous"),
AUTHENTICATED("authenticated"),
LOGGED_OUT("loggedOut");

private String name;

private AuthenticationState(final String name) {
this.name = name;
}

public String getName() {
return name;
}

public static AuthenticationState fromString(final String state) {
if ("authenticated".equalsIgnoreCase(state)) {
return AUTHENTICATED;
} else if ("loggedOut".equalsIgnoreCase(state)) {
return LOGGED_OUT;
} else {
return AMBIGUOUS;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.adobe.marketing.mobile.LoggingMode;
import com.adobe.marketing.mobile.MobileCore;

import java.util.Map;
import java.util.List;

public class IdentityEdge {
private static final String LOG_TAG = "IdentityEdge";
Expand Down Expand Up @@ -63,7 +63,7 @@ public static void getExperienceCloudId(final AdobeCallback<String> callback) {
}

final Event event = new Event.Builder(IdentityEdgeConstants.EventNames.IDENTITY_REQUEST_IDENTITY_ECID,
IdentityEdgeConstants.EventType.IDENTITY_EDGE,
IdentityEdgeConstants.EventType.EDGE_IDENTITY,
IdentityEdgeConstants.EventSource.REQUEST_IDENTITY).build();

final ExtensionErrorCallback<ExtensionError> errorCallback = new ExtensionErrorCallback<ExtensionError>() {
Expand All @@ -90,11 +90,11 @@ public void call(Event responseEvent) {
return;
}

final ECID ecid = identityMap.getFirstECID();
if (ecid != null) {
callback.call(ecid.toString());
} else {
final List<IdentityItem> ecidItems = identityMap.getIdentityItemsForNamespace(IdentityEdgeConstants.Namespaces.ECID);
if (ecidItems == null || ecidItems.isEmpty() || ecidItems.get(0).getId() == null) {
callback.call("");
} else {
callback.call(ecidItems.get(0).getId());
}

}
Expand Down Expand Up @@ -123,7 +123,7 @@ public void error(final ExtensionError extensionError) {


final Event updateIdentitiesEvent = new Event.Builder(IdentityEdgeConstants.EventNames.UPDATE_IDENTITIES,
IdentityEdgeConstants.EventType.IDENTITY_EDGE,
IdentityEdgeConstants.EventType.EDGE_IDENTITY,
IdentityEdgeConstants.EventSource.UPDATE_IDENTITY).setEventData(identityMap.asEventData()).build();
MobileCore.dispatchEvent(updateIdentitiesEvent, errorCallback);
}
Expand All @@ -133,7 +133,7 @@ public void error(final ExtensionError extensionError) {
*/
public static void resetIdentities() {
final Event event = new Event.Builder(IdentityEdgeConstants.EventNames.REQUEST_RESET,
IdentityEdgeConstants.EventType.IDENTITY_EDGE,
IdentityEdgeConstants.EventType.EDGE_IDENTITY,
IdentityEdgeConstants.EventSource.REQUEST_RESET).build();

final ExtensionErrorCallback<ExtensionError> errorCallback = new ExtensionErrorCallback<ExtensionError>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private EventSource() { }

final class EventType {
static final String GENERIC_IDENTITY = "com.adobe.eventType.generic.identity";
static final String IDENTITY_EDGE = "com.adobe.eventType.identityEdge";
static final String EDGE_IDENTITY = "com.adobe.eventType.edgeIdentity";
static final String IDENTITY = "com.adobe.eventType.identity";
private EventType() { }
}
Expand All @@ -39,7 +39,7 @@ final class EventNames {
static final String IDENTITY_REQUEST_IDENTITY_ECID = "Identity Edge Request ECID";
static final String IDENTITY_RESPONSE_CONTENT_ONE_TIME = "Identity Edge Response Content One Time";
static final String UPDATE_IDENTITIES = "Identity Edge Update Identities";
static final String REMOVE_IDENTITIES = "Idetity Edge Remove Identities";
static final String REMOVE_IDENTITIES = "Identity Edge Remove Identities";
static final String REQUEST_RESET = "Identity Edge Request Reset";
private EventNames() { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ class IdentityEdgeExtension extends Extension {
* Called during the Identity extension's registration.
* The following listeners are registered during this extension's registration.
* <ul>
* <li> Listener {@link ListenerIdentityRequestIdentity} to listen for event with eventType {@link IdentityEdgeConstants.EventType#IDENTITY_EDGE}
* <li> Listener {@link ListenerIdentityRequestIdentity} to listen for event with eventType {@link IdentityEdgeConstants.EventType#EDGE_IDENTITY}
* and EventSource {@link IdentityEdgeConstants.EventSource#REQUEST_IDENTITY}</li>
* <li> Listener {@link ListenerGenericIdentityRequestContent} to listen for event with eventType {@link IdentityEdgeConstants.EventType#GENERIC_IDENTITY}
* and EventSource {@link IdentityEdgeConstants.EventSource#REQUEST_CONTENT}</li>
* <li> Listener {@link ListenerIdentityEdgeUpdateIdentity} to listen for event with eventType {@link IdentityEdgeConstants.EventType#IDENTITY_EDGE}
* <li> Listener {@link ListenerIdentityEdgeUpdateIdentity} to listen for event with eventType {@link IdentityEdgeConstants.EventType#EDGE_IDENTITY}
* and EventSource {@link IdentityEdgeConstants.EventSource#UPDATE_IDENTITY}</li>
* <li> Listener {@link ListenerIdentityEdgeRemoveIdentity} to listen for event with eventType {@link IdentityEdgeConstants.EventType#IDENTITY_EDGE}
* <li> Listener {@link ListenerIdentityEdgeRemoveIdentity} to listen for event with eventType {@link IdentityEdgeConstants.EventType#EDGE_IDENTITY}
* and EventSource {@link IdentityEdgeConstants.EventSource#REMOVE_IDENTITY}</li>
* and EventSource {@link IdentityEdgeConstants.EventSource#REQUEST_CONTENT}</li>
* <li> Listener {@link ListenerIdentityRequestReset} to listen for event with eventType {@link IdentityEdgeConstants.EventType#IDENTITY_EDGE}
* <li> Listener {@link ListenerIdentityRequestReset} to listen for event with eventType {@link IdentityEdgeConstants.EventType#EDGE_IDENTITY}
* and EventSource {@link IdentityEdgeConstants.EventSource#REQUEST_RESET}</li>
* </ul>
* <p>
Expand All @@ -60,11 +60,11 @@ public void error(final ExtensionError extensionError) {
}
};

extensionApi.registerEventListener(IdentityEdgeConstants.EventType.IDENTITY_EDGE, IdentityEdgeConstants.EventSource.REQUEST_IDENTITY, ListenerIdentityRequestIdentity.class, listenerErrorCallback);
extensionApi.registerEventListener(IdentityEdgeConstants.EventType.EDGE_IDENTITY, IdentityEdgeConstants.EventSource.REQUEST_IDENTITY, ListenerIdentityRequestIdentity.class, listenerErrorCallback);
extensionApi.registerEventListener(IdentityEdgeConstants.EventType.GENERIC_IDENTITY, IdentityEdgeConstants.EventSource.REQUEST_CONTENT, ListenerGenericIdentityRequestContent.class, listenerErrorCallback);
extensionApi.registerEventListener(IdentityEdgeConstants.EventType.IDENTITY_EDGE, IdentityEdgeConstants.EventSource.UPDATE_IDENTITY, ListenerIdentityEdgeUpdateIdentity.class, listenerErrorCallback);
extensionApi.registerEventListener(IdentityEdgeConstants.EventType.IDENTITY_EDGE, IdentityEdgeConstants.EventSource.REMOVE_IDENTITY, ListenerIdentityEdgeRemoveIdentity.class, listenerErrorCallback);
extensionApi.registerEventListener(IdentityEdgeConstants.EventType.IDENTITY_EDGE, IdentityEdgeConstants.EventSource.REQUEST_RESET, ListenerIdentityRequestReset.class, listenerErrorCallback);
extensionApi.registerEventListener(IdentityEdgeConstants.EventType.EDGE_IDENTITY, IdentityEdgeConstants.EventSource.UPDATE_IDENTITY, ListenerIdentityEdgeUpdateIdentity.class, listenerErrorCallback);
extensionApi.registerEventListener(IdentityEdgeConstants.EventType.EDGE_IDENTITY, IdentityEdgeConstants.EventSource.REMOVE_IDENTITY, ListenerIdentityEdgeRemoveIdentity.class, listenerErrorCallback);
extensionApi.registerEventListener(IdentityEdgeConstants.EventType.EDGE_IDENTITY, IdentityEdgeConstants.EventSource.REQUEST_RESET, ListenerIdentityRequestReset.class, listenerErrorCallback);
}

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ void handleIdentityRequest(final Event event) {

Map<String, Object> xdmData = state.getIdentityEdgeProperties().toXDMData(true);
Event responseEvent = new Event.Builder(IdentityEdgeConstants.EventNames.IDENTITY_RESPONSE_CONTENT_ONE_TIME,
IdentityEdgeConstants.EventType.IDENTITY_EDGE,
IdentityEdgeConstants.EventType.EDGE_IDENTITY,
IdentityEdgeConstants.EventSource.RESPONSE_IDENTITY)
.setEventData(xdmData)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ class IdentityEdgeProperties {

IdentityMap identityMap = IdentityMap.fromData(xdmData);
if (identityMap != null) {
ecid = identityMap.getFirstECID();
final List<IdentityItem> ecidItems = identityMap.getIdentityItemsForNamespace(IdentityEdgeConstants.Namespaces.ECID);
boolean containsEcid = ecidItems != null && !ecidItems.isEmpty() && ecidItems.get(0).getId() != null;
if (containsEcid) {
ecid = new ECID(ecidItems.get(0).getId());
}
}
}

Expand Down Expand Up @@ -71,7 +75,8 @@ Map<String, Object> toXDMData(final boolean allowEmpty) {
final IdentityMap identityMap = new IdentityMap();

if (ecid != null) {
identityMap.addItem(IdentityEdgeConstants.Namespaces.ECID, ecid.toString());
IdentityItem ecidItem = new IdentityItem(ecid.toString());
identityMap.addItem(IdentityEdgeConstants.Namespaces.ECID, ecidItem);
}

final Map<String, List<Map<String, Object>>> dict = identityMap.toObjectMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

package com.adobe.marketing.mobile.identityedge;

import com.adobe.marketing.mobile.LoggingMode;
import com.adobe.marketing.mobile.MobileCore;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Represents an identity item
*/
public final class IdentityItem {
private String id;
private AuthenticationState authenticationState;
private boolean primary;

private static final String LOG_TAG = "IdentityItem";
private static final String JSON_KEY_ID = "id";
private static final String JSON_KEY_AUTHENTICATION_STATE = "authenticationState";
private static final String JSON_KEY_PRIMARY = "primary";

/**
* Creates a new {@link IdentityItem}
* @param id id for the item
* @param authenticationState {@link AuthenticationState} for the item
* @param primary primary flag for the item
* @throws IllegalArgumentException if id is null
*/
public IdentityItem(final String id, final AuthenticationState authenticationState, final boolean primary) {
if (id == null) {
throw new IllegalArgumentException("id must be non-null");
}
this.id = id;
this.authenticationState = authenticationState;
if (authenticationState == null) {
this.authenticationState = AuthenticationState.AMBIGUOUS;
}
this.primary = primary;
}

/**
* Creates a new {@link IdentityItem} with default values
* authenticationState is set to AMBIGUOUS
* primary is set to false
* @param id the id for this {@link IdentityItem}
*/
public IdentityItem(final String id) {
this(id, AuthenticationState.AMBIGUOUS, false);
}

/**
* Creates a copy of item
* @param item A {@link IdentityItem} to be copied
*/
public IdentityItem(final IdentityItem item) {
this(item.id, item.authenticationState, item.primary);
}

/**
* Converts this object into a map representation
* @return this object in a map representation
*/
Map<String, Object> toObjectMap() {
Map<String, Object> map = new HashMap<>();
if (id != null) {
map.put(JSON_KEY_ID, id);
}

if (authenticationState != null) {
map.put(JSON_KEY_AUTHENTICATION_STATE, authenticationState.toString());
} else {
map.put(JSON_KEY_AUTHENTICATION_STATE, AuthenticationState.AMBIGUOUS.toString());
}

map.put(JSON_KEY_PRIMARY, primary);
return map;
}

/**
* @return The id for this identity item
*/
public String getId() {
return id;
}

/**
* @return Current {@link AuthenticationState} for this item
*/
public AuthenticationState getAuthenticationState() {
return authenticationState;
}

/**
* @return true if this item is primary, false otherwise
*/
public boolean isPrimary() {
return primary;
}

/**
* Creates an {@link IdentityItem} from the data
* @param data the data representing an {@link IdentityItem}
* @return an initialized {@link IdentityItem} based on the data, null if data is invalid
*/
static IdentityItem fromData(final Map<String, Object> data) {
if (data == null) { return null; }

try {
final String id = (String) data.get(JSON_KEY_ID);
AuthenticationState authenticationState = AuthenticationState.fromString((String) data.get(JSON_KEY_AUTHENTICATION_STATE));
if (authenticationState == null) {
authenticationState = AuthenticationState.AMBIGUOUS;
}

boolean primary = false;
if (data.get(JSON_KEY_PRIMARY) != null) {
primary = (boolean) data.get(JSON_KEY_PRIMARY);
}

return new IdentityItem(id, authenticationState, primary);
} catch (ClassCastException e) {
MobileCore.log(LoggingMode.DEBUG, LOG_TAG, "Failed to create IdentityItem from data.");
return null;
}
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IdentityItem that = (IdentityItem) o;
return id.equalsIgnoreCase(that.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Loading

0 comments on commit 7eefc91

Please sign in to comment.