Sunday, February 15, 2015

org.osgi.framework.ServiceException: Service cannot be cast: javax.servlet.Servlet - Adobe Experience Manager(AEM)

org.osgi.framework.ServiceException: Service cannot be cast: javax.servlet.Servlet - Adobe Experience Manager(AEM)

I was getting the below exception while invoking the servlet from Adobe Experience Manager(AEM) bundle.

15.02.2015 20:58:11.615 *ERROR* [FelixDispatchQueue] FrameworkEvent ERROR (org.osgi.framework.ServiceException: Service cannot be cast: javax.servlet.Servlet) org.osgi.framework.ServiceException: Service cannot be cast: javax.servlet.Servlet
at org.apache.felix.framework.ServiceRegistrationImpl.getFactoryUnchecked(ServiceRegistrationImpl.java:332)
at org.apache.felix.framework.ServiceRegistrationImpl.getService(ServiceRegistrationImpl.java:219)
at org.apache.felix.framework.ServiceRegistry.getService(ServiceRegistry.java:320)
at org.apache.felix.framework.Felix.getService(Felix.java:3556)
at org.apache.felix.framework.BundleContextImpl.getService(BundleContextImpl.java:468)

 The root cause of the problem is different versions of servlet API classes are loaded by two different class-loaders.

To fix the issue make sure <scope>provided</scope> is added for the Servlet API dependency in POM.xml

<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
</dependency>






Saturday, February 14, 2015

com.google.gson,version=[2.2,3) -- Cannot be resolved in bundle - Adobe Experience Manager(AEM)

com.google.gson,version=[2.2,3) -- Cannot be resolved in bundle - Adobe Experience Manager(AEM)

I was able compile and  deploy the bundle successfully with gson dependency but the bundle is not getting started with the error "com.google.gson,version=[2.2,3) -- Cannot be resolved in bundle" in Adobe Experience Manager(AEM)

The root cause of the problem is the gson jar is not available in the server for bundle access.

This issue can be resolved  by following any of the below approach.

1. Export the gson  package - com.google.gson.*

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<scope>compile</scope>
</dependency>

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Activator>com.sample.activator.Activator</Bundle-Activator>
<Import-Package>*,com.sample.*</Import-Package>
<Export-Package>com.sample.*,com.google.gson.*</Export-Package>
<Bundle-SymbolicName>com.sample</Bundle-SymbolicName>
<Bundle-Vendor>Sample</Bundle-Vendor>
<Bundle-Category>Sample</Bundle-Category>
<Embed-Directory>dependencies</Embed-Directory>
<Embed-Transitive>true</Embed-Transitive>
</instructions>
</configuration>
</plugin>

2. Add the gson jar in the bundle class path

Specify the gson dependency scpes as compile or runtime(compile is the default scope)

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<scope>compile</scope>
</dependency

The gson jar will be included in the bundle classpath.

The dependencies with what are all the scopes are included in the bundle classpath is configured in maven-bundle-plugin

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<configuration>
<instructions>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Directory>OSGI-INF/lib</Embed-Directory>
<Embed-Transitive>true</Embed-Transitive>
</instructions>
</configuration>
</plugin>



How to create the contact in Eloqua through java

How to create the contact in Eloqua through java

This post explains how to create the contact in Eloqua through java.

Create the model class for Contact:

public class Contact
{
 public String accountName;
 public String address1;
 public String address2;
 public String address3;
 public String businessPhone;
 public String city;
 public String country;
 public String firstName;
 public String emailAddress;
 public String id;
 public boolean isBounceback;
 public boolean isSubscribed;
 public String lastName;
 public String salesPerson;
 public String title;
 public String type;
 public String name;

}

RestClient class to connect to Eloqua:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class RestClient
{
 private String authToken;
 private String baseUrl;

 public RestClient(String user, String password, String url)
 {
  baseUrl = url;
  String authString = user + ":" + password;
  authToken = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(authString.getBytes());        }

 public String execute(String uri, String method, String body)  throws Exception
 {
  String response ="";
  try
  {
   URL url = new URL(baseUrl + uri);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();

   conn.setInstanceFollowRedirects(false);
   conn.setRequestMethod(method.toString());
   conn.setRequestProperty("Content-Type", "application/json");
   conn.setRequestProperty("Accept", "application/json");
         conn.setRequestProperty("Authorization", authToken);    
       
         if (method == "POST" || method == "PUT")
         {
          if(null != body){
    conn.setDoOutput(true);
          final OutputStream os = conn.getOutputStream();
                os.write(body.getBytes());
                os.flush();
                os.close();
          }
         }
       
         InputStream is = conn.getInputStream();
         BufferedReader rd = new BufferedReader(new InputStreamReader( is));

          String line;
         while ((line = rd.readLine()) != null)
         {
             response += line;
         }      
         rd.close();
         conn.disconnect();
  }
  catch (Exception e)
  {
   throw e;
  }
        return response;
 }
}




Tuesday, February 10, 2015

Expected a string but was BEGIN_ARRAY at line 1 column - Gson

Expected a string but was BEGIN_ARRAY at line 1 column 

I was receiving "Expected a string but was BEGIN_ARRAY at line 1 column" error while deserializing the json string to java object using Gson.

Model class:

public class EmailGroup{
private String type;
private String id;
private String name;
private String permissions;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPermissions() {
return permissions;
}
public void setPermissions(String permissions) {
this.permissions = permissions;
}
}

JSON Response:

{"emailGroup":{"type":"EmailGroup","id":"1","depth":"minimal","description":"","name":"sample","permissions":[2,5,4,3]}}

Gson gson = new Gson();
EmailGroup obj = gson.fromJson(jsonstring, EmailGroup.class)

The root cause of the exception is the permissions attribute defined in the model class is of type String but the type of permissions in the jsonstring is List.

To fix the issue convert the type of permissions in the model class to List<String>(type string will change based on the type of the list entries)

public class EmailGroup{
private String type;
private String id;
private String name;
private List<String> permissions;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getPermissions() {
return permissions;
}
public void setPermissions(List<String> permissions) {
this.permissions = permissions;
}
}



Saturday, February 7, 2015

How to retrieve the pages created with particular template using Query Builder API - AEM/Adobe CQ5

How to retrieve the pages created with particular template using Query Builder API - AEM/Adobe CQ5

This post will explain how to retrieve the pages created with particular template using Query Builder API in Adobe Experience Manager(AEM)/Adobe CQ5

API Reference:

@Reference
QueryBuilder queryBuilder;

Session session = repository.loginAdministrative(null);

String resourcePath="/content/sample/site-demo/en";

Map<String, String> searchMap = new HashMap<String, String>();
searchMap.put("type", "cq:Page");
searchMap.put("path", resourcePath);
searchMap.put("property", "jcr:content/cq:template");
searchMap.put("property.value", "/apps/common/templates/sampleview");
searchMap.put("p.limit", "-1");

Query query = queryBuilder.createQuery(PredicateGroup.create(searchMap),session);

SearchResult result = query.getResult();
for(Hit hit:result.getHits()) {

    String pagePath = hit.getPath();
    Node pageNode = hit.getNode
}