diff options
Diffstat (limited to 'cos/cosmgr/src')
5 files changed, 205 insertions, 0 deletions
diff --git a/cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CORSResponseFilter.java b/cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CORSResponseFilter.java new file mode 100644 index 0000000..50a118a --- /dev/null +++ b/cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CORSResponseFilter.java @@ -0,0 +1,25 @@ +package com.cablelabs.vcpe.cos.cosmgr; + +import java.io.IOException; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; +import javax.ws.rs.core.MultivaluedMap; + +// +// enable cross origin responses, otherwise we can't send rest requests from domain different than that of the server hosting our service +// + +public class CORSResponseFilter implements ContainerResponseFilter { + + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) + throws IOException { + + MultivaluedMap<String, Object> headers = responseContext.getHeaders(); + headers.add("Access-Control-Allow-Origin", "*"); + headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); + headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia"); + } + +} diff --git a/cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CoSJaxRsApplication.java b/cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CoSJaxRsApplication.java new file mode 100644 index 0000000..8d9c889 --- /dev/null +++ b/cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CoSJaxRsApplication.java @@ -0,0 +1,18 @@ +package com.cablelabs.vcpe.cos.cosmgr; + +import org.glassfish.jersey.server.ResourceConfig; + +// +// In order to avoid CORS issues, register our CORS Response filter +// + +public class CoSJaxRsApplication extends ResourceConfig { + + /** + * Register JAX-RS application components. + */ + public CoSJaxRsApplication() { + packages("com.cablelabs.vcpe.cos.cosmgr"); + register(CORSResponseFilter.class); + } +}
\ No newline at end of file diff --git a/cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CoSService.java b/cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CoSService.java new file mode 100644 index 0000000..e9390fc --- /dev/null +++ b/cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CoSService.java @@ -0,0 +1,130 @@ +package com.cablelabs.vcpe.cos.cosmgr; + +import com.cablelabs.vcpe.common.Dbg; +import com.cablelabs.vcpe.cos.cosbase.model.CoS; +import com.cablelabs.vcpe.cos.cosbase.repository.CoSRespositoryInMem; + +import javax.ws.rs.*; +import javax.ws.rs.core.GenericEntity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.List; + +/** + * Root resource (exposed at "myresource" path) + */ +@Path("cos") +public class CoSService +{ + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + //-------------------------------------------------------- + public Response create(CoS cos) + //-------------------------------------------------------- + { + if ( cos == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + + Dbg.p("\nADDING [" + cos.getId() + "] to cos repo"); + CoSRespositoryInMem.INSTANCE.add(cos); + CoSRespositoryInMem.INSTANCE.dump(0); + return Response.ok().entity(cos).build(); + } + + @PUT + @Path("{cosId}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + //-------------------------------------------------------- + public Response update(CoS cos) + //-------------------------------------------------------- + { + Dbg.p("\nUPDATING [" + cos.getId()+"]"); + CoSRespositoryInMem.INSTANCE.update(cos); + CoSRespositoryInMem.INSTANCE.dump(0); + return Response.ok().entity(cos).build(); + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("{cosId}") + //-------------------------------------------------------- + public Response get( @PathParam("cosId") String cosId ) + //-------------------------------------------------------- + { + if ( cosId == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + Dbg.p("\nRETRIEVING ["+cosId+"]"); + CoSRespositoryInMem.INSTANCE.dump(1); + CoS cos = CoSRespositoryInMem.INSTANCE.get(cosId); + if (cos == null) { + return Response.status(Response.Status.NOT_FOUND).build(); + } + cos.dump(1); + return Response.ok().entity(cos).build(); + } + + @DELETE + @Path("{cosId}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + //-------------------------------------------------------- + public Response delete(@PathParam("cosId") String cosId) + //-------------------------------------------------------- + + { + if ( cosId == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + Dbg.p("\nDELETE:" + cosId); + CoSRespositoryInMem.INSTANCE.delete(cosId); + CoSRespositoryInMem.INSTANCE.dump(0); + return Response.ok().build(); + } + + + @GET + @Path("list") + @Produces(MediaType.APPLICATION_JSON) + //-------------------------------------------------------- + public Response cosList() + //-------------------------------------------------------- + // get a list of all CoS objects + { + Dbg.p("\nCOS GET ALL:"); + List cosList = CoSRespositoryInMem.INSTANCE.getAll(); + if (cosList == null ) + { + return Response.status(Response.Status.NOT_FOUND).build(); + } + return Response.ok().entity(new GenericEntity<List<CoS>>(cosList) {}).build(); + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + //-------------------------------------------------------- + public CoS testGet() + //-------------------------------------------------------- + // simple get to check out json format + + { + CoS cos = new CoS(); + cos.setAllProps("testGetCoS", 100, 0.99, 17.43, 2.43, 0.01); + return cos; + } + + @GET + @Produces(MediaType.TEXT_PLAIN) + //-------------------------------------------------------- + public String ping() + //-------------------------------------------------------- + // simple ping to check connectivity + { + Dbg.p("In CoS Service simple get connection test server"); + return "... pingCos reponse"; + } +} diff --git a/cos/cosmgr/src/main/webapp/WEB-INF/web.xml b/cos/cosmgr/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..49cf6ad --- /dev/null +++ b/cos/cosmgr/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- This web.xml file is not required when using Servlet 3.0 container, +see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html --> +<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> + <servlet> + <servlet-name>Jersey Web Application</servlet-name> + <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> + <init-param> + <param-name>jersey.config.server.provider.packages</param-name> + <param-value>com.cablelabs.vcpe.cos</param-value> + </init-param> + <init-param> + <param-name>javax.ws.rs.Application</param-name> + <param-value>com.cablelabs.vcpe.cos.cosmgr.CoSJaxRsApplication</param-value> + </init-param> + <load-on-startup>1</load-on-startup> + </servlet> + <servlet-mapping> + <servlet-name>Jersey Web Application</servlet-name> + <url-pattern>/webapi/*</url-pattern> + </servlet-mapping> +</web-app> + diff --git a/cos/cosmgr/src/main/webapp/index.jsp b/cos/cosmgr/src/main/webapp/index.jsp new file mode 100644 index 0000000..a064b45 --- /dev/null +++ b/cos/cosmgr/src/main/webapp/index.jsp @@ -0,0 +1,8 @@ +<html> +<body> + <h2>Jersey RESTful Web Application!</h2> + <p><a href="webapi/myresource">Jersey resource</a> + <p>Visit <a href="http://jersey.java.net">Project Jersey website</a> + for more information on Jersey! +</body> +</html> |