From ed73dbf357aff41edcbab401a94e5fbc266d9391 Mon Sep 17 00:00:00 2001 From: Mufaddal Makati Date: Fri, 7 Aug 2015 13:09:49 -0700 Subject: First Commit --- .../vcpe/cos/cosmgr/CORSResponseFilter.java | 25 ++++ .../vcpe/cos/cosmgr/CoSJaxRsApplication.java | 18 +++ .../com/cablelabs/vcpe/cos/cosmgr/CoSService.java | 130 +++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CORSResponseFilter.java create mode 100644 cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CoSJaxRsApplication.java create mode 100644 cos/cosmgr/src/main/java/com/cablelabs/vcpe/cos/cosmgr/CoSService.java (limited to 'cos/cosmgr/src/main/java/com/cablelabs') 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 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>(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"; + } +} -- cgit 1.2.3-korg