|
1 | 1 | // tag::copyright[]
|
2 | 2 | /*******************************************************************************
|
3 |
| - * Copyright (c) 2017, 2022 IBM Corporation and others. |
| 3 | + * Copyright (c) 2017, 2023 IBM Corporation and others. |
4 | 4 | * All rights reserved. This program and the accompanying materials
|
5 |
| - * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
6 | 6 | * which accompanies this distribution, and is available at
|
7 |
| - * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * http://www.eclipse.org/legal/epl-2.0/ |
8 | 8 | *
|
9 |
| - * Contributors: |
10 |
| - * IBM Corporation - Initial implementation |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
11 | 10 | *******************************************************************************/
|
12 | 11 | // end::copyright[]
|
13 | 12 | package io.openliberty.guides.inventory.client;
|
|
30 | 29 | @RequestScoped
|
31 | 30 | public class SystemClient {
|
32 | 31 |
|
33 |
| - // Constants for building URI to the system service. |
34 |
| - private final String SYSTEM_PROPERTIES = "/system/properties"; |
35 |
| - private final String PROTOCOL = "http"; |
| 32 | + // Constants for building URI to the system service. |
| 33 | + private final String SYSTEM_PROPERTIES = "/system/properties"; |
| 34 | + private final String PROTOCOL = "http"; |
36 | 35 |
|
37 |
| - @Inject |
38 |
| - @ConfigProperty(name = "default.http.port") |
39 |
| - String DEFAULT_PORT; |
| 36 | + @Inject |
| 37 | + @ConfigProperty(name = "default.http.port") |
| 38 | + String DEFAULT_PORT; |
40 | 39 |
|
41 |
| - // Wrapper function that gets properties |
42 |
| - public Properties getProperties(String hostname) { |
43 |
| - String url = buildUrl(PROTOCOL, hostname, Integer.valueOf(DEFAULT_PORT), |
44 |
| - SYSTEM_PROPERTIES); |
45 |
| - Builder clientBuilder = buildClientBuilder(url); |
46 |
| - return getPropertiesHelper(clientBuilder); |
47 |
| - } |
48 |
| - |
49 |
| - // tag::doc[] |
50 |
| - /** |
51 |
| - * Builds the URI string to the system service for a particular host. |
52 |
| - * @param protocol |
53 |
| - * - http or https. |
54 |
| - * @param host |
55 |
| - * - name of host. |
56 |
| - * @param port |
57 |
| - * - port number. |
58 |
| - * @param path |
59 |
| - * - Note that the path needs to start with a slash!!! |
60 |
| - * @return String representation of the URI to the system properties service. |
61 |
| - */ |
62 |
| - // end::doc[] |
63 |
| - protected String buildUrl(String protocol, String host, int port, String path) { |
64 |
| - try { |
65 |
| - URI uri = new URI(protocol, null, host, port, path, null, null); |
66 |
| - return uri.toString(); |
67 |
| - } catch (Exception e) { |
68 |
| - System.err.println("Exception thrown while building the URL: " + e.getMessage()); |
69 |
| - return null; |
| 40 | + // Wrapper function that gets properties |
| 41 | + public Properties getProperties(String hostname) { |
| 42 | + Properties properties = null; |
| 43 | + Client client = ClientBuilder.newClient(); |
| 44 | + try { |
| 45 | + Builder builder = getBuilder(hostname, client); |
| 46 | + properties = getPropertiesHelper(builder); |
| 47 | + } catch (Exception e) { |
| 48 | + System.err.println( |
| 49 | + "Exception thrown while getting properties: " + e.getMessage()); |
| 50 | + } finally { |
| 51 | + client.close(); |
| 52 | + } |
| 53 | + return properties; |
70 | 54 | }
|
71 |
| - } |
72 | 55 |
|
73 |
| - // Method that creates the client builder |
74 |
| - protected Builder buildClientBuilder(String urlString) { |
75 |
| - try { |
76 |
| - Client client = ClientBuilder.newClient(); |
77 |
| - Builder builder = client.target(urlString).request(); |
78 |
| - return builder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); |
79 |
| - } catch (Exception e) { |
80 |
| - System.err.println("Exception thrown while building the client: " |
81 |
| - + e.getMessage()); |
82 |
| - return null; |
| 56 | + // Method that creates the client builder |
| 57 | + private Builder getBuilder(String hostname, Client client) throws Exception { |
| 58 | + URI uri = new URI( |
| 59 | + PROTOCOL, null, hostname, Integer.valueOf(DEFAULT_PORT), |
| 60 | + SYSTEM_PROPERTIES, null, null); |
| 61 | + String urlString = uri.toString(); |
| 62 | + Builder builder = client.target(urlString).request(); |
| 63 | + builder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); |
| 64 | + return builder; |
83 | 65 | }
|
84 |
| - } |
85 | 66 |
|
86 |
| - // Helper method that processes the request |
87 |
| - protected Properties getPropertiesHelper(Builder builder) { |
88 |
| - try { |
89 |
| - Response response = builder.get(); |
90 |
| - if (response.getStatus() == Status.OK.getStatusCode()) { |
91 |
| - return response.readEntity(Properties.class); |
92 |
| - } else { |
93 |
| - System.err.println("Response Status is not OK."); |
94 |
| - } |
95 |
| - } catch (RuntimeException e) { |
96 |
| - System.err.println("Runtime exception: " + e.getMessage()); |
97 |
| - } catch (Exception e) { |
98 |
| - System.err.println("Exception thrown while invoking the request: " |
99 |
| - + e.getMessage()); |
| 67 | + // Helper method that processes the request |
| 68 | + private Properties getPropertiesHelper(Builder builder) throws Exception { |
| 69 | + Response response = builder.get(); |
| 70 | + if (response.getStatus() == Status.OK.getStatusCode()) { |
| 71 | + return response.readEntity(Properties.class); |
| 72 | + } else { |
| 73 | + System.err.println("Response Status is not OK."); |
| 74 | + return null; |
| 75 | + } |
100 | 76 | }
|
101 |
| - return null; |
102 |
| - } |
103 | 77 |
|
104 | 78 | }
|
0 commit comments