blob: 4a9ec092a716af584f3bc0c1dc89fd791d5321e9 [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
18import org.onosproject.xosclient.api.XosAccess;
Hyunsun Moon141892e2016-04-26 15:50:33 -050019import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070021
22import javax.ws.rs.client.Client;
23import javax.ws.rs.client.ClientBuilder;
24import javax.ws.rs.client.Invocation;
25import javax.ws.rs.client.WebTarget;
Hyunsun Moon141892e2016-04-26 15:50:33 -050026import javax.ws.rs.core.Response;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070027
28import static com.google.common.net.MediaType.JSON_UTF_8;
Hyunsun Moon141892e2016-04-26 15:50:33 -050029import static java.net.HttpURLConnection.HTTP_OK;
Hyunsun Moon7ad92202016-04-20 10:36:02 -070030
31/**
32 * XOS common REST API implementation.
33 */
34public class XosApi {
35
Hyunsun Moon141892e2016-04-26 15:50:33 -050036 private final Logger log = LoggerFactory.getLogger(getClass());
37
Hyunsun Moon7ad92202016-04-20 10:36:02 -070038 protected static final String EMPTY_STRING = "";
Hyunsun Moon141892e2016-04-26 15:50:33 -050039 protected static final String EMPTY_JSON_STRING = "{}";
Hyunsun Moon7ad92202016-04-20 10:36:02 -070040
41 protected final String baseUrl;
42 protected final XosAccess access;
43 protected final Client client;
44
45 /**
46 * Default constructor.
47 *
48 * @param baseUrl base url of this api
49 * @param xosAccess xos access
50 */
51 public XosApi(String baseUrl, XosAccess xosAccess) {
52 this.baseUrl = baseUrl;
53 this.access = xosAccess;
54 this.client = ClientBuilder.newClient();
55 }
56
57 /**
58 * Returns the access of this api.
59 *
60 * @return xos access
61 */
62 public XosAccess access() {
63 return this.access;
64 }
65
66 /**
67 * Returns the base url of this api.
68 *
69 * @return base url
70 */
71 public String baseUrl() {
72 return this.baseUrl;
73 }
74
75 /**
76 * Returns response of the REST get operation with a given additional path.
77 *
78 * @param path path or null
79 * @return response json string
80 */
81 public String restGet(String path) {
82 WebTarget wt = client.target(access.endpoint() + baseUrl).path(path);
83 Invocation.Builder builder = wt.request(JSON_UTF_8.toString());
Hyunsun Moone0f3e282016-05-13 18:58:35 -070084 try {
85 Response response = builder.get();
86 if (response.getStatus() != HTTP_OK) {
87 log.warn("Failed to get resource {}", access.endpoint() + baseUrl + path);
88 return EMPTY_JSON_STRING;
89 }
90 } catch (javax.ws.rs.ProcessingException e) {
Hyunsun Moon141892e2016-04-26 15:50:33 -050091 return EMPTY_JSON_STRING;
92 }
Hyunsun Moon7ad92202016-04-20 10:36:02 -070093 return builder.get(String.class);
94 }
95}