Created
October 11, 2012 14:46
-
-
Save ascandroli/3872902 to your computer and use it in GitHub Desktop.
copy of BaseURLSourceImpl 5.3 version adapted to make it work with 5.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AmnerisSymbols { | |
/** | |
* The hostname that application should use when constructing an absolute URL. The default is "", i.e. an empty | |
* string, | |
* in which case system will use request.getServerName(). Not the same as environment variable HOSTNAME, but you can | |
* also | |
* contribute "$HOSTNAME" as the value to make it the same as the environment variable HOSTNAME. | |
* | |
* @since 5.3 copy of SymbolConstants.HOSTNAME | |
*/ | |
public static final String TAPESTRY_HOSTNAME = "tapestry.hostname"; | |
/** | |
* The hostport that application should use when constructing an absolute URL. The default is "0", i.e. use the port | |
* value from | |
* the request. | |
* | |
* @since 5.3 copy of SymbolConstants.HOSTPORT | |
*/ | |
public static final String TAPESTRY_HOSTPORT = "tapestry.hostport"; | |
/** | |
* The secure (https) hostport that application should use when constructing an absolute URL. The default is "0", | |
* i.e. use | |
* the value from the request. | |
* | |
* @since 5.3 copy of SymbolConstants.HOSTPORT_SECURE | |
*/ | |
public static final String TAPESTRY_HOSTPORT_SECURE = "tapestry.hostport-secure"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AppModule { | |
public static void bind(ServiceBinder binder) { | |
binder.bind(BaseURLSource.class, eu.amneris.feebbo.services.BaseURLSourceImpl.class).withId("AmnerisBaseURLSource"); | |
} | |
@Contribute(SymbolProvider.class) | |
@FactoryDefaults | |
public static void configureBaseURLSource(MappedConfiguration<String, String> configuration) | |
{ | |
// The default values denote "use values from request" | |
configuration.add(AmnerisSymbols.TAPESTRY_HOSTNAME, ""); | |
configuration.add(AmnerisSymbols.TAPESTRY_HOSTPORT, "0"); | |
configuration.add(AmnerisSymbols.TAPESTRY_HOSTPORT_SECURE, "0"); | |
} | |
@Contribute(ServiceOverride.class) | |
public static void overrideBaseURLSource(MappedConfiguration<Class<?>, Object> configuration, | |
@Local BaseURLSource baseURLSource) { | |
/** | |
* It is not strictly mandatory but you should also explicitly configure your application's hostname instead of | |
* relying on the hostname taken from request. This is required anyway if you are creating absolute urls without | |
* request context (for example in a background process) or when you are running loadbalancers in front of your | |
* application server. Oauth provider's callback URL handler may require that hostname matches with the provider's | |
* configuration (that's exactly the case with Facebook). | |
* | |
* Pre-T5.3 you have to override BaseUrlSource, in T5.3 you can simply contribute symbols instead. | |
* | |
*/ | |
configuration.add(BaseURLSource.class, baseURLSource); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 eu.amneris.feebbo.services; | |
import eu.amneris.AmnerisSymbols; | |
import org.apache.tapestry5.ioc.annotations.Symbol; | |
import org.apache.tapestry5.services.BaseURLSource; | |
import org.apache.tapestry5.services.Request; | |
/** | |
* It is not strictly mandatory but you should also explicitly configure your application's hostname instead of | |
* relying on the hostname taken from request. This is required anyway if you are creating absolute urls without | |
* request context (for example in a background process) or when you are running loadbalancers in front of your | |
* application server. Oauth provider's callback URL handler may require that hostname matches with the provider's | |
* configuration (that's exactly the case with Facebook). | |
* | |
* Pre-T5.3 you have to override BaseUrlSource, in T5.3 you can simply contribute symbols instead. | |
* | |
* This is a COPY of BaseURLSourceImpl 5.3 version adapted to make it work with 5.2 | |
* | |
*/ | |
public class BaseURLSourceImpl implements BaseURLSource | |
{ | |
private final Request request; | |
private String hostname; | |
private int hostPort; | |
private int secureHostPort; | |
public BaseURLSourceImpl(Request request, | |
@Symbol(AmnerisSymbols.TAPESTRY_HOSTNAME) final String hostname, // SymbolConstants.HOSTNAME | |
@Symbol(AmnerisSymbols.TAPESTRY_HOSTPORT) final int hostPort, // SymbolConstants.HOSTPORT | |
@Symbol(AmnerisSymbols.TAPESTRY_HOSTPORT_SECURE) final int secureHostPort) // SymbolConstants.HOSTPORT_SECURE | |
{ | |
this.request = request; | |
this.hostname = hostname; | |
this.hostPort = hostPort; | |
this.secureHostPort = secureHostPort; | |
} | |
public String getBaseURL(boolean secure) | |
{ | |
int port = secure ? secureHostPort : hostPort; | |
String portSuffix = ""; | |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment