-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added ODP RestApi interface for sending Events to ODP (#485)
## Summary This module provides an internal service for ODP event REST api access. ## Test plan - Manually tested thoroughly. - Added new unit tests. - All Existing Unit tests pass. - All existing Full stack compatibility tests pass. ## Jira [OASIS-8387](https://optimizely.atlassian.net/browse/OASIS-8387)
- Loading branch information
1 parent
1cf2d72
commit 8b8a983
Showing
13 changed files
with
541 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
core-api/src/main/java/com/optimizely/ab/odp/ODPEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright 2022, Optimizely Inc. and contributors | ||
* | ||
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp; | ||
|
||
import java.util.Map; | ||
|
||
public class ODPEvent { | ||
private String type; | ||
private String action; | ||
private Map<String, String > identifiers; | ||
private Map<String, Object> data; | ||
|
||
public ODPEvent(String type, String action, Map<String, String> identifiers, Map<String, Object> data) { | ||
this.type = type; | ||
this.action = action; | ||
this.identifiers = identifiers; | ||
this.data = data; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public String getAction() { | ||
return action; | ||
} | ||
|
||
public void setAction(String action) { | ||
this.action = action; | ||
} | ||
|
||
public Map<String, String> getIdentifiers() { | ||
return identifiers; | ||
} | ||
|
||
public void setIdentifiers(Map<String, String> identifiers) { | ||
this.identifiers = identifiers; | ||
} | ||
|
||
public Map<String, Object> getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Map<String, Object> data) { | ||
this.data = data; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
core-api/src/main/java/com/optimizely/ab/odp/serializer/ODPJsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright 2022, Optimizely Inc. and contributors | ||
* | ||
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp.serializer; | ||
|
||
import com.optimizely.ab.odp.ODPEvent; | ||
|
||
import java.util.List; | ||
|
||
public interface ODPJsonSerializer { | ||
public String serializeEvents(List<ODPEvent> events); | ||
} |
49 changes: 49 additions & 0 deletions
49
core-api/src/main/java/com/optimizely/ab/odp/serializer/ODPJsonSerializerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright 2022, Optimizely Inc. and contributors | ||
* | ||
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp.serializer; | ||
|
||
import com.optimizely.ab.internal.JsonParserProvider; | ||
import com.optimizely.ab.odp.serializer.impl.GsonSerializer; | ||
import com.optimizely.ab.odp.serializer.impl.JacksonSerializer; | ||
import com.optimizely.ab.odp.serializer.impl.JsonSerializer; | ||
import com.optimizely.ab.odp.serializer.impl.JsonSimpleSerializer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ODPJsonSerializerFactory { | ||
private static final Logger logger = LoggerFactory.getLogger(ODPJsonSerializerFactory.class); | ||
|
||
public static ODPJsonSerializer getSerializer() { | ||
JsonParserProvider parserProvider = JsonParserProvider.getDefaultParser(); | ||
ODPJsonSerializer jsonSerializer = null; | ||
switch (parserProvider) { | ||
case GSON_CONFIG_PARSER: | ||
jsonSerializer = new GsonSerializer(); | ||
break; | ||
case JACKSON_CONFIG_PARSER: | ||
jsonSerializer = new JacksonSerializer(); | ||
break; | ||
case JSON_CONFIG_PARSER: | ||
jsonSerializer = new JsonSerializer(); | ||
break; | ||
case JSON_SIMPLE_CONFIG_PARSER: | ||
jsonSerializer = new JsonSimpleSerializer(); | ||
break; | ||
} | ||
logger.info("Using " + parserProvider.toString() + " serializer"); | ||
return jsonSerializer; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
core-api/src/main/java/com/optimizely/ab/odp/serializer/impl/GsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright 2022, Optimizely Inc. and contributors | ||
* | ||
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp.serializer.impl; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.optimizely.ab.odp.ODPEvent; | ||
import com.optimizely.ab.odp.serializer.ODPJsonSerializer; | ||
|
||
import java.util.List; | ||
|
||
public class GsonSerializer implements ODPJsonSerializer { | ||
@Override | ||
public String serializeEvents(List<ODPEvent> events) { | ||
Gson gson = new GsonBuilder().serializeNulls().create(); | ||
return gson.toJson(events); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
core-api/src/main/java/com/optimizely/ab/odp/serializer/impl/JacksonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright 2022, Optimizely Inc. and contributors | ||
* | ||
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp.serializer.impl; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.optimizely.ab.odp.ODPEvent; | ||
import com.optimizely.ab.odp.serializer.ODPJsonSerializer; | ||
|
||
import java.util.List; | ||
|
||
public class JacksonSerializer implements ODPJsonSerializer { | ||
@Override | ||
public String serializeEvents(List<ODPEvent> events) { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try { | ||
return objectMapper.writeValueAsString(events); | ||
} catch (JsonProcessingException e) { | ||
// log error here | ||
} | ||
return null; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
core-api/src/main/java/com/optimizely/ab/odp/serializer/impl/JsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright 2022, Optimizely Inc. and contributors | ||
* | ||
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp.serializer.impl; | ||
|
||
import com.optimizely.ab.odp.ODPEvent; | ||
import com.optimizely.ab.odp.serializer.ODPJsonSerializer; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class JsonSerializer implements ODPJsonSerializer { | ||
@Override | ||
public String serializeEvents(List<ODPEvent> events) { | ||
JSONArray jsonArray = new JSONArray(); | ||
for (ODPEvent event: events) { | ||
JSONObject eventObject = new JSONObject(); | ||
eventObject.put("type", event.getType()); | ||
eventObject.put("action", event.getAction()); | ||
|
||
if (event.getIdentifiers() != null) { | ||
JSONObject identifiers = new JSONObject(); | ||
for (Map.Entry<String, String> identifier : event.getIdentifiers().entrySet()) { | ||
identifiers.put(identifier.getKey(), identifier.getValue()); | ||
} | ||
eventObject.put("identifiers", identifiers); | ||
} | ||
|
||
if (event.getData() != null) { | ||
JSONObject data = new JSONObject(); | ||
for (Map.Entry<String, Object> dataEntry : event.getData().entrySet()) { | ||
data.put(dataEntry.getKey(), getJSONObjectValue(dataEntry.getValue())); | ||
} | ||
eventObject.put("data", data); | ||
} | ||
|
||
jsonArray.put(eventObject); | ||
} | ||
return jsonArray.toString(); | ||
} | ||
|
||
private static Object getJSONObjectValue(Object value) { | ||
return value == null ? JSONObject.NULL : value; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
core-api/src/main/java/com/optimizely/ab/odp/serializer/impl/JsonSimpleSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright 2022, Optimizely Inc. and contributors | ||
* | ||
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.optimizely.ab.odp.serializer.impl; | ||
|
||
import com.optimizely.ab.odp.ODPEvent; | ||
import com.optimizely.ab.odp.serializer.ODPJsonSerializer; | ||
import org.json.simple.JSONArray; | ||
import org.json.simple.JSONObject; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class JsonSimpleSerializer implements ODPJsonSerializer { | ||
@Override | ||
public String serializeEvents(List<ODPEvent> events) { | ||
JSONArray jsonArray = new JSONArray(); | ||
for (ODPEvent event: events) { | ||
JSONObject eventObject = new JSONObject(); | ||
eventObject.put("type", event.getType()); | ||
eventObject.put("action", event.getAction()); | ||
|
||
if (event.getIdentifiers() != null) { | ||
JSONObject identifiers = new JSONObject(); | ||
for (Map.Entry<String, String> identifier : event.getIdentifiers().entrySet()) { | ||
identifiers.put(identifier.getKey(), identifier.getValue()); | ||
} | ||
eventObject.put("identifiers", identifiers); | ||
} | ||
|
||
if (event.getData() != null) { | ||
JSONObject data = new JSONObject(); | ||
for (Map.Entry<String, Object> dataEntry : event.getData().entrySet()) { | ||
data.put(dataEntry.getKey(), dataEntry.getValue()); | ||
} | ||
eventObject.put("data", data); | ||
} | ||
|
||
jsonArray.add(eventObject); | ||
} | ||
return jsonArray.toJSONString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.