All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hcxprotocol.helper.ValidateHelper Maven / Gradle / Ivy

Go to download

The SDK for HCX Participant System to help in integrating with HCX Gateway easily.

There is a newer version: 1.0.8
Show newest version
package io.hcxprotocol.helper;

import io.hcxprotocol.exception.ErrorCodes;
import io.hcxprotocol.dto.JSONRequest;
import io.hcxprotocol.dto.JWERequest;
import io.hcxprotocol.impl.HCXIncomingRequest;
import io.hcxprotocol.utils.JSONUtils;
import io.hcxprotocol.utils.Operations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.hcxprotocol.utils.Constants.*;
import static io.hcxprotocol.utils.ResponseMessage.INVALID_JSON_REQUEST_BODY_ERR_MSG;

/**
 * This is to validate the incoming request protocol headers.
 */
public class ValidateHelper {

    private static final Logger logger = LoggerFactory.getLogger(ValidateHelper.class);
    private static ValidateHelper validateHelper = null;

    private ValidateHelper() {
    }

    public static ValidateHelper getInstance() {
        if (validateHelper == null)
            validateHelper = new ValidateHelper();
        return validateHelper;
    }

    /**
     * Validates the incoming payload by verifying the structure and contents of the headers inside the payload
     * 

* * ERR_INVALID_PAYLOAD: * * 1. Request body is not a valid JWE token (as defined in RFC 7516) * * 2. Any mandatory elements of JWE token are missing * * 3. Any elements of the JWE token are in invalid format * * * HCX Protocol errors: * * ERR_MANDATORY_HEADER_MISSING * * ERR_INVALID_API_CALL_ID * * ERR_INVALID_CORRELATION_ID: check only for the format correctness * * ERR_INVALID_TIMESTAMP * * ERR_INVALID_REDIRECT_TO * * ERR_INVALID_STATUS * * ERR_INVALID_DEBUG_FLAG * * ERR_INVALID_ERROR_DETAILS * * ERR_INVALID_DEBUG_DETAILS * * ERR_INVALID_WORKFLOW_ID: check only for the format correctness * * * * @param payload json string with payload * @param operation which operation is being processed * @param error holds any validation errors * @return true if it is valid request otherwise returns false along with proper error message in the error map */ public boolean validateRequest(String payload, Operations operation, Map error) { try { // Convert the input string into a Map Map requestBody = JSONUtils.deserialize(payload, HashMap.class); if (requestBody.containsKey(PAYLOAD)) { if (validateJWERequest(operation, error, requestBody)) return false; } else { if (!operation.toString().contains("ON_")) { error.put(ErrorCodes.ERR_INVALID_PAYLOAD.toString(), INVALID_JSON_REQUEST_BODY_ERR_MSG); return false; } if (!validateJsonRequest(operation, error, requestBody)) return false; } } catch (Exception e) { e.printStackTrace(); error.put(ErrorCodes.ERR_INVALID_PAYLOAD.toString(), e.getMessage()); return false; } return true; } private boolean validateJWERequest(Operations operation, Map error, Map requestBody) throws Exception { // Fetch the value of the only key(payload) from the map JWERequest jweRequest = new JWERequest(requestBody); // Split the extracted above value into an array using . as delimiter String[] payloadArr = jweRequest.getPayloadValues(); if (jweRequest.validateJwePayload(error, payloadArr)) return true; // Validate the headers and if there are any failures add the corresponding error message to the error Map // protocol_mandatory_headers:x-hcx-sender_code, x-hcx-recipient_code, x-hcx-api_call_id, x-hcx-timestamp, x-hcx-correlation_id boolean result = jweRequest.validateHeadersData(List.of(ALG,ENC,HCX_SENDER_CODE,HCX_RECIPIENT_CODE,HCX_API_CALL_ID,HCX_TIMESTAMP,HCX_CORRELATION_ID), operation, error); logger.info("Request is validated successfully :: api call id: {}", jweRequest.getApiCallId()); return result; } boolean validateJsonRequest(Operations operation, Map error, Map requestBody) throws Exception { JSONRequest jsonRequest = new JSONRequest(requestBody); boolean result; if (ERROR_RESPONSE.equalsIgnoreCase(jsonRequest.getStatus())) { //error_mandatory_headers:x-hcx-status, x-hcx-sender_code, x-hcx-recipient_code, x-hcx-error_details, x-hcx-correlation_id, x-hcx-api_call_id, x-hcx-timestamp result = jsonRequest.validateHeadersData(List.of(STATUS,HCX_SENDER_CODE,HCX_RECIPIENT_CODE,ERROR_DETAILS,HCX_CORRELATION_ID,HCX_API_CALL_ID,HCX_TIMESTAMP), operation, error); } else { //redirect_mandatory_headers:x-hcx-sender_code, x-hcx-recipient_code, x-hcx-api_call_id, x-hcx-timestamp, x-hcx-correlation_id, x-hcx-status, x-hcx-redirect_to result = jsonRequest.validateHeadersData(List.of(HCX_SENDER_CODE,HCX_RECIPIENT_CODE,HCX_API_CALL_ID,HCX_TIMESTAMP,HCX_CORRELATION_ID,STATUS,REDIRECT_TO), operation, error); if (result) return true; result = jsonRequest.validateRedirect(error); } if (result) logger.info("Request is validated successfully :: api call id: {}", jsonRequest.getApiCallId()); return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy