From 40a4f54b9ff77c56bee48efa5bb1414491ccf72a Mon Sep 17 00:00:00 2001 From: harshanL Date: Tue, 9 Dec 2014 17:34:37 +0530 Subject: [PATCH] Android-REST API implementation. --- product/modules/agents/android/jax-rs/pom.xml | 29 ++++-- .../java/cdm/api/android/Authentication.java | 98 ++++--------------- .../src/main/java/cdm/api/android/Device.java | 48 +++++++++ .../main/java/cdm/api/android/Enrollment.java | 95 ++++++++++++++++++ .../cdm/api/android/util/AndroidAPIUtil.java | 42 ++++++++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 15 ++- .../jax-rs/src/main/webapp/WEB-INF/web.xml | 10 +- 7 files changed, 242 insertions(+), 95 deletions(-) create mode 100644 product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java create mode 100644 product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java create mode 100644 product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtil.java diff --git a/product/modules/agents/android/jax-rs/pom.xml b/product/modules/agents/android/jax-rs/pom.xml index da31fd2a72..c9d0901cf6 100644 --- a/product/modules/agents/android/jax-rs/pom.xml +++ b/product/modules/agents/android/jax-rs/pom.xml @@ -18,12 +18,19 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + org.wso2.cdmserver + wso2cdmserver-parent + 2.0.0-SNAPSHOT + ../../../../pom.xml + + 4.0.0 - org.wso2.appserver - android-api - 5.2.0 - JAX-RS Basic Demo - JAX-RS Basic Demo + org.wso2.carbon + cdm-android-api + 1.0.0-SNAPSHOT + JAX-RS Android API + JAX-RS Android API war @@ -40,13 +47,10 @@ maven-war-plugin 2.2 - WEB-INF/lib/*.jar ${project.artifactId} - - @@ -137,6 +141,15 @@ jsr311-api 1.1.1 + + com.google.code.gson + gson + 2.2.4 + + + org.wso2.carbon + org.wso2.carbon.utils + 2.6.1 diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java index bb8ad815f1..6bf19ed71d 100644 --- a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Authentication.java @@ -16,98 +16,36 @@ 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; @Path("/authenticate/") public class Authentication { @POST @Path("/device/") - @Produces("text/plain") + @Produces("application/json") public String authenticateDevice(@FormParam("username") String username, @FormParam("password") String password) { - System.out.println(password+"----invoking getCustomerName, Customer id is: "+username); - return "Isuru Suriarachchi"; + JsonObject result = new JsonObject(); + result.addProperty("senderId","jwwfowrjwqporqwrpqworpq"); + return result.toString(); } - // - // @GET - // @Path("/customers/{id}/") - // public Customer getCustomer(@PathParam("id") String id) { - // System.out.println("----invoking getCustomer, Customer id is: " + id); - // long idNumber = Long.parseLong(id); - // Customer c = customers.get(idNumber); - // return c; - // } - // - // @PUT - // @Path("/customers/") - // public Response updateCustomer(Customer customer) { - // System.out.println("----invoking updateCustomer, Customer name is: " + customer.getName()); - // Customer c = customers.get(customer.getId()); - // Response r; - // if (c != null) { - // customers.put(customer.getId(), customer); - // r = Response.ok().build(); - // } else { - // r = Response.notModified().build(); - // } - // - // return r; - // } - // - // @POST - // @Path("/customers/") - // public Response addCustomer(Customer customer) { - // System.out.println("----invoking addCustomer, Customer name is: " + customer.getName()); - // customer.setId(++currentId); - // - // customers.put(customer.getId(), customer); - // - // return Response.ok(customer).build(); - // } - // - // // Adding a new method to demonstrate Consuming and Producing text/plain - // - // - // @DELETE - // @Path("/customers/{id}/") - // public Response deleteCustomer(@PathParam("id") String id) { - // System.out.println("----invoking deleteCustomer, Customer id is: " + id); - // long idNumber = Long.parseLong(id); - // Customer c = customers.get(idNumber); - // - // Response r; - // if (c != null) { - // r = Response.ok().build(); - // customers.remove(idNumber); - // } else { - // r = Response.notModified().build(); - // } - // - // return r; - // } - // - // @Path("/orders/{orderId}/") - // public Order getOrder(@PathParam("orderId") String orderId) { - // System.out.println("----invoking getOrder, Order id is: " + orderId); - // long idNumber = Long.parseLong(orderId); - // Order c = orders.get(idNumber); - // return c; - // } - // - // final void init() { - // Customer c = new Customer(); - // c.setName("John"); - // c.setId(123); - // customers.put(c.getId(), c); - // - // Order o = new Order(); - // o.setDescription("order 223"); - // o.setId(223); - // orders.put(o.getId(), o); - // } + @POST + @Path("/device/license") + public String getLicense() { + return "License Agreement"; + } + @POST + @Path("/device/enroll") + public Response enrollDevice() { + return Response.status(201).entity("Registration Successful").build(); + } } diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java new file mode 100644 index 0000000000..abca85290d --- /dev/null +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Device.java @@ -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; + +import com.google.gson.JsonObject; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; + +import javax.ws.rs.*; +import javax.ws.rs.core.Response; + +/** + * Android Device Management REST-API implementation. + */ +@Path("/devices") +public class Device { + + @GET + public String getAllDevices() { + return "License Agreement"; + } + + @GET + @Path("{id}") + public String getDevice(@PathParam("id") String id) { + return "License Agreement"; + } + + @PUT + @Path("{id}") + public Response updateDevice(@PathParam("id") String id) { + return Response.status(201).entity("Registration Successful").build(); + } +} diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java new file mode 100644 index 0000000000..1699c78910 --- /dev/null +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/Enrollment.java @@ -0,0 +1,95 @@ +/* + * 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; + +import cdm.api.android.util.AndroidAPIUtil; +import com.google.gson.JsonObject; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.dto.Device; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; + +import javax.ws.rs.*; +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 + @Consumes("application/json") + public Response enrollDevice() { + JsonObject result = new JsonObject(); + result.addProperty("senderId","jwwfowrjwqporqwrpqworpq"); + CarbonContext context = CarbonContext.getThreadLocalCarbonContext(); + DeviceManagementService dmService = (DeviceManagementService) context.getOSGiService(DeviceManagementService.class,null); + Device device = AndroidAPIUtil.convertToDeviceDTO(result); + try { + dmService.enrollDevice(null); + } catch (DeviceManagementException e) { + String msg = "Error occurred while enrolling the device"; + log.error(msg, e); + } + return Response.status(201).entity("Registration Successful").build(); + } + + @GET + @Path("{id}") + public String isEnrolled(@PathParam("id") String id) { + CarbonContext context = CarbonContext.getThreadLocalCarbonContext(); + DeviceManagementService dmService = (DeviceManagementService) context.getOSGiService(DeviceManagementService.class,null); + try { + Device device = AndroidAPIUtil.convertToDeviceDTO(id); + dmService.isRegistered(null); + } catch (DeviceManagementException e) { + e.printStackTrace(); + } + return "true"; + } + + @PUT + @Consumes("application/json") + @Path("{id}") + public Response modifyEnrollment(@PathParam("id") String id) { + CarbonContext context = CarbonContext.getThreadLocalCarbonContext(); + DeviceManagementService dmService = (DeviceManagementService) context.getOSGiService(DeviceManagementService.class,null); + try { + dmService.isRegistered(null); + } catch (DeviceManagementException e) { + e.printStackTrace(); + } + return Response.status(201).entity("Registration Successful").build(); + } + + @DELETE + @Path("{id}") + public Response disenrollDevice(@PathParam("id") String id) { + CarbonContext context = CarbonContext.getThreadLocalCarbonContext(); + DeviceManagementService dmService = (DeviceManagementService) context.getOSGiService(DeviceManagementService.class,null); + try { + dmService.isRegistered(null); + } catch (DeviceManagementException e) { + e.printStackTrace(); + } + return Response.status(201).entity("Registration Successful").build(); + } +} diff --git a/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtil.java b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtil.java new file mode 100644 index 0000000000..0995f27cc9 --- /dev/null +++ b/product/modules/agents/android/jax-rs/src/main/java/cdm/api/android/util/AndroidAPIUtil.java @@ -0,0 +1,42 @@ +/* + * 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; + +import com.google.gson.JsonObject; +import org.wso2.carbon.device.mgt.core.dto.Device; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.mobile.impl.MobileDeviceManagementConstants; + +/** + * AndroidAPIUtil class provides utility function used by Android REST-API classes. + */ +public class AndroidAPIUtil { + + public static Device convertToDeviceDTO(JsonObject json){ + Device device = new Device(); + return device; + } + + public static Device convertToDeviceDTO(String deviceId){ + Device device = new Device(); + DeviceType type = new DeviceType(); + device.setId(deviceId); + type.setName(MobileDeviceManagementConstants.MOBILE_DEVICE_TYPE_ANDROID); + device.setDeviceType(type); + return device; + } +} diff --git a/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml b/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml index ceb430b10f..230a2b45e1 100644 --- a/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/product/modules/agents/android/jax-rs/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -1,7 +1,7 @@ - JAX-WS/JAX-RS Webapp + CDM-Android-API JAX-WS/JAX-RS Endpoint JAX-WS/JAX-RS Servlet JAXServlet org.apache.cxf.transport.servlet.CXFServlet - + service-list-stylesheet servicelist.css @@ -31,7 +31,7 @@ JAXServlet - /services/* + /* 60