This article will explain the steps needed to create a profile in Adobe Campaign Standard on an Adaptive Form submission. This process makes use of custom submit mechanism to handle the Adaptive Form submission.

This tutorial will walk through the steps of creating Campaign profile on adaptive form submission. To accomplish this use case we need to do the following

  • Create a AEM Service(CampaignService) to create Adobe Campaign Standard Profile using REST API
  • Create a custom submit action for handling Adaptive Form submission
  • Invoke the creatProfile method of the CampaignService


Create AEM Service

Create AEM Service to create an Adobe Campaign Profile. This AEM service will fetch the Adobe Campaign credentials from the OSGI configuration. Once the campaign credentials are obtained access token is generated and using the access token HTTP Post call is made to create the profile in Adobe Campaign. The following is the code for creating profile.

import static io.jsonwebtoken.SignatureAlgorithm.RS256;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;

import javax.jcr.Node;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mergeandfuse.getserviceuserresolver.GetResolver;

import aemforms.campaign.core.CampaignService;
import aemformwithcampaign.core.*;
import formsandcampaign.demo.CampaignConfigurationService;
import io.jsonwebtoken.Jwts;
@Component(service=CampaignService.class, immediate = true)
public class CampaignServiceImpl implements CampaignService {
	private final Logger log = LoggerFactory.getLogger(getClass());

    CampaignConfigurationService config;
    GetResolver getResolver;
    private static final String SERVER_FQDN = "";
    private static final String AUTH_SERVER_FQDN = "";
    private static final String AUTH_ENDPOINT = "/ims/exchange/jwt/";
    private static final String CREATE_PROFILE_ENDPOINT = "/campaign/profileAndServicesExt/profile/";
	public String getAccessToken() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
		// TODO Auto-generated method stub"JWT: Generating Token");

        String apikey = config.getApiKey();
        log.debug("The API Key i got was "+apikey);
        String techact = config.getTechAcct();
        String orgid = config.getOrgId();
        String clientsecret = config.getClientSecret();
        String realm = config.getDomainRealm();
        HttpClient httpClient = HttpClientBuilder.create().build();
        Long expirationTime = System.currentTimeMillis() / 1000 + 86400L;
        try {
        	ResourceResolver rr = getResolver.getServiceResolver();
           	Resource privateKeyRes = rr.getResource("/etc/key/campaign/private.key");
        	InputStream is = privateKeyRes.adaptTo(InputStream.class);
           	BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            int result =;
            while (result != -1) {
                byte b = (byte) result;
                result =;

            String privatekeyString = buf.toString();
            privatekeyString  = privatekeyString.replaceAll("\\n", "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
            log.debug("The sanitized private key string is "+privatekeyString);
            // Create the private key
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            log.debug("The key factory algorithm is "+keyFactory.getAlgorithm());
            byte []byteArray = privatekeyString.getBytes();
            log.debug("The array length is "+byteArray.length);
            //KeySpec ks = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(keyString));
            byte[] encodedBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(privatekeyString);
            //KeySpec ks = new PKCS8EncodedKeySpec(byteArray);
            KeySpec ks = new PKCS8EncodedKeySpec(encodedBytes);
            String metascopes[] = new String[]{"ent_campaign_sdk"};
            RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(ks);
            HashMap<String, Object> jwtClaims = new HashMap<>();
            jwtClaims.put("iss", orgid);
            jwtClaims.put("sub", techact);
            jwtClaims.put("exp", expirationTime);
            jwtClaims.put("aud", "https://" + AUTH_SERVER_FQDN + "/c/" + apikey);
            //jwtClaims.put("https://" + AUTH_SERVER_FQDN + "/s/" + realm, true);
            for (String metascope : metascopes) {
                jwtClaims.put("https://" + AUTH_SERVER_FQDN + "/s/" + metascope,java.lang.Boolean.TRUE);
            // Create the final JWT token
            String jwtToken = Jwts.builder().setClaims(jwtClaims).signWith(RS256, privateKey).compact();
            log.debug("#####The jwtToken is  ####"+jwtToken+"#######");
            HttpHost authServer = new HttpHost(AUTH_SERVER_FQDN, 443, "https");
            HttpPost authPostRequest = new HttpPost(AUTH_ENDPOINT);
            authPostRequest.addHeader("Cache-Control", "no-cache");
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("client_id", apikey));
            params.add(new BasicNameValuePair("client_secret", clientsecret));
            params.add(new BasicNameValuePair("jwt_token", jwtToken));
            authPostRequest.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
            HttpResponse response = httpClient.execute(authServer, authPostRequest);
            if (200 != response.getStatusLine().getStatusCode()) {
                HttpEntity ent = response.getEntity();
                String content = EntityUtils.toString(ent);
                log.error("JWT: Server Returned Error\n", response.getStatusLine().getReasonPhrase());
                log.error("ERROR DETAILS: \n", content);
                throw new IOException("Server returned error: " + response.getStatusLine().getReasonPhrase());
            HttpEntity entity = response.getEntity();
           	org.json.JSONObject jo = new org.json.JSONObject(EntityUtils.toString(entity));
            log.debug("Returning access_token "+jo.getString("access_token"));
            return (String) jo.get("access_token");

        catch (Exception e)
		return null;
	public String createProfile(JSONObject profile) {
		// TODO Auto-generated method stub
		String jwtToken = null;
		try {
			jwtToken = getAccessToken();
		} catch (NoSuchAlgorithmException e2) {
			// TODO Auto-generated catch block
		} catch (InvalidKeySpecException e2) {
			// TODO Auto-generated catch block
		} catch (IOException e2) {
			// TODO Auto-generated catch block
        String tenant = config.getTenant();
        String apikey = config.getApiKey();
        String path = "/" + tenant + CREATE_PROFILE_ENDPOINT;
        log.debug("The API Key is "+apikey);
        log.debug("###The Path is "+path);
        HttpHost server = new HttpHost(SERVER_FQDN, 443, "https");
        HttpPost postReq = new HttpPost(path);
        postReq.addHeader("Cache-Control", "no-cache");
        postReq.addHeader("Content-Type", "application/json");
        postReq.addHeader("X-Api-Key", apikey);
        postReq.addHeader("Authorization", "Bearer " + jwtToken);
        StringEntity se = null;
        log.debug("Creating profile for"+profile.toString());
		try {
			se = new StringEntity(profile.toString());
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
		HttpClient httpClient = HttpClientBuilder.create().build();
		HttpResponse result;
			try {
				result = httpClient.execute(server, postReq);
				JSONObject responseJson = new JSONObject(EntityUtils.toString(result.getEntity()));
				log.debug("The response on creating profile is "+responseJson.toString());
		        return responseJson.getString("PKey");

			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
			} catch (IOException e) {
				// TODO Auto-generated catch block
		return null;


Custom Submit

Create a custom submit handler to handle the Adaptive Form submission. In this custom submit handler we will make a call to the createProfile method of the CampaignService. The createProfile method accepts a JSONObject that represents the profile that needs to be created.

To learn more about custom submit handler in AEM Forms, please follow this link

The following is the code in the custom submit

aemforms.campaign.core.CampaignService addNewProfile = sling.getService(aemforms.campaign.core.CampaignService.class);
JSONObject profile = new JSONObject();
String pkey = addNewProfile.createProfile(profile);

Test the solution

Once we have defined the service and the custom submit action, we are ready to test our solution. To test the solution please perform the following steps

  • Create Adaptive Forms with the appropriate fields
    • Make sure the form fields are named "email","fname","lname","phone"
  • Configure the submit action of the form
    • Make sure to configure the submit action is configured to "createcampaignprofile"
  • Submit the form to create the profile in Adobe Campaign


To get this sample working on your system please follow the following steps:

  • Deploy the bundles and import the packages associated with this article into your AEM instance
  • Provide the appropriate settings for Adobe Campaign in OSGi configuration
  • Deploy the bundles using Felix web console
  • Create a service user as mentioned in this article. Make sure to deploy the OSGi bundle associated with the article.
  • Store the ACS private key in etc/key/campaign/private.key. You will have to create a folder called campaign under etc/key.
  • Provide read access to the campaign folder to the service user "data".
  • Preview the form
  • Fill in all the fields and submit

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy