use of org.hippoecm.hst.content.beans.standard.HippoBeanIterator 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.content.beans.standard.HippoBeanIterator in project hippo by NHS-digital-website.
the class CyberAlertResource method fetchAllThreatIds.
@GET
@Path("/getAllThreatIds/")
public ThreatIds fetchAllThreatIds(@Context HttpServletRequest request, @Context HttpServletResponse servletResponse) {
ThreatIds threatId = new ThreatIds();
List<ThreatIdDate> threatIdDateList = new ArrayList<ThreatIdDate>();
try {
final HstQuery query = createQuery(new DefaultRestContext(this, request), CyberAlert.class, Subtypes.INCLUDE);
query.setLimit(100);
final HstQueryResult result = query.execute();
HippoBeanIterator iterator = result.getHippoBeans();
while (iterator.hasNext()) {
CyberAlert cyberAlert = (CyberAlert) iterator.nextHippoBean();
List<Calendar> calList = new ArrayList<Calendar>();
ThreatIdDate threDate = new ThreatIdDate();
if (cyberAlert != null) {
List<HippoBean> cyberAcknowledgementList = (List<HippoBean>) cyberAlert.getCyberAcknowledgements();
for (HippoBean cyberAckn : cyberAcknowledgementList) {
if (cyberAckn instanceof CyberAcknowledgement) {
CyberAcknowledgement cybAck = (CyberAcknowledgement) cyberAckn;
calList.add(cybAck.getResponseDatetime());
}
}
threDate.setResponsedates(calList);
threDate.setThreatid(cyberAlert.getThreatId());
}
threatIdDateList.add(threDate);
}
} catch (Exception e) {
log.error("Error finding beans", e);
}
threatId.setThreatids(threatIdDateList);
return threatId;
}
use of org.hippoecm.hst.content.beans.standard.HippoBeanIterator in project hippo by NHS-digital-website.
the class FeedHubComponent method getFeed.
private <T extends HippoBean> List<T> getFeed(HstRequest request) throws QueryException {
final HstRequestContext context = request.getRequestContext();
FeedHub feedHub = (FeedHub) context.getContentBean();
HippoBean folder = feedHub.getParentBean();
ArrayList<Constraint> constraints = new ArrayList<>();
if ("Site-wide documents".equalsIgnoreCase(feedHub.getHubType())) {
folder = RequestContextProvider.get().getSiteContentBaseBean();
if (feedHub.getFeedType().equals("Cyber Alerts")) {
constraints.add(constraint("website:publicallyaccessible").equalTo(true));
} else if (!feedHub.getFeedType().equals("Supplementary information")) {
constraints.add(constraint("website:display").equalTo(true));
}
}
String dateField = "website:publisheddatetime";
Class feedClass = null;
switch(feedHub.getFeedType()) {
case "News":
feedClass = News.class;
if (filterValues.get("year").length > 0) {
Calendar newsDateFilter = Calendar.getInstance();
newsDateFilter.set(Calendar.YEAR, Integer.parseInt(filterValues.get("year")[0]));
newsDateFilter.set(Calendar.DAY_OF_YEAR, 1);
constraints.add(constraint(dateField).equalTo(newsDateFilter, DateTools.Resolution.YEAR));
}
break;
case "Events":
feedClass = Event.class;
dateField = "website:events/@website:startdatetime";
if (filterValues.get("year").length > 0) {
Calendar eventsDateFilter = Calendar.getInstance();
eventsDateFilter.set(Calendar.YEAR, Integer.parseInt(filterValues.get("year")[0]));
eventsDateFilter.set(Calendar.DAY_OF_YEAR, 1);
constraints.add(constraint("website:events/website:startdatetime").equalTo(eventsDateFilter, DateTools.Resolution.YEAR));
if (filterValues.get("month").length > 0) {
Integer month = getMonth(filterValues.get("month")[0]);
if (month != null) {
eventsDateFilter.set(Calendar.MONTH, month);
eventsDateFilter.set(Calendar.DAY_OF_MONTH, 1);
constraints.add(constraint("website:events/website:startdatetime").equalTo(eventsDateFilter, DateTools.Resolution.MONTH));
}
}
}
if (filterValues.get("type[]").length > 0) {
String[] types = filterValues.get("type[]");
for (String type : types) {
constraints.add(constraint("website:type").equalTo(type));
}
}
break;
case "Cyber Alerts":
feedClass = CyberAlert.class;
dateField = "publicationsystem:NominalDate";
if (filterValues.get("year").length > 0) {
Calendar cyberAlertsDateFilter = Calendar.getInstance();
cyberAlertsDateFilter.set(Calendar.YEAR, Integer.parseInt(filterValues.get("year")[0]));
cyberAlertsDateFilter.set(Calendar.DAY_OF_YEAR, 1);
constraints.add(constraint(dateField).equalTo(cyberAlertsDateFilter, DateTools.Resolution.YEAR));
}
if (filterValues.get("type[]").length > 0) {
String[] types = filterValues.get("type[]");
for (String type : types) {
constraints.add(constraint("website:threattype").equalTo(type));
}
}
if (filterValues.get("severity").length > 0) {
constraints.add(constraint("website:severity").equalTo(filterValues.get("severity")[0]));
}
break;
case "Supplementary information":
feedClass = SupplementaryInformation.class;
dateField = "publicationsystem:NominalDate";
if (filterValues.get("year").length > 0) {
Calendar supplimentaryInfoDateFilter = Calendar.getInstance();
String year = filterValues.get("year")[0];
if (year.equals("Unknown")) {
constraints.add(constraint(dateField).notExists());
} else {
supplimentaryInfoDateFilter.set(Calendar.YEAR, Integer.parseInt(year));
supplimentaryInfoDateFilter.set(Calendar.DAY_OF_YEAR, 1);
DateTools.Resolution dateResolution = DateTools.Resolution.YEAR;
if (filterValues.get("month").length > 0) {
Integer month = getMonth(filterValues.get("month")[0]);
if (month != null) {
supplimentaryInfoDateFilter.set(Calendar.MONTH, month);
supplimentaryInfoDateFilter.set(Calendar.DAY_OF_MONTH, 1);
dateResolution = DateTools.Resolution.MONTH;
}
}
constraints.add(constraint(dateField).equalTo(supplimentaryInfoDateFilter, dateResolution));
}
}
break;
default:
}
if (queryText != null && !queryText.isEmpty()) {
constraints.add(or(constraint("website:title").contains(queryText), constraint("website:shortsummary").contains(queryText)));
}
HstQueryBuilder query = HstQueryBuilder.create(folder);
query.where(and(constraints.toArray(new Constraint[0]))).ofTypes(feedClass);
if (sort.equals("date-asc")) {
query.orderByAscending(dateField);
} else {
query.orderByDescending(dateField);
}
HippoBeanIterator beanIterator = query.build().execute().getHippoBeans();
return toList(beanIterator);
}
use of org.hippoecm.hst.content.beans.standard.HippoBeanIterator in project hippo by NHS-digital-website.
the class Series method getLatestPublication.
public Publication getLatestPublication() throws HstComponentException, QueryException {
if (!getShowLatest()) {
return null;
}
HippoBean folder = getCanonicalBean().getParentBean();
HippoBeanIterator hippoBeans = HstQueryBuilder.create(folder).ofTypes(Publication.class).orderByDescending("publicationsystem:NominalDate").build().execute().getHippoBeans();
boolean found = false;
Publication publication = null;
while (!found && hippoBeans.hasNext()) {
HippoBean hippoBean = hippoBeans.nextHippoBean();
publication = (Publication) hippoBean;
found = publication.isPubliclyAccessible();
}
if (found) {
return publication;
}
return null;
}
Aggregations