diff --git a/spring-vault-core/src/main/java/org/springframework/vault/client/VaultEndpoint.java b/spring-vault-core/src/main/java/org/springframework/vault/client/VaultEndpoint.java index c848e91b..de912492 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/client/VaultEndpoint.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/client/VaultEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.vault.client; import java.io.Serializable; +import java.net.MalformedURLException; import java.net.URI; import lombok.EqualsAndHashCode; @@ -27,7 +28,7 @@ import org.springframework.util.Assert; *
* A {@link VaultEndpoint} defines the hostname, TCP port and the protocol scheme (HTTP or * HTTPS). - * + * * @author Mark Paluch */ @EqualsAndHashCode @@ -53,7 +54,7 @@ public class VaultEndpoint implements Serializable { /** * Create a secure {@link VaultEndpoint} given a {@code host} and {@code port} using * {@code https}. - * + * * @param host must not be empty or {@literal null}. * @param port must be a valid port in the range of 1-65535 * @return a new {@link VaultEndpoint}. @@ -86,7 +87,14 @@ public class VaultEndpoint implements Serializable { VaultEndpoint vaultEndpoint = new VaultEndpoint(); vaultEndpoint.setHost(uri.getHost()); - vaultEndpoint.setPort(uri.getPort()); + try { + vaultEndpoint.setPort(uri.getPort() == -1 ? uri.toURL().getDefaultPort() + : uri.getPort()); + } + catch (MalformedURLException e) { + throw new IllegalArgumentException(String.format( + "Can't retrieve default port from %s", uri), e); + } vaultEndpoint.setScheme(uri.getScheme()); return vaultEndpoint; @@ -101,7 +109,7 @@ public class VaultEndpoint implements Serializable { /** * Sets the hostname. - * + * * @param host must not be empty or {@literal null}. */ public void setHost(String host) { @@ -156,7 +164,7 @@ public class VaultEndpoint implements Serializable { /** * Build the Vault URI string based on the given {@code path}. - * + * * @param path must not be empty or {@literal null}. * @return constructed URI String. */ diff --git a/spring-vault-core/src/test/java/org/springframework/vault/client/VaultEndpointUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/client/VaultEndpointUnitTests.java index 9fe023ab..92867faa 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/client/VaultEndpointUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/client/VaultEndpointUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,13 +23,13 @@ import static org.assertj.core.api.Assertions.assertThat; /** * Unit tests for {@link VaultEndpoint}. - * + * * @author Mark Paluch */ public class VaultEndpointUnitTests { @Test - public void shouldCreateEndpointFromHostAndPort() throws Exception { + public void shouldCreateEndpointFromHostAndPort() { VaultEndpoint endpoint = VaultEndpoint.create("host", 1234); @@ -39,7 +39,7 @@ public class VaultEndpointUnitTests { } @Test - public void shouldCreateEndpointFromURI() throws Exception { + public void shouldCreateEndpointFromURI() { VaultEndpoint endpoint = VaultEndpoint.from(URI.create("http://127.0.0.1:443")); @@ -47,4 +47,14 @@ public class VaultEndpointUnitTests { assertThat(endpoint.getHost()).isEqualTo("127.0.0.1"); assertThat(endpoint.getPort()).isEqualTo(443); } + + @Test + public void shouldCreateEndpointFromURIWithoutPort() { + + VaultEndpoint endpoint = VaultEndpoint.from(URI.create("http://127.0.0.1")); + + assertThat(endpoint.getScheme()).isEqualTo("http"); + assertThat(endpoint.getHost()).isEqualTo("127.0.0.1"); + assertThat(endpoint.getPort()).isEqualTo(80); + } }