Update port related code in UriComponentsBuilder

Use null to indicate no port has been provided.
Raise exception when port has not been expanded.

Issue: SPR-12123
This commit is contained in:
Rossen Stoyanchev
2014-08-29 09:21:15 -04:00
parent 8fbd310b07
commit a0b231d36d
3 changed files with 35 additions and 27 deletions

View File

@@ -109,6 +109,13 @@ final class HierarchicalUriComponents extends UriComponents {
@Override
public int getPort() {
if (this.port == null) {
return -1;
}
else if (this.port.contains("{")) {
throw new IllegalStateException(
"The port contains a URI variable but has not been expanded yet: " + this.port);
}
return Integer.parseInt(this.port);
}
@@ -360,7 +367,7 @@ final class HierarchicalUriComponents extends UriComponents {
if (this.host != null) {
uriBuilder.append(host);
}
if (!"-1".equals(this.port)) {
if (getPort() != -1) {
uriBuilder.append(':');
uriBuilder.append(port);
}

View File

@@ -96,7 +96,7 @@ public class UriComponentsBuilder {
private String host;
private String port = "-1";
private String port;
private CompositePathComponentBuilder pathBuilder = new CompositePathComponentBuilder();
@@ -272,7 +272,7 @@ public class UriComponentsBuilder {
return new OpaqueUriComponents(this.scheme, this.ssp, this.fragment);
}
else {
return new HierarchicalUriComponents(this.scheme, this.userInfo, this.host, String.valueOf(this.port),
return new HierarchicalUriComponents(this.scheme, this.userInfo, this.host, this.port,
this.pathBuilder.build(), this.queryParams, this.fragment, encoded, true);
}
}
@@ -353,7 +353,7 @@ public class UriComponentsBuilder {
private void resetHierarchicalComponents() {
this.userInfo = null;
this.host = null;
this.port = "-1";
this.port = null;
this.pathBuilder = new CompositePathComponentBuilder();
this.queryParams.clear();
}
@@ -468,8 +468,9 @@ public class UriComponentsBuilder {
}
/**
* Set the URI port. Passing {@code "-1"} will clear the port of this builder.
* The given port may contain URI template variables.
* Set the URI port. Use this method only when the port needs to be
* parameterized with a URI variable. Otherwise use {@link #port(int)}.
* Passing {@code null} will clear the port of this builder.
* @param port the URI port
* @return this UriComponentsBuilder
*/