Search in sources :

Example 1 with CyberAlert

use of uk.nhs.digital.website.beans.CyberAlert in project hippo by NHS-digital-website.

the class CyberAlertComponent method createCyberAlertsList.

private List<CyberAlert> createCyberAlertsList(final int configuredAlertSize, final HstQueryResult alertsQueryResult) {
    Calendar twoWeekAgo = Calendar.getInstance();
    twoWeekAgo.add(Calendar.WEEK_OF_YEAR, -2);
    HippoBeanIterator allAlerts = alertsQueryResult.getHippoBeans();
    final List<CyberAlert> severeWithinTwoWeeks = new ArrayList<>();
    final List<CyberAlert> allOtherAlerts = new ArrayList<>();
    while (allAlerts.hasNext()) {
        CyberAlert cyberAlert = (CyberAlert) allAlerts.next();
        if (cyberAlert.getSeverity() != null && cyberAlert.getPublishedDate() != null) {
            if (cyberAlert.getSeverity().equals("High") && cyberAlert.getPublishedDate().getTimeInMillis() > twoWeekAgo.getTimeInMillis()) {
                severeWithinTwoWeeks.add(cyberAlert);
            } else {
                allOtherAlerts.add(cyberAlert);
            }
        }
    }
    List<CyberAlert> cyberAlertsList;
    if (severeWithinTwoWeeks.size() > configuredAlertSize) {
        cyberAlertsList = severeWithinTwoWeeks.subList(0, configuredAlertSize);
    } else {
        cyberAlertsList = severeWithinTwoWeeks;
    }
    if (cyberAlertsList.size() < configuredAlertSize) {
        int numberOfAlertsToAdd = configuredAlertSize - cyberAlertsList.size();
        for (int i = 0; i < numberOfAlertsToAdd && i < allOtherAlerts.size(); ) {
            cyberAlertsList.add(allOtherAlerts.get(i++));
        }
    }
    return cyberAlertsList;
}
Also used : HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) CyberAlert(uk.nhs.digital.website.beans.CyberAlert)

Example 2 with CyberAlert

use of uk.nhs.digital.website.beans.CyberAlert in project hippo by NHS-digital-website.

the class CyberAlertComponent method doBeforeRender.

@Override
public void doBeforeRender(HstRequest request, HstResponse response) {
    super.doBeforeRender(request, response);
    final CyberAlertComponentInfo componentParametersInfo = getComponentParametersInfo(request);
    final int configuredAlertSize = componentParametersInfo.getNumberOfAlertsToDisplay();
    request.setAttribute("title", componentParametersInfo.getTitle());
    try {
        final HippoBean baseContentBean = request.getRequestContext().getSiteContentBaseBean();
        final HippoBean cyberAlertScope = (HippoBean) request.getRequestContext().getObjectBeanManager().getObject(baseContentBean.getPath() + "/cyber-alerts");
        HstQueryBuilder builder = HstQueryBuilder.create(cyberAlertScope);
        HstQueryResult alertsQueryResult = builder.ofTypes(CyberAlert.class).orderByDescending("publicationsystem:NominalDate").build().execute();
        List<CyberAlert> alertsListToDisplay;
        if (alertsQueryResult != null && configuredAlertSize > 0) {
            alertsListToDisplay = createCyberAlertsList(configuredAlertSize, alertsQueryResult);
            request.setAttribute("cyberAlertList", alertsListToDisplay);
        }
    } catch (QueryException | ObjectBeanManagerException e) {
        LOGGER.error("Failed to execute Cyber Alerts Query ", e);
    }
}
Also used : HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) ObjectBeanManagerException(org.hippoecm.hst.content.beans.ObjectBeanManagerException) QueryException(org.hippoecm.hst.content.beans.query.exceptions.QueryException) HstQueryBuilder(org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder) CyberAlertComponentInfo(uk.nhs.digital.common.components.info.CyberAlertComponentInfo) CyberAlert(uk.nhs.digital.website.beans.CyberAlert) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult)

Example 3 with CyberAlert

use of uk.nhs.digital.website.beans.CyberAlert 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)

Example 4 with CyberAlert

use of uk.nhs.digital.website.beans.CyberAlert 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;
}
Also used : HstQuery(org.hippoecm.hst.content.beans.query.HstQuery) DefaultRestContext(org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext) CyberAcknowledgement(uk.nhs.digital.website.beans.CyberAcknowledgement) ThreatIds(uk.nhs.digital.website.beans.ThreatIds) HippoBeanIterator(org.hippoecm.hst.content.beans.standard.HippoBeanIterator) CyberAlert(uk.nhs.digital.website.beans.CyberAlert) HstQueryResult(org.hippoecm.hst.content.beans.query.HstQueryResult) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) ThreatIdDate(uk.nhs.digital.website.beans.ThreatIdDate) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

CyberAlert (uk.nhs.digital.website.beans.CyberAlert)4 HstQueryResult (org.hippoecm.hst.content.beans.query.HstQueryResult)3 HippoBeanIterator (org.hippoecm.hst.content.beans.standard.HippoBeanIterator)3 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 HstQuery (org.hippoecm.hst.content.beans.query.HstQuery)2 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)2 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Node (javax.jcr.Node)1 ObjectBeanManagerException (org.hippoecm.hst.content.beans.ObjectBeanManagerException)1 HstQueryManager (org.hippoecm.hst.content.beans.query.HstQueryManager)1 HstQueryBuilder (org.hippoecm.hst.content.beans.query.builder.HstQueryBuilder)1 QueryException (org.hippoecm.hst.content.beans.query.exceptions.QueryException)1 Filter (org.hippoecm.hst.content.beans.query.filter.Filter)1 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)1 JSONObject (org.json.simple.JSONObject)1 DefaultRestContext (org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext)1 CyberAlertComponentInfo (uk.nhs.digital.common.components.info.CyberAlertComponentInfo)1 CyberAcknowledgement (uk.nhs.digital.website.beans.CyberAcknowledgement)1