blob: 47738e78037d8b785dfe4499a53bc8bf3505f572 [file] [log] [blame]
Hyunsun Moon7ad92202016-04-20 10:36:02 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.xosclient.impl;
17
Hyunsun Moon2e39d3b2016-06-22 14:41:48 -070018import org.glassfish.jersey.client.ClientProperties;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070019import org.onosproject.xosclient.api.XosAccess;
Hyunsun Moon141892e2016-04-26 15:50:33 -050020import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070022
23import javax.ws.rs.client.Client;
24import javax.ws.rs.client.ClientBuilder;
25import javax.ws.rs.client.Invocation;
26import javax.ws.rs.client.WebTarget;
Hyunsun Moon141892e2016-04-26 15:50:33 -050027import javax.ws.rs.core.Response;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070028
29import static com.google.common.net.MediaType.JSON_UTF_8;
Hyunsun Moon141892e2016-04-26 15:50:33 -050030import static java.net.HttpURLConnection.HTTP_OK;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070031
32/**
33 * XOS common REST API implementation.
34 */
35public class XosApi {
36
Hyunsun Moon141892e2016-04-26 15:50:33 -050037 private final Logger log = LoggerFactory.getLogger(getClass());
38
Hyunsun Moon7ad92202016-04-20 10:36:02 -070039 protected static final String EMPTY_STRING = "";
Hyunsun Moon141892e2016-04-26 15:50:33 -050040 protected static final String EMPTY_JSON_STRING = "{}";
Hyunsun Moon7ad92202016-04-20 10:36:02 -070041
42 protected final String baseUrl;
43 protected final XosAccess access;
44 protected final Client client;
45
Hyunsun Moon2e39d3b2016-06-22 14:41:48 -070046 private static final int DEFAULT_TIMEOUT_MS = 2000;
47
Hyunsun Moon7ad92202016-04-20 10:36:02 -070048 /**
49 * Default constructor.
50 *
51 * @param baseUrl base url of this api
52 * @param xosAccess xos access
53 */
54 public XosApi(String baseUrl, XosAccess xosAccess) {
55 this.baseUrl = baseUrl;
56 this.access = xosAccess;
57 this.client = ClientBuilder.newClient();
Hyunsun Moon2e39d3b2016-06-22 14:41:48 -070058
59 client.property(ClientProperties.CONNECT_TIMEOUT, DEFAULT_TIMEOUT_MS);
60 client.property(ClientProperties.READ_TIMEOUT, DEFAULT_TIMEOUT_MS);
Hyunsun Moon7ad92202016-04-20 10:36:02 -070061 }
62
63 /**
64 * Returns the access of this api.
65 *
66 * @return xos access
67 */
68 public XosAccess access() {
69 return this.access;
70 }
71
72 /**
73 * Returns the base url of this api.
74 *
75 * @return base url
76 */
77 public String baseUrl() {
78 return this.baseUrl;
79 }
80
81 /**
82 * Returns response of the REST get operation with a given additional path.
83 *
84 * @param path path or null
85 * @return response json string
86 */
87 public String restGet(String path) {
88 WebTarget wt = client.target(access.endpoint() + baseUrl).path(path);
89 Invocation.Builder builder = wt.request(JSON_UTF_8.toString());
Hyunsun Moone0f3e282016-05-13 18:58:35 -070090 try {
91 Response response = builder.get();
92 if (response.getStatus() != HTTP_OK) {
93 log.warn("Failed to get resource {}", access.endpoint() + baseUrl + path);
94 return EMPTY_JSON_STRING;
95 }
96 } catch (javax.ws.rs.ProcessingException e) {
Hyunsun Moon141892e2016-04-26 15:50:33 -050097 return EMPTY_JSON_STRING;
98 }
Hyunsun Moon7ad92202016-04-20 10:36:02 -070099 return builder.get(String.class);
100 }
101}