How to invoke another OSGI bundle servlet from different OSGI bundle? | Community
Skip to main content
October 16, 2015
Solved

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

  • October 16, 2015
  • 11 replies
  • 3344 views

Hi there,

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

 

Thanks a lot.

Best regards,

Brian

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

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

11 replies

Level 3
October 16, 2015

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.

smacdonald2008
Level 10
October 16, 2015

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>

smacdonald2008
Level 10
October 16, 2015

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/package-summary.html

October 16, 2015

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

October 16, 2015

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.

smacdonald2008
Level 10
October 16, 2015

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

smacdonald2008
Level 10
October 16, 2015

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>

 

 
 
October 16, 2015

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

smacdonald2008
Level 10
October 16, 2015

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. 

smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

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