Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How can we access domain name from the AEM cloud and use it inside configuration.

Avatar

Level 1

Hi Experts, 

 

Good Morning,

 

Recently I got to learn about the AEM cloud environment variable, I understood how we can add a custom environment variable to an AEM cloud environment and use it on the OSGI services configuration. But I was wondering if we can somehow use the domain name, set for the AEM cloud instance. I did not find any reference about if there is any existing environment variable available for the domain name.

 

Please guide me if this is possible.

 

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @RP2777007 

 

We get domain name from Request using APIs

 

    private String getDomainFromURL() {
		String url = request.getRequestURL().toString();
		String hostComponent = "";
        try {
            URI uri = new URI(url);
            hostComponent = uri.getHost();
        } catch (URISyntaxException e) {
	   logger.debug("Unable to parse request URL");
        }
    }

 

 

For constructing URLs to say send in an email, you can use externalizer like https://medium.com/@toimrank/aem-externalizer-and-multisiteexternalizer-e3afad105b8

 

If the requirement is beyond these suggestions, please do let us know


Aanchal Sikka

View solution in original post

4 Replies

Avatar

Community Advisor

Hello @RP2777007  - 

 

  • Actually, the domain name for your instance is not directly exposed as an environment variable.
  • But what is the exact requirement here? As in why do you want to set the domain name for an instance via environment variables? 

Avatar

Level 1

hi @Tanika02 , 

 

I don't want to set the domain name, but I want to utilise the domain name and serve the endpoint of my API along with the domain. Usually we do it using Externalizer configuration, but in my case, we have two dev instances, and if we set configuration according to the environment and service type, it's taking one's domain to another.

Avatar

Correct answer by
Community Advisor

Hello @RP2777007 

 

We get domain name from Request using APIs

 

    private String getDomainFromURL() {
		String url = request.getRequestURL().toString();
		String hostComponent = "";
        try {
            URI uri = new URI(url);
            hostComponent = uri.getHost();
        } catch (URISyntaxException e) {
	   logger.debug("Unable to parse request URL");
        }
    }

 

 

For constructing URLs to say send in an email, you can use externalizer like https://medium.com/@toimrank/aem-externalizer-and-multisiteexternalizer-e3afad105b8

 

If the requirement is beyond these suggestions, please do let us know


Aanchal Sikka

Avatar

Community Advisor

Hi @RP2777007 ,

 

As I understand, you have two dev instances and both should use a different domain for API.

You can create an OSGI config which should read domain from environment variables.

Now, it can be deployed to both AEM dev instances. 

But, you can configure different domains for both AEM instances as environment variable. By this way, you will use same OSGI config but with different domains from environment variables.

 

I hope it helps!