aboutsummaryrefslogtreecommitdiffstats
path: root/svc/svcbase/src/main/java/com/cablelabs/vcpe/svc/svcbase/client/EplClient.java
blob: 9bc7368cd2d0cfe080200c3237a274608f5850f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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<Epl> 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<Epl> response = target.path("epl/list")
                .request(MediaType.APPLICATION_JSON)
                .get(new GenericType<List<Epl>>() {
                });
        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);
    }
}