JAX-RS Refractor

4.x.x
manoj 10 years ago
parent 69c730d147
commit 37a47ec347

@ -15,8 +15,10 @@
*/
package org.wso2.carbon.device.mgt.common;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement
public class Device {
private int id;
@ -45,6 +47,7 @@ public class Device {
private List<Property> properties;
@XmlElement
public int getId() {
return id;
}
@ -52,7 +55,7 @@ public class Device {
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getDescription() {
return description;
}
@ -60,7 +63,7 @@ public class Device {
public void setDescription(String description) {
this.description = description;
}
@XmlElement
public String getName() {
return name;
}
@ -68,7 +71,7 @@ public class Device {
public void setName(String name) {
this.name = name;
}
@XmlElement
public Long getDateOfEnrolment() {
return dateOfEnrolment;
}
@ -76,7 +79,7 @@ public class Device {
public void setDateOfEnrolment(Long dateOfEnrolment) {
this.dateOfEnrolment = dateOfEnrolment;
}
@XmlElement
public Long getDateOfLastUpdate() {
return dateOfLastUpdate;
}
@ -84,7 +87,7 @@ public class Device {
public void setDateOfLastUpdate(Long dateOfLastUpdate) {
this.dateOfLastUpdate = dateOfLastUpdate;
}
@XmlElement
public String getOwnership() {
return ownership;
}
@ -92,7 +95,7 @@ public class Device {
public void setOwnership(String ownership) {
this.ownership = ownership;
}
@XmlElement
public boolean isStatus() {
return status;
}
@ -100,7 +103,7 @@ public class Device {
public void setStatus(boolean status) {
this.status = status;
}
@XmlElement
public int getDeviceTypeId() {
return deviceTypeId;
}
@ -108,7 +111,7 @@ public class Device {
public void setDeviceTypeId(int deviceTypeId) {
this.deviceTypeId = deviceTypeId;
}
@XmlElement
public String getDeviceIdentifier() {
return deviceIdentifier;
}
@ -116,7 +119,7 @@ public class Device {
public void setDeviceIdentifier(String deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
@XmlElement
public String getOwner() {
return owner;
}
@ -124,7 +127,7 @@ public class Device {
public void setOwner(String owner) {
this.owner = owner;
}
@XmlElement
public List<Feature> getFeatures() {
return features;
}
@ -132,7 +135,7 @@ public class Device {
public void setFeatures(List<Feature> features) {
this.features = features;
}
@XmlElement
public String getType() {
return type;
}
@ -140,7 +143,7 @@ public class Device {
public void setType(String type) {
this.type = type;
}
@XmlElement
public List<Property> getProperties() {
return properties;
}

@ -109,7 +109,12 @@ public final class DeviceManagementDAOUtil {
deviceBO.setName(device.getName());
deviceBO.setDateOfEnrollment(device.getDateOfEnrolment());
deviceBO.setDateOfLastUpdate(device.getDateOfLastUpdate());
deviceBO.setStatus(Status.valueOf(String.valueOf(device.isStatus())));
if (!device.isStatus()){
deviceBO.setStatus(Status.INACTIVE);
}else{
deviceBO.setStatus(Status.ACTIVE);
}
deviceBO.setOwnerId(device.getOwner());
deviceBO.setOwnerShip(device.getOwnership());
deviceBO.setTenantId(DeviceManagementDAOUtil.getTenantId());

@ -41,7 +41,7 @@ import org.wso2.carbon.user.core.service.RealmService;
* unbind="unsetRealmService"
* @scr.reference name="device.manager.service"
* interface="org.wso2.carbon.device.mgt.common.spi.DeviceManagerService"
* cardinality="1..n"
* cardinality="0..n"
* policy="dynamic"
* bind="setDeviceManagerService"
* unbind="unsetDeviceManagerService"

@ -144,11 +144,6 @@
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
@ -171,6 +166,11 @@
<artifactId>org.wso2.carbon.logging</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<properties>
<cxf.version>2.6.1</cxf.version>

@ -18,18 +18,15 @@ package cdm.api.android;
import com.google.gson.JsonObject;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.*;
@Produces({ "application/json", "application/xml" })
@Consumes({ "application/json", "application/xml" })
@Path("/authenticate/")
public class Authentication {
@POST
@Path("/device/")
@Produces("application/json")
public String authenticateDevice(@FormParam("username") String username,
@FormParam("password") String password) {
JsonObject result = new JsonObject();
@ -39,7 +36,6 @@ public class Authentication {
@POST
@Path("/device/license")
@Produces("application/json")
public String getLicense() {
JsonObject result = new JsonObject();
result.addProperty("licenseText", "License Agreement");

@ -18,7 +18,7 @@ package cdm.api.android;
import cdm.api.android.util.AndroidAPIUtils;
import cdm.api.android.util.AndroidConstants;
import com.google.gson.Gson;
import cdm.api.android.util.Message;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -30,21 +30,25 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
/**
* Android Device Management REST-API implementation.
*/
@Produces({ "application/json", "application/xml" })
@Consumes({ "application/json", "application/xml" })
public class Device {
private static Log log = LogFactory.getLog(Device.class);
@GET
public Response getAllDevices() {
List<org.wso2.carbon.device.mgt.common.Device> result = null;
int status = 0;
public List<org.wso2.carbon.device.mgt.common.Device> getAllDevices() {
List<org.wso2.carbon.device.mgt.common.Device> devices = null;
String msg = "";
DeviceManagementService dmService;
try {
dmService = AndroidAPIUtils.getDeviceManagementService();
} finally {
@ -52,39 +56,27 @@ public class Device {
}
try {
if (dmService != null) {
result = dmService.getAllDevices(
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
status = 1;
devices = dmService.getAllDevices(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
Response.status(HttpStatus.SC_OK);
} else {
status = -1;
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
} catch (DeviceManagementException e) {
msg = "Error occurred while fetching the device list";
log.error(msg, e);
status = -1;
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
switch (status) {
case 1:
if(result!=null){
String response = new Gson().toJson(result);
return Response.status(HttpStatus.SC_OK).entity(response).build();
}
case -1:
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
}
return Response.status(HttpStatus.SC_NOT_FOUND).entity("Unable to fetch device list").build();
return devices;
}
@GET
@Path("{id}")
public Response getDevice(@PathParam("id") String id) {
int status = 0;
public org.wso2.carbon.device.mgt.common.Device getDevice(@PathParam("id") String id) {
String msg = "";
DeviceManagementService dmService;
org.wso2.carbon.device.mgt.common.Device device =
new org.wso2.carbon.device.mgt.common.Device();
org.wso2.carbon.device.mgt.common.Device device = new org.wso2.carbon.device.mgt.common.Device();
try {
dmService = AndroidAPIUtils.getDeviceManagementService();
} finally {
@ -94,37 +86,31 @@ public class Device {
try {
if (dmService != null) {
device = dmService.getDevice(deviceIdentifier);
status = 1;
if (device == null) {
Response.status(HttpStatus.SC_NOT_FOUND);
}
} else {
status = -1;
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
} catch (DeviceManagementException e) {
msg = "Error occurred while fetching the device information";
log.error(msg, e);
status = -1;
}
switch (status) {
case 1:
if(device!=null) {
String response = new Gson().toJson(device);
return Response.status(HttpStatus.SC_OK).entity(response).build();
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
break;
case -1:
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
}
return Response.status(HttpStatus.SC_NOT_FOUND).entity("Unable to fetch device information").build();
return device;
}
@PUT
@Path("{id}")
public Response updateDevice(@PathParam("id") String id, String jsonPayload) {
public Message updateDevice(@PathParam("id") String id, String jsonPayload) {
boolean result = false;
int status = 0;
String msg = "";
DeviceManagementService dmService;
Message responseMessage = new Message();
try {
dmService = AndroidAPIUtils.getDeviceManagementService();
} finally {
@ -135,25 +121,27 @@ public class Device {
try {
if (dmService != null) {
result = dmService.updateDeviceInfo(device);
status = 1;
if (result) {
Response.status(HttpStatus.SC_OK);
responseMessage.setResponseMessage("Device has modified");
} else {
Response.status(HttpStatus.SC_NOT_MODIFIED);
responseMessage.setResponseMessage("Update device has failed");
}
} else {
status = -1;
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
responseMessage.setResponseMessage(msg);
}
} catch (DeviceManagementException e) {
msg = "Error occurred while modifying the device information";
log.error(msg, e);
status = -1;
}
switch (status) {
case 1:
if (result) {
return Response.status(HttpStatus.SC_OK).entity("Device has modified").build();
}
break;
case -1:
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
responseMessage.setResponseMessage(msg);
}
return Response.status(HttpStatus.SC_NOT_MODIFIED).entity("Update device has failed").build();
return responseMessage;
}
}

@ -18,9 +18,10 @@ package cdm.api.android;
import cdm.api.android.util.AndroidAPIUtils;
import cdm.api.android.util.AndroidConstants;
import cdm.api.android.util.Message;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.httpclient.HttpStatus;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
@ -33,16 +34,21 @@ import javax.ws.rs.core.Response;
/**
* Android Device Enrollment REST-API implementation.
*/
@Produces({ "application/json", "application/xml" })
@Consumes({ "application/json", "application/xml" })
public class Enrollment {
private static Log log = LogFactory.getLog(Enrollment.class);
@POST
public Response enrollDevice(String jsonPayload) {
public Message enrollDevice(String jsonPayload) {
boolean result = false;
int status = 0;
String msg = "";
DeviceManagementService dmService;
Message responseMsg = new Message();
try {
dmService = AndroidAPIUtils.getDeviceManagementService();
} finally {
@ -52,35 +58,33 @@ public class Enrollment {
try {
if (dmService != null) {
result = dmService.enrollDevice(device);
status = 1;
Response.status(HttpStatus.SC_CREATED);
responseMsg.setResponseMessage("Device enrollment has succeeded");
return responseMsg;
} else {
status = -1;
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
return responseMsg;
}
} catch (DeviceManagementException e) {
msg = "Error occurred while enrolling the device";
log.error(msg, e);
status = -1;
responseMsg.setResponseMessage("Error occurred while enrolling the device");
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
return responseMsg;
}
switch (status) {
case 1:
if (result) {
return Response.status(HttpStatus.SC_CREATED).entity("Device enrollment has succeeded").build();
}
break;
case -1:
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
}
return Response.status(HttpStatus.SC_BAD_REQUEST).entity("Device enrollment has Failed").build();
}
@GET
@Path("{id}")
public Response isEnrolled(@PathParam("id") String id) {
public Message isEnrolled(@PathParam("id") String id) {
boolean result = false;
int status = 0;
String msg = "";
DeviceManagementService dmService;
Message responseMsg = new Message();
try {
dmService = AndroidAPIUtils.getDeviceManagementService();
} finally {
@ -90,35 +94,38 @@ public class Enrollment {
try {
if (dmService != null) {
result = dmService.isEnrolled(deviceIdentifier);
status = 1;
if (result) {
Response.status(HttpStatus.SC_OK);
responseMsg.setResponseMessage("Device already enroll");
} else {
status = -1;
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
Response.status(HttpStatus.SC_NOT_FOUND);
responseMsg.setResponseMessage("Device not enroll");
}
return responseMsg;
} else {
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
responseMsg.setResponseMessage(AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE);
return responseMsg;
}
} catch (DeviceManagementException e) {
msg = "Error occurred while checking enrollment of the device";
log.error(msg, e);
status = -1;
}
switch (status) {
case 1:
if (result) {
return Response.status(HttpStatus.SC_OK).entity(result).build();
responseMsg.setResponseMessage(msg);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
return responseMsg;
}
break;
case -1:
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
}
return Response.status(HttpStatus.SC_NOT_FOUND).entity(result).build();
}
@PUT
@Path("{id}")
public Response modifyEnrollment(@PathParam("id") String id, String jsonPayload) {
public Message modifyEnrollment(@PathParam("id") String id, String jsonPayload) {
boolean result = false;
int status = 0;
String msg = "";
DeviceManagementService dmService;
Message responseMsg = new Message();
try {
dmService = AndroidAPIUtils.getDeviceManagementService();
} finally {
@ -128,35 +135,40 @@ public class Enrollment {
try {
if (dmService != null) {
result = dmService.modifyEnrollment(device);
status = 1;
if (result) {
responseMsg.setResponseMessage("update device");
Response.status(HttpStatus.SC_OK);
}else{
responseMsg.setResponseMessage("Update enrollment has failed");
Response.status(HttpStatus.SC_NOT_MODIFIED);
}
} else {
status = -1;
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
responseMsg.setResponseMessage(msg);
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
return responseMsg;
} catch (DeviceManagementException e) {
msg = "Error occurred while modifying enrollment of the device";
log.error(msg, e);
status = -1;
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
responseMsg.setResponseMessage(msg);
return responseMsg;
}
switch (status) {
case 1:
if (result) {
return Response.status(HttpStatus.SC_OK).entity("Enrollment information has modified").build();
}
break;
case -1:
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
}
return Response.status(HttpStatus.SC_NOT_MODIFIED).entity("Update enrollment has failed").build();
}
@DELETE
@Path("{id}")
public Response disenrollDevice(@PathParam("id") String id) {
public Message disenrollDevice(@PathParam("id") String id) {
boolean result = false;
int status = 0;
String msg = "";
DeviceManagementService dmService;
Message responseMsg = new Message();
try {
dmService = AndroidAPIUtils.getDeviceManagementService();
} finally {
@ -166,25 +178,26 @@ public class Enrollment {
try {
if (dmService != null) {
result = dmService.disenrollDevice(deviceIdentifier);
status = 1;
if (result) {
responseMsg.setResponseMessage("Dis enrolled device");
Response.status(HttpStatus.SC_OK);
}else{
responseMsg.setResponseMessage("Device not found");
Response.status(HttpStatus.SC_NOT_FOUND);
}
} else {
status = -1;
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
msg = AndroidConstants.Messages.DEVICE_MANAGER_SERVICE_NOT_AVAILABLE;
responseMsg.setResponseMessage(msg);
}
return responseMsg;
} catch (DeviceManagementException e) {
msg = "Error occurred while disenrolling the device";
log.error(msg, e);
status = -1;
}
switch (status) {
case 1:
if (result) {
return Response.status(HttpStatus.SC_OK).entity(result).build();
}
break;
case -1:
return Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).entity(msg).build();
Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR);
responseMsg.setResponseMessage(msg);
return responseMsg;
}
return Response.status(HttpStatus.SC_NOT_FOUND).entity("Device not found").build();
}
}

@ -27,20 +27,33 @@
<jaxrs:serviceBeans>
<ref bean="serviceBean"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
<jaxrs:server id="deviceManagementService" address="/devices">
<jaxrs:serviceBeans>
<ref bean="deviceMgtServiceBean"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
<jaxrs:server id="enrollmentService" address="/enrollment">
<jaxrs:serviceBeans>
<ref bean="enrollmentServiceBean"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider"/>
</jaxrs:providers>
</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"/>
<bean id="testServiceBean" class="cdm.api.android.Test"/>
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
</beans>

Loading…
Cancel
Save