You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2012/08/18 02:27:11 UTC
git commit: TAP5-1973: :443 added to URLs when using the
Link.toAbsoluteURI(true)
Updated Branches:
refs/heads/5.3 476968ae4 -> 282773de8
TAP5-1973: :443 added to URLs when using the Link.toAbsoluteURI(true)
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/282773de
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/282773de
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/282773de
Branch: refs/heads/5.3
Commit: 282773de8beb7391a83b6cb63667f2cfebd2b936
Parents: 476968a
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Aug 17 17:26:25 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Aug 17 17:26:47 2012 -0700
----------------------------------------------------------------------
.../internal/services/BaseURLSourceImpl.java | 57 ++++-
.../internal/services/BaseURLSourceImplTest.java | 186 +++++++++------
2 files changed, 156 insertions(+), 87 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/282773de/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/BaseURLSourceImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/BaseURLSourceImpl.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/BaseURLSourceImpl.java
index 6fa049c..b7ea719 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/BaseURLSourceImpl.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/BaseURLSourceImpl.java
@@ -1,4 +1,4 @@
-// Copyright 2008, 2010, 2011 The Apache Software Foundation
+// Copyright 2008, 2010, 2011, 2012 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -17,19 +17,20 @@ package org.apache.tapestry5.internal.services;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;
+import org.apache.tapestry5.ioc.internal.util.InternalUtils;
import org.apache.tapestry5.services.BaseURLSource;
import org.apache.tapestry5.services.Request;
public class BaseURLSourceImpl implements BaseURLSource
{
private final Request request;
-
+
private String hostname;
private int hostPort;
private int secureHostPort;
public BaseURLSourceImpl(Request request, @Inject @Symbol(SymbolConstants.HOSTNAME) String hostname,
- @Symbol(SymbolConstants.HOSTPORT) int hostPort, @Symbol(SymbolConstants.HOSTPORT_SECURE) int secureHostPort)
+ @Symbol(SymbolConstants.HOSTPORT) int hostPort, @Symbol(SymbolConstants.HOSTPORT_SECURE) int secureHostPort)
{
this.request = request;
this.hostname = hostname;
@@ -39,19 +40,49 @@ public class BaseURLSourceImpl implements BaseURLSource
public String getBaseURL(boolean secure)
{
+ return String.format("%s://%s%s",
+ secure ? "https" : "http",
+ hostname(),
+ portExtension(secure));
+ }
+
+ private String portExtension(boolean secure)
+ {
int port = secure ? secureHostPort : hostPort;
- String portSuffix = "";
- if (port <= 0) {
+ // The default for the ports is 0, which means to use Request.serverPort. That's mostly
+ // for development.
+ if (port <= 0)
+ {
port = request.getServerPort();
- int schemeDefaultPort = request.isSecure() ? 443 : 80;
- portSuffix = port == schemeDefaultPort ? "" : ":" + port;
}
- else if (secure && port != 443) portSuffix = ":" + port;
- else if (port != 80) portSuffix = ":" + port;
-
- String hostname = "".equals(this.hostname) ? request.getServerName() : this.hostname.startsWith("$") ? System.getenv(this.hostname.substring(1)) : this.hostname;
-
- return String.format("%s://%s%s", secure ? "https" : "http", hostname, portSuffix);
+
+ int expectedPort = secure ? 443 : 80;
+
+ if (port == expectedPort)
+ {
+ return "";
+ }
+
+ return ":" + port;
+ }
+
+ private String hostname()
+ {
+
+ if (InternalUtils.isBlank(hostname))
+ {
+ return request.getServerName();
+ }
+
+ // This is common in some PaaS deployments, such as Heroku, where the port is passed in as
+ // and environment variable.
+
+ if (this.hostname.startsWith("$"))
+ {
+ return System.getenv(hostname.substring(1));
+ }
+
+ return hostname;
}
}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/282773de/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/BaseURLSourceImplTest.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/BaseURLSourceImplTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/BaseURLSourceImplTest.java
index b937b33..5dc4f89 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/BaseURLSourceImplTest.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/BaseURLSourceImplTest.java
@@ -1,74 +1,112 @@
-// Copyright 2008, 2010, 2011 The Apache Software Foundation
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package org.apache.tapestry5.internal.services;
-
-import org.apache.tapestry5.internal.test.InternalBaseTestCase;
-import org.apache.tapestry5.services.BaseURLSource;
-import org.apache.tapestry5.services.Request;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-
-public class BaseURLSourceImplTest extends InternalBaseTestCase {
- private Request request;
-
- @BeforeMethod
- public void setUp() {
- request = mockRequest();
- }
-
- @Test
- public void getBaseURLFromRequest() {
- expect(request.getServerName()).andReturn("localhost").once();
- expect(request.getServerPort()).andReturn(80).once();
- expect(request.isSecure()).andReturn(false).once();
- replay();
- BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "localhost", 0, 0);
- assertEquals(baseURLSource.getBaseURL(false), "http://localhost");
- }
-
- @Test
- public void getBaseURLWithContributedHostname() {
- expect(request.getServerPort()).andReturn(80).once();
- expect(request.isSecure()).andReturn(false).once();
- replay();
- BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "my.server.com", 0, 0);
- assertEquals(baseURLSource.getBaseURL(false), "http://my.server.com");
- }
-
- @Test
- public void getBaseURLWithEnvHostname() {
- expect(request.getServerPort()).andReturn(80).once();
- expect(request.isSecure()).andReturn(false).once();
- replay();
- BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "$HOSTNAME", 0, 0);
- assertEquals(baseURLSource.getBaseURL(false), "http://" + System.getenv("HOSTNAME"));
- }
-
- @Test
- public void getBaseURLWithContributedValuesDontUseRequest() {
- replay();
- BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "localhost", 80, 443);
- assertEquals(baseURLSource.getBaseURL(false), "http://localhost");
- }
-
- @Test
- public void getBaseURLWithContributedNonStandardSecurePort() {
- replay();
- BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "localhost", 80, 8443);
- assertEquals(baseURLSource.getBaseURL(true), "https://localhost:8443");
- }
-
-}
+// Copyright 2008, 2010, 2011, 2012 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry5.internal.services;
+
+import org.apache.tapestry5.internal.test.InternalBaseTestCase;
+import org.apache.tapestry5.services.BaseURLSource;
+import org.apache.tapestry5.services.Request;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+
+public class BaseURLSourceImplTest extends InternalBaseTestCase
+{
+ private Request request;
+
+ @BeforeMethod
+ public void setUp()
+ {
+ request = mockRequest();
+ }
+
+ @Test
+ public void server_name_from_request_object()
+ {
+ expect(request.getServerName()).andReturn("localhost").once();
+ expect(request.getServerPort()).andReturn(80).once();
+
+ replay();
+
+ BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "", 0, 0);
+ assertEquals(baseURLSource.getBaseURL(false), "http://localhost");
+
+ verify();
+ }
+
+ @Test
+ public void contributed_hostname()
+ {
+ expect(request.getServerPort()).andReturn(80).once();
+
+ replay();
+
+ BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "my.server.com", 0, 0);
+
+ assertEquals(baseURLSource.getBaseURL(false), "http://my.server.com");
+
+ verify();
+ }
+
+ @Test
+ public void hostname_from_environment_variable()
+ {
+ expect(request.getServerPort()).andReturn(80).once();
+
+ replay();
+
+ BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "$HOSTNAME", 0, 0);
+
+ assertEquals(baseURLSource.getBaseURL(false), "http://" + System.getenv("HOSTNAME"));
+
+ verify();
+ }
+
+ @Test
+ public void insecure_url_using_default_port()
+ {
+ replay();
+
+ BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "localhost", 80, 443);
+
+ assertEquals(baseURLSource.getBaseURL(false), "http://localhost");
+
+ verify();
+ }
+
+ @Test
+ public void secure_url_using_default_port()
+ {
+ replay();
+
+ BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "localhost", 80, 443);
+
+ assertEquals(baseURLSource.getBaseURL(true), "https://localhost");
+
+ verify();
+ }
+
+ @Test
+ public void getBaseURLWithContributedNonStandardSecurePort()
+ {
+ replay();
+
+ BaseURLSource baseURLSource = new BaseURLSourceImpl(request, "localhost", 80, 8443);
+
+ assertEquals(baseURLSource.getBaseURL(true), "https://localhost:8443");
+
+ verify();
+ }
+
+}