001// Copyright 2008, 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.http.services;
016
017import org.apache.tapestry5.ioc.services.ServiceOverride;
018
019/**
020 * Used when switching between normal/insecure (HTTP) and secure (HTTPS) mode. When a switch occurs, it is no longer
021 * possible to use just paths, instead absolute URLs (including the scheme, hostname and possibly port) must be
022 * generated. The default implementation of this is simple-minded: it just tacks the correct scheme in front of
023 * {@link org.apache.tapestry5.http.services.Request#getServerName()}. In production, behind a firewall, it is often
024 * necessary to do a bit more, since <code>getServerName()</code> will often be the name of the internal server (not
025 * visible to the client web browser), and a hard-coded name of a server that <em>is</em> visible to the web browser
026 * is needed. Further, in testing, non-default ports are often used. In those cases, an overriding contribution to the
027 * {@link ServiceOverride} service will allow a custom implementation to supercede the default version. You may also
028 * contribute application specific values for the following {@link org.apache.tapestry5.SymbolConstants}:
029 * {@link org.apache.tapestry5.http.TapestryHttpSymbolConstants#HOSTNAME}, {@link org.apache.tapestry5.http.TapestryHttpSymbolConstants#HOSTPORT} and 
030 * {@link org.apache.tapestry5.http.TapestryHttpSymbolConstants#HOSTPORT_SECURE} to alter the behavior of the default BaseURLSource 
031 * implementation. The default values for the SymbolConstants require {@link org.apache.tapestry5.http.services.Request} 
032 * context to be available. If you contribute specific values for the specified SymbolConstants, it's safe to use 
033 * the default implementation of this service outside of request context, for example in a batch job. For 
034 * {@link org.apache.tapestry5.http.TapestryHttpSymbolConstants#HOSTNAME}, a value starting with a dollar sign ($) will be resolved 
035 * using {@link java.lang.System#getenv()} - contributing "$HOSTNAME" for {@link org.apache.tapestry5.http.TapestryHttpSymbolConstants#HOSTNAME}
036 * is the most sensible choice for a dynamic value that doesn't use 
037 * {@link org.apache.tapestry5.http.services.Request#getServerName()}.
038 */
039public interface BaseURLSource
040{
041    /**
042     * Returns the base portion of the URL, before the context path and servlet path are appended. The return value
043     * should <em>not</em> end with a slash; it should end after the host name, or after the port number. The context
044     * path, servlet path, and path info will be appended to the returned value.
045     * 
046     * @param secure
047     *            whether a secure "https" or insecure "http" base URL should be returned
048     * @return the base URL ready for additional extensions
049     */
050    String getBaseURL(boolean secure);
051}