Added JSON to Device object conversion

revert-70aa11f8
harshanL 10 years ago
parent 8cc549d762
commit 6d5ea7f586

@ -44,6 +44,8 @@ public class Device {
private List<Feature> features;
private List<Property> properties;
public int getId() {
return id;
}
@ -140,4 +142,11 @@ public class Device {
this.type = type;
}
public List<Property> getProperties() {
return properties;
}
public void setProperties(List<Property> properties) {
this.properties = properties;
}
}

@ -0,0 +1,39 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* 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 org.wso2.carbon.device.mgt.common;
public class Property {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

@ -26,7 +26,6 @@ import javax.ws.rs.core.Response;
/**
* Android Device Management REST-API implementation.
*/
@Path("/devices")
public class Device {
@GET

@ -17,6 +17,7 @@
package cdm.api.android;
import cdm.api.android.util.AndroidAPIUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -34,13 +35,12 @@ import javax.ws.rs.core.Response;
/**
* Android Device Enrollment REST-API implementation.
*/
@Path("/enrollment")
public class Enrollment {
private static Log log = LogFactory.getLog(Enrollment.class);
@POST
public Response enrollDevice() {
public Response enrollDevice(String jsonPayload) {
boolean result = false;
int status = 0;
String msg = "";
@ -50,16 +50,21 @@ public class Enrollment {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
ctx.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
ctx.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
dmService = (DeviceManagementService) ctx
.getOSGiService(DeviceManagementService.class, null);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
Device device = AndroidAPIUtil.convertToDeviceObject(null);
Device device = AndroidAPIUtil.convertToDeviceObject(jsonPayload);
try {
result = dmService.enrollDevice(device);
status = 1;
if(dmService!=null){
result = dmService.enrollDevice(device);
status = 1;
}else{
status = -1;
msg = "Device Manager service not available";
}
} catch (DeviceManagementException e) {
msg = "Error occurred while enrolling the device";
log.error(msg, e);

@ -16,26 +16,76 @@
package cdm.api.android.util;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.*;
import java.util.*;
/**
* AndroidAPIUtil class provides utility function used by Android REST-API classes.
* AndroidAPIUtil class provides utility function used by Android REST-API classes.
*/
public class AndroidAPIUtil {
public static Device convertToDeviceObject(JsonObject json){
public static Device convertToDeviceObject(String jsonString) {
JsonObject obj = new Gson().fromJson(jsonString, JsonObject.class);
JsonObject properties =
new Gson().fromJson(obj.get(AndroidConstants.DeviceConstants.DEVICE_PROPERTIES_KEY)
, JsonObject.class);
JsonObject features =
new Gson().fromJson(
obj.get(AndroidConstants.DeviceConstants.DEVICE_FEATURES_KEY),
JsonObject.class);
Device device = new Device();
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
device.setName("Test Device");
device.setOwner("harshan");
device.setOwner(properties.get(AndroidConstants.DeviceProperties.PROPERTY_USER_KEY).getAsString());
device.setDeviceIdentifier(
obj.get(AndroidConstants.DeviceConstants.DEVICE_MAC_KEY).getAsString());
device.setDescription(
obj.get(AndroidConstants.DeviceConstants.DEVICE_DESCRIPTION_KEY).getAsString());
device.setOwnership(
obj.get(AndroidConstants.DeviceConstants.DEVICE_OWNERSHIP_KEY).getAsString());
device.setName(properties.get(AndroidConstants.DeviceProperties.PROPERTY_DEVICE_KEY)
.getAsString());
device.setFeatures(parseFeatures(features));
device.setProperties(parseProperties(properties));
return device;
}
public static DeviceIdentifier convertToDeviceIdentifierObject(String deviceId){
private static List<Property> parseProperties(JsonObject properties) {
List<Property> propertyList = new ArrayList<Property>(0);
for (Map.Entry<String, JsonElement> entry : properties.entrySet()) {
propertyList.add(parseProperty(entry.getKey(), entry.getValue()));
}
// propertyList.add(parseProperty("regid", properties.get("regid").getAsString()));
// propertyList.add(parseProperty("osversion", properties.get("osversion").getAsString()));
// propertyList.add(parseProperty("vendor", properties.get("vendor").getAsString()));
// propertyList.add(parseProperty("imei", properties.get("imei").getAsString()));
// propertyList.add(parseProperty("imsi", properties.get("imsi").getAsString()));
// propertyList.add(parseProperty("model", properties.get("model").getAsString()));
return propertyList;
}
private static List<Feature> parseFeatures(JsonObject features) {
List<Feature> featureList = new ArrayList<Feature>(0);
return featureList;
}
private static Property parseProperty(String property, JsonElement value) {
Property prop = new Property();
prop.setName(property);
prop.setValue(value.getAsString());
return prop;
}
private static Feature parseFeature(JsonElement featureElement) {
Feature feature = new Feature();
return feature;
}
public static DeviceIdentifier convertToDeviceIdentifierObject(String deviceId) {
DeviceIdentifier identifier = new DeviceIdentifier();
identifier.setId(deviceId);
identifier.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);

@ -0,0 +1,48 @@
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* 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 cdm.api.android.util;
/**
* Defines constants used in Android-REST API bundle.
*/
public final class AndroidConstants {
public final class DeviceProperties{
private DeviceProperties() {
throw new AssertionError();
}
public static final String PROPERTY_USER_KEY = "username";
public static final String PROPERTY_DEVICE_KEY = "device";
}
public final class DeviceFeatures{
private DeviceFeatures() {
throw new AssertionError();
}
}
public final class DeviceConstants{
private DeviceConstants() {
throw new AssertionError();
}
public static final String DEVICE_MAC_KEY = "mac";
public static final String DEVICE_DESCRIPTION_KEY = "description";
public static final String DEVICE_OWNERSHIP_KEY = "ownership";
public static final String DEVICE_PROPERTIES_KEY = "properties";
public static final String DEVICE_FEATURES_KEY = "features";
}
}

@ -38,6 +38,7 @@
<ref bean="enrollmentServiceBean"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="serviceBean" class="cdm.api.android.Authentication"/>
<bean id="deviceMgtServiceBean" class="cdm.api.android.Device"/>
<bean id="enrollmentServiceBean" class="cdm.api.android.Enrollment"/>

Loading…
Cancel
Save