Added JSON to Device object conversion

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

@ -44,6 +44,8 @@ public class Device {
private List<Feature> features; private List<Feature> features;
private List<Property> properties;
public int getId() { public int getId() {
return id; return id;
} }
@ -140,4 +142,11 @@ public class Device {
this.type = type; 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. * Android Device Management REST-API implementation.
*/ */
@Path("/devices")
public class Device { public class Device {
@GET @GET

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

@ -16,26 +16,76 @@
package cdm.api.android.util; package cdm.api.android.util;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
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 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 device = new Device();
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
device.setName("Test Device"); device.setOwner(properties.get(AndroidConstants.DeviceProperties.PROPERTY_USER_KEY).getAsString());
device.setOwner("harshan"); 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; 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(); DeviceIdentifier identifier = new DeviceIdentifier();
identifier.setId(deviceId); identifier.setId(deviceId);
identifier.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); 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"/> <ref bean="enrollmentServiceBean"/>
</jaxrs:serviceBeans> </jaxrs:serviceBeans>
</jaxrs:server> </jaxrs:server>
<bean id="serviceBean" class="cdm.api.android.Authentication"/> <bean id="serviceBean" class="cdm.api.android.Authentication"/>
<bean id="deviceMgtServiceBean" class="cdm.api.android.Device"/> <bean id="deviceMgtServiceBean" class="cdm.api.android.Device"/>
<bean id="enrollmentServiceBean" class="cdm.api.android.Enrollment"/> <bean id="enrollmentServiceBean" class="cdm.api.android.Enrollment"/>

Loading…
Cancel
Save