Sunday, 16 April 2017

Adobe Salesforce Connector - INVALID_SESSION_ID

Issue:

I have integrated AEM with Salesforce using salesforce cloud config. I am able to connect to salesforce successfully and able to pull data from salesforce. After sometime when I access aem page am getting error below
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]  
I have also checked below settings in salesforce
  • Session timeout after 4 hours of inactivity
  • Selecte OAuth scopes
    •  Perform requests on your behalf at any time (refresh_token, offline_access)
    • Access and manage your data (api)
    • Full Access (Full)

Resolution:

In sales force we need to remove Full Access (Full) OAuth Scopes, if we use this then refresh token will not be effective.

Friday, 14 April 2017

AEM: design and preview mode toolbar not visible

Issue
If you are using the sidekick on an author instance of CQ, you may notice that the toolbar at the bottom which includes the design and preview mode buttons, is not visible or available.
Reason
The functionality in the sidekick is determined by the ACLs (Access-Control-Lists) defined in the CQ server.  If you do not have the appropriate privileges then functionality may be hidden in your sidekick.  Sometimes the sidekick may become unstable if you add faulty components to the page, or after you have installed some packages that may have overwritten required objects in your application, breaking some dependencies.

Solution
You should first try to clear your browser cache, and then reload the page from WCM console to refresh the sidekick.
You should also ensure you have the correct privileges to access the appropriate design in /etc/designs.  This can be changed by an administrator on the Users tab in the siteadmin console.  If the privileges appear to be correct, then try to disable them, save, and then re-enable them and save.  The sidekick should now display the toolbar again as expected.

To display the design button on the sidekick enable Modify permission for etc path.

Thursday, 13 April 2017

Lazybones - Project Creation Tool

Lazybones was born out of frustration that Ratpack does not and will not have a command line tool that will bootstrap a project. It's a good decision for Ratpack, but I'm lazy and want tools to do the boring stuff for me.
The tool is very simple: it allows you to create a new project structure for any framework or library for which the tool has a template. You can even contribute templates by sending pull requests to this GitHub project or publishing the packages to the relevant Bintray repository (more info available below).
The concept of Lazybones is very similar to Maven archetypes, and what Yeoman does for web applications. Lazybones also includes a sub templates feature that resembles the behaviour of Yeoman's sub-generators, allowing you to generate optional extras (controllers, scaffolding etc.) inside a project.

Encrypting and Decrypting String in Java

The Advanced Encryption Standard (AES), also known by its original name Rijndael (Dutch pronunciation: [ˈrɛindaːl]),[5][6] is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology (NIST) in 2001.[7]

Below is sample java code to encrypt and decrypt a string.


package com.kishore;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Encrypt {

    protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    
    public static void main(String[] args) {
        String encryptString = "45634634";
        final String KEY = "kishore";
        Encrypt obj = new Encrypt();
        String encryptedString = obj.encrypt(encryptString, obj.encryptSecretKey(KEY));
        System.out.println("encryptedString: "+encryptedString);        
        String decyptedString = obj.decrypt(encryptedString, obj.encryptSecretKey(KEY));
        System.out.println("decyptedString: "+decyptedString);
    }
    public SecretKeySpec encryptSecretKey(final String secretKey) {
        MessageDigest sha = null;
        SecretKeySpec encryptApiSecret = null;
        try {
            byte[] key = secretKey.getBytes("UTF-8");
            sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16);
            encryptApiSecret = new SecretKeySpec(key, "AES");
        } catch (final NoSuchAlgorithmException e) {
            LOGGER.error("Exception occurred in encryptApiSecret",e);
        } catch (final UnsupportedEncodingException e) {
            LOGGER.error("Exception occurred in encryptApiSecret",e);
        }
        return encryptApiSecret;
    }
    
    public String decrypt(final String strToDecrypt, final SecretKeySpec secretKey) {
        String decryptedApiKey = "";
        try {
            final Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] c = Base64.decodeBase64(strToDecrypt.getBytes("UTF-8"));
            decryptedApiKey = new String(cipher.doFinal(c));
        } catch (final Exception e) {
            LOGGER.error("Exception occurred in decrypt",e);
        }
        return decryptedApiKey;
    }

    public String encrypt(final String strToDecrypt, final SecretKeySpec secretKey) {
        String encryptedApiKey = "";
        try {
            final Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] c = cipher.doFinal(strToDecrypt.getBytes());
            encryptedApiKey = new String(Base64.encodeBase64(c));
        } catch (final Exception e) {
            LOGGER.error("Exception occurred in encrypt",e);
        }
        return encryptedApiKey;
    }

}


Tuesday, 11 April 2017

AEM Component is in satisfied state, not in active

Some times we face OSGI component is in active state when the component is installed for the first time. When we modify the component from configMgr, component state changes to satisfied.

To avoid this and make the component in active state add the annotation @Activate to the activate method. This helps in changing the component state to Active whenever the component is modified.

Saturday, 8 April 2017

Salesforce REST API implementation - JAVA

This post demonstrates the following basic use cases for the REST API:
- authentication with OAuth 2.0 (This is for development purposes only. Not a real implementation.)
- querying (using account records)
- inserting (using a contact record related to one of the retrieved account records)
- updating (updates contact record added in previous step)

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import org.json.JSONException;
import org.json.JSONObject;