|
1 | 1 | /* |
2 | | - * Copyright (c) 2017 Otávio Santana and others |
| 2 | + * Copyright (c) 2022 Otávio Santana and others |
3 | 3 | * All rights reserved. This program and the accompanying materials |
4 | 4 | * are made available under the terms of the Eclipse Public License v1.0 |
5 | 5 | * and Apache License v2.0 which accompanies this distribution. |
|
11 | 11 | * Contributors: |
12 | 12 | * |
13 | 13 | * Otavio Santana |
| 14 | + * Alessandro Moscatelli |
14 | 15 | */ |
15 | 16 | package org.eclipse.jnosql.communication.elasticsearch.document; |
16 | 17 |
|
| 18 | +import java.net.MalformedURLException; |
| 19 | +import java.net.URL; |
| 20 | +import java.util.Objects; |
17 | 21 | import org.apache.http.HttpHost; |
18 | 22 |
|
19 | 23 | final class ElasticsearchAddress { |
20 | 24 |
|
21 | | - private final String host; |
22 | | - |
23 | | - private final int port; |
| 25 | + private HttpHost host; |
24 | 26 |
|
25 | 27 | private ElasticsearchAddress(String address, int defaultPort) { |
26 | | - String[] values = address.split(":"); |
27 | | - |
28 | | - this.host = values[0]; |
29 | | - this.port = values.length == 2 ? Integer.valueOf(values[1]) : defaultPort; |
| 28 | + try { |
| 29 | + URL tmp = new URL(address); |
| 30 | + this.host = new HttpHost( |
| 31 | + tmp.getHost(), |
| 32 | + Objects.equals(tmp.getPort(), -1) ? defaultPort : tmp.getPort(), |
| 33 | + tmp.getProtocol() |
| 34 | + ); |
| 35 | + } catch (MalformedURLException ex) { |
| 36 | + String[] values = address.split(":"); |
| 37 | + this.host = new HttpHost( |
| 38 | + values[0], |
| 39 | + values.length == 2 ? Integer.valueOf(values[1]) : defaultPort |
| 40 | + ); |
| 41 | + } |
30 | 42 | } |
31 | 43 |
|
32 | 44 | public HttpHost toHttpHost() { |
33 | | - return new HttpHost(host, port); |
| 45 | + return this.host; |
34 | 46 | } |
35 | 47 |
|
36 | 48 | @Override |
37 | 49 | public String toString() { |
38 | 50 | final StringBuilder sb = new StringBuilder("ElasticsearchAddress{"); |
39 | | - sb.append("host='").append(host).append('\''); |
40 | | - sb.append(", port=").append(port); |
| 51 | + sb.append("host='").append(host.getHostName()).append('\''); |
| 52 | + sb.append(", port=").append(host.getPort()); |
41 | 53 | sb.append('}'); |
42 | 54 | return sb.toString(); |
43 | 55 | } |
|
0 commit comments