How do I get the base URL of a Confluence installation?

What are the base URL and context path?

The base URL is the URL for the root of your Confluence site. For example, the base URL for this site is http://confluence.atlassian.com. If you have installed Confluence somewhere other than the root directory of the webserver, for example http://www.example.com/confluence, then your base URL would be http://www.example.com/confluence.

Confluence attempts to guess the correct base URL for the site during setup. You can change it in the site's General Configuration.

The context path is the path to Confluence relative to the root directory of the webserver. For example, the context path for this site is an empty string, because it is deployed at the root. The context path for a Confluence instance deployed at http://www.example.com/confluence would be /confluence.

How do I determine the base URL and context path?

First you need the BootstrapManager (see how to retrieve it) then simply call the following method:

String baseUrl = bootstrapManager.getBaseUrl();

In Confluence 2.0 and earlier the method was called bootstrapManager.getDomain(). The getDomain() method was deprecated in favour of getBaseUrl() in Confluence 2.1, because the latter name better describes the information it returns.

To get the context path, use

String contextPath = bootstrapManager.getWebAppContextPath()