Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Using HttpClient = Error importing OSGI bundle from Eclispe

Avatar

Level 2

Hi Everyone,

I hope someone can help.

Im trying to import a servlet into CQ via maven and getting an error which I dont understand.  My servlet is using HttpClient to call a third party api. 

When I import via maven I get the following error

16.09.2013 12:36:08.210 *ERROR* [OsgiInstallerImpl] org.apache.sling.servlets.resolver.internal.SlingServletResolver bindServlet: Servlet service not available from reference [javax.servlet.Servlet, javax.servlet.ServletConfig, java.io.Serializable]

Here is my code

@Component(immediate = true, metatype = false, description = "Darren Test") @Service @Properties({ @Property(name = "sling.servlet.paths", value = "/bin/darrentest") }) /** * How to send a request via proxy using {@link HttpClient}. * * @since 4.0 */ public class DarrenTest extends SlingSafeMethodsServlet { /** * */ private static final long serialVersionUID = 1L; private final int connectionTimeoutMillis = 5000; private final int socketTimeoutMillis = 5000; protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException { HttpClient httpClient = new DefaultHttpClient(); HttpParams httpParams = httpClient.getParams(); httpParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3,false)); HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis); HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);

 

Can anyone help me unsertand what is going on here?

1 Accepted Solution

Avatar

Correct answer by
Level 10

I have discovered your issue. Look at this illustration:

[img]TestSer33.png[/img]

It means that there is missing dependencies within AEM. You need to include these JAR files within a bundle fragment:

  • httpclient-4.3
  • httpcore-4.3

Once you get these into a bundle fragment and deployed -- you will be able to start your bundle that contains the sling servlet. To learn how to create a bundle fragment, see the first link above. We placed a JSON JAR into a bundle fragment. 

HTH

View solution in original post

4 Replies

Avatar

Level 10

We have a community article that discusses how to successfully create a sling Servlet for AEM. In your code -- i am not seeing this annotation: 
@SlingServlet

See this community article: 

http://scottsdigitalcommunity.blogspot.ca/2013/06/posting-form-data-to-adobe-cq-using.html

In this article -- the OSGi bundle is deployed manually; however, in this article, we use a Maven plugin to automatically deploy the OSGi bundle:

http://scottsdigitalcommunity.blogspot.ca/2013/03/creating-adobe-cq-shopping-cart.html

Build the servlet as shown in article 1 and then you can use the Maven plugin shown in the 2nd article to deploy via Maven (or you can deploy manually).

Hope this helps you.

 


 




 

Avatar

Level 2

Hi,

Thanks for the post..

I believe the I made a mistake with the pasting of my code as I had to remove some client specific stuff... And must of removed the annotation by mistake

Ive created a small test bundle

 

package osgi.test;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

@SlingServlet(paths = "/bin/test", methods = "GET")
public class ApiServletTwo extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = -3960692666512058118L;
    
    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)  throws ServletException, IOException {
        
        HttpClient httpClient = new DefaultHttpClient();
        HttpParams httpParams = httpClient.getParams();
        httpParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3,false));
//        HttpConnectionParams.setConnectionTimeout(httpParams, 200);
//        HttpConnectionParams.setSoTimeout(httpParams, 200);
    
    }
     
}

If I comment out the httpParams the bundle is fine, but as it stands I get

16.09.2013 14:32:52.708 *ERROR* [OsgiInstallerImpl] org.apache.sling.servlets.resolver.internal.SlingServletResolver bindServlet: Servlet service not available from reference [javax.servlet.Servlet, javax.servlet.ServletConfig, java.io.Serializable] 16.09.2013 14:32:52.711 *ERROR* [OsgiInstallerImpl] org.apache.sling.servlets.resolver.internal.SlingServletResolver bindServlet: Servlet service not available from reference [javax.servlet.Servlet]

 

Is there a conflict of some sort?

Avatar

Level 10

It looks that way -- i am going to try building an OSGi bundle with this code and see if i can duplicate your results. 

Avatar

Correct answer by
Level 10

I have discovered your issue. Look at this illustration:

[img]TestSer33.png[/img]

It means that there is missing dependencies within AEM. You need to include these JAR files within a bundle fragment:

  • httpclient-4.3
  • httpcore-4.3

Once you get these into a bundle fragment and deployed -- you will be able to start your bundle that contains the sling servlet. To learn how to create a bundle fragment, see the first link above. We placed a JSON JAR into a bundle fragment. 

HTH