2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics;
10 import javax.ws.rs.core.MediaType;
11 import javax.ws.rs.core.MultivaluedMap;
13 import com.google.common.base.Preconditions;
14 import com.sun.jersey.api.client.Client;
15 import com.sun.jersey.api.client.ClientHandlerException;
16 import com.sun.jersey.api.client.ClientResponse;
17 import com.sun.jersey.api.client.WebResource;
18 import com.sun.jersey.api.client.config.ClientConfig;
19 import com.sun.jersey.api.client.config.DefaultClientConfig;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class JsonRestClient {
25 private static final Logger LOG = LoggerFactory.getLogger(JsonRestClient.class);
28 private ClientConfig clientConfig;
29 private Client client;
30 private WebResource webResource;
32 public JsonRestClient(String uri, Integer connectTimeout, Integer readTimeout) {
33 Preconditions.checkNotNull(uri);
36 clientConfig = new DefaultClientConfig();
37 clientConfig.getProperties()
38 .put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, connectTimeout);
39 clientConfig.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, readTimeout);
41 client = Client.create(clientConfig);
42 webResource = client.resource(this.uri);
45 public String getHost() {
46 return webResource.getURI().getHost();
49 public JsonRestClientResponse get(String path) throws ClientHandlerException {
50 return get(path, null);
53 public JsonRestClientResponse get(String path, MultivaluedMap<String, String> params)
54 throws ClientHandlerException {
55 ClientResponse response;
56 WebResource r = this.webResource.path(path);
58 response = r.accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
60 response = r.queryParams(params)
61 .accept(MediaType.APPLICATION_JSON_TYPE)
62 .get(ClientResponse.class);
64 return new JsonRestClientResponse(response);
67 public JsonRestClientResponse post(String path, String someJson) throws ClientHandlerException {
68 ClientResponse response;
69 response = webResource.path(path)
70 .accept(MediaType.APPLICATION_JSON_TYPE)
71 .type(MediaType.APPLICATION_JSON_TYPE)
72 .post(ClientResponse.class, someJson);
73 return new JsonRestClientResponse(response);
76 public JsonRestClientResponse put(String path, String someJson) throws ClientHandlerException {
77 ClientResponse response;
78 response = webResource.path(path)
79 .accept(MediaType.APPLICATION_JSON_TYPE)
80 .type(MediaType.APPLICATION_JSON_TYPE)
81 .put(ClientResponse.class, someJson);
82 return new JsonRestClientResponse(response);
85 public JsonRestClientResponse delete(String path) throws ClientHandlerException {
86 ClientResponse response;
87 response = webResource.path(path)
88 .accept(MediaType.APPLICATION_JSON_TYPE)
89 .delete(ClientResponse.class);
90 return new JsonRestClientResponse(response);