© 2019 by Sayantan Basu

  • Sayantan Basu

Create Custom Claim

In this blog I am going to discuss “How do you create custom claim?” So before we move to the detailed implementation lets discuss the scenario first.

Scenario:

Business wants to personalize their site on the basis of browser version of the user. So we have to create a claim to store the user’s browser details.

E.g.  Business wants to show Product A as a promotion when user is browsing the site from Chrome 32 and above, and Product B for the rest.

PreRequisite:

Follow the below steps –

  1. Analyse the business requirement and create a flow chart.

  2. Create custom claim and store value.

  3. Create the custom cartridge for that claim.

  4. Add your claim to ADF configuration file.

Analyze the business requirement:

It is very important to analyze the requirement before you jump into the coding.

Once you have done your analysis you will come up with the detail flow chart of the requirement. As per the scenario discussed above here is the flow chart.


Create custom claim and store value:

Here is an additional step required before you start coding – Add your claim in the ClaimStoreUriConstants.

public static final URI BROWSER_VERSION = URI.create(“taf:claim:ambientdata:sessioncartridge:browsermodel”);

Once your claim is ready then start the coding. To create your custom claim processor you have to extend AbstractClaimProcessor

public class BrowserVersionClaimProcessor extends AbstractClaimProcessor {}

Next read your claims and implement your business logic. Here is my code for custom method-

void createIfNoClaimForThisSession(ClaimStore store) { //Here we are storing the values of Browser Model and Version. //Get the value from claim store. String browserModelClaim = (String)store.get(MODEL_CLAIM_URI); Version browserVersionClaim = (Version)store.get(VERSION_CLAIM_URI); if(browserVersionClaim != null) { //Converting the browser version to string value. String browserVersionClaimStr = browserVersionClaim.toString(); // Only storing the major version. browserVersionClaimStr = StringUtils.substringBefore(browserVersionClaimStr, “.”); // Checking whether Model is Chrome or not. if (CHROME.equals(browserModelClaim)) { // Checking whether Version is greater than 32 or not. if (Integer.parseInt(browserVersionClaimStr) >= BROWSER_CHROME_STANDARD) { // Set the value to claim store Chrome 32 And Above store.put(ClaimStoreUriConstants.BROWSER_VERSION, “Chrome 32 And Above”); } else { // Set the value to claim store Below Chrome 32 store.put(ClaimStoreUriConstants.BROWSER_VERSION, “Below Chrome 32”); } } else { // Set the value to claim store OTHERS store.put(ClaimStoreUriConstants.BROWSER_VERSION, OTHERS); } } else { // Set the value to claim store OTHERS store.put(ClaimStoreUriConstants.BROWSER_VERSION, OTHERS); } }

Now your claim processor is ready to store your claim in the claim store.

Create the custom cartridge for that claim:

Next create your custom cartridge. Here is the sample cartridge for this claim –

<?xml version=”1.0″ encoding=”UTF-8″?> <CartridgeDefinition Version=”6.1″ Uri=”taf:cartridge:BrowserVersion” Description=”cartridge Browser Version” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=”../../schemas/cd_ambient_cartridge_conf.xsd”> <ClaimDefinitions> <ClaimDefinition Uri=”taf:claim:ambientdata:sessioncartridge: browserversion” Scope=”SESSION” Description=” Browser Version” /> </ClaimDefinitions>  <!—Definition of the claim processor> <ClaimProcessorDefinitions> <ClaimProcessorDefinition Uri=”tcd:claim:browsersversion” ImplementationClass=”com.adf.BrowserVersionClaimProcessor” Description=”Browser Version Claim”> <RequestStart> <!—input for the claim processor> <InputClaims> <ClaimDefinition Uri=”taf:claim:context:browser:model” /> <ClaimDefinition Uri=”taf:claim:context:browser:version” /> </InputClaims>  <!—output of the claim processor> <OutputClaims> <ClaimDefinition Uri=”taf:claim:ambientdata:sessioncartridge:browsermodel” /> </OutputClaims> </RequestStart> </ClaimProcessorDefinition> </ClaimProcessorDefinitions> </CartridgeDefinition>

So we are now ready with our implementation. Now the very next step or you can say last step is add your claim into the ADF configuration file –

Open cd_ambient_conf.xml and check for the <GloballyAcceptedClaims> node.Under that node add your claim like below –

<Security> <GloballyAcceptedClaims> <!– TODO: all of these claims are not needed on deployment environments. Change pom.xml of the parent project to exclude these entries in production, etc. –>    <Claim Uri=”taf:claim:ambientdata:sessioncartridge:browsermodel”/> </GloballyAcceptedClaims> </Security>

Hope this blog helps you to create your own custom claim. If you need any further information or the source code please reply on this blog.

#TridionSmartTarget