Search in sources :

Example 1 with HstQueryManager

use of org.hippoecm.hst.content.beans.query.HstQueryManager in project hippo by NHS-digital-website.

the class CyberAlertResource method fetchCyberAlert.

@GET
@Path("/single/")
public CyberAlert fetchCyberAlert(@Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse, @Context UriInfo uriInfo, @PathParam("threatid") String threatid) {
    CyberAlert cyberAlert = null;
    try {
        threatid = servletRequest.getParameter("threatid");
        if (threatid != null) {
            HstRequestContext requestContext = RequestContextProvider.get();
            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, CyberAlert.class);
            Filter filter = hstQuery.createFilter();
            filter.addEqualTo("website:threatid", threatid);
            hstQuery.setFilter(filter);
            hstQuery.setLimit(1);
            HstQueryResult result = hstQuery.execute();
            HippoBeanIterator iterator = result.getHippoBeans();
            if (iterator.hasNext()) {
                cyberAlert = (CyberAlert) iterator.nextHippoBean();
            } else {
                JSONObject json = new JSONObject();
                json.put("error", "The threatid=" + threatid + " is not found");
                servletResponse.resetBuffer();
                servletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
                servletResponse.setHeader("Content-Type", "application/json");
                servletResponse.setCharacterEncoding("UTF-8");
                servletResponse.getWriter().write(json.toString());
                servletResponse.flushBuffer();
            }
        } else {
            JSONObject json = new JSONObject();
            json.put("error", "The URL is not correct. Use /single?threatid=<threatid>");
            servletResponse.resetBuffer();
            servletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
            servletResponse.setHeader("Content-Type", "application/json");
            servletResponse.setCharacterEncoding("UTF-8");
            servletResponse.getWriter().write(json.toString());
            servletResponse.flushBuffer();
        }
    } catch (Exception queryException) {
        log.warn("QueryException ", queryException);
    }
    return cyberAlert;
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) JSONObject(org.json.simple.JSONObject) Filter(org.hippoecm.hst.content.beans.query.filter.Filter) HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator) Node(javax.jcr.Node) HstQueryManager(org.hippoecm.hst.content.beans.query.HstQueryManager) CyberAlert(uk.nhs.digital.website.beans.CyberAlert) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

Node (javax.jcr.Node)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)1 HstQueryManager (org.hippoecm.hst.content.beans.query.HstQueryManager)1 HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)1 Filter (org.hippoecm.hst.content.beans.query.filter.Filter)1 HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)1 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)1 JSONObject (org.json.simple.JSONObject)1 CyberAlert (uk.nhs.digital.website.beans.CyberAlert)1