Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to invoke another OSGI bundle servlet from different OSGI bundle?

Avatar

Former Community Member

Hi there,

i just wondering How to invoke another OSGI bundle servlet from different OSGI bundle?

 

Thanks a lot.

Best regards,

Brian

1 Accepted Solution

Avatar

Correct answer by
Level 10

I got this use case to work. The following pic shows an AEM web page that displays data returned from a Sling Servlet (the Sling Servlet invoked by an AEM Service):

[img]SlingInvoke.png[/img]

Here is the community article that steps you through this use case:

https://helpx.adobe.com/experience-manager/using/HttpClient_AEM.html

Only use this use case if you have to invoke a servlet from a service. It better to invoke the servlet from a web page using AJAX - as talked about here:

http://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

View solution in original post

11 Replies

Avatar

Level 3

Servlets are registered services in OSGi so you can access it just like a service. If you have shared logic, its probably better to move that code into a class you can define as an OSGi service.

Avatar

Level 10

IN CQ 5.6 - you have access to this API:

Package org.apache.commons.httpclient

Here is this API showing up the AEM Dependency Finder:

[img]cleinthttp.png[/img]

So you can code a custom service in the OSGi that can use an object of type org.apache.commons.httpclient.HttpClient to send an HTTP request to an AEM Sling Servlet.  

Also - here is the Maven dependency you need:

<dependency>
    <groupId>com.day.commons.osgi.wrapper</groupId>
    <artifactId>com.day.commons.osgi.wrapper.commons-httpclient</artifactId>
    <version>3.1.0.018</version>
    <scope>provided</scope>
</dependency>

Avatar

Level 10

If you want to invoke an AEM servlet from another OSGi within the same AEM instance (as opposed to using Service injection), you can use Java org.apache.http.client.methods API. See:

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/pack...

Avatar

Former Community Member

Hi scott,

Thanks for your comments. and the question is if AEM support third party httpclient.jar dependency well?

 

Thanks a lot.

Best regards,

Brian

Avatar

Former Community Member

hi scott,

i did it following your suggestion. However, maven can't download the jar com.day.commons.osgi.wrapper.commons-httpclient-3.1.0.018.jar

i don't know how to deal with it.  Details please see below screenshot.

[img]httpclient.png[/img]

 

Thanks a lot.

Avatar

Level 10

What you do is inject service A (defined in bundle X) into service B (defined in bundle Z). If you are not familiar with how to perform dependency injection - see this article:

 Injecting a DataSourcePool Service into an Adobe Experience Manager OSGi bundle

Avatar

Level 10

To get the aem http client api to work with maven, you have to add the following elements to your POM file. This pulls from the adobe repos

<repositories>
    <repository>
    <id>adobe</id>
    <name>Adobe Public Repository</name>
    <url>http://repo.adobe.com/nexus/content/groups/public/</url>
    <layout>default</layout>
    </repository>
    </repositories>
    <pluginRepositories>
    <pluginRepository>
    <id>adobe</id>
    <name>Adobe Public Repository</name>
    <url>http://repo.adobe.com/nexus/content/groups/public/</url>
    <layout>default</layout>
    </pluginRepository>
</pluginRepositories>

 

 
 

Avatar

Former Community Member

Hi Scott,

After add that elements to my POM file, it happened the error either as before.

[img]httpclient.png[/img]

 

So weird here.

 

Best regards,

Brian

Avatar

Level 10

I will have a community article that shows you HOW TO use org.apache.commons.httpclient APIS in AEM - including how to build the OSGi and how to use it to invoke a Sling Servlet within AEM. This will be posted sometime in the next day. 

Avatar

Correct answer by
Level 10

I got this use case to work. The following pic shows an AEM web page that displays data returned from a Sling Servlet (the Sling Servlet invoked by an AEM Service):

[img]SlingInvoke.png[/img]

Here is the community article that steps you through this use case:

https://helpx.adobe.com/experience-manager/using/HttpClient_AEM.html

Only use this use case if you have to invoke a servlet from a service. It better to invoke the servlet from a web page using AJAX - as talked about here:

http://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

Avatar

Former Community Member

When i delete below element within POM file, run maven command "mvn clean install" very well.

<dependency> <groupId>com.day.commons.osgi.wrapper</groupId> <artifactId>com.day.commons.osgi.wrapper.commons-httpclient</artifactId> <version>3.1.0.018</version> <scope>provided</scope> </dependency>

However, if i add the above element within POM file, run maven command appears error as below immediately.

[img]mvnerror.png[/img]