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 | |
| 18 | import org.onosproject.xosclient.api.XosAccess; |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 21 | |
| 22 | import javax.ws.rs.client.Client; |
| 23 | import javax.ws.rs.client.ClientBuilder; |
| 24 | import javax.ws.rs.client.Invocation; |
| 25 | import javax.ws.rs.client.WebTarget; |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 26 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 27 | |
| 28 | import static com.google.common.net.MediaType.JSON_UTF_8; |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 29 | import static java.net.HttpURLConnection.HTTP_OK; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * XOS common REST API implementation. |
| 33 | */ |
| 34 | public class XosApi { |
| 35 | |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 36 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 37 | |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 38 | protected static final String EMPTY_STRING = ""; |
Hyunsun Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 39 | protected static final String EMPTY_JSON_STRING = "{}"; |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 40 | |
| 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 Moon | e0f3e28 | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 84 | 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 Moon | 141892e | 2016-04-26 15:50:33 -0500 | [diff] [blame] | 91 | return EMPTY_JSON_STRING; |
| 92 | } |
Hyunsun Moon | 7ad9220 | 2016-04-20 10:36:02 -0700 | [diff] [blame] | 93 | return builder.get(String.class); |
| 94 | } |
| 95 | } |