package org.example.beans; import java.util.ArrayList; import java.util.List; import javax.jcr.Node; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import org.hippoecm.hst.content.beans.query.HstQuery; import org.hippoecm.hst.content.beans.query.HstQueryManager; import org.hippoecm.hst.content.beans.query.HstQueryResult; import org.hippoecm.hst.content.beans.query.filter.Filter; import org.hippoecm.hst.content.beans.standard.HippoBeanIterator; import org.hippoecm.hst.core.request.HstRequestContext; import org.hippoecm.hst.restapi.AbstractResource; import org.hippoecm.hst.util.PathUtils; import org.slf4j.Logger; @Path("/products/") public class ProductPlainResource extends AbstractResource { @GET @Path("/{productType}/") public List getProductResources( @Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse, @Context UriInfo uriInfo, @PathParam("productType") String productType) { List products = new ArrayList(); try { HstRequestContext requestContext = getRequestContext(servletRequest); HstQueryManager hstQueryManager = getHstQueryManager(requestContext.getSession(), requestContext); String mountContentPath = requestContext.getResolvedMount().getMount() .getContentPath(); Node mountContentNode = requestContext.getSession().getRootNode() .getNode(PathUtils.normalizePath(mountContentPath)); HstQuery hstQuery = hstQueryManager.createQuery(mountContentNode, ProductBean.class); Filter filter = hstQuery.createFilter(); filter.addEqualTo("demosite:product", productType); hstQuery.setFilter(filter); hstQuery.addOrderByDescending("demosite:price"); hstQuery.setLimit(10); HstQueryResult result = hstQuery.execute(); HippoBeanIterator iterator = result.getHippoBeans(); while (iterator.hasNext()) { ProductBean productBean = (ProductBean) iterator.nextHippoBean(); if (productBean != null) { ProductRepresentation productRep = new ProductRepresentation().represent(productBean); productRep.addLink(getNodeLink(requestContext, productBean)); productRep.addLink(getSiteLink(requestContext, productBean)); products.add(productRep); } } } catch (Exception e) { throw new WebApplicationException(e); } return products; } @Override public Logger getLogger() { // TODO Auto-generated method stub return null; } }