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/svc/svcbase/client/EplClient.java | 149 +++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 svc/svcbase/src/main/java/com/cablelabs/vcpe/svc/svcbase/client/EplClient.java (limited to 'svc/svcbase/src/main/java/com/cablelabs/vcpe/svc/svcbase/client') diff --git a/svc/svcbase/src/main/java/com/cablelabs/vcpe/svc/svcbase/client/EplClient.java b/svc/svcbase/src/main/java/com/cablelabs/vcpe/svc/svcbase/client/EplClient.java new file mode 100644 index 0000000..9bc7368 --- /dev/null +++ b/svc/svcbase/src/main/java/com/cablelabs/vcpe/svc/svcbase/client/EplClient.java @@ -0,0 +1,149 @@ +package com.cablelabs.vcpe.svc.svcbase.client; + +import com.cablelabs.vcpe.svc.svcbase.model.Epl; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.List; + +/** + * Created by steve on 6/8/15. + */ +public class EplClient { + + private Client client; // provided by Jersey + + public EplClient() { + client = ClientBuilder.newClient(); + } + + //-------------------------------------------------------- + public Epl create(Epl epl) + //-------------------------------------------------------- + { + WebTarget target =client.target("http://localhost:9090/svcmgr/webapi/svc/"); + Response response = target.path("epl") + .request(MediaType.APPLICATION_JSON) + .post(Entity.entity(epl, MediaType.APPLICATION_JSON)); + + if (response.getStatus() != 200 ) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + return response.readEntity(Epl.class); + } + + //-------------------------------------------------------- + public Epl update(Epl epl) + //-------------------------------------------------------- + { + WebTarget target =client.target("http://localhost:9090/svcmgr/webapi/svc/"); + Response response = target.path("epl/"+ epl.getId()) + .request(MediaType.APPLICATION_JSON) + .put(Entity.entity(epl, MediaType.APPLICATION_JSON)); + + if (response.getStatus() != 200 ) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + return response.readEntity(Epl.class); + } + + //-------------------------------------------------------- + public Epl get(String eplId) + //-------------------------------------------------------- + // get Epl of specified ID + + { + WebTarget target = client.target("http://localhost:9090/svcmgr/webapi/svc"); + + Response response = target.path("epl/"+eplId).request(MediaType.APPLICATION_JSON).get(Response.class); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + + //return response; + return response.readEntity(Epl.class); + } + + //-------------------------------------------------------- + public List getAll() + //-------------------------------------------------------- + // get a list of all Epl instances + { + + WebTarget target = client.target("http://localhost:9090/svcmgr/webapi/svc/"); + + // Can I do this with a Response, so that I can check for errors + List response = target.path("epl/list") + .request(MediaType.APPLICATION_JSON) + .get(new GenericType>() { + }); + if (response == null) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException("there was an error on the server."); + } + return response; + } + + //-------------------------------------------------------- + public void delete(String eplId) + //-------------------------------------------------------- + // delete Epl of specified ID + { + WebTarget target = client.target("http://localhost:9090/svcmgr/webapi/svc/"); + Response response = target.path("epl/"+eplId).request(MediaType.APPLICATION_JSON).delete(); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + } + + //-------------------------------------------------------- + public Epl testGet() + //-------------------------------------------------------- + // test marshaling of Epl class from server json + + { + WebTarget target = client.target("http://localhost:9090/svcmgr/webapi/svc/"); + + Response response = target.path("epl").request(MediaType.APPLICATION_JSON).get(Response.class); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + + //return response; + return response.readEntity(Epl.class); + } + + //-------------------------------------------------------- + public String ping() + //-------------------------------------------------------- + // test connectivity + { + + WebTarget target = client.target("http://localhost:9090/svcmgr/webapi/svc/"); + + Response response = target.path("epl").request(MediaType.TEXT_PLAIN).get(); + if (response.getStatus() != 200) // figure out how to use Status.OK + { + // in production you can be more specific based on reponse code, id, etc + throw new RuntimeException(response.getStatus() + ": there was an error on the server."); + } + + return response.readEntity(String.class); + } +} -- cgit 1.2.3-korg