Accessing Cloud Services Configurations using HTL | Community
Skip to main content
MavicNZ
Level 2
Jun 19, 2017 12:16 AM UTC
Solved

Accessing Cloud Services Configurations using HTL

Hi Guys,

Is it possible to access a page property on a could services configuration page using HTL or do you have to use Java to create an object first?

Cheers Jeff

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Feike_Visser1
Feike_Visser1Accepted solution
Employee
Jun 22, 2017 5:57 AM UTC

Most of the Java-code must indeed placed into a Java-class (WcmUsePojo / Sling Model)

11 replies

joerghoh
Employee
Jun 24, 2017 7:38 PM UTC

Just be aware, that you don't need special privileges to create/change Cloud Configuration settings, a regular author (with properly training) can do that. When you store it in an OSGI configuration, it's job for the admin user.

So prefer a CloudConfiguration if an author is supposed to change that.

Translate
Level 3
Jun 23, 2017 2:37 PM UTC

Is the Cloud Service Configuration for something that's OOTB (like Analytics etc), or is it a custom API service?

If it's custom, I would suggest moving from Cloud Service Configuration to a more simple OSGI service with a configuration.  Doing this would allow you to load the configuration much easier using a WCMUsePojo (or in 6.1, WCMUse) class and give access to the view.

Translate
Feike_Visser1
Feike_Visser1Accepted solution
Employee
Jun 22, 2017 5:57 AM UTC

Most of the Java-code must indeed placed into a Java-class (WcmUsePojo / Sling Model)

Translate
MavicNZ
MavicNZAuthor
Level 2
Jun 21, 2017 10:38 PM UTC

Hi Feike,

Here is the contents of the JSP component that we are looking to replace with an HTL component.

Cheers Jeff

<%@page session="false"
   import="org.apache.sling.api.resource.Resource,
  org.apache.sling.api.resource.ValueMap,
  org.apache.sling.api.resource.ResourceUtil,
   com.day.cq.wcm.webservicesupport.Configuration,
   com.day.cq.wcm.webservicesupport.ConfigurationManager" %><%
%><%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %><%
%><cq:defineObjects/><%
   String[] services = pageProperties.getInherited("cq:cloudserviceconfigs", new String[]{});
  ConfigurationManager cfgMgr = resource.getResourceResolver().adaptTo(ConfigurationManager.class);
   if(cfgMgr != null) {
  String api_key = null;
  Configuration cfg = cfgMgr.getConfiguration("servicename", services);
   if(cfg != null) {
  api_key = cfg.get("api_key", null);
  }
%>
<!-- API Key -->
<script type="text/javascript">

   var apikey = '<%= api_key %>';

</script>

<%
   }
%>

Translate
Feike_Visser1
Employee
Jun 21, 2017 11:23 AM UTC

Do have some code that I can look at?

OSGi services can be consumed in HTL, only limitation is that can't invoke methods with parameters.

Translate
MavicNZ
MavicNZAuthor
Level 2
Jun 20, 2017 9:11 PM UTC

Hi Feike,

I am a front end dev and have a few years CQ/AEM experience, I've just started working with 6.1 and am new to HTL.

We are storing API keys as a property on a Cloud Services Configurations pages for a service that we use across multiple websites in our AEM stack, each website has it's own unique key for the service.

We then configure the correct cloud service from each website home page properties we have a JSP component that uses Java Scriptlet code to pull the API key into the site for the service to use.

We would like to change this JSP to an HTL component and as I'm new to HTL I wasn't sure if we could access the cloud services configuration directly in HTL or still needed a Java tag to create the object needed.

Cheers Jeff

Translate
Feike_Visser1
Employee
Jun 20, 2017 5:45 AM UTC

Do you have a more concrete example what you are try to do?

Normally you do a lot with pageProperties or resourcePage.

Translate
MavicNZ
MavicNZAuthor
Level 2
Jun 20, 2017 4:59 AM UTC

Hi Feike,

Using 6.1 but I understand we are upgrading to 6.2 or 6.3 in the near future.

I have found that there was an HTL component that does what we want but the Java tag providing the data needs some updating so hopefully this will be the simplest solution.

Cheers Jeff

Translate
Feike_Visser1
Employee
Jun 20, 2017 4:42 AM UTC

What APIs do you use here? Also are you using 6.2 or 6.3?

Translate
MavicNZ
MavicNZAuthor
Level 2
Jun 19, 2017 9:28 PM UTC

Thanks Scott, we are already using AEM cloud services configurations, we are just wanting to redevelop some components that are currently coded using Java scriptlets into HTL components and I was wondering if there was a way to pull the data directly from cloud services.

Translate