<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gov.hhs.fha.nhinc.auditquery;

import com.services.nhinc.schema.auditmessage.FindAuditEventsResponseType;
import com.services.nhinc.schema.auditmessage.FindAuditEventsType;
import gov.hhs.fha.nhinc.auditqueryadapter.proxy.AuditQueryAdapterProxy;
import gov.hhs.fha.nhinc.auditqueryadapter.proxy.AuditQueryAdapterProxyObjectFactory;
import gov.hhs.fha.nhinc.auditrepository.AuditRepositoryLogger;
import gov.hhs.fha.nhinc.auditrepository.proxy.AuditRepositoryProxy;
import gov.hhs.fha.nhinc.auditrepository.proxy.AuditRepositoryProxyObjectFactory;
import gov.hhs.fha.nhinc.common.auditlog.LogEventRequestType;
import gov.hhs.fha.nhinc.common.eventcommon.FindAuditEventsEventType;
import gov.hhs.fha.nhinc.common.nhinccommon.AcknowledgementType;
import gov.hhs.fha.nhinc.common.nhinccommonadapter.CheckPolicyRequestType;
import gov.hhs.fha.nhinc.common.nhinccommonadapter.FindCommunitiesAndAuditEventsRequestType;
import gov.hhs.fha.nhinc.common.nhinccommonadapter.FindCommunitiesAndAuditEventsResponseType;
import gov.hhs.fha.nhinc.common.nhinccommoninternalorch.FindAuditEventsRequestType;
import gov.hhs.fha.nhinc.common.nhinccommonadapter.CheckPolicyResponseType;
import gov.hhs.fha.nhinc.nhinclib.NhincConstants;
import gov.hhs.fha.nhinc.nhinclib.NullChecker;
import gov.hhs.fha.nhinc.policyengine.proxy.PolicyEngineProxy;
import gov.hhs.fha.nhinc.policyengine.proxy.PolicyEngineProxyObjectFactory;
import gov.hhs.fha.nhinc.policyengine.PolicyEngineChecker;
import gov.hhs.fha.nhinc.properties.PropertyAccessException;
import gov.hhs.fha.nhinc.properties.PropertyAccessor;
import gov.hhs.fha.nhinc.saml.extraction.SamlTokenExtractor;
import javax.xml.ws.WebServiceContext;
import oasis.names.tc.xacml._2_0.context.schema.os.DecisionType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author <NAME>
 */
public class AuditQueryImpl {

    private static Log log = LogFactory.getLog(AuditQueryImpl.class);

    /**
     * This method will perform an audit log query to the local audit repository.  A list of audit
     * log records will be returned to the calling community that match the search criteria.
     *
     * @param query The audit log query search criteria
     * @return A list of Audit Log records that match the specified criteria
     */
    public FindAuditEventsResponseType query(FindAuditEventsType query, WebServiceContext context) {
        log.debug("Entering AuditQueryImpl.query...");

        FindAuditEventsResponseType resp = new FindAuditEventsResponseType();

        // Set up the audit query request message
        FindAuditEventsRequestType request = new FindAuditEventsRequestType();
        request.setFindAuditEvents(query);
        request.setAssertion(SamlTokenExtractor.GetAssertion(context));

        // Audit the Audit Log Query Request Message received on the Nhin Interface
        AcknowledgementType ack = audit(request, NhincConstants.AUDIT_LOG_INBOUND_DIRECTION, NhincConstants.AUDIT_LOG_NHIN_INTERFACE);

        // Check the policy engine to make sure processing can proceed
        if (checkPolicy(request)) {
            // Check if the Audit Log Query Service is enabled
            if (isServiceEnabled()) {
                // Check to see if in adapter pass through mode for this service
                if (isInPassThroughMode()) {
                    resp = forwardToAgency(request);
                } else {
                    resp = queryAuditRepo(request);
                }
            }
        }

        log.debug("Exiting AuditQueryImpl.query...");
        return resp;
    }

    private FindAuditEventsResponseType forwardToAgency(FindAuditEventsRequestType auditMsg) {
        FindAuditEventsResponseType resp = new FindAuditEventsResponseType();

        // Audit the Audit Log Query Request Message received on the Nhin Interface
        AcknowledgementType ack = audit(auditMsg, NhincConstants.AUDIT_LOG_OUTBOUND_DIRECTION, NhincConstants.AUDIT_LOG_ADAPTER_INTERFACE);

        gov.hhs.fha.nhinc.common.nhinccommonadapter.FindAuditEventsRequestType findAuditEventsRequest = new gov.hhs.fha.nhinc.common.nhinccommonadapter.FindAuditEventsRequestType();
        findAuditEventsRequest.setAssertion(auditMsg.getAssertion());
        findAuditEventsRequest.setFindAuditEvents(auditMsg.getFindAuditEvents());

        AuditQueryAdapterProxyObjectFactory adapterFactory = new AuditQueryAdapterProxyObjectFactory();
        AuditQueryAdapterProxy adapterProxy = adapterFactory.getAdapterAuditQueryProxy();
        resp = adapterProxy.auditQuery(findAuditEventsRequest);

        return resp;
    }

    private FindAuditEventsResponseType queryAuditRepo(FindAuditEventsRequestType auditMsg) {
        FindAuditEventsResponseType resp = new FindAuditEventsResponseType();

        FindCommunitiesAndAuditEventsResponseType result = new FindCommunitiesAndAuditEventsResponseType();
        FindCommunitiesAndAuditEventsRequestType request = new FindCommunitiesAndAuditEventsRequestType();
        request.setAssertion(auditMsg.getAssertion());
        request.setFindAuditEvents(auditMsg.getFindAuditEvents());

        AuditRepositoryProxyObjectFactory auditRepoFactory = new AuditRepositoryProxyObjectFactory();
        AuditRepositoryProxy auditProxy = auditRepoFactory.getAuditRepositoryProxy();
        result = auditProxy.auditQuery(request);
        resp = result.getFindAuditEventResponse();

        return resp;
    }

    private AcknowledgementType audit(FindAuditEventsRequestType auditMsg, String direction, String _interface) {
        // Set up the audit logging request message
        AuditRepositoryLogger auditLogger = new AuditRepositoryLogger();

        // Need to resolve the namespace issues here because this type is defined in multiple schemas
        gov.hhs.fha.nhinc.common.auditlog.FindAuditEventsMessageType message = new gov.hhs.fha.nhinc.common.auditlog.FindAuditEventsMessageType();
        message.setAssertion(auditMsg.getAssertion());
        message.setFindAuditEvents(auditMsg.getFindAuditEvents());

        LogEventRequestType auditLogMsg = auditLogger.logFindAuditEvents(message, direction, _interface);

        AuditRepositoryProxyObjectFactory auditRepoFactory = new AuditRepositoryProxyObjectFactory();
        AuditRepositoryProxy proxy = auditRepoFactory.getAuditRepositoryProxy();

        return proxy.auditLog(auditLogMsg);
    }

    private boolean checkPolicy(FindAuditEventsRequestType message) {
        boolean policyIsValid = false;

        FindAuditEventsEventType policyCheckReq = new FindAuditEventsEventType();
        policyCheckReq.setDirection(NhincConstants.POLICYENGINE_INBOUND_DIRECTION);
        gov.hhs.fha.nhinc.common.eventcommon.FindAuditEventsMessageType request = new gov.hhs.fha.nhinc.common.eventcommon.FindAuditEventsMessageType();
        request.setAssertion(message.getAssertion());<commit_msg>#18442 Fixed case in Audit Query and Doc Query services have audit disabled<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gov.hhs.fha.nhinc.auditquery;

import com.services.nhinc.schema.auditmessage.FindAuditEventsResponseType;
import com.services.nhinc.schema.auditmessage.FindAuditEventsType;
import gov.hhs.fha.nhinc.auditqueryadapter.proxy.AuditQueryAdapterProxy;
import gov.hhs.fha.nhinc.auditqueryadapter.proxy.AuditQueryAdapterProxyObjectFactory;
import gov.hhs.fha.nhinc.auditrepository.AuditRepositoryLogger;
import gov.hhs.fha.nhinc.auditrepository.proxy.AuditRepositoryProxy;
import gov.hhs.fha.nhinc.auditrepository.proxy.AuditRepositoryProxyObjectFactory;
import gov.hhs.fha.nhinc.common.auditlog.LogEventRequestType;
import gov.hhs.fha.nhinc.common.eventcommon.FindAuditEventsEventType;
import gov.hhs.fha.nhinc.common.nhinccommon.AcknowledgementType;
import gov.hhs.fha.nhinc.common.nhinccommonadapter.CheckPolicyRequestType;
import gov.hhs.fha.nhinc.common.nhinccommonadapter.FindCommunitiesAndAuditEventsRequestType;
import gov.hhs.fha.nhinc.common.nhinccommonadapter.FindCommunitiesAndAuditEventsResponseType;
import gov.hhs.fha.nhinc.common.nhinccommoninternalorch.FindAuditEventsRequestType;
import gov.hhs.fha.nhinc.common.nhinccommonadapter.CheckPolicyResponseType;
import gov.hhs.fha.nhinc.nhinclib.NhincConstants;
import gov.hhs.fha.nhinc.nhinclib.NullChecker;
import gov.hhs.fha.nhinc.policyengine.proxy.PolicyEngineProxy;
import gov.hhs.fha.nhinc.policyengine.proxy.PolicyEngineProxyObjectFactory;
import gov.hhs.fha.nhinc.policyengine.PolicyEngineChecker;
import gov.hhs.fha.nhinc.properties.PropertyAccessException;
import gov.hhs.fha.nhinc.properties.PropertyAccessor;
import gov.hhs.fha.nhinc.saml.extraction.SamlTokenExtractor;
import javax.xml.ws.WebServiceContext;
import oasis.names.tc.xacml._2_0.context.schema.os.DecisionType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author <NAME>
 */
public class AuditQueryImpl {

    private static Log log = LogFactory.getLog(AuditQueryImpl.class);

    /**
     * This method will perform an audit log query to the local audit repository.  A list of audit
     * log records will be returned to the calling community that match the search criteria.
     *
     * @param query The audit log query search criteria
     * @return A list of Audit Log records that match the specified criteria
     */
    public FindAuditEventsResponseType query(FindAuditEventsType query, WebServiceContext context) {
        log.debug("Entering AuditQueryImpl.query...");

        FindAuditEventsResponseType resp = new FindAuditEventsResponseType();

        // Set up the audit query request message
        FindAuditEventsRequestType request = new FindAuditEventsRequestType();
        request.setFindAuditEvents(query);
        request.setAssertion(SamlTokenExtractor.GetAssertion(context));

        // Audit the Audit Log Query Request Message received on the Nhin Interface
        AcknowledgementType ack = audit(request, NhincConstants.AUDIT_LOG_INBOUND_DIRECTION, NhincConstants.AUDIT_LOG_NHIN_INTERFACE);

        // Check the policy engine to make sure processing can proceed
        if (checkPolicy(request)) {
            // Check if the Audit Log Query Service is enabled
            if (isServiceEnabled()) {
                // Check to see if in adapter pass through mode for this service
                if (isInPassThroughMode()) {
                    resp = forwardToAgency(request);
                } else {
                    resp = queryAuditRepo(request);
                }
            }
        }

        log.debug("Exiting AuditQueryImpl.query...");
        return resp;
    }

    private FindAuditEventsResponseType forwardToAgency(FindAuditEventsRequestType auditMsg) {
        FindAuditEventsResponseType resp = new FindAuditEventsResponseType();

        // Audit the Audit Log Query Request Message received on the Nhin Interface
        AcknowledgementType ack = audit(auditMsg, NhincConstants.AUDIT_LOG_OUTBOUND_DIRECTION, NhincConstants.AUDIT_LOG_ADAPTER_INTERFACE);

        gov.hhs.fha.nhinc.common.nhinccommonadapter.FindAuditEventsRequestType findAuditEventsRequest = new gov.hhs.fha.nhinc.common.nhinccommonadapter.FindAuditEventsRequestType();
        findAuditEventsRequest.setAssertion(auditMsg.getAssertion());
        findAuditEventsRequest.setFindAuditEvents(auditMsg.getFindAuditEvents());

        AuditQueryAdapterProxyObjectFactory adapterFactory = new AuditQueryAdapterProxyObjectFactory();
        AuditQueryAdapterProxy adapterProxy = adapterFactory.getAdapterAuditQueryProxy();
        resp = adapterProxy.auditQuery(findAuditEventsRequest);

        return resp;
    }

    private FindAuditEventsResponseType queryAuditRepo(FindAuditEventsRequestType auditMsg) {
        FindAuditEventsResponseType resp = new FindAuditEventsResponseType();

        FindCommunitiesAndAuditEventsResponseType result = new FindCommunitiesAndAuditEventsResponseType();
        FindCommunitiesAndAuditEventsRequestType request = new FindCommunitiesAndAuditEventsRequestType();
        request.setAssertion(auditMsg.getAssertion());
        request.setFindAuditEvents(auditMsg.getFindAuditEvents());

        AuditRepositoryProxyObjectFactory auditRepoFactory = new AuditRepositoryProxyObjectFactory();
        AuditRepositoryProxy auditProxy = auditRepoFactory.getAuditRepositoryProxy();
        result = auditProxy.auditQuery(request);
        resp = result.getFindAuditEventResponse();

        return resp;
    }

    private AcknowledgementType audit(FindAuditEventsRequestType auditMsg, String direction, String _interface) {
        AcknowledgementType ack = new AcknowledgementType();

        // Set up the audit logging request message
        AuditRepositoryLogger auditLogger = new AuditRepositoryLogger();

        // Need to resolve the namespace issues here because this type is defined in multiple schemas
        gov.hhs.fha.nhinc.common.auditlog.FindAuditEventsMessageType message = new gov.hhs.fha.nhinc.common.auditlog.FindAuditEventsMessageType();
        message.setAssertion(auditMsg.getAssertion());
        message.setFindAuditEvents(auditMsg.getFindAuditEvents());

        LogEventRequestType auditLogMsg = auditLogger.logFindAuditEvents(message, direction, _interface);

        if (auditLogMsg != null) {
            AuditRepositoryProxyObjectFactory auditRepoFactory = new AuditRepositoryProxyObjectFactory();
            AuditRepositoryProxy proxy = auditRepoFactory.getAuditRepositoryProxy();
            ack = proxy.auditLog(auditLogMsg);
        }

        return ack;
    }

    private boolean checkPolicy(FindAuditEventsRequestType message) {
        boolean policyIsValid = false;

        FindAuditEventsEventType policyCheckReq = new FindAuditEventsEventType();
        policyCheckReq.setDirection(NhincConstants.POLICYENGINE_INBOUND_DIRECTION);
        gov.hhs.fha.nhinc.common.eventcommon.FindAuditEventsMessageType request = new gov.hhs.fha.nhinc.common.eventcommon.FindAuditEventsMessageType();
        request.setAssertion(message.getAssertion());
{'fraction_non_alphanumeric': 0.07525295109612141, 'fraction_numerical': 0.0006323777403035413, 'mean_word_length': 4.255908419497785, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '836273', 'n_tokens_mistral': 4159, 'n_tokens_neox': 4051, 'n_words': 748}