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;
}
Aggregations