use of org.hippoecm.hst.core.request.HstRequestContext 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;
}
use of org.hippoecm.hst.core.request.HstRequestContext in project hippo by NHS-digital-website.
the class SearchPageComponent method doBeforeRender.
@Override
public void doBeforeRender(HstRequest request, HstResponse response) {
super.doBeforeRender(request, response);
HstRequestContext requestContext = RequestContextProvider.get();
List<SearchResultTab> searchResultTabs = new ArrayList<>();
try {
searchContent(request, searchResultTabs);
searchPeople(request, searchResultTabs);
} finally {
request.setAttribute(REQUEST_ATTR_AREA, getAreaOption(request).getDisplayName());
request.setAttribute(REQUEST_ATTR_QUERY, getSearchQuery(request, true));
searchResultTabs.add(new SearchResultTab(SearchArea.ALL, searchResultTabs.stream().reduce(0, (sum, tab) -> sum + tab.getNumberOfResults(), Integer::sum)));
searchResultTabs.sort(Comparator.comparing(SearchResultTab::getTabName));
request.setAttribute(REQUEST_ATTR_SEARCH_TABS, searchResultTabs);
request.setAttribute("authorizationUrl", getAuthorizationUrl());
request.setAttribute("loginRequired", !hasValidAccessToken(requestContext));
}
}
Aggregations