Sunday, January 25, 2015

How to Modify the Node permissions through Java - Adobe Experience Manager(AEM)

How to Modify the Node permissions through Java - Adobe Experience Manager(AEM)

This post will explain how  to Modify the Node permissions through Java in Adobe Experience Manager(AEM)

Java API:

import java.util.NoSuchElementException;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.security.AccessControlEntry;
import javax.jcr.security.AccessControlManager;
import javax.jcr.security.AccessControlPolicyIterator;
import javax.jcr.security.Privilege;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.JackrabbitAccessControlList;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.framework.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(label = "ModifyNodePermissions", metatype = false, immediate = true)
@Properties({
@Property(name = Constants.SERVICE_DESCRIPTION, value = "ModifyNodePermissions") })
@Service(value = ModifyNodePermissions.class)
public class ModifyNodePermissions {

private static final Logger log = LoggerFactory.getLogger(ModifyNodePermissions.class);
@Reference
private SlingRepository repository;

public void modifyNodePermissions(String nodePath,String groupName)
{
Session session = null;
try {

session = repository.loginAdministrative(null);

UserManager userMgr = ((org.apache.jackrabbit.api.JackrabbitSession) session).getUserManager();
AccessControlManager accessControlManager = session.getAccessControlManager();
Authorizable authorizable  = userMgr.getAuthorizable(groupName);
AccessControlPolicyIterator policyIterator = accessControlManager.getApplicablePolicies(nodePath);

org.apache.jackrabbit.api.security.JackrabbitAccessControlList acl = null;

try {
acl = (JackrabbitAccessControlList) policyIterator.nextAccessControlPolicy();              

} catch (NoSuchElementException nse) {
acl = (JackrabbitAccessControlList) accessControlManager.getPolicies(nodePath)[0];
}

//Remove the Access Control Entry
 /*for (AccessControlEntry e : acl.getAccessControlEntries()) {
  if (e.getPrincipal().equals(authorizable.getPrincipal()))
 {
      acl.removeAccessControlEntry(e);
 }
                }*/

//Allow
/*Privilege[] allowPrivileges = {accessControlManager.privilegeFromName(Privilege.JCR_REMOVE_NODE),
accessControlManager.privilegeFromName(Privilege.JCR_REMOVE_CHILD_NODES) };

acl.addEntry(authorizable.getPrincipal(), allowPrivileges, true);
  */
//Deny
Privilege[] denyPrivileges = {accessControlManager.privilegeFromName(Privilege.JCR_REMOVE_NODE),
accessControlManager.privilegeFromName(Privilege.JCR_REMOVE_CHILD_NODES) };

acl.addEntry(authorizable.getPrincipal(), denyPrivileges, false);

//Add Policy
accessControlManager.setPolicy(nodePath, acl);
//Remove Policy
//accessControlManager.removePolicy(nodePath, acl);
session.save();

} catch (RepositoryException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null)
session.logout();
}
}
}


Saturday, January 24, 2015

How to Retrieve/Update the configuration details through Configuration Admin Service - Adobe Experience Manager(AEM)

How to Retrieve/Update the configuration details through Configuration Admin Service  - Adobe Experience Manager(AEM)

This post will explain how to Retrieve/Update the configuration details through Configuration Admin Service  in Adobe Experience Manager(AEM)

Java API

//Add the reference
@Reference
org.osgi.service.cm.ConfigurationAdmin configAdmin;

//Reterive/Update the configration

org.osgi.service.cm.Configuration config = configAdmin.getConfiguration("com.day.cq.mailer.DefaultMailService");
Dictionary d = config.getProperties();
String fromAddress=(String)d.get("from.address");
d.put("from.address", "albin.issac1@gmail.com");
config.update(d);

Saturday, January 17, 2015

Configuring proxy server details for HttpClient communication in Adobe Experience Manager(AEM)

Configuring proxy server details for HttpClient communication in Adobe Experience Manager(AEM)

This post will explain how to configure proxy server details for HttpClient communication in Adobe Experience Manager(AEM)

Go to config manager - http://localhot:4502/system/console/configMgr
Open Day Commons HTTP Client 3.1 and provide the proxy server details.


Check "Enable HTTP Proxy" and provide the HTTP Proxy Host and Port details.
If authentication required provide the proxy server user name and password.

Provide the host name and ip address for which the proxy is not required.

This proxy details will be used for all the communication happens via Apache HTTP commons. Client
This details will be used upon replication, if the proxy is not required to connect to publisher then add the publisher host name to No Proxy For list.

This proxy details will not be used if we are using HttpURLConnection for http communication.

Wednesday, January 14, 2015

Invoking NTLM authentication enabled service through Java

Invoking NTLM authentication enabled service through Java

Java API

The below program help us to invoke NTLM authentication enabled services.

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.HttpStatus;

public class NTLMAuthentication {

public String invokeService(String url, String soapMessage) {
String responseString = null;
try {

HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestEntity(new StringRequestEntity(soapMessage,"text/xml","utf-8"));
postMethod.setRequestHeader("SOAPAction", "http://www.service.com/GetEmployeeData");
postMethod.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
NTCredentials credentials = new NTCredentials("username","password", "hostName", "domain");
client.getState().setCredentials(new AuthScope(null, -1, null), credentials);
int status = client.executeMethod(postMethod);
if (status == HttpStatus.SC_OK)
{
responseString = postMethod.getResponseBodyAsString();
}
} catch (Exception e) {

}
return responseString;

}

}

Tuesday, December 30, 2014

Building a executable JAR file with external libraries through JDeveloper

Building a executable JAR file with external libraries through JDeveloper

This post explain the different approaches to build the executable JAR file with external libraries through JDeveloper.

Referring the external libraries from JAR:

Right Click project and select properties
Click on Deployment and create new JAR deployment profile.
Select "Include Manifest File" option.
Select the main class.



Create a manifest file(any location) and add the required jar files to the Class-Path header

Class-Path.mf

Class-Path: json.jar log4j.jar

 End the Class-Path.mf file by a carriage return.


Add the Class-Path.mf file to Additional Manifest Files to Merge into Manifest.mf


Saturday, December 20, 2014

Encrypting/Decrypting data in Adobe Experience Manager(AEM)

Encrypting/Decrypting data in Adobe Experience Manager

com.adobe.granite.crypto.CryptoSupport service can be used to encrypt/decrypt the data in Adobe Experience Manager(AEM).

Maven Dependency:

<dependency>
  <groupId>com.adobe.granite</groupId>
        <artifactId>com.adobe.granite.crypto</artifactId>
         <version>0.0.18</version>
         <scope>provided</scope>
 </dependency>

Service to encrypt/decrypt the message:

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;

@Component(immediate = true, metatype = true)
@Service(value = EncrypionService.class)
public class EncrypionService {
@Reference
private CryptoSupport cryptoSupport;

public String encrypt(String plainText) throws Exception
{
try {
return cryptoSupport.protect(plainText);
} catch (CryptoException e) {
throw e;
}
}

public String decrypt(String encryptedText) throws Exception
{
try {
return cryptoSupport.unprotect(encryptedText);
} catch (CryptoException e) {
throw e;
}
}
}

org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught Throwable java.lang.LinkageError: loader constraint violation - Adobe Experience Manager(AEM)

org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught Throwable java.lang.LinkageError: loader constraint violation - Adobe Experience Manager(AEM)

Sometimes we may receive the following exception while invoking the Adobe Experience Manager(AEM) services.

20.12.2014 11:15:31.816 *ERROR* [0:0:0:0:0:0:0:1 [1419054331815] GET /services/EmailServlet HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught Throwable java.lang.LinkageError: loader consaint violation: when resolving interface method "com..commerce.connector.CommerceService.getSession(Ljavax/servlet/http/HttpServleequest;Ljavax/servlet/http/HttpServleesponse;)Lcom//commerce/connector/session/CommerceSession;" the class loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) of the current class, com///servlet/EmailServlet, and the class loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) for resolved class, com//commerce/connector/CommerceService, have different Class objects for the type connector.CommerceService.getSession(Ljavax/servlet/http/HttpServleequest;Ljavax/servlet/http/HttpServleesponse;)Lcom//commerce/connector/session/CommerceSession; used in the signature
at com...servlet.EmailServlet.doPost(EmailServlet.java:67)
at com...servlet.EmailServlet.doGet(EmailServlet.java:56)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:268)
at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:139)

This error will be thrown while invoking the service of one bundle from another bundle.

To fix this issue, make sure the required packages from the target bundle is imported to the source bundle.