Accessing SiteMenus with REST API

170 views
Skip to first unread message

prasad.sai

unread,
Apr 11, 2016, 11:56:07 AM4/11/16
to Hippo Community
Hi ,
I have a requirement where i need to access the Sitemenus from cms with REST API call, I have copied the sitemenus node under rest mount point and trying to write MenuResource to return the menusitems configured for the site. but i am getting error at hstRequest.getHstSiteMenus().getSiteMenu("main") it expects ResolvedSiteMapItem matched i do find a way to set this explicitly. please find the code below.
Can somebody please help if there is any better way  of getting menus using rest api.


@GET
@Path("/sitemenu")
public void index(@Context HttpServletRequest request)
throws PathNotFoundException, LoginException, RepositoryException {
HstRequestContext hstRequest = getRequestContext(request);
HippoBean deepestMenuBean = null;
Mount siteMount = hstRequest.getResolvedMount().getMount();
String contentPath = siteMount.getContentPath();
System.out.println("content path" + contentPath + "");
HstSiteMenu selectedSiteMapItem = hstRequest.getHstSiteMenus().getSiteMenu("main"); // Throws exception  at this line Caused by: java.lang.IllegalStateException: HstRequestContext#getHstSiteMenus() is not allowed to be invoked without there being a ResolvedSiteMapItem matched and set on this HstRequestContext.
at org.hippoecm.hst.site.request.HstRequestContextImpl.getHstSiteMenus(HstRequestContextImpl.java:322)
}

marijan milicevic

unread,
Apr 11, 2016, 12:05:26 PM4/11/16
to hippo-c...@googlegroups.com
Hi,

I am afraid this wont work because you are not  accessing the site through mapped sitemap (you are using plain rest service, that's what error above indicates)...I am also not sure if this will work if you setup a sitemap item matching your request, but you could try. 


cheers
marijan

 

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.

prasad.sai

unread,
Apr 11, 2016, 12:44:43 PM4/11/16
to Hippo Community
Hi Marijan ,

Thank you for the Response, i am using JaxrsRestContentPipeline i even tried approach given in other post on forum below is the code though i am using version 10. can you please explain what do you mean by "if you setup a sitemap item matching your request" i will try if that works.

Approach given in other post on forum : Returning Empty {}. menu list 

                HstSiteMenusManager siteMenusManager = HstServices.getComponentManager().getComponent("org.hippoecm.hst.core.sitemenu.HstSiteMenusManager");

HstSiteMenus selectedSiteMapItem = siteMenusManager.getSiteMenus(hstRequest);
System.out.println(selectedSiteMapItem.getSiteMenus());


hosts.xml config :
 <sv:node sv:name="api-manual">
          <sv:property sv:name="jcr:primaryType" sv:type="Name">
            <sv:value>hst:mount</sv:value>
          </sv:property>
          <sv:property sv:name="hst:alias" sv:type="String">
            <sv:value>api-manual</sv:value>
          </sv:property>
          <sv:property sv:name="hst:ismapped" sv:type="Boolean">
            <sv:value>false</sv:value>
          </sv:property>
          <sv:property sv:name="hst:namedpipeline" sv:type="String">
            <sv:value>JaxrsRestContentPipeline</sv:value>
          </sv:property>
          <sv:property sv:name="hst:types" sv:type="String" sv:multiple="true">
            <sv:value>rest</sv:value>
          </sv:property>
        </sv:node>

Thanks
Prasad

marijan milicevic

unread,
Apr 12, 2016, 6:12:40 AM4/12/16
to hippo-c...@googlegroups.com
Hi Prasad,

On Mon, Apr 11, 2016 at 6:44 PM, prasad.sai <mah...@qualityideaslab.com> wrote:
Hi Marijan ,

Thank you for the Response, i am using JaxrsRestContentPipeline i even tried approach given in other post on forum below is the code though i am using version 10. can you please explain what do you mean by "if you setup a sitemap item matching your request" i will try if that works.

I am talking about is to add sitemap item to your project so request is mathced through sitemap item, so for your example this would be following:
<sv:node sv:name="sitemenu">

<sv:property sv:name="jcr:primaryType" sv:type="Name">
    <sv:value>hst:sitemapitem</sv:value>

</sv:property>
<sv:property sv:name="hst:namedpipeline" sv:type="String">
<sv:value>JaxrsRestContentPipeline</sv:value>
</sv:property>
  <sv:node sv:name="_any_">

<sv:property sv:name="jcr:primaryType" sv:type="Name">
      <sv:value>hst:sitemapitem</sv:value>

</sv:property>
<sv:property sv:name="hst:namedpipeline" sv:type="String">
<sv:value>JaxrsRestContentPipeline</sv:value>
</sv:property>
  </sv:node>
</sv:node>
hth
cheers
marijan

prasad

unread,
Apr 12, 2016, 8:04:11 AM4/12/16
to Hippo Community
Hi Marijan , Thank you for the response.

   I tried your approach by configuring following mount in host.xml as follows and I got the 404 error while calling url : http://localhost:8080/site/sitemenu.
 

host.xml file:

<?xml version="1.0" encoding="UTF-8"?><sv:node xmlns:sv="http://www.jcp.org/jcr/sv/1.0" sv:name="hst:hosts">
  <sv:property sv:name="jcr:primaryType" sv:type="Name">
    <sv:value>hst:virtualhosts</sv:value>
  </sv:property>
  <sv:property sv:name="hst:defaultcontextpath" sv:type="String">
    <sv:value>/site</sv:value>
  </sv:property>
  <sv:node sv:name="dev-localhost">
    <sv:property sv:name="jcr:primaryType" sv:type="Name">
      <sv:value>hst:virtualhostgroup</sv:value>
    </sv:property>
    <sv:property sv:name="hst:cmslocation" sv:type="String">
      <sv:value>http://localhost:8080/cms</sv:value>
    </sv:property>
    <sv:property sv:name="hst:defaultport" sv:type="Long">
      <sv:value>8080</sv:value>
    </sv:property>
    <sv:node sv:name="localhost">
      <sv:property sv:name="jcr:primaryType" sv:type="Name">
        <sv:value>hst:virtualhost</sv:value>
      </sv:property>
      <sv:node sv:name="hst:root">
        <sv:property sv:name="jcr:primaryType" sv:type="Name">
          <sv:value>hst:mount</sv:value>
        </sv:property>
        <sv:property sv:name="hst:channelpath" sv:type="String">
          <sv:value>/hst:hst/hst:channels/qilstorefront</sv:value>
        </sv:property>
        <sv:property sv:name="hst:mountpoint" sv:type="String">
          <sv:value>/hst:hst/hst:sites/qilstorefront</sv:value>
        </sv:property>
        <sv:node sv:name="api-manual">
          <sv:property sv:name="jcr:primaryType" sv:type="Name">
            <sv:value>hst:mount</sv:value>
          </sv:property>
          <sv:property sv:name="hst:alias" sv:type="String">
            <sv:value>api-manual</sv:value>
          </sv:property>
          <sv:property sv:name="hst:ismapped" sv:type="Boolean">
            <sv:value>false</sv:value>
          </sv:property>
          <sv:property sv:name="hst:namedpipeline" sv:type="String">
            <sv:value>JaxrsRestPlainPipeline</sv:value>
          </sv:property>
          <sv:property sv:name="hst:types" sv:type="String" sv:multiple="true">
            <sv:value>rest</sv:value>
          </sv:property>
        </sv:node>
        <sv:node sv:name="api">
          <sv:property sv:name="jcr:primaryType" sv:type="Name">
            <sv:value>hst:mount</sv:value>
          </sv:property>
          <sv:property sv:name="hst:alias" sv:type="String">
            <sv:value>api</sv:value>
          </sv:property>
          <sv:property sv:name="hst:ismapped" sv:type="Boolean">
            <sv:value>false</sv:value>
          </sv:property>
          <sv:property sv:name="hst:namedpipeline" sv:type="String">
            <sv:value>RestApiPipeline</sv:value>
          </sv:property>
          <sv:property sv:name="hst:types" sv:type="String" sv:multiple="true">
            <sv:value>rest</sv:value>
          </sv:property>
        </sv:node>
<sv:node sv:name="sitemenu">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>hst:sitemapitem</sv:value>
</sv:property>
<sv:property sv:name="hst:namedpipeline" sv:type="String">
<sv:value>JaxrsRestContentPipeline</sv:value>
</sv:property>
<sv:node sv:name="_any_">
<sv:property sv:name="jcr:primaryType" sv:type="Name">
<sv:value>hst:sitemapitem</sv:value>
</sv:property>
<sv:property sv:name="hst:namedpipeline" sv:type="String">
<sv:value>JaxrsRestContentPipeline</sv:value>
</sv:property>
</sv:node>
</sv:node>
      </sv:node>
    </sv:node>
  </sv:node>
</sv:node>


and the response is in the attahed file jpg file.

I need to access menu items in the attached screenshot.

thanks
Prasad





 
notfound.jpg

marijan milicevic

unread,
Apr 12, 2016, 8:12:12 AM4/12/16
to hippo-c...@googlegroups.com
you cannot add hst:sitemapitem to your host file (most probably you are getting bootstrapping error), it should go  to /hst:hst/hst:configurations/SITENAME/hst:sitemap 
where SITENAME is name of your site,
you can access it through Hippo Console (/cms/console)

cheers
marijan

thanks
Prasad





 

prasad

unread,
Apr 12, 2016, 9:03:08 AM4/12/16
to Hippo Community
Hi, Marijan  

I made the following changes In cms/console and still I am unable get the menus 

See the following jpg file and suggest me for any changes to get the menus.and I am confusing what should be the correct url path to get the menus 

please help us.


thanks 
prasad


eror.png

marijan milicevic

unread,
Apr 12, 2016, 9:11:02 AM4/12/16
to hippo-c...@googlegroups.com
Hi,

On Tue, Apr 12, 2016 at 3:03 PM, prasad <mah...@qualityideaslab.com> wrote:
Hi, Marijan  

I made the following changes In cms/console and still I am unable get the menus 



could you try to rename above "sitemenu" node into "api-manual" (I am assuming your rest mount name is "api-manual" and let us know if that works,

cheers
marijan 

See the following jpg file and suggest me for any changes to get the menus.and I am confusing what should be the correct url path to get the menus 

please help us.


thanks 
prasad


marijan milicevic

unread,
Apr 12, 2016, 9:11:58 AM4/12/16
to hippo-c...@googlegroups.com
On Tue, Apr 12, 2016 at 3:11 PM, marijan milicevic <m.mil...@onehippo.com> wrote:
Hi,

On Tue, Apr 12, 2016 at 3:03 PM, prasad <mah...@qualityideaslab.com> wrote:
Hi, Marijan  

I made the following changes In cms/console and still I am unable get the menus 



could you try to rename above "sitemenu" node into "api-manual" (I am assuming your rest mount name is "api-manual" and let us know if that works,


forgot to say, you should call: /site/api-manual/sitemenu

cheers
marijan 

prasad

unread,
Apr 12, 2016, 10:47:57 AM4/12/16
to Hippo Community
Hi Marijan,

Yes my rest mount is configured to api-manual, have changed the node to api-manual but still not able to get the menuitems, adding some more details this mount is pointing to "/hst:hst/hst:sites/qilstorefront"  whose hst:content is mapped to content/documents/qilstorefront. Attaching the screenshot of the cms/console.
Also attaching the Java code ( have tried with out this MenuResource code as well) and hosts.xml

java:
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED })
@Path("/sitemenu")
public class MenuResource extends BaseRestResource {
@GET
@Path("/")
public void index(@Context HttpServletRequest request)
throws PathNotFoundException, LoginException, RepositoryException {
HstRequestContext hstRequest = getRequestContext(request);
HstSiteMenusManager siteMenusManager = HstServices.getComponentManager().getComponent("org.hippoecm.hst.core.sitemenu.HstSiteMenusManager");
    //System.out.println("sitemenus manger"+siteMenusManager);
System.out.println(     RequestContextProvider.get().getResolvedMount().getMount().getHstSite());
    siteMenusManager.getSiteMenus(hstRequest);
    HstSiteMenu selectedSiteMapItem = hstRequest.getHstSiteMenus().getSiteMenu("api-manual");
ResolvedSiteMapItem resolvedSiteMapItem = hstRequest.getResolvedSiteMapItem();
hstRequest.getResolvedMount().getResolvedMountPath();
 final Mount mount = hstRequest.getResolvedMount().getMount();
   ResolvedSiteMapItem resolvedSiteMapItem2 = hstRequest.getMount("api-manual").getHstSiteMapMatcher().match(hstRequest.getBaseURL().getPathInfo(), hstRequest.getResolvedMount());
}

sorry for troubling, we are new to hippo cms and trying get it rit.

Thanks 
Prasad





screenshot.jpg
hosts.xml

marijan milicevic

unread,
Apr 12, 2016, 11:45:44 AM4/12/16
to hippo-c...@googlegroups.com
Hi,


ok, I see where the problem is. 
We are mixing two concepts here (Content and Plain pipeline). 
Few things:
- make sure you are extending from AbstractContentResource  (BaseRestResource is probably Hippo essentials one, and is meant for Plain pipelines)
- for content resource to work, you'll need to map *content* to your sitemap item. 

<?xml version="1.0" encoding="UTF-8"?>
<sv:node sv:name="sitemenu" xmlns:sv="http://www.jcp.org/jcr/sv/1.0">
  <sv:property sv:name="jcr:primaryType" sv:type="Name">
    <sv:value>hst:sitemapitem</sv:value>
  </sv:property>
  <sv:property sv:name="jcr:uuid" sv:type="String">
    <sv:value>f87b6a62-4983-4d90-90e8-497c4b0977a4</sv:value>
  </sv:property>
  <sv:property sv:name="hst:namedpipeline" sv:type="String">
    <sv:value>JaxrsRestContentPipeline</sv:value>
  </sv:property>
  <sv:property sv:name="hst:relativecontentpath" sv:type="String">
    <sv:value>banners</sv:value>
  </sv:property>
  <sv:node sv:name="_any_">
    <sv:property sv:name="jcr:primaryType" sv:type="Name">
      <sv:value>hst:sitemapitem</sv:value>
    </sv:property>
    <sv:property sv:name="jcr:uuid" sv:type="String">
      <sv:value>ce595027-2b0b-4982-9183-bf39ae2e1870</sv:value>
    </sv:property>
    <sv:property sv:name="hst:namedpipeline" sv:type="String">
      <sv:value>JaxrsRestContentPipeline</sv:value>
    </sv:property>
    <sv:property sv:name="hst:relativecontentpath" sv:type="String">
      <sv:value>banners/{1}</sv:value>
    </sv:property>
  </sv:node>
</sv:node>


Now calling my rest url works fine: /site/api-manual/sitemenus works fine.


You'll also need to set your rest mount hst:ismapped to true. see below screenshot:



I've tested with both content and plain ones and it works







Thanks 
Prasad





prasad

unread,
Apr 12, 2016, 2:18:34 PM4/12/16
to Hippo Community
Hi marijan ,

Could you please  tell me how to map *content* on sitemapitem and give the exact url you have called 


Thanks 
Prasad
 
 
 

Minos Chatzidakis

unread,
Apr 12, 2016, 3:04:02 PM4/12/16
to Hippo Community
Hi Prasad,

The sitemenus of a site belong to that site, and a site is represented by a mount. When in your code you get the resolvedMount, that mount is the "api-manual" and that one is not backed by an hst:configuration, thus it has no sitemenus. 

I think the simplest approach would be to:
-Change your class to extend AbstractContentResource
-Keep your sitemapitem but rename it back to "sitemenu"
-Do not use "api-manual" in your url, but just /site/sitemenu
-Remove your rest mount, I doubt if it's needed since you're using the JaxrsRestContentPipeline on the sitemapitem
-Your sitemapitems don't need to have a relativecontentpath

I would expect that the following will happen:
-Request comes in for /site/sitemenu
-This url will be handled by the hst:root mount (cause you removed the "api-manual" part)
-HST matches the url to a sitemapitem in the site the hst:root mount points to. This sitemapitem is your "sitemenu" item
-HST sees the sitemapitem has a namedpipeline, and then checks to see if there are any services registered for path "sitemenu"
-Your service is called cause it registers path "sitemenu"
-Your code works because the mount is hst:root, which indeed has sitemenus to fetch

Could you please try the above and let us know if it works?

I'm not entirely sure if you should use the plain or content rest services, but the sitemenus are not considered content in Hippo and so I'd expect the plain services are a better match. To do this you need to:
-Change your class to extend AbstractResource
-Change the named pipeline on your sitemapitems to "JaxrsRestPlainPipeline"

Could you also try this?

HTH!
Minos
--


--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.



--

Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - Inc. 71 Summer Street, 2nd Floor, MA 02110

Minos Chatzidakis

unread,
Apr 12, 2016, 4:56:49 PM4/12/16
to Hippo Community
On Tue, Apr 12, 2016 at 9:03 PM, Minos Chatzidakis <m.chat...@onehippo.com> wrote:
Hi Prasad,

The sitemenus of a site belong to that site, and a site is represented by a mount. When in your code you get the resolvedMount, that mount is the "api-manual" and that one is not backed by an hst:configuration, thus it has no sitemenus. 

I think the simplest approach would be to:
-Change your class to extend AbstractContentResource
-Keep your sitemapitem but rename it back to "sitemenu"
-Do not use "api-manual" in your url, but just /site/sitemenu
-Remove your rest mount, I doubt if it's needed since you're using the JaxrsRestContentPipeline on the sitemapitem
-Your sitemapitems don't need to have a relativecontentpath

I would expect that the following will happen:
-Request comes in for /site/sitemenu
-This url will be handled by the hst:root mount (cause you removed the "api-manual" part)
-HST matches the url to a sitemapitem in the site the hst:root mount points to. This sitemapitem is your "sitemenu" item
-HST sees the sitemapitem has a namedpipeline, and then checks to see if there are any services registered for path "sitemenu"
-Your service is called cause it registers path "sitemenu"
-Your code works because the mount is hst:root, which indeed has sitemenus to fetch

Should have been more specific here, the code that I expect to work is RequestContextProvider.get().getHstSiteMenus().getSiteMenu("main");

prasad

unread,
Apr 12, 2016, 11:18:16 PM4/12/16
to Hippo Community
Hi Chatzidakis,

  Its really great ,Its absolutely working fine as we expected.

Thanks for your help.

Thanks a lot.
   
 

Minos Chatzidakis

unread,
Apr 14, 2016, 8:29:23 AM4/14/16
to Hippo Community
Thanks for your feedback Sai, glad to have helped!


--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages