Search in sources :

Example 1 with DefaultRestContext

use of org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext in project hippo by NHS-digital-website.

the class OrganisationDataResource method getOrg.

@GET
@Path("/")
public List<OdsOrganisation> getOrg(@Context HttpServletRequest request, @Context HttpServletResponse servletResponse, @QueryParam("orgName") String orgName) {
    if (odsResults == null) {
        try {
            log.debug("Loading ODS Data");
            HstRequestContext context = new DefaultRestContext(this, request).getRequestContext();
            QueryManager manager = context.getSession().getWorkspace().getQueryManager();
            Query jcrQuery = manager.createQuery("/jcr:root/content/assets//*[@jcr:primaryType='externalstorage:resource']", "xpath");
            QueryResult execute = jcrQuery.execute();
            NodeIterator iterator = execute.getNodes();
            while (iterator.hasNext()) {
                Node node = iterator.nextNode();
                if (node.getPath().contains("/content/assets/ODS_Data")) {
                    Value val = node.getProperty("jcr:data").getValue();
                    // convert JSON array to Java List
                    odsResults = new ObjectMapper().readValue(val.getString().replace("\n", ""), new TypeReference<List<OdsOrganisation>>() {
                    });
                    break;
                }
            }
        } catch (RepositoryException | JsonProcessingException e) {
            log.debug("Failed to load ODS Data ", e);
        }
    }
    List<OdsOrganisation> filterOrg = odsResults.stream().filter(b -> (b.getOrgName() + " " + b.getCode()).toUpperCase().contains(orgName.toUpperCase())).collect(Collectors.toList());
    return filterOrg;
}
Also used : DefaultRestContext(org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext) NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) Logger(org.slf4j.Logger) OdsOrganisation(uk.nhs.digital.model.OdsOrganisation) Context(javax.ws.rs.core.Context) QueryManager(javax.jcr.query.QueryManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) QueryResult(javax.jcr.query.QueryResult) Collectors(java.util.stream.Collectors) BaseRestResource(org.onehippo.cms7.essentials.components.rest.BaseRestResource) DefaultRestContext(org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Value(javax.jcr.Value) RepositoryException(javax.jcr.RepositoryException) javax.ws.rs(javax.ws.rs) Node(javax.jcr.Node) TypeReference(com.fasterxml.jackson.core.type.TypeReference) NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) OdsOrganisation(uk.nhs.digital.model.OdsOrganisation) RepositoryException(javax.jcr.RepositoryException) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager) Value(javax.jcr.Value) TypeReference(com.fasterxml.jackson.core.type.TypeReference) HstRequestContext(org.hippoecm.hst.core.request.HstRequestContext) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with DefaultRestContext

use of org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext 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

DefaultRestContext (org.onehippo.cms7.essentials.components.rest.ctx.DefaultRestContext)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Node (javax.jcr.Node)1 NodeIterator (javax.jcr.NodeIterator)1 RepositoryException (javax.jcr.RepositoryException)1 Value (javax.jcr.Value)1 Query (javax.jcr.query.Query)1 QueryManager (javax.jcr.query.QueryManager)1 QueryResult (javax.jcr.query.QueryResult)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 javax.ws.rs (javax.ws.rs)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Context (javax.ws.rs.core.Context)1 MediaType (javax.ws.rs.core.MediaType)1