Errors trying to implement Adobe Helpx RESTful Services example | Community
Skip to main content
au4liferz
Level 4
January 31, 2016
Solved

Errors trying to implement Adobe Helpx RESTful Services example

  • January 31, 2016
  • 11 replies
  • 3751 views

I'm trying to implement the example found at: https://helpx.adobe.com/experience-manager/using/restful-services.html , however, I keep getting the "Unexpected token <", due to the server probably returning a 404 or some other HTML error code. I followed the article and implemented it step by step in a 5.6.1 environment. When I initially imported my OSGI bundle containing the org.apache.httpclient jars, it was running/active, but now, it is only listed as "Installed", and when I click on the "start/run" button, I get the error shown below. After I couldn't get that service running, I installed the example 6.1 package in an AEM 6.1 environment, and it was working initially, but after uninstalling, re-installing and restarting the server, I can't get the associated OSGI services started anymore. They are in a "Installed" state, but won't start. Please help.

1. I'm also a little confused as to what my OSGI bundle should list in the manifest under "Export-package" and "Import-package".

Errors from 5.6.1 error.log:

An error occurred at line: 20 in the jsp file: /apps/dev-sandbox/components/restService/query.json.jsp com.adobe.restservice.Distance cannot be resolved to a type 17: String filter = request.getParameter("filter"); 18: 19: // Instatiate an instance of the appropriate service using Sling 20: com.adobe.restservice.Distance googleTagService = sling.getService(com.adobe.restservice.Distance.class); 21: 22: // Invoke the service 23: String jsonResponse = googleTagService.getDistance() ; at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388) at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:171) at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:463) at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:508) at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64) at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilterWithErrorHandling(WCMDebugFilter.java:182) at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:149) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:356) at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:168) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at com.day.cq.personalization.impl.TargetComponentFilter.doFilter(TargetComponentFilter.java:96) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60) at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:254) at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64) at com.day.cq.wcm.core.impl.AuthoringUIModeServiceImpl.doFilter(AuthoringUIModeServiceImpl.java:301)

-------------

Errors from the 6.1  error.log:
31.01.2016 03:10:10.802 *INFO* [OsgiInstallerImpl] org.apache.sling.installer.core.impl.tasks.BundleInstallTask Exception during install of bundle TaskResource(url=jcrinstall:/apps/restservice/osgi-bundles/install/restful-bundle-1.0-SNAPSHOT.jar, entity=bundle:com.adobe.restservice.restful-bundle, state=INSTALL, attributes=[org.apache.sling.installer.api.tasks.ResourceTransformer=:24:43:21:, Bundle-SymbolicName=com.adobe.restservice.restful-bundle, Bundle-Version=1.0.0.SNAPSHOT], digest=1454226994663) : Bundle installation rejected by hook.. Retrying later. org.osgi.framework.BundleException: Bundle installation rejected by hook. at org.apache.felix.framework.Felix.installBundle(Felix.java:3123) at org.apache.felix.framework.BundleContextImpl.installBundle(BundleContextImpl.java:167) at org.apache.sling.installer.core.impl.tasks.BundleInstallTask.execute(BundleInstallTask.java:47) at org.apache.sling.installer.core.impl.OsgiInstallerImpl.doExecuteTasks(OsgiInstallerImpl.java:847) at org.apache.sling.installer.core.impl.OsgiInstallerImpl.executeTasks(OsgiInstallerImpl.java:689) at org.apache.sling.installer.core.impl.OsgiInstallerImpl.run(OsgiInstallerImpl.java:265) at java.lang.Thread.run(Thread.java:745) 31.01.2016 03:10:55.334 *ERROR* [qtp552317373-248] org.apache.felix.http.jetty %bundles.pluginTitle: Cannot start (org.osgi.framework.BundleException: Unresolved constraint in bundle RestFulDep [448]: Unable to resolve 448.0: missing requirement [448.0] osgi.wiring.package; (osgi.wiring.package=javax.security.auth.x500)) org.osgi.framework.BundleException: Unresolved constraint in bundle RestFulDep [448]: Unable to resolve 448.0: missing requirement [448.0] osgi.wiring.package; (osgi.wiring.package=javax.security.auth.x500) at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4095) at org.apache.felix.framework.Felix.startBundle(Felix.java:2114) at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:977) at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:964) at org.apache.felix.webconsole.internal.core.BundlesServlet.doPost(BundlesServlet.java:365) at javax.servlet.http.HttpServlet.service(HttpServlet.java:644) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) --------- And also ------- An error occurred at line: 5 in the jsp file: /apps/restservice/components/page/templateRest/query.json.jsp com.adobe.restservice.Distance cannot be resolved to a type 2: <%@ page import="org.apache.sling.commons.json.io.*,org.w3c.dom.*" %><% 3: String filter = request.getParameter("filter"); 4: 5: com.adobe.restservice.Distance cs = sling.getService(com.adobe.restservice.Distance.class); 6: 7: String myJSON= cs.getDistance() ; 8: at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388) at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:171) at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:463) at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:533) at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:44) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:77)
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

"Please help me understand how to go about determining which classes to export and import when I create OSGI bundles"

Here is a good article that talks about OSGi and the role of the Export-Package and Import-Package sections

http://www-01.ibm.com/support/knowledgecenter/SSCKBL_8.5.5/com.ibm.websphere.osgi.nd.doc/ae/ra_bundle_mf.html

Now in the Creating Adobe Experience Manager bundles that invoke third party Restful web services example - the main Java OSGi bundle contains a service that uses these APIs to invoke the 3rd party Restful API: 

DefaultHttpClient httpClient = new DefaultHttpClient();
              
            HttpGet getRequest = new HttpGet("http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver%20BC&destinations=San%20Francisco&sensor=false");
            getRequest.addHeader("accept", "application/json");
 
            HttpResponse response = httpClient.execute(getRequest);

Because we are using these Java HTTP APIs - we need to ensure that the OSGi service imports the required Java packages.

The main OSGi service that contains these HTTP APIs has this Manifest file Import-Package section (lets call this BUNDLE A): 

Import-Package:

org.apache.http,

org.apache.http.client.methods,

org.apache.http.conn,

org.apache.http.impl.client,

Now - we have to make sure that all of these packages are exported in ANOTHER OSGi bundle in AEM. Otherwise BundleA will not start. 

This is why we create a 2nd OSGi bundle in the article that uses these 2 JAR Files: 

  • httpclient-4.0.jar  
  • httpcore-4.3.2.jar

Lets call this bundle B.  

The job of BUNDLE B is to expose these Java packages. That is why we wrap these 2 JAR files by using an Eclipse plug-in project. This is the Manifest file of Bundle B-  notice that this bundle exposes these Java packages in the Export Package section. 

Export-Package: org.apache.http,org.apache.http.annotation,org.apache.
 http.auth,org.apache.http.auth.params,org.apache.http.client,org.apac
 he.http.client.entity,org.apache.http.client.methods,org.apache.http.
 client.params,org.apache.http.client.protocol,org.apache.http.client.
 utils,org.apache.http.concurrent,org.apache.http.config,org.apache.ht
 tp.conn,org.apache.http.conn.params,org.apache.http.conn.routing,org.
 apache.http.conn.scheme,org.apache.http.conn.ssl,org.apache.http.conn
 .util,org.apache.http.cookie,org.apache.http.cookie.params,org.apache
 .http.entity,org.apache.http.impl,org.apache.http.impl.auth,org.apach
 e.http.impl.client,org.apache.http.impl.conn,org.apache.http.impl.con
 n.tsccm,org.apache.http.impl.cookie,org.apache.http.impl.entity,org.a
 pache.http.impl.io,org.apache.http.impl.pool,org.apache.http.io,org.a
 pache.http.message,org.apache.http.params,org.apache.http.pool,org.ap
 ache.http.protocol,org.apache.http.util

 

To summarize - Bundle A imports the Java HTTP packages and Bundle B exports Java HTTP packages. Therefore the MF file of Bundle A has the IMPORT-Package section and Bundle B has the Export-Package section (shown above). 

When you follow the article - you DO NOT NEED TO ADD any Java packages to the IMPORT-Package or Export-Package section. This is done automatically. When using Maven to build Bundle A (that contain the HTTP API to invoke the 3rd party Restful service)- Maven adds the Java packages to the IMPORT-Package section of the MF file of the bundle.

When you use Eclipse plug-in project - Eclipse adds Java packages to the Export-Package  section. All you need to do is remove the version information (if there is any in the MF file in eclipse). 

For this article - install the package on a clean version of AEM. If you ahve a clean AEM 6.1 instance - install the package at the start of the article. The example app - that invokes Google Restful service- will run. 

If you want to see another example of using an API in AEM that uses dependancies - see this new community article:

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

This uses the WordPress API. We also need to get WordPress OSGi dependences into AEM. 

11 replies

au4liferz
au4liferzAuthor
Level 4
February 1, 2016

smacdonald2008 wrote...

I will add a note that says it is automatically done. If it's empty - this means that Eclipse did not do its job for some reason. I am very glad you got it working.

 

 

The article tells you to "make sure that the Import-Package header is MANIFEST.MF is populated", but it doesn't tell you what to do if Eclipse doesn't populate it. How would a AEM user typically find out that he needed to import the org.apache.commons.logging package, (and other dependent packages) in the Import-Package section???