|
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; |
|
28 | 27 | @ApplicationScoped |
29 | 28 | public class SystemClient { |
30 | 29 |
|
31 | | - // Constants for building URI to the system service. |
32 | | - private final String SYSTEM_PROPERTIES = "/system/properties"; |
33 | | - private final String PROTOCOL = "http"; |
| 30 | + // Constants for building URI to the system service. |
| 31 | + private final String SYSTEM_PROPERTIES = "/system/properties"; |
| 32 | + private final String PROTOCOL = "http"; |
34 | 33 |
|
35 | | - @Inject |
36 | | - @ConfigProperty(name = "system.http.port") |
37 | | - String SYS_HTTP_PORT; |
| 34 | + @Inject |
| 35 | + @ConfigProperty(name = "system.http.port") |
| 36 | + String SYS_HTTP_PORT; |
38 | 37 |
|
39 | | - // Wrapper function that gets properties |
40 | | - public Properties getProperties(String hostname) { |
41 | | - String url = buildUrl( |
42 | | - PROTOCOL, hostname, Integer.valueOf(SYS_HTTP_PORT), SYSTEM_PROPERTIES); |
43 | | - Builder clientBuilder = buildClientBuilder(url); |
44 | | - return getPropertiesHelper(clientBuilder); |
45 | | - } |
46 | | - |
47 | | - // tag::doc[] |
48 | | - /** |
49 | | - * Builds the URI string to the system service for a particular host. |
50 | | - * @param protocol |
51 | | - * - http or https. |
52 | | - * @param host |
53 | | - * - name of host. |
54 | | - * @param port |
55 | | - * - port number. |
56 | | - * @param path |
57 | | - * - Note that the path needs to start with a slash!!! |
58 | | - * @return String representation of the URI to the system properties service. |
59 | | - */ |
60 | | - // end::doc[] |
61 | | - protected String buildUrl(String protocol, String host, int port, String path) { |
62 | | - try { |
63 | | - URI uri = new URI(protocol, null, host, port, path, null, null); |
64 | | - return uri.toString(); |
65 | | - } catch (Exception e) { |
66 | | - System.err.println("Exception thrown while building the URL: " + e.getMessage()); |
67 | | - return null; |
| 38 | + // Wrapper function that gets properties |
| 39 | + public Properties getProperties(String hostname) { |
| 40 | + Properties properties = null; |
| 41 | + Client client = ClientBuilder.newClient(); |
| 42 | + try { |
| 43 | + Builder builder = getBuilder(hostname, client); |
| 44 | + properties = getPropertiesHelper(builder); |
| 45 | + } catch (Exception e) { |
| 46 | + System.err.println( |
| 47 | + "Exception thrown while getting properties: " + e.getMessage()); |
| 48 | + } finally { |
| 49 | + client.close(); |
| 50 | + } |
| 51 | + return properties; |
68 | 52 | } |
69 | | - } |
70 | 53 |
|
71 | | - // Method that creates the client builder |
72 | | - protected Builder buildClientBuilder(String urlString) { |
73 | | - try { |
74 | | - Client client = ClientBuilder.newClient(); |
75 | | - Builder builder = client.target(urlString).request(); |
76 | | - return builder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); |
77 | | - } catch (Exception e) { |
78 | | - System.err.println( |
79 | | - "Exception thrown while building the client: " + e.getMessage()); |
80 | | - return null; |
| 54 | + // Method that creates the client builder |
| 55 | + private Builder getBuilder(String hostname, Client client) throws Exception { |
| 56 | + URI uri = new URI( |
| 57 | + PROTOCOL, null, hostname, Integer.valueOf(SYS_HTTP_PORT), |
| 58 | + SYSTEM_PROPERTIES, null, null); |
| 59 | + String urlString = uri.toString(); |
| 60 | + Builder builder = client.target(urlString).request(); |
| 61 | + builder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); |
| 62 | + return builder; |
81 | 63 | } |
82 | | - } |
83 | 64 |
|
84 | | - // Helper method that processes the request |
85 | | - protected Properties getPropertiesHelper(Builder builder) { |
86 | | - try { |
87 | | - Response response = builder.get(); |
88 | | - if (response.getStatus() == Status.OK.getStatusCode()) { |
89 | | - return response.readEntity(Properties.class); |
90 | | - } else { |
91 | | - System.err.println("Response Status is not OK."); |
92 | | - } |
93 | | - } catch (RuntimeException e) { |
94 | | - System.err.println("Runtime exception: " + e.getMessage()); |
95 | | - } catch (Exception e) { |
96 | | - System.err.println( |
97 | | - "Exception thrown while invoking the request: " + e.getMessage()); |
| 65 | + // Helper method that processes the request |
| 66 | + private Properties getPropertiesHelper(Builder builder) throws Exception { |
| 67 | + Response response = builder.get(); |
| 68 | + if (response.getStatus() == Status.OK.getStatusCode()) { |
| 69 | + return response.readEntity(Properties.class); |
| 70 | + } else { |
| 71 | + System.err.println("Response Status is not OK."); |
| 72 | + return null; |
| 73 | + } |
98 | 74 | } |
99 | | - return null; |
100 | | - } |
101 | | - |
102 | 75 | } |
0 commit comments