RESTful API Support - Plain JAX-RS Services

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

RESTful API Support - Plain JAX-RS Services

chaga
Hello everyone,
I am using spring Rest template to extract the data from hippo using plain JAX-RS Services and displaying this data in the frontend using the rest client, I followed these tutorials
http://blog.jeroenreijn.com/2011/02/hippos-restful-jax-rs-component-support.html
http://www.onehippo.org/7_7/library/concepts/rest/restful-api-support---plain-jax-rs-services.html

I have a document type called ContentBlock which has one String and a image.

This is the code which extracts all the documents of ContentBlock and converts it to a list of ContentBlockRepresentation
@Path("/contentBlocks/")
public class ContentBlockResource extends AbstractContentResource {

    @GET
    @Path("/")
    @Produces("text/xml")
    public CollectionContentBlockRepresentation getAllContentBlocks(@Context HttpServletRequest servletRequest,
            @Context HttpServletResponse servletResponse) {

        CollectionContentBlockRepresentation contentBlocks = new CollectionContentBlockRepresentation();

        HstRequestContext requestContext = getRequestContext(servletRequest);

        try {

            String mountContentPath = requestContext.getResolvedMount().getMount().getContentPath();
            Node mountContentNode = requestContext.getSession().getRootNode().getNode(PathUtils.normalizePath(mountContentPath));

            HstQueryManager manager = getHstQueryManager(requestContext.getSession(), requestContext);
            HstQuery hstQuery = manager.createQuery(mountContentNode, ContentBlock.class, true);
            HstQueryResult result = hstQuery.execute();
            HippoBeanIterator iterator = result.getHippoBeans();

            while (iterator.hasNext()) {
                ContentBlock contentBlock = (ContentBlock) iterator.nextHippoBean();

                if (contentBlock != null) {
                    ContentBlockRepresentation contentBlockRep = new ContentBlockRepresentation(contentBlock.getTitle(),
                            contentBlock.getDescription(), getImage(contentBlock.getImage()));
                    contentBlocks.getContentBlocks().add(contentBlockRep);
                }
            }

        } catch (Exception e) {
        }

        return contentBlocks;
    }

private ImageRepresentation getImage(HippoGalleryImageSetBean image) {
        if (image != null) {
            ImageRepresentation imageRepresentation = new ImageRepresentation();
            imageRepresentation.setPath(image.getOriginal().getPath());
            imageRepresentation.setFileName(image.getFileName());
            return imageRepresentation;
        } else
            return null;

    }

   }

@XmlRootElement(name = "contentBlock")
public class ContentBlockRepresentation {

    private String title;
    private String description;
    private ImageRepresentation image;

    public ContentBlockRepresentation() {
    }

    public ContentBlockRepresentation(String title, String description, ImageRepresentation image) {
        this.title = title;
        this.description = description;
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @XmlElement(name = "image")
    public ImageRepresentation getImage() {
        return image;
    }

    public void setImage(ImageRepresentation image) {
        this.image = image;
    }

}

@XmlRootElement(name = "image")
public class ImageRepresentation {

    private String path;
    private String fileName;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

}

I am using JAX-B to map the ContentBlock object to ContentBlockRepresentation object. This object is returned by the restClient to the frontend of my site
 In my ImageRepresentation object I have a variable called path in which i want to store the path of the image. The problem is that which path should i store so that i can display it in the jsp. I was able to display the variables like title and description as they are of string type but image is of type HippoGalleryImageSetBean.

Could you suggest me the mapping for the  image type inorder to display it in the Jsp.



Cdt,
Chandan AGARWAL
IED
Smile Benelux
Brussels
Mb - (+32) 0492101358


_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html
Reply | Threaded
Open this post in threaded view
|

Re: RESTful API Support - Plain JAX-RS Services

Jeroen Reijn
Administrator
Hi,

the GoGreen demo has an example of this. See:

http://svn.onehippo.org/viewvc/hippo-demos/hippo-go-green/tags/hippogogreen-3.05.06/site/src/main/java/com/onehippo/gogreen/jaxrs/model/ProductRepresentation.java?revision=34780&view=markup

There is a method that shows an example of how to create the REST URL
for the image.

Let us know if that helps.

Jeroen

On Tue, Sep 25, 2012 at 6:42 PM, Chandan AGARWAL
<[hidden email]> wrote:

> Hello everyone,
> I am using spring Rest template to extract the data from hippo using plain
> JAX-RS Services and displaying this data in the frontend using the rest
> client, I followed these tutorials
> http://blog.jeroenreijn.com/2011/02/hippos-restful-jax-rs-component-support.html
> http://www.onehippo.org/7_7/library/concepts/rest/restful-api-support---plain-jax-rs-services.html
>
> I have a document type called ContentBlock which has one String and a image.
>
> This is the code which extracts all the documents of ContentBlock and
> converts it to a list of ContentBlockRepresentation
> @Path("/contentBlocks/")
> public class ContentBlockResource extends AbstractContentResource {
>
>     @GET
>     @Path("/")
>     @Produces("text/xml")
>     public CollectionContentBlockRepresentation getAllContentBlocks(@Context
> HttpServletRequest servletRequest,
>             @Context HttpServletResponse servletResponse) {
>
>         CollectionContentBlockRepresentation contentBlocks = new
> CollectionContentBlockRepresentation();
>
>         HstRequestContext requestContext =
> getRequestContext(servletRequest);
>
>         try {
>
>             String mountContentPath =
> requestContext.getResolvedMount().getMount().getContentPath();
>             Node mountContentNode =
> requestContext.getSession().getRootNode().getNode(PathUtils.normalizePath(mountContentPath));
>
>             HstQueryManager manager =
> getHstQueryManager(requestContext.getSession(), requestContext);
>             HstQuery hstQuery = manager.createQuery(mountContentNode,
> ContentBlock.class, true);
>             HstQueryResult result = hstQuery.execute();
>             HippoBeanIterator iterator = result.getHippoBeans();
>
>             while (iterator.hasNext()) {
>                 ContentBlock contentBlock = (ContentBlock)
> iterator.nextHippoBean();
>
>                 if (contentBlock != null) {
>                     ContentBlockRepresentation contentBlockRep = new
> ContentBlockRepresentation(contentBlock.getTitle(),
>                             contentBlock.getDescription(),
> getImage(contentBlock.getImage()));
>                     contentBlocks.getContentBlocks().add(contentBlockRep);
>                 }
>             }
>
>         } catch (Exception e) {
>         }
>
>         return contentBlocks;
>     }
>
> private ImageRepresentation getImage(HippoGalleryImageSetBean image) {
>         if (image != null) {
>             ImageRepresentation imageRepresentation = new
> ImageRepresentation();
>             imageRepresentation.setPath(image.getOriginal().getPath());
>             imageRepresentation.setFileName(image.getFileName());
>             return imageRepresentation;
>         } else
>             return null;
>
>     }
>
>    }
>
> @XmlRootElement(name = "contentBlock")
> public class ContentBlockRepresentation {
>
>     private String title;
>     private String description;
>     private ImageRepresentation image;
>
>     public ContentBlockRepresentation() {
>     }
>
>     public ContentBlockRepresentation(String title, String description,
> ImageRepresentation image) {
>         this.title = title;
>         this.description = description;
>         this.image = image;
>     }
>
>     public String getTitle() {
>         return title;
>     }
>
>     public void setTitle(String title) {
>         this.title = title;
>     }
>
>     public String getDescription() {
>         return description;
>     }
>
>     public void setDescription(String description) {
>         this.description = description;
>     }
>
>     @XmlElement(name = "image")
>     public ImageRepresentation getImage() {
>         return image;
>     }
>
>     public void setImage(ImageRepresentation image) {
>         this.image = image;
>     }
>
> }
>
> @XmlRootElement(name = "image")
> public class ImageRepresentation {
>
>     private String path;
>     private String fileName;
>
>     public String getPath() {
>         return path;
>     }
>
>     public void setPath(String path) {
>         this.path = path;
>     }
>
>     public String getFileName() {
>         return fileName;
>     }
>
>     public void setFileName(String fileName) {
>         this.fileName = fileName;
>     }
>
> }
>
> I am using JAX-B to map the ContentBlock object to
> ContentBlockRepresentation object. This object is returned by the restClient
> to the frontend of my site
>  In my ImageRepresentation object I have a variable called path in which i
> want to store the path of the image. The problem is that which path should i
> store so that i can display it in the jsp. I was able to display the
> variables like title and description as they are of string type but image is
> of type HippoGalleryImageSetBean.
>
> Could you suggest me the mapping for the  image type inorder to display it
> in the Jsp.
>
>
>
> Cdt,
> Chandan AGARWAL
> IED
> Smile Benelux
> Brussels
> Mb - (+32) 0492101358
>
>
> _______________________________________________
> Hippo-cms7-user mailing list and forums
> http://www.onehippo.org/cms7/support/forums.html



--
Jeroen Reijn
Solution Architect
Hippo

Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 1 Broadway, Cambridge, MA 02142

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com

http://about.me/jeroenreijn
_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html
Reply | Threaded
Open this post in threaded view
|

Re: RESTful API Support - Plain JAX-RS Services

Woonsan Ko-3
In reply to this post by chaga
On 9/25/12 12:42 PM, Chandan AGARWAL wrote:

> I am using JAX-B to map the ContentBlock object to
> ContentBlockRepresentation object. This object is returned by the
> restClient to the frontend of my site
>   In my ImageRepresentation object I have a variable called path in
> which i want to store the path of the image. The problem is that which
> path should i store so that i can display it in the jsp. I was able to
> display the variables like title and description as they are of string
> type but image is of type HippoGalleryImageSetBean.
>
> Could you suggest me the mapping for the  image type inorder to display
> it in the Jsp.

In your REST Service bean (which extends AbstractResource like you did),
you can invoke #getSiteLink(requestContext, HippoBean), which returns a
link object which you can use.
So, for example, if you have a requestContext and
HippoGalleryImageSetBean, then you could invoke
super.getSiteLink(requestContext, galleryImageSetBean.getThumbnail()) or
super.getSiteLink(requestContext, galleryImageSetBean.getOriginal()).
You can also use the method for other documents to create proper website
links.

Regards,

Woonsan



--
[hidden email]     www.onehippo.com
Boston - 1 Broadway, Cambridge, MA 02142
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html
Reply | Threaded
Open this post in threaded view
|

Re: RESTful API Support - Plain JAX-RS Services

chaga
Thanks everyone...it worked

Cdt,
Chandan AGARWAL
IED
Smile Benelux
Brussels
Mb - (+32) 0492101358


From: "Woonsan Ko" <[hidden email]>
To: "Hippo CMS 7 implementation list" <[hidden email]>
Sent: Thursday, September 27, 2012 4:56:49 AM
Subject: Re: [Hippo-cms7-user] RESTful API Support - Plain JAX-RS Services

On 9/25/12 12:42 PM, Chandan AGARWAL wrote:

> I am using JAX-B to map the ContentBlock object to
> ContentBlockRepresentation object. This object is returned by the
> restClient to the frontend of my site
>   In my ImageRepresentation object I have a variable called path in
> which i want to store the path of the image. The problem is that which
> path should i store so that i can display it in the jsp. I was able to
> display the variables like title and description as they are of string
> type but image is of type HippoGalleryImageSetBean.
>
> Could you suggest me the mapping for the  image type inorder to display
> it in the Jsp.

In your REST Service bean (which extends AbstractResource like you did),
you can invoke #getSiteLink(requestContext, HippoBean), which returns a
link object which you can use.
So, for example, if you have a requestContext and
HippoGalleryImageSetBean, then you could invoke
super.getSiteLink(requestContext, galleryImageSetBean.getThumbnail()) or
super.getSiteLink(requestContext, galleryImageSetBean.getOriginal()).
You can also use the method for other documents to create proper website
links.

Regards,

Woonsan



--
[hidden email]     www.onehippo.com
Boston - 1 Broadway, Cambridge, MA 02142
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html


_______________________________________________
Hippo-cms7-user mailing list and forums
http://www.onehippo.org/cms7/support/forums.html