提交 a0b231d3 编写于 作者: R Rossen Stoyanchev

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
上级 8fbd310b
......@@ -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);
}
......
......@@ -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
*/
......
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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,8 @@
package org.springframework.web.util;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
......@@ -23,10 +25,12 @@ import java.io.ObjectOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
/**
* @author Arjen Poutsma
......@@ -84,22 +88,18 @@ public class UriComponentsTests {
@Test
public void port() {
UriComponents uriComponents1 = UriComponentsBuilder.fromUriString(
"http://example.com:8080/bar").build();
UriComponents uriComponents2 = UriComponentsBuilder.fromUriString(
"http://example.com/bar").port(8080).build();
UriComponents uriComponents3 = UriComponentsBuilder.fromUriString(
"http://example.com/bar").port("{port}").build().expand(8080);
UriComponents uriComponents4 = UriComponentsBuilder.fromUriString(
"http://example.com/bar").port("808{digit}").build().expand(0);
assertEquals(8080, uriComponents1.getPort());
assertEquals("http://example.com:8080/bar", uriComponents1.toUriString());
assertEquals(8080, uriComponents2.getPort());
assertEquals("http://example.com:8080/bar", uriComponents2.toUriString());
assertEquals(8080, uriComponents3.getPort());
assertEquals("http://example.com:8080/bar", uriComponents3.toUriString());
assertEquals(8080, uriComponents4.getPort());
assertEquals("http://example.com:8080/bar", uriComponents4.toUriString());
UriComponents uri1 = fromUriString("http://example.com:8080/bar").build();
UriComponents uri2 = fromUriString("http://example.com/bar").port(8080).build();
UriComponents uri3 = fromUriString("http://example.com/bar").port("{port}").build().expand(8080);
UriComponents uri4 = fromUriString("http://example.com/bar").port("808{digit}").build().expand(0);
assertEquals(8080, uri1.getPort());
assertEquals("http://example.com:8080/bar", uri1.toUriString());
assertEquals(8080, uri2.getPort());
assertEquals("http://example.com:8080/bar", uri2.toUriString());
assertEquals(8080, uri3.getPort());
assertEquals("http://example.com:8080/bar", uri3.toUriString());
assertEquals(8080, uri4.getPort());
assertEquals("http://example.com:8080/bar", uri4.toUriString());
}
@Test(expected = IllegalStateException.class)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册