Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.xosclient.impl; |
| 17 | |
Hyunsun Moon | 2e39d3b | 2016-06-22 14:41:48 -0700 | [diff] [blame] | 18 | import org.glassfish.jersey.client.ClientProperties; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 19 | import org.onosproject.xosclient.api.XosAccess; |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 22 | |
| 23 | import javax.ws.rs.client.Client; |
| 24 | import javax.ws.rs.client.ClientBuilder; |
| 25 | import javax.ws.rs.client.Invocation; |
| 26 | import javax.ws.rs.client.WebTarget; |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 27 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 28 | |
| 29 | import static com.google.common.net.MediaType.JSON_UTF_8; |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 30 | import static java.net.HttpURLConnection.HTTP_OK; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * XOS common REST API implementation. |
| 34 | */ |
| 35 | public class XosApi { |
| 36 | |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 38 | |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 39 | protected static final String EMPTY_STRING = ""; |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 40 | protected static final String EMPTY_JSON_STRING = "{}"; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 41 | |
| 42 | protected final String baseUrl; |
| 43 | protected final XosAccess access; |
| 44 | protected final Client client; |
| 45 | |
Hyunsun Moon | 2e39d3b | 2016-06-22 14:41:48 -0700 | [diff] [blame] | 46 | private static final int DEFAULT_TIMEOUT_MS = 2000; |
| 47 | |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 48 | /** |
| 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 Moon | 2e39d3b | 2016-06-22 14:41:48 -0700 | [diff] [blame] | 58 | |
| 59 | client.property(ClientProperties.CONNECT_TIMEOUT, DEFAULT_TIMEOUT_MS); |
| 60 | client.property(ClientProperties.READ_TIMEOUT, DEFAULT_TIMEOUT_MS); |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 61 | } |
| 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 Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 90 | 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 Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 97 | return EMPTY_JSON_STRING; |
| 98 | } |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 99 | return builder.get(String.class); |
| 100 | } |
| 101 | } |