Search code examples
javaxpages

Get the base url of application in Java


Is there a nicer way to get the base URL of ref to the current application in XPages/Java?

FacesContext facesContext = FacesContext.getCurrentInstance();
XSPContext context = XSPContext.getXSPContext(facesContext);
XSPUrl url = context.getUrl();
String host = url.getScheme() + "://" + url.getHost() + "/" + url.getPath();

Solution

  • Maybe just a little bit nicer:

    String serverPathNsf = url.getAddress().replace(url.getSiteRelativeAddress(context), "")
    

    url.getAddress() gives you server + path to nsf + xsp

        http://yourServer/pathTo.nsf/your.xsp
    

    url.getSiteRelativeAddress(context) gives you the xsp

        /your.xsp
    

    and url.getAddress().replace(url.getSiteRelativeAddress(context), "") returns server + path to nsf

        http://yourServer/pathTo.nsf