通常當您實作專案時,AEM和Adobe Campaign中都有自訂程式碼。 使用現有API後,您就可以從AEM或從AEM呼叫Adobe Campaign中的自訂程式碼至Adobe Campaign。 本檔案說明如何執行此動作。
您需要安裝下列程式:
如需詳細資訊,請參閱將AEM與Adobe Campaign 6.1整合。
AEM和Campaign之間的標準整合是以JSON和JSSP(「JavaScript伺服器頁面」)為基礎。 您可以在Campaign主控台中找到這些JSSP檔案,且所有檔案的開頭皆為amc(Adobe Marketing Cloud)。
如需此範例,請參閱「Geometrixx」,此檔案可從「封裝共用」取得。
在此範例中,我們會建立新的自訂JSSP檔案,並從AEM端呼叫該檔案以擷取結果。 例如,這可用來從Adobe Campaign擷取資料,或將資料儲存至Adobe Campaign。
在Adobe Campaign中,若要建立新的JSSP檔案,請按一下New圖示。
輸入此JSSP檔案的名稱。 在此範例中,我們使用cus:custom.jssp(這表示它將位於cus命名空間中)。
將下列程式碼放入jssp-file中:
<%
var origin = request.getParameter("origin");
document.write("Hello from Adobe Campaign, origin : " + origin);
%>
保存您的工作。 其餘工作在AEM中。
在AEM端建立簡單的servlet以呼叫此JSSP。 在此範例中,我們假設如下:
此範例中最重要的物件是GenericCampaignConnector,可讓您在Adobe Campaign端呼叫(取得並張貼)jssp檔案。
以下是一小段程式碼片段:
@Reference
private GenericCampaignConnector campaignConnector;
...
Map<String, String> params = new HashMap<String, String>();
params.put("origin", "AEM");
CallResults results = campaignConnector.callGeneric("/jssp/cus/custom.jssp", params, credentials);
return results.bodyAsString();
如您在此範例中所見,您需要將憑證傳入呼叫中。 您可以透過getCredentials()方法取得此資訊,方法會在您傳遞已設定Campaign雲端服務的頁面。
// page containing the cloudservice for Adobe Campaign
Configuration config = campaignConnector.getWebserviceConfig(page.getContentResource().getParent());
CampaignCredentials credentials = campaignConnector.retrieveCredentials(config);
完整的程式碼如下:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.mcm.campaign.CallResults;
import com.day.cq.mcm.campaign.CampaignCredentials;
import com.day.cq.mcm.campaign.GenericCampaignConnector;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.PageManagerFactory;
import com.day.cq.wcm.webservicesupport.Configuration;
@SlingServlet(paths="/bin/campaign", methods="GET")
public class CustomServlet extends SlingSafeMethodsServlet {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Reference
private GenericCampaignConnector campaignConnector;
@Reference
private PageManagerFactory pageManagerFactory;
@Override
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException,
IOException {
PageManager pm = pageManagerFactory.getPageManager(request.getResourceResolver());
Page page = pm.getPage("/content/geometrixx-outdoors");
String result = null;
if ( page != null) {
result = callCustomFunction(page);
}
if ( result != null ) {
PrintWriter pw = response.getWriter();
pw.print(result);
}
}
private String callCustomFunction(Page page ) {
try {
Configuration config = campaignConnector.getWebserviceConfig(page.getContentResource().getParent());
CampaignCredentials credentials = campaignConnector.retrieveCredentials(config);
Map<String, String> params = new HashMap<String, String>();
params.put("origin", "AEM");
CallResults results = campaignConnector.callGeneric("/jssp/cus/custom.jssp", params, credentials);
return results.bodyAsString();
} catch (Exception e ) {
log.error("Something went wrong during the connection", e);
}
return null;
}
}
AEM提供現成可用的API,可擷取網站管理員檔案總管檢視中任何位置可用的物件。
如需此範例,請參閱「Geometrixx」,此檔案可從「封裝共用」取得。
瀏覽器中的每個節點都有一個連結到該節點的API。 例如,對於節點:
API為:
URL .1.json的結尾可以由.2.json、.3.json取代,根據您想要獲得的子層數,若要取得所有子層數,可以使用關鍵字infinity:
現在,若要使用API,我們必須知道AEM依預設會使用基本驗證。
名為amcIntegration.js的JS程式庫可在6.1.1(組建版本8624及更新版本)中使用,在其他數個程式庫中實作該邏輯。
loadLibrary("nms:amcIntegration.js");
var cmsAccountId = sqlGetInt("select iExtAccountId from NmsExtAccount where sName=$(sz)","aemInstance")
var cmsAccount = nms.extAccount.load(String(cmsAccountId));
var cmsServer = cmsAccount.server;
var request = new HttpClientRequest(cmsServer+"/content/campaigns/geometrixx.infinity.json")
aemAddBasicAuthentication(cmsAccount, request);
request.method = "GET"
request.header["Content-Type"] = "application/json; charset=UTF-8";
request.execute();
var response = request.response;