
fiftyone.devicedetection.cloud.flowelements.DeviceDetectionCloudEngine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of device-detection.cloud Show documentation
Show all versions of device-detection.cloud Show documentation
Device detection that consumes the 51Degrees cloud service.
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
package fiftyone.devicedetection.cloud.flowelements;
import fiftyone.devicedetection.cloud.data.DeviceDataCloud;
import fiftyone.pipeline.cloudrequestengine.data.CloudRequestData;
import fiftyone.pipeline.cloudrequestengine.flowelements.CloudAspectEngineBase;
import fiftyone.pipeline.cloudrequestengine.flowelements.CloudRequestEngine;
import fiftyone.pipeline.core.data.AccessiblePropertyMetaData;
import fiftyone.pipeline.core.data.EvidenceKeyFilter;
import fiftyone.pipeline.core.data.EvidenceKeyFilterWhitelist;
import fiftyone.pipeline.core.data.FlowData;
import fiftyone.pipeline.core.data.factories.ElementDataFactory;
import fiftyone.pipeline.core.data.types.JavaScript;
import fiftyone.pipeline.core.exceptions.PipelineConfigurationException;
import fiftyone.pipeline.core.flowelements.Pipeline;
import fiftyone.pipeline.engines.data.AspectPropertyMetaData;
import fiftyone.pipeline.engines.data.AspectPropertyMetaDataDefault;
import fiftyone.pipeline.engines.data.AspectPropertyValue;
import fiftyone.pipeline.engines.data.AspectPropertyValueDefault;
import org.json.JSONObject;
import org.slf4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import org.json.JSONArray;
/**
* Engine that takes the JSON response from the {@link CloudRequestEngine} and
* uses it populate a {@link DeviceDataCloud} instance for easier consumption.
* @see Specification
*/
public class DeviceDetectionCloudEngine
extends CloudAspectEngineBase {
List aspectProperties;
private String dataSourceTier;
CloudRequestEngine cloudRequestEngine;
/**
* Construct a new instance of the {@link DeviceDetectionCloudEngine}.
* @param logger logger instance to use for logging
* @param deviceDataFactory the factory to use when creating a
* {@link DeviceDataCloud} instance
*/
public DeviceDetectionCloudEngine(
Logger logger,
ElementDataFactory deviceDataFactory) {
super(logger, deviceDataFactory);
this.cloudRequestEngine = null;
}
@Override
public List getProperties() {
return aspectProperties;
}
@Override
public String getDataSourceTier() {
return dataSourceTier;
}
@Override
public String getElementDataKey() {
return "device";
}
@Override
public EvidenceKeyFilter getEvidenceKeyFilter() {
// This engine needs no evidence.
// It works from the cloud request data.
return new EvidenceKeyFilterWhitelist(new ArrayList());
}
@Override
protected void processEngine(FlowData data, DeviceDataCloud aspectData) {
if (cloudRequestEngine == null) {
throw new PipelineConfigurationException(
"The '" + getClass().getName() + "' requires a " +
"'CloudRequestEngine' before it in the Pipeline. This " +
"engine will be unable to produce results until this is " +
"corrected.");
}
else {
CloudRequestData requestData =
data.getFromElement(cloudRequestEngine);
String json = "";
json = requestData.getJsonResponse();
// Extract data from json to the aspectData instance.
JSONObject jsonObj = new JSONObject(json);
JSONObject deviceObj = jsonObj.getJSONObject("device");
Map deviceMap =
new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
for (AspectPropertyMetaData property : getProperties()) {
String type = property.getType().getSimpleName();
switch(type) {
case ("List"):
deviceMap.put(
property.getName(),
getListAspectPropertyValue(deviceObj, property));
break;
case ("JavaScript"):
deviceMap.put(
property.getName(),
getJavaScriptAspectPropertyValue(deviceObj, property));
break;
case ("String"):
deviceMap.put(
property.getName(),
getStringAspectPropertyValue(deviceObj, property));
break;
case ("boolean"):
deviceMap.put(
property.getName(),
getBooleanAspectPropertyValue(deviceObj, property));
break;
case ("int"):
deviceMap.put(
property.getName(),
getIntegerAspectPropertyValue(deviceObj, property));
break;
case ("double"):
deviceMap.put(
property.getName(),
getDoubleAspectPropertyValue(deviceObj, property));
break;
default:
deviceMap.put(
property.getName(),
getStringAspectPropertyValue(deviceObj, property));
break;
}
}
// Add no value messages for any properties which have not yet been
// populated.
for (String key : deviceObj.keySet()) {
if (key.endsWith("nullreason")) {
String actualKey = key
.replace("nullreason", "");
if (deviceMap.containsKey(actualKey) == false) {
AspectPropertyValue> nullValue =
new AspectPropertyValueDefault
© 2015 - 2025 Weber Informatics LLC | Privacy Policy