1 package org.metricshub.winrm.service;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.Arrays;
26 import java.util.Objects;
27 import org.metricshub.winrm.Utils;
28 import org.metricshub.winrm.WinRMHttpProtocolEnum;
29 import org.metricshub.winrm.WmiHelper;
30
31 public class WinRMEndpoint {
32
33 private static final int DEFAULT_WIN_RM_HTTP_PORT = 5985;
34 private static final int DEFAULT_WIN_RM_HTTPS_PORT = 5986;
35
36 private final String hostname;
37 private final String endpoint;
38 private final String domain;
39 private final String username;
40 private final char[] password;
41 private final String namespace;
42 private final String rawUsername;
43 private final WinRMHttpProtocolEnum protocol;
44
45
46
47
48
49
50
51
52
53
54
55 public WinRMEndpoint(
56 final WinRMHttpProtocolEnum protocol,
57 final String hostname,
58 final Integer port,
59 final String username,
60 final char[] password,
61 final String namespace
62 ) {
63 Utils.checkNonNull(hostname, "hostname");
64 Utils.checkNonNull(username, "username");
65 Utils.checkNonNull(password, "password");
66
67 this.hostname = hostname.replaceAll("\\s", Utils.EMPTY);
68 this.password = password;
69 rawUsername = username;
70
71 this.namespace = buildNamespace(namespace);
72
73 final String user = username.replaceAll("\\s", Utils.EMPTY);
74 if (user.contains("\\")) {
75 final String[] array = user.split("\\\\");
76 domain = array[0];
77 this.username = array[1];
78 } else {
79 domain = null;
80 this.username = user;
81 }
82
83 this.protocol = protocol != null ? protocol : WinRMHttpProtocolEnum.HTTP;
84 final String endpointUrl = buildEndpointUrl(this.protocol, this.hostname, port);
85
86 endpoint = buildWSManEndpoint(endpointUrl);
87 }
88
89
90 public String getHostname() {
91 return hostname;
92 }
93
94
95
96
97
98
99
100 public String getDomain() {
101 return domain;
102 }
103
104
105 public String getRawUsername() {
106 return rawUsername;
107 }
108
109
110 public String getUsername() {
111 return username;
112 }
113
114
115 public char[] getPassword() {
116 return password;
117 }
118
119
120 public String getNamespace() {
121 return namespace;
122 }
123
124
125 public WinRMHttpProtocolEnum getProtocol() {
126 return protocol;
127 }
128
129
130
131
132
133
134
135
136
137
138 public static String buildEndpointUrl(
139 final WinRMHttpProtocolEnum protocol,
140 final String hostname,
141 final Integer port
142 ) {
143 final int endpointPort = getEndpointPort(protocol, port);
144
145 return String.format("%s://%s:%d", protocol.toString(), hostname, endpointPort);
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160 public static int getEndpointPort(final WinRMHttpProtocolEnum protocol, final Integer port) {
161 if (port != null) {
162 return port;
163 }
164 return protocol == WinRMHttpProtocolEnum.HTTPS ? DEFAULT_WIN_RM_HTTPS_PORT : DEFAULT_WIN_RM_HTTP_PORT;
165 }
166
167
168
169
170
171
172
173
174 public static String buildNamespace(final String namespace) {
175 final String cleanNamespace = namespace != null ? namespace.replaceAll("\\s", Utils.EMPTY) : Utils.EMPTY;
176
177 final String usedNamespace = Utils.isNotBlank(cleanNamespace) ? cleanNamespace : WmiHelper.DEFAULT_NAMESPACE;
178
179 return usedNamespace.replace('\\', '/');
180 }
181
182
183
184
185
186
187
188
189 private static String buildWSManEndpoint(final String endpoint) {
190 try {
191 return new URL(String.format("%s/wsman", endpoint)).toExternalForm();
192 } catch (final MalformedURLException e) {
193 throw new IllegalArgumentException(String.format("endpoint %s is invalid.", endpoint), e);
194 }
195 }
196
197 @Override
198 public int hashCode() {
199 final int prime = 31;
200 int result = 1;
201 result = prime * result + Arrays.hashCode(password);
202 result = prime * result + Objects.hash(endpoint, namespace, rawUsername);
203 return result;
204 }
205
206 @Override
207 public boolean equals(final Object obj) {
208 if (this == obj) {
209 return true;
210 }
211 if (obj == null) {
212 return false;
213 }
214 if (!(obj instanceof WinRMEndpoint)) {
215 return false;
216 }
217 final WinRMEndpoint other = (WinRMEndpoint) obj;
218 return (
219 Objects.equals(endpoint, other.endpoint) &&
220 Objects.equals(namespace, other.namespace) &&
221 Arrays.equals(password, other.password) &&
222 Objects.equals(rawUsername, other.rawUsername)
223 );
224 }
225
226 @Override
227 public String toString() {
228 return new StringBuilder()
229 .append("WinRMEndpoint [")
230 .append("endpoint=")
231 .append(endpoint)
232 .append(", domain=")
233 .append(domain)
234 .append(", username=")
235 .append(username)
236 .append(", namespace=")
237 .append(namespace)
238 .append("]")
239 .toString();
240 }
241 }