Android-REST API implementation.

revert-70aa11f8
harshanL 10 years ago
parent 18f858ab3a
commit 40a4f54b9f

@ -18,12 +18,19 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.wso2.cdmserver</groupId>
<artifactId>wso2cdmserver-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../../../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.wso2.appserver</groupId> <groupId>org.wso2.carbon</groupId>
<artifactId>android-api</artifactId> <artifactId>cdm-android-api</artifactId>
<version>5.2.0</version> <version>1.0.0-SNAPSHOT</version>
<name>JAX-RS Basic Demo</name> <name>JAX-RS Android API</name>
<description>JAX-RS Basic Demo</description> <description>JAX-RS Android API</description>
<packaging>war</packaging> <packaging>war</packaging>
<build> <build>
@ -40,13 +47,10 @@
<artifactId>maven-war-plugin</artifactId> <artifactId>maven-war-plugin</artifactId>
<version>2.2</version> <version>2.2</version>
<configuration> <configuration>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes> <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
<warName>${project.artifactId}</warName> <warName>${project.artifactId}</warName>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
@ -137,6 +141,15 @@
<artifactId>jsr311-api</artifactId> <artifactId>jsr311-api</artifactId>
<version>1.1.1</version> <version>1.1.1</version>
</dependency> </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>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<cxf.version>2.6.1</cxf.version> <cxf.version>2.6.1</cxf.version>

@ -16,98 +16,36 @@
package cdm.api.android; package cdm.api.android;
import com.google.gson.JsonObject;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.FormParam; import javax.ws.rs.FormParam;
import javax.ws.rs.core.Response;
@Path("/authenticate/") @Path("/authenticate/")
public class Authentication { public class Authentication {
@POST @POST
@Path("/device/") @Path("/device/")
@Produces("text/plain") @Produces("application/json")
public String authenticateDevice(@FormParam("username") String username, @FormParam("password") String password) { public String authenticateDevice(@FormParam("username") String username, @FormParam("password") String password) {
System.out.println(password+"----invoking getCustomerName, Customer id is: "+username); JsonObject result = new JsonObject();
return "Isuru Suriarachchi"; result.addProperty("senderId","jwwfowrjwqporqwrpqworpq");
return result.toString();
} }
// @POST
// @GET @Path("/device/license")
// @Path("/customers/{id}/") public String getLicense() {
// public Customer getCustomer(@PathParam("id") String id) { return "License Agreement";
// 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/enroll")
public Response enrollDevice() {
return Response.status(201).entity("Registration Successful").build();
}
} }

@ -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();
}
}

@ -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();
}
}

@ -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;
}
}

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright 2011-2012 WSO2, Inc. (http://wso2.com) ~ Copyright 2014 WSO2, Inc. (http://wso2.com)
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
@ -28,7 +28,18 @@
<ref bean="serviceBean"/> <ref bean="serviceBean"/>
</jaxrs:serviceBeans> </jaxrs:serviceBeans>
</jaxrs:server> </jaxrs:server>
<jaxrs:server id="deviceManagementService" address="/devices">
<jaxrs:serviceBeans>
<ref bean="deviceMgtServiceBean"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<jaxrs:server id="enrollmentService" address="/enrollment">
<jaxrs:serviceBeans>
<ref bean="enrollmentServiceBean"/>
</jaxrs:serviceBeans>
</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="enrollmentServiceBean" class="cdm.api.android.Enrollment"/>
</beans> </beans>

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright 2011-2012 WSO2, Inc. (http://wso2.com) ~ Copyright 2014 WSO2, Inc. (http://wso2.com)
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at ~ You may obtain a copy of the License at
~ ~1-2012
~ http://www.apache.org/licenses/LICENSE-2.0 ~ http://www.apache.org/licenses/LICENSE-2.0
~ ~
~ Unless required by applicable law or agreed to in writing, software ~ Unless required by applicable law or agreed to in writing, software
@ -15,7 +15,7 @@
~ limitations under the License. ~ limitations under the License.
--> -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>JAX-WS/JAX-RS Webapp</display-name> <display-name>CDM-Android-API</display-name>
<servlet> <servlet>
<description>JAX-WS/JAX-RS Endpoint</description> <description>JAX-WS/JAX-RS Endpoint</description>
<display-name>JAX-WS/JAX-RS Servlet</display-name> <display-name>JAX-WS/JAX-RS Servlet</display-name>
@ -31,7 +31,7 @@
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>JAXServlet</servlet-name> <servlet-name>JAXServlet</servlet-name>
<url-pattern>/services/*</url-pattern> <url-pattern>/*</url-pattern>
</servlet-mapping> </servlet-mapping>
<session-config> <session-config>
<session-timeout>60</session-timeout> <session-timeout>60</session-timeout>

Loading…
Cancel
Save