function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
d3developerd3developer 

How to get full host name in Apex?

 

I'm trying to build the url for a page to be called via Ajax in a Salesforce Sites page which is in the format:

 

http://sitename-developer-edition.na3.force.com/extension

 

When I do {!$Page.myPage}" in Visualforce it it brings up the full correct reference to the page:

 

/extension/PackageName__pageName

 

This works great.

 

However, I cannot find out how to correctly find out the "extension" through native Apex code.

 

ApexPages.currentPage().getHeaders('Host') returns only the first part, "sitename-developer-edition.na3.force.com" without the extension, and there is no other information in any of the Headers element which contains it (i.e. Accept-Charset, Accept-Encoding, Accept-Language, CipherSuite, Connection, Host, Keep-Alive, User-Agent, X-Salesforce-Forwarded-To, X-Salesforce-SIP).

 

Advice?

 

 

 

 

 

 

 

 

 

Vincent IpVincent Ip

The way I would approach this is to write a little bit of javascript and feed window.location.href (or any other property of window.location you like) to an apex:actionFunction.

 

Here's some sample code which will feed the entire url to the controller at the load of the VF page.

 

VF page:

 

<apex:page controller="MyController" >
<script>
function setUrl(){
callControllerMethod(window.location.href);
}
window.onload=setUrl;
</script>

<apex:form >
<apex:actionFunction name="callControllerMethod" action="{!callControllerMethod}" reRender="pageblock">
<apex:param name="siteUrl" assignTo="{!siteUrl}" value="" />
</apex:actionFunction>

<!-- for our example, just display the variable value -->
<apex:pageBlock id="pageblock">
<apex:outputText value="{!siteUrl}" />
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

 

Apex Controller:

 

public with sharing class MyController {
public String siteUrl {get;set;}

public ScratchWork(){
}

public PageReference callControllerMethod(){

// Do some Apex Code here...
// siteUrl will contain the full Url of the page.

return null;
}
}

 

You can adapt this approach to anywhere else you can invoke javascript, (like on command links / buttons).  Hope this helps.

 

d3developerd3developer

I suggested the same thing in another thread on a similar topic. The problem is that the page has to finish loading first and if I want to provide another link to a different page on that page I cannot (unless I do a similarly quirky thing and load the extension into the DOM and populate the correct page extension). So I am still searching for another solution.

 

That said, this works assuming you have the actionFunction listed above:

 

 

		String host = ApexPages.currentPage().getHeaders().get('Host');
		String fullUrl;
		Integer startIndex = siteUrl.indexOf(host) + host.length() + 1;
		Integer endIndex = siteUrl.lastIndexOf('/');
		//System.debug('si:' + startIndex + 'ei: ' + endIndex);
		if(startIndex < endIndex && startIndex != -1)  
			extension = siteUrl.substring(startIndex,endIndex);
		else
			extension = '';
		if(extension != 'apex')
			fullUrl = siteUrl.substring(0, startIndex) + extension + System.Page.largeLookup.getUrl();
		else
			fullUrl = siteUrl.substring(0, startIndex - 1) + System.Page.largeLookup.getUrl();

 

 

d3developerd3developer

This is much easier and cleaner though:

 

 

			
		    String fullUrl;
		    if (Site.getPrefix() != null)
		         fullUrl = Site.getPrefix() + Page.largeLookup.getUrl();
		    else
		    	fullUrl = Page.largeLookup.getUrl();
		        

 

some how I missed the getPrefix method since I was looking in the wrong docs...

 

Vincent IpVincent Ip

Ahh very nice.