Content/Context Aware JAX-RS Services - HOWTO

175 views
Skip to first unread message

Gangel Dávid

unread,
Mar 30, 2015, 12:17:21 PM3/30/15
to hippo-c...@googlegroups.com
Hi,

I'm stuck with the above topic.
What I have sofar:
1. With the essentials app I was able to setup a plain REST Api endpoint end it works fine with it's generated sample:

package com.cit_group.ac.cobra.rest.plain;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.onehippo.cms7.essentials.components.paging.Pageable;
import org.onehippo.cms7.essentials.components.rest.BaseRestResource;
import org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext;
import com.cit_group.ac.cobra.beans.ContentDocument;

/**
 * @version "$Id$"
 */

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
@Path("/ContentDocument/")
public class ContentDocumentResource extends BaseRestResource {


    @GET
    @Path("/")
    public Pageable<ContentDocument> index(@Context HttpServletRequest request) {
        return findBeans(new DefaultRestContext(this, request), ContentDocument.class);
    }


    @GET
    @Path("/page/{page}")
    public Pageable<ContentDocument> page(@Context HttpServletRequest request, @PathParam("page") int page) {
        return findBeans(new DefaultRestContext(this, request, page, DefaultRestContext.PAGE_SIZE), ContentDocument.class);
    }


    @GET
    @Path("/page/{page}/{pageSize}")
    public Pageable<ContentDocument> pageForSize(@Context HttpServletRequest request, @PathParam("page") int page, @PathParam("pageSize") int pageSize) {
        return findBeans(new DefaultRestContext(this, request, page, pageSize), ContentDocument.class);
    }


}


I also did follow this tutorial for the context aware rest endpoint for setting up the mountpoint:

My hosts.xml looks like this:

<sv:node sv:name="restapi">
          <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>restapi</sv:value>
          </sv:property>
          <sv:property sv:name="hst:isSite" sv:type="Boolean">
            <sv:value>false</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="contentrestapi">
          <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>contentrestapi</sv:value>
          </sv:property>
          <sv:property sv:name="hst:isSite" sv:type="Boolean">
            <sv:value>false</sv:value>
          </sv:property>
          <sv:property sv:name="hst:ismapped" sv:type="Boolean">
            <sv:value>true</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>

My question would be how can I implement the same method as above just using the context aware endpoint?

Gangel Dávid

unread,
Apr 1, 2015, 12:00:15 PM4/1/15
to hippo-c...@googlegroups.com
So what I want to reach is just to get a better understand between the plain and context aware rest api. Thats why I would like to see the same implementation for the same content.

Can someone add some hit at least please?

From the documentation I don't see what is the structure of the repository and it's content.

Jeroen Reijn

unread,
Apr 1, 2015, 3:49:35 PM4/1/15
to hippo-c...@googlegroups.com
Hi,

We don't have an exact example because in nature they are slightly different. I would advise you to take a look at the product related resources in the hippo test suite.


You will notice they are quite different.

The content based resource will only match based on the primary type of a document/node.

The plain resource is based on the provided url path only. It does not care about the type.

I hope this explains. Let me know if you have further questions.

Jeroen
--
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 http://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.


--
Sent from Gmail Mobile

Gangel Dávid

unread,
Apr 2, 2015, 6:55:42 AM4/2/15
to hippo-c...@googlegroups.com
Hi,

Thank you for your answer.
Ok I did found the 2 most helpfull class and methods where I can see the difference:
public ProductRepresentation getProductResource
and


http://svn.onehippo.org/viewvc/hippo-cms7/testsuite/tags/hippo-testsuite-1.06.02/components/src/main/java/org/hippoecm/hst/demo/jaxrs/services/ProductPlainResource.java?revision=46175&view=markup

public ProductRepresentation getProductResources

Based on the @Path annotation I guess for the Plain rest implementation the caller URL would be this:

http://localhost:8080/site/plainrest/products/brand/nameofTheBrand

But how would look like in the case of the context aware rest call?


And can you please add a bit more info about the paramaters of the Path annotation?

Thank you.

To post to this group, send email to hippo-community@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-community+unsubscribe@googlegroups.com.

Bert Leunis

unread,
Apr 2, 2015, 8:13:51 AM4/2/15
to hippo-c...@googlegroups.com
On Thu, Apr 2, 2015 at 12:55 PM, Gangel Dávid <gan...@gmail.com> wrote:
Hi,

Thank you for your answer.
Ok I did found the 2 most helpfull class and methods where I can see the difference:
public ProductRepresentation getProductResource
and


http://svn.onehippo.org/viewvc/hippo-cms7/testsuite/tags/hippo-testsuite-1.06.02/components/src/main/java/org/hippoecm/hst/demo/jaxrs/services/ProductPlainResource.java?revision=46175&view=markup

public ProductRepresentation getProductResources

Based on the @Path annotation I guess for the Plain rest implementation the caller URL would be this:

http://localhost:8080/site/plainrest/products/brand/nameofTheBrand

But how would look like in the case of the context aware rest call?
The /contentrest channel needs sitemap items like /products and /products/_any_.html. Through this URL a node of a certain type is found, say "myproject:product". A Resource is found that has a @Path("myproject:product") annotation.

So mounts with a JaxrsRestContentPipeline must have sitemap items (just like for a default html producing channel), and then Resources that are mapped to primary nodetypes.



And can you please add a bit more info about the paramaters of the Path annotation?
These are regular jax-rs annotations, nothing Hippo specific. So you can look for documentation and tutorials online. One example: https://docs.oracle.com/cd/E19798-01/821-1841/ginpw/

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.

Gangel Dávid

unread,
Apr 26, 2015, 11:49:19 AM4/26/15
to hippo-c...@googlegroups.com
Hi,

Thank you for your explanation.
Now I understood this.
Just one more question.
Can I use the go green hippo demo as a reference point for configuring the mount point and the sitemapitem(and possible other nodes that I found for need my project) as shown on the screenshot? Or it's outdated?

Thank you.


Bert Leunis

unread,
May 4, 2015, 2:25:11 AM5/4/15
to hippo-c...@googlegroups.com
On Sun, Apr 26, 2015 at 5:49 PM, Gangel Dávid <gan...@gmail.com> wrote:
Hi,

Thank you for your explanation.
Now I understood this.
Just one more question.
Can I use the go green hippo demo as a reference point for configuring the mount point and the sitemapitem(and possible other nodes that I found for need my project) as shown on the screenshot? Or it's outdated?
No, that should work just fine.
 

Gangel Dávid

unread,
May 5, 2015, 6:20:23 AM5/5/15
to hippo-c...@googlegroups.com
Hi,

I was able to setup now fully the rest api and it works good!
Thank you!

Last question:
Would I be able to expose the menus through rest api?
I mean the type of:
hst:sitemenus
and it's:
hst:sitemenuitems

Under the:
/hst:hst/hst:configurations/myproject

b.leunis

unread,
May 8, 2015, 3:28:41 AM5/8/15
to hippo-c...@googlegroups.com


Op dinsdag 5 mei 2015 12:20:23 UTC+2 schreef Gangel Dávid:
Hi,

I was able to setup now fully the rest api and it works good!
Thank you!

Last question:
Would I be able to expose the menus through rest api?
The same question came up on an internal project. Two possible options came up (Thanks Minos and Ard for this info):

  1. Obtain the siteMenusManager from HstServices:
    HstSiteMenusManager siteMenusManager = HstServices.getComponentManager().getComponent("org.hippoecm.hst.core.sitemenu.HstSiteMenusManager");

  2. Inject the siteMenusManager via your custom resource spring definition (and you'll need setters in your REST service class) :

    <bean id="customRestPlainResourceProviders" class="org.springframework.beans.factory.config.ListFactoryBean">
        <property name="sourceList">
          <list>
            <bean class="org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider">
              <constructor-arg>
                <bean class="com.example.rest.HstConfigurationRESTService"/>
                  <property name="hstSiteMenusManager" ref="org.hippoecm.hst.core.sitemenu.HstSiteMenusManager"/>
                </bean>
              </constructor-arg>
            </bean>
          </list>
        </property>
      </bean>
This works for Hippo 7.9 (an probably 7.8 too). From Hippo 10 this won't be needed any more since then the sitemenus will always be available on the HstRequestContext.

 
Reply all
Reply to author
Forward
0 new messages