From 43090a23bd975722f968b75d3a50e9f44ebd612e Mon Sep 17 00:00:00 2001 From: Ashirwada Date: Mon, 30 Sep 2024 12:06:25 +0530 Subject: [PATCH] Adding gateway implementation --- .../accelerators/pom.xml | 4 +- .../pom.xml | 3 +- .../gateway/cache/GatewayCache.java | 2 +- .../gateway/cache/GatewayCacheKey.java | 2 +- .../executor/core/DefaultRequestRouter.java | 16 +- .../core/FSExtensionListenerImpl.java | 125 +++++++--------- .../executor/model/FSAPIRequestContext.java | 137 +++++++++--------- .../executor/model/FSAPIResponseContext.java | 44 +++--- .../gateway/internal/GatewayDataHolder.java | 3 +- .../gateway/util/GatewayConstants.java | 10 ++ .../gateway/util/GatewayUtils.java | 18 ++- pom.xml | 8 +- 12 files changed, 172 insertions(+), 200 deletions(-) diff --git a/financial-services-accelerator/accelerators/pom.xml b/financial-services-accelerator/accelerators/pom.xml index 0d0ea9f9..53ffdeab 100644 --- a/financial-services-accelerator/accelerators/pom.xml +++ b/financial-services-accelerator/accelerators/pom.xml @@ -33,7 +33,7 @@ http://wso2.org - ob-apim - ob-is + fs-apim + fs-is diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/pom.xml b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/pom.xml index b6545669..dc5c18f5 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/pom.xml +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/pom.xml @@ -195,8 +195,7 @@ org.json;version="${org.json.version.range}", org.wso2.carbon.apimgt.common.gateway.*;version="${org.wso2.carbon.apimgt.version.range}", org.wso2.carbon.apimgt.impl;version="${org.wso2.carbon.apimgt.version.range}", - org.wso2 .financial.services.accelerator.common.*;version="${project.version}", - * + org.wso2.financial.services.accelerator.common.*;version="${project.version}" !org.wso2.financial.services.accelerator.gateway.internal, diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/cache/GatewayCache.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/cache/GatewayCache.java index 534fdc3f..7bffb01f 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/cache/GatewayCache.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/cache/GatewayCache.java @@ -26,7 +26,7 @@ */ public class GatewayCache extends FinancialServicesBaseCache { - private static final String cacheName = "OPEN_BANKING_GATEWAY_CACHE"; + private static final String cacheName = "FINANCIAL_SERVICES_GATEWAY_CACHE"; private final Integer accessExpiryMinutes; private final Integer modifiedExpiryMinutes; diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/cache/GatewayCacheKey.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/cache/GatewayCacheKey.java index 5b92d358..e8df4485 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/cache/GatewayCacheKey.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/cache/GatewayCacheKey.java @@ -25,7 +25,7 @@ import java.util.Objects; /** - * Cache Key for Open Banking Gateway cache. + * Cache Key for Financial Services Gateway cache. */ public class GatewayCacheKey extends FinancialServicesBaseCacheKey implements Serializable { diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/DefaultRequestRouter.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/DefaultRequestRouter.java index 624e9443..cc3fcf9d 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/DefaultRequestRouter.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/DefaultRequestRouter.java @@ -43,11 +43,11 @@ public List getExecutorsForRequest(FSAPIReques //add support for consent management portal APIs requestContext.addContextProperty(GatewayConstants.API_TYPE_CUSTOM_PROP, GatewayConstants.API_TYPE_CONSENT); - return this.getExecutorMap().get("Consent"); - } else if (requestContext.getMsgInfo().getResource().contains("/register")) { - return this.getExecutorMap().get("DCR"); + return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_CONSENT); + } else if (requestContext.getMsgInfo().getResource().contains(GatewayConstants.DCR_PATH)) { + return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_DCR); } else { - return this.getExecutorMap().get("Default"); + return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_DEFAULT); } } @@ -59,14 +59,14 @@ public List getExecutorsForResponse(FSAPIRespo return EMPTY_LIST; } else if (GatewayConstants.API_TYPE_CONSENT .equals(responseContext.getContextProps().get(GatewayConstants.API_TYPE_CUSTOM_PROP))) { - return this.getExecutorMap().get("Consent"); + return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_CONSENT); } } - if (responseContext.getMsgInfo().getResource().contains("/register")) { - return this.getExecutorMap().get("DCR"); + if (responseContext.getMsgInfo().getResource().contains(GatewayConstants.DCR_PATH)) { + return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_DCR); } else { - return this.getExecutorMap().get("Default"); + return this.getExecutorMap().get(GatewayConstants.EXECUTOR_TYPE_DEFAULT); } } } diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionListenerImpl.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionListenerImpl.java index e79a8a7d..95e9622b 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionListenerImpl.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionListenerImpl.java @@ -37,10 +37,9 @@ import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -import java.util.TreeMap; /** - * Open Banking implementation for Extension listener. + * Financial Services implementation for Extension listener. */ public class FSExtensionListenerImpl implements ExtensionListener { @@ -50,21 +49,17 @@ public class FSExtensionListenerImpl implements ExtensionListener { @Generated(message = "Ignoring since the method has covered in other tests") public ExtensionResponseDTO preProcessRequest(RequestContextDTO requestContextDTO) { - FSAPIRequestContext fsapiRequestContext = new FSAPIRequestContext(requestContextDTO, new HashMap<>(), - new HashMap<>()); + FSAPIRequestContext fsapiRequestContext = new FSAPIRequestContext(requestContextDTO, new HashMap<>()); for (FinancialServicesGatewayExecutor gatewayExecutor : GatewayDataHolder.getInstance().getRequestRouter().getExecutorsForRequest(fsapiRequestContext)) { + if (log.isDebugEnabled()) { + log.debug("Executing preProcessRequest for executor: " + gatewayExecutor.getClass().getName()); + } gatewayExecutor.preProcessRequest(fsapiRequestContext); } if (!fsapiRequestContext.isError()) { - setPropertiesToCache(requestContextDTO.getMsgInfo().getMessageId() + - GatewayConstants.CONTEXT_PROP_CACHE_KEY, fsapiRequestContext.getContextProps()); - - setPropertiesToCache(requestContextDTO.getMsgInfo().getMessageId() + - GatewayConstants.ANALYTICS_PROP_CACHE_KEY, fsapiRequestContext.getAnalyticsData()); - } else { - publishAnalyticsData(fsapiRequestContext.getAnalyticsData()); + setPropertiesToCache(requestContextDTO.getMsgInfo().getMessageId(), fsapiRequestContext.getContextProps()); } return getResponseDTOForRequest(fsapiRequestContext); } @@ -73,26 +68,21 @@ public ExtensionResponseDTO preProcessRequest(RequestContextDTO requestContextDT @Generated(message = "Ignoring since the method has covered in other tests") public ExtensionResponseDTO postProcessRequest(RequestContextDTO requestContextDTO) { - Map contextProps = getPropertiesFromCache(requestContextDTO.getMsgInfo().getMessageId() + + Map contextProps = getPropertiesFromCache(requestContextDTO.getMsgInfo().getMessageId() + GatewayConstants.CONTEXT_PROP_CACHE_KEY); - Map analyticsData = getPropertiesFromCache(requestContextDTO.getMsgInfo().getMessageId() + - GatewayConstants.ANALYTICS_PROP_CACHE_KEY); - FSAPIRequestContext fsapiRequestContext = - new FSAPIRequestContext(requestContextDTO, contextProps, analyticsData); + FSAPIRequestContext fsapiRequestContext = new FSAPIRequestContext(requestContextDTO, contextProps); for (FinancialServicesGatewayExecutor gatewayExecutor : GatewayDataHolder.getInstance().getRequestRouter().getExecutorsForRequest(fsapiRequestContext)) { + if (log.isDebugEnabled()) { + log.debug("Executing postProcessRequest for executor: " + gatewayExecutor.getClass().getName()); + } gatewayExecutor.postProcessRequest(fsapiRequestContext); } if (!fsapiRequestContext.isError()) { setPropertiesToCache(requestContextDTO.getMsgInfo().getMessageId() + GatewayConstants.CONTEXT_PROP_CACHE_KEY, fsapiRequestContext.getContextProps()); - - setPropertiesToCache(requestContextDTO.getMsgInfo().getMessageId() + - GatewayConstants.ANALYTICS_PROP_CACHE_KEY, fsapiRequestContext.getAnalyticsData()); - } else { - publishAnalyticsData(fsapiRequestContext.getAnalyticsData()); } return getResponseDTOForRequest(fsapiRequestContext); } @@ -101,25 +91,20 @@ public ExtensionResponseDTO postProcessRequest(RequestContextDTO requestContextD @Generated(message = "Ignoring since the method has covered in other tests") public ExtensionResponseDTO preProcessResponse(ResponseContextDTO responseContextDTO) { - Map contextProps = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + + Map contextProps = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + GatewayConstants.CONTEXT_PROP_CACHE_KEY); - Map analyticsData = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + - GatewayConstants.ANALYTICS_PROP_CACHE_KEY); - FSAPIResponseContext fsapiResponseContext = - new FSAPIResponseContext(responseContextDTO, contextProps, analyticsData); + FSAPIResponseContext fsapiResponseContext = new FSAPIResponseContext(responseContextDTO, contextProps); for (FinancialServicesGatewayExecutor gatewayExecutor : GatewayDataHolder.getInstance().getRequestRouter().getExecutorsForResponse(fsapiResponseContext)) { + if (log.isDebugEnabled()) { + log.debug("Executing preProcessResponse for executor: " + gatewayExecutor.getClass().getName()); + } gatewayExecutor.preProcessResponse(fsapiResponseContext); } if (!fsapiResponseContext.isError()) { setPropertiesToCache(responseContextDTO.getMsgInfo().getMessageId() + GatewayConstants.CONTEXT_PROP_CACHE_KEY, fsapiResponseContext.getContextProps()); - - setPropertiesToCache(responseContextDTO.getMsgInfo().getMessageId() + - GatewayConstants.ANALYTICS_PROP_CACHE_KEY, fsapiResponseContext.getAnalyticsData()); - } else { - publishAnalyticsData(fsapiResponseContext.getAnalyticsData()); } return getResponseDTOForResponse(fsapiResponseContext); } @@ -128,22 +113,19 @@ public ExtensionResponseDTO preProcessResponse(ResponseContextDTO responseContex @Generated(message = "Ignoring since the method has covered in other tests") public ExtensionResponseDTO postProcessResponse(ResponseContextDTO responseContextDTO) { - Map contextProps = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + + Map contextProps = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + GatewayConstants.CONTEXT_PROP_CACHE_KEY); - Map analyticsData = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + - GatewayConstants.ANALYTICS_PROP_CACHE_KEY); - FSAPIResponseContext fsapiResponseContext = - new FSAPIResponseContext(responseContextDTO, contextProps, analyticsData); + FSAPIResponseContext fsapiResponseContext = new FSAPIResponseContext(responseContextDTO, contextProps); for (FinancialServicesGatewayExecutor gatewayExecutor : GatewayDataHolder.getInstance().getRequestRouter().getExecutorsForResponse(fsapiResponseContext)) { + if (log.isDebugEnabled()) { + log.debug("Executing postProcessResponse for executor: " + gatewayExecutor.getClass().getName()); + } gatewayExecutor.postProcessResponse(fsapiResponseContext); } - publishAnalyticsData(fsapiResponseContext.getAnalyticsData()); ExtensionResponseDTO responseDTOForResponse = getResponseDTOForResponse(fsapiResponseContext); removePropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + GatewayConstants.CONTEXT_PROP_CACHE_KEY); - removePropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + - GatewayConstants.ANALYTICS_PROP_CACHE_KEY); return responseDTOForResponse; } @@ -151,20 +133,19 @@ protected ExtensionResponseDTO getResponseDTOForRequest(FSAPIRequestContext fsap ExtensionResponseDTO extensionResponseDTO = new ExtensionResponseDTO(); if (fsapiRequestContext.isError()) { - int statusCode = (!fsapiRequestContext.getContextProps().containsKey(GatewayConstants.ERROR_STATUS_PROP)) ? HttpStatus.SC_INTERNAL_SERVER_ERROR : - Integer.parseInt(fsapiRequestContext.getContextProperty(GatewayConstants.ERROR_STATUS_PROP)); + (int) (fsapiRequestContext.getContextProperty(GatewayConstants.ERROR_STATUS_PROP)); extensionResponseDTO.setStatusCode(statusCode); extensionResponseDTO.setResponseStatus(ExtensionResponseStatus.RETURN_ERROR.toString()); } else if (fsapiRequestContext.getContextProps().containsKey(GatewayConstants.IS_RETURN_RESPONSE) && - Boolean.parseBoolean(fsapiRequestContext.getContextProps().get(GatewayConstants.IS_RETURN_RESPONSE))) { + (boolean) (fsapiRequestContext.getContextProps().get(GatewayConstants.IS_RETURN_RESPONSE))) { Map headers = fsapiRequestContext.getMsgInfo().getHeaders(); headers.put(GatewayConstants.CONTENT_TYPE_TAG, GatewayConstants.JSON_CONTENT_TYPE); fsapiRequestContext.getMsgInfo().setHeaders(headers); extensionResponseDTO.setHeaders(headers); if (fsapiRequestContext.getContextProps().containsKey(GatewayConstants.MODIFIED_STATUS)) { - extensionResponseDTO.setStatusCode(Integer.parseInt(fsapiRequestContext.getContextProps() + extensionResponseDTO.setStatusCode((int) (fsapiRequestContext.getContextProps() .get(GatewayConstants.MODIFIED_STATUS))); } extensionResponseDTO.setResponseStatus(ExtensionResponseStatus.RETURN_ERROR.toString()); @@ -176,15 +157,9 @@ protected ExtensionResponseDTO getResponseDTOForRequest(FSAPIRequestContext fsap if (modifiedPayload != null) { extensionResponseDTO.setPayload(new ByteArrayInputStream(modifiedPayload.getBytes(StandardCharsets.UTF_8))); } - Map addedHeaders = fsapiRequestContext.getAddedHeaders(); - if (addedHeaders.size() != 0) { - TreeMap headers = new TreeMap<>(); - headers.putAll(fsapiRequestContext.getMsgInfo().getHeaders()); - for (Map.Entry headerEntry : addedHeaders.entrySet()) { - headers.put(headerEntry.getKey(), headerEntry.getValue()); - } - extensionResponseDTO.setHeaders(headers); - } + + setHeadersToResponse(extensionResponseDTO, fsapiRequestContext.getAddedHeaders(), + fsapiRequestContext.getMsgInfo().getHeaders()); return extensionResponseDTO; } @@ -194,17 +169,17 @@ protected ExtensionResponseDTO getResponseDTOForResponse(FSAPIResponseContext fs if (fsapiResponseContext.isError()) { int statusCode = (!fsapiResponseContext.getContextProps().containsKey(GatewayConstants.ERROR_STATUS_PROP)) ? HttpStatus.SC_INTERNAL_SERVER_ERROR : - Integer.parseInt(fsapiResponseContext.getContextProperty(GatewayConstants.ERROR_STATUS_PROP)); + (int) (fsapiResponseContext.getContextProperty(GatewayConstants.ERROR_STATUS_PROP)); extensionResponseDTO.setStatusCode(statusCode); extensionResponseDTO.setResponseStatus(ExtensionResponseStatus.RETURN_ERROR.toString()); } else if (fsapiResponseContext.getContextProps().containsKey(GatewayConstants.IS_RETURN_RESPONSE) && - Boolean.parseBoolean(fsapiResponseContext.getContextProps().get(GatewayConstants.IS_RETURN_RESPONSE))) { + (boolean) (fsapiResponseContext.getContextProps().get(GatewayConstants.IS_RETURN_RESPONSE))) { Map headers = fsapiResponseContext.getMsgInfo().getHeaders(); headers.put(GatewayConstants.CONTENT_TYPE_TAG, GatewayConstants.JSON_CONTENT_TYPE); fsapiResponseContext.getMsgInfo().setHeaders(headers); extensionResponseDTO.setHeaders(headers); if (fsapiResponseContext.getContextProps().containsKey(GatewayConstants.MODIFIED_STATUS)) { - extensionResponseDTO.setStatusCode(Integer.parseInt(fsapiResponseContext.getContextProps() + extensionResponseDTO.setStatusCode((int) (fsapiResponseContext.getContextProps() .get(GatewayConstants.MODIFIED_STATUS))); } extensionResponseDTO.setResponseStatus(ExtensionResponseStatus.RETURN_ERROR.toString()); @@ -216,15 +191,9 @@ protected ExtensionResponseDTO getResponseDTOForResponse(FSAPIResponseContext fs if (modifiedPayload != null) { extensionResponseDTO.setPayload(new ByteArrayInputStream(modifiedPayload.getBytes(StandardCharsets.UTF_8))); } - Map addedHeaders = fsapiResponseContext.getAddedHeaders(); - if (addedHeaders.size() != 0) { - HashMap headers = new HashMap<>(); - headers.putAll(fsapiResponseContext.getMsgInfo().getHeaders()); - for (Map.Entry headerEntry : addedHeaders.entrySet()) { - headers.put(headerEntry.getKey(), headerEntry.getValue()); - } - extensionResponseDTO.setHeaders(headers); - } + + setHeadersToResponse(extensionResponseDTO, fsapiResponseContext.getAddedHeaders(), + fsapiResponseContext.getMsgInfo().getHeaders()); return extensionResponseDTO; } @@ -240,7 +209,7 @@ public String getType() { * @param key unique cache key * @param contextProps properties to store */ - private void setPropertiesToCache(String key, Map contextProps) { + private void setPropertiesToCache(String key, Map contextProps) { GatewayDataHolder.getGatewayCache().addToCache(GatewayCacheKey.of(key), contextProps); } @@ -251,28 +220,34 @@ private void setPropertiesToCache(String key, Map contextProps) { * @param key unique cache key * @return context properties */ - private Map getPropertiesFromCache(String key) { - //Need to implement after adding base cache implementation to the common module. + private Map getPropertiesFromCache(String key) { Object cachedObject = GatewayDataHolder.getGatewayCache().getFromCache(GatewayCacheKey.of(key)); - return cachedObject == null ? new HashMap<>() : (Map) cachedObject; + return cachedObject == null ? new HashMap<>() : (Map) cachedObject; } /** * Method to remove context properties from cache. * * @param key unique cache key - * @return context properties */ private void removePropertiesFromCache(String key) { - //Need to implement after adding base cache implementation to the common module. GatewayDataHolder.getGatewayCache().removeFromCache(GatewayCacheKey.of(key)); } - private void publishAnalyticsData(Map analyticsData) { - - if (analyticsData != null && !analyticsData.isEmpty()) { - //Need to implement after adding base cache implementation to the common module. + /** + * Method to add headers to the response. + * + * @param extensionResponseDTO Extension response DTO + * @param addedHeaders Added headers + * @param requestHeaders Request headers + */ + private void setHeadersToResponse(ExtensionResponseDTO extensionResponseDTO, Map addedHeaders, + Map requestHeaders) { + if (addedHeaders.size() != 0) { + HashMap headers = new HashMap<>(); + headers.putAll(requestHeaders); + headers.putAll(addedHeaders); + extensionResponseDTO.setHeaders(headers); } } - } diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIRequestContext.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIRequestContext.java index 83fe04a0..98c9fc39 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIRequestContext.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIRequestContext.java @@ -40,7 +40,6 @@ import java.util.HashMap; import java.util.Map; -import javax.security.cert.X509Certificate; /** * Open Banking executor request context. @@ -49,44 +48,25 @@ public class FSAPIRequestContext extends RequestContextDTO { private static final Log log = LogFactory.getLog(FSAPIRequestContext.class); private final RequestContextDTO requestContextDTO; - private Map contextProps; + private Map contextProps; private String modifiedPayload; private String requestPayload; private Map addedHeaders; private boolean isError; private ArrayList errors; private String consentId; - private Map analyticsData; private OpenAPI openAPI; - public FSAPIRequestContext(RequestContextDTO requestContextDTO, - Map contextProps, Map analyticsData) { + public FSAPIRequestContext(RequestContextDTO requestContextDTO, Map contextProps) { this.requestContextDTO = requestContextDTO; this.contextProps = contextProps; this.addedHeaders = new HashMap<>(); this.errors = new ArrayList<>(); - this.contextProps = new HashMap<>(); - this.analyticsData = analyticsData; - Map headers = requestContextDTO.getMsgInfo().getHeaders(); - String authHeader = headers.get(GatewayConstants.AUTH_HEADER); - if (authHeader != null && !authHeader.isEmpty() && - GatewayUtils.isValidJWTToken(authHeader.replace(GatewayConstants.BEARER_TAG, ""))) { - this.consentId = extractConsentID(authHeader); - } + this.consentId = extractConsentID(requestContextDTO); + this.openAPI = retrieveOpenAPI(requestContextDTO); - String apiId = requestContextDTO.getApiRequestInfo().getApiId(); - Object cacheObject = GatewayDataHolder.getGatewayCache() - .getFromCache(GatewayCacheKey.of(apiId)); - if (cacheObject == null) { - String swaggerDefinition = GatewayUtils.getSwaggerDefinition(apiId); - OpenAPIParser parser = new OpenAPIParser(); - this.openAPI = parser.readContents(swaggerDefinition, null, null).getOpenAPI(); - GatewayDataHolder.getGatewayCache().addToCache(GatewayCacheKey.of(apiId), this.openAPI); - } else { - this.openAPI = (OpenAPI) cacheObject; - } if (requestContextDTO.getMsgInfo().getHeaders().get(GatewayConstants.CONTENT_TYPE_TAG) != null) { String contentType = requestContextDTO.getMsgInfo().getHeaders().get(GatewayConstants.CONTENT_TYPE_TAG); String httpMethod = requestContextDTO.getMsgInfo().getHttpMethod(); @@ -99,7 +79,7 @@ public FSAPIRequestContext(RequestContextDTO requestContextDTO, } catch (Exception e) { log.error(String.format("Failed to read the text payload from request. %s", e.getMessage().replaceAll("\n\r", ""))); - handleContentTypeErrors(FinancialServicesErrorCodes.INVALID_CONTENT_TYPE, errorMessage); + handleContentTypeErrors(errorMessage); } } else if (GatewayUtils.isEligibleRequest(contentType, httpMethod)) { try { @@ -107,7 +87,7 @@ public FSAPIRequestContext(RequestContextDTO requestContextDTO, } catch (Exception e) { log.error(String.format("Failed to read the payload from request. %s", e.getMessage().replaceAll("\n\r", ""))); - handleContentTypeErrors(FinancialServicesErrorCodes.INVALID_CONTENT_TYPE, errorMessage); + handleContentTypeErrors(errorMessage); } } else { this.requestPayload = null; @@ -135,16 +115,26 @@ public void setAddedHeaders(Map addedHeaders) { this.addedHeaders = addedHeaders; } - public Map getContextProps() { + public Map getContextProps() { return contextProps; } - public void setContextProps(Map contextProps) { + public void setContextProps(Map contextProps) { this.contextProps = contextProps; } + public void addContextProperty(String key, String value) { + + this.contextProps.put(key, value); + } + + public Object getContextProperty(String key) { + + return this.contextProps.get(key); + } + public boolean isError() { return isError; @@ -186,16 +176,6 @@ public void setOpenAPI(OpenAPI openAPI) { this.openAPI = openAPI; } - public Map getAnalyticsData() { - - return analyticsData; - } - - public void setAnalyticsData(Map analyticsData) { - - this.analyticsData = analyticsData; - } - @Override public MsgInfoDTO getMsgInfo() { @@ -208,11 +188,6 @@ public APIRequestInfoDTO getApiRequestInfo() { return requestContextDTO.getApiRequestInfo(); } - @Override - public X509Certificate[] getClientCerts() { - - return requestContextDTO.getClientCerts(); - } @Override public Certificate[] getClientCertsLatest() { @@ -224,43 +199,65 @@ public String getRequestPayload() { return requestPayload; } - private String extractConsentID(String jwtToken) { - - String consentIdClaim = null; - try { - if (!jwtToken.contains(GatewayConstants.BASIC_TAG)) { - jwtToken = jwtToken.replace(GatewayConstants.BEARER_TAG, ""); - JSONObject jwtClaims = GatewayUtils.decodeBase64(GatewayUtils.getPayloadFromJWT(jwtToken)); - String consentIdClaimName = - GatewayDataHolder.getInstance().getFinancialServicesConfigurationService().getConfigurations() - .get(FinancialServicesConstants.CONSENT_ID_CLAIM_NAME).toString(); - if (!jwtClaims.isNull(consentIdClaimName) && - !jwtClaims.getString(consentIdClaimName).isEmpty()) { - consentIdClaim = jwtClaims.getString(consentIdClaimName); + /** + * Extract consent ID from the Auth header in the request context. + * + * @param requestContextDTO Request context DTO + * @return consent ID + */ + private String extractConsentID(RequestContextDTO requestContextDTO) { + + Map headers = requestContextDTO.getMsgInfo().getHeaders(); + String authHeader = headers.get(GatewayConstants.AUTH_HEADER); + if (authHeader != null && !authHeader.isEmpty() && + GatewayUtils.isValidJWTToken(authHeader.replace(GatewayConstants.BEARER_TAG, ""))) { + String consentIdClaim = null; + try { + if (!authHeader.contains(GatewayConstants.BASIC_TAG)) { + authHeader = authHeader.replace(GatewayConstants.BEARER_TAG, ""); + JSONObject jwtClaims = GatewayUtils.decodeBase64(GatewayUtils.getPayloadFromJWT(authHeader)); + String consentIdClaimName = GatewayDataHolder.getInstance() + .getFinancialServicesConfigurationService().getConfigurations() + .get(FinancialServicesConstants.CONSENT_ID_CLAIM_NAME).toString(); + if (!jwtClaims.isNull(consentIdClaimName) && + !jwtClaims.getString(consentIdClaimName).isEmpty()) { + consentIdClaim = jwtClaims.getString(consentIdClaimName); + } } + } catch (UnsupportedEncodingException | JSONException | IllegalArgumentException e) { + log.error("Failed to retrieve the consent ID from JWT claims. %s", e); } - } catch (UnsupportedEncodingException | JSONException | IllegalArgumentException e) { - log.error("Failed to retrieve the consent ID from JWT claims. %s", e); + return consentIdClaim; } - return consentIdClaim; - } - - public void addContextProperty(String key, String value) { - - this.contextProps.put(key, value); + return null; } - public String getContextProperty(String key) { + /** + * Retrieve OpenAPI definition from the cache or from the publisher API. + * + * @param requestContextDTO Request context DTO + * @return OpenAPI definition + */ + private OpenAPI retrieveOpenAPI(RequestContextDTO requestContextDTO) { - return this.contextProps.get(key); + String apiId = requestContextDTO.getApiRequestInfo().getApiId(); + Object cacheObject = GatewayDataHolder.getGatewayCache() + .getFromCache(GatewayCacheKey.of(apiId)); + if (cacheObject == null) { + String swaggerDefinition = GatewayUtils.getSwaggerDefinition(apiId); + OpenAPIParser parser = new OpenAPIParser(); + OpenAPI openAPIDefinition = parser.readContents(swaggerDefinition, null, null).getOpenAPI(); + GatewayDataHolder.getGatewayCache().addToCache(GatewayCacheKey.of(apiId), openAPIDefinition); + return openAPIDefinition; + } + return (OpenAPI) cacheObject; } - private void handleContentTypeErrors(String errorCode, String errorMessage) { - FSExecutorError error = new FSExecutorError(errorCode, errorMessage, errorMessage, - FinancialServicesErrorCodes.UNSUPPORTED_MEDIA_TYPE_CODE); + private void handleContentTypeErrors(String errorMessage) { + FSExecutorError error = new FSExecutorError(FinancialServicesErrorCodes.INVALID_CONTENT_TYPE, errorMessage, + errorMessage, FinancialServicesErrorCodes.UNSUPPORTED_MEDIA_TYPE_CODE); this.isError = true; this.errors.add(error); } - } diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIResponseContext.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIResponseContext.java index 67345238..1b942aea 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIResponseContext.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIResponseContext.java @@ -41,45 +41,45 @@ public class FSAPIResponseContext extends ResponseContextDTO { private static final Log log = LogFactory.getLog(FSAPIResponseContext.class); private ResponseContextDTO responseContextDTO; - private Map contextProps; + private Map contextProps; private String responsePayload; private String modifiedPayload; private Map addedHeaders; private boolean isError; private ArrayList errors; - private Map analyticsData; - public FSAPIResponseContext(ResponseContextDTO responseContextDTO, Map contextProps, - Map analyticsData) { + public FSAPIResponseContext(ResponseContextDTO responseContextDTO, Map contextProps) { this.responseContextDTO = responseContextDTO; this.contextProps = contextProps; this.errors = new ArrayList<>(); - this.analyticsData = analyticsData; this.addedHeaders = new HashMap<>(); + if (responseContextDTO.getMsgInfo().getHeaders().get(GatewayConstants.CONTENT_TYPE_TAG) != null) { String contentType = responseContextDTO.getMsgInfo().getHeaders().get(GatewayConstants.CONTENT_TYPE_TAG); String httpMethod = responseContextDTO.getMsgInfo().getHttpMethod(); String errorMessage = "Request Content-Type header does not match any allowed types"; - if (contentType.startsWith(GatewayConstants.JWT_CONTENT_TYPE)) { + if (contentType.startsWith(GatewayConstants.JWT_CONTENT_TYPE) || contentType.startsWith(GatewayConstants + .JOSE_CONTENT_TYPE)) { try { this.responsePayload = GatewayUtils.getTextPayload(responseContextDTO.getMsgInfo() .getPayloadHandler().consumeAsString()); } catch (Exception e) { log.error(String.format("Failed to read the text payload from response. %s", e.getMessage().replaceAll("\n\r", ""))); - handleContentTypeErrors(FinancialServicesErrorCodes.INVALID_CONTENT_TYPE, errorMessage); + handleContentTypeErrors(errorMessage); } } else if (GatewayUtils.isEligibleResponse(contentType, httpMethod) && HttpStatus.SC_NO_CONTENT != responseContextDTO.getStatusCode()) { try { this.responsePayload = responseContextDTO.getMsgInfo().getPayloadHandler().consumeAsString(); if (contentType.contains(GatewayConstants.JSON_CONTENT_TYPE) && - this.responsePayload.contains("soapenv:Body")) { + this.responsePayload.contains(GatewayConstants.SOAP_BODY)) { JSONObject soapPayload = XML.toJSONObject(responseContextDTO.getMsgInfo().getPayloadHandler() - .consumeAsString()).getJSONObject("soapenv:Body"); + .consumeAsString()).getJSONObject(GatewayConstants.SOAP_BODY); if (soapPayload.has("jsonObject")) { - this.responsePayload = soapPayload.getJSONObject("jsonObject").toString(); + this.responsePayload = soapPayload.getJSONObject(GatewayConstants.SOAP_JSON_OBJECT) + .toString(); } else { this.responsePayload = null; } @@ -87,7 +87,7 @@ public FSAPIResponseContext(ResponseContextDTO responseContextDTO, Map addedHeaders) { this.addedHeaders = addedHeaders; } - public Map getContextProps() { + public Map getContextProps() { return contextProps; } - public void setContextProps(Map contextProps) { + public void setContextProps(Map contextProps) { this.contextProps = contextProps; } @@ -146,16 +146,6 @@ public void setErrors( this.errors = errors; } - public Map getAnalyticsData() { - - return analyticsData; - } - - public void setAnalyticsData(Map analyticsData) { - - this.analyticsData = analyticsData; - } - @Override public APIRequestInfoDTO getApiRequestInfo() { @@ -184,14 +174,14 @@ public void addContextProperty(String key, String value) { this.contextProps.put(key, value); } - public String getContextProperty(String key) { + public Object getContextProperty(String key) { return this.contextProps.get(key); } - private void handleContentTypeErrors(String errorCode, String errorMessage) { - FSExecutorError error = new FSExecutorError(errorCode, errorMessage, errorMessage, - FinancialServicesErrorCodes.UNSUPPORTED_MEDIA_TYPE_CODE); + private void handleContentTypeErrors(String errorMessage) { + FSExecutorError error = new FSExecutorError(FinancialServicesErrorCodes.INVALID_CONTENT_TYPE, errorMessage, + errorMessage, FinancialServicesErrorCodes.UNSUPPORTED_MEDIA_TYPE_CODE); this.isError = true; this.errors.add(error); diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/internal/GatewayDataHolder.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/internal/GatewayDataHolder.java index ed553c1c..63201a45 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/internal/GatewayDataHolder.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/internal/GatewayDataHolder.java @@ -39,7 +39,6 @@ public class GatewayDataHolder { private static volatile CloseableHttpClient httpClient; private static volatile GatewayCache gatewayCache; private FinancialServicesConfigurationService financialServicesConfigurationService; - private Map configurations; private int gatewayCacheAccessExpiry; private int gatewayCacheModifiedExpiry; private APIManagerConfigurationService apiManagerConfigurationService; @@ -95,7 +94,7 @@ public void setFinancialServicesConfigurationService( this.financialServicesConfigurationService = financialServicesConfigurationService; if (financialServicesConfigurationService != null) { - this.configurations = financialServicesConfigurationService.getConfigurations(); + Map configurations = financialServicesConfigurationService.getConfigurations(); setGatewayCacheAccessExpiry((String) configurations.get(FinancialServicesConstants.GATEWAY_CACHE_EXPIRY)); setGatewayCacheModifiedExpiry((String) configurations .get(FinancialServicesConstants.GATEWAY_CACHE_MODIFIED_EXPIRY)); diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayConstants.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayConstants.java index 9553d8fd..39159bbf 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayConstants.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayConstants.java @@ -32,6 +32,12 @@ public class GatewayConstants { public static final String JOSE_CONTENT_TYPE = "application/jose"; public static final String APPLICATION_XML_CONTENT_TYPE = "application/xml"; public static final String TEXT_XML_CONTENT_TYPE = "text/xml"; + public static final String SOAP_BODY = "soapenv:Body"; + public static final String SOAP_BODY_TEXT = "text"; + public static final String SOAP_BODY_CONTENT = "content"; + public static final String SOAP_JSON_OBJECT = "jsonObject"; + public static final String COLON = ":"; + public static final String SLASH = "/"; public static final String POST_HTTP_METHOD = "POST"; public static final String PUT_HTTP_METHOD = "PUT"; public static final String GET_HTTP_METHOD = "GET"; @@ -44,6 +50,10 @@ public class GatewayConstants { public static final String API_TYPE_CONSENT = "consent"; public static final String API_TYPE_NON_REGULATORY = "non-regulatory"; public static final String API_TYPE_CUSTOM_PROP = "x-wso2-api-type"; + public static final String EXECUTOR_TYPE_CONSENT = "Consent"; + public static final String EXECUTOR_TYPE_DCR = "DCR"; + public static final String EXECUTOR_TYPE_DEFAULT = "Default"; + public static final String DCR_PATH = "/register"; public static final String CONTEXT_PROP_CACHE_KEY = "_contextProp"; public static final String ANALYTICS_PROP_CACHE_KEY = "_analyticsData"; public static final String ERROR_STATUS_PROP = "errorStatusCode"; diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtils.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtils.java index b88e4334..b83effd4 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtils.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtils.java @@ -91,9 +91,9 @@ public static String getSwaggerDefinition(String apiId) { .getConfigurations() .get(FinancialServicesConstants.PUBLISHER_HOSTNAME).toString(); - String publisherAPIURL = publisherHostName.endsWith("/") ? + String publisherAPIURL = publisherHostName.endsWith(GatewayConstants.SLASH) ? publisherHostName + GatewayConstants.PUBLISHER_API_PATH + apiId + GatewayConstants.SWAGGER_ENDPOINT : - publisherHostName + "/" + GatewayConstants.PUBLISHER_API_PATH + apiId + + publisherHostName + GatewayConstants.SLASH + GatewayConstants.PUBLISHER_API_PATH + apiId + GatewayConstants.SWAGGER_ENDPOINT; try { URIBuilder uriBuilder = new URIBuilder(publisherAPIURL); @@ -119,8 +119,8 @@ public static String getSwaggerDefinition(String apiId) { */ public static String getAPIMgtConfig(String key) { - return GatewayDataHolder.getInstance() - .getApiManagerConfigurationService().getAPIManagerConfiguration().getFirstProperty(key); + return GatewayDataHolder.getInstance().getApiManagerConfigurationService() + .getAPIManagerConfiguration().getFirstProperty(key); } /** @@ -132,13 +132,15 @@ public static String getAPIMgtConfig(String key) { */ public static String getBasicAuthHeader(String username, String password) { - byte[] authHeader = Base64.getEncoder().encode((username + ":" + password).getBytes(StandardCharsets.UTF_8)); + byte[] authHeader = Base64.getEncoder().encode((username + GatewayConstants.COLON + password) + .getBytes(StandardCharsets.UTF_8)); return GatewayConstants.BASIC_TAG + new String(authHeader, StandardCharsets.UTF_8); } public static String getTextPayload(String payload) { - return XML.toJSONObject(payload).getJSONObject("soapenv:Body").getJSONObject("text").getString("content"); + return XML.toJSONObject(payload).getJSONObject(GatewayConstants.SOAP_BODY) + .getJSONObject(GatewayConstants.SOAP_BODY_TEXT).getString(GatewayConstants.SOAP_BODY_CONTENT); } @@ -147,7 +149,7 @@ public static String getTextPayload(String payload) { * * @param contentType - contentType * @param httpMethod - httpMethod - * @return + * @return true if the request is eligible */ public static boolean isEligibleRequest(String contentType, String httpMethod) { @@ -163,7 +165,7 @@ public static boolean isEligibleRequest(String contentType, String httpMethod) { * * @param contentType - contentType * @param httpMethod - httpMethod - * @return + * @return true if the response is eligible */ public static boolean isEligibleResponse(String contentType, String httpMethod) { diff --git a/pom.xml b/pom.xml index e9b34664..41d64619 100644 --- a/pom.xml +++ b/pom.xml @@ -595,7 +595,7 @@ 7.0.75 [7.0.75, 8.0.0) 7.0.26 - [7.0.26, 7.0.62) + [6.13.0, 7.0.62) 2.5.10 9.29.120 [9.29.120, 9.29.121) @@ -607,13 +607,13 @@ 1.4 1.2.140.wso2v3 - [1.7.0, 2.0.0) - [1.2.0, 2.0.0) + [1.7.0, 4.0.0) + [1.2.0, 4.0.0) [2.6.0, 3.0.0) [2.9,3) [4.4.0, 5.0.0) [1.2.11, 2.0.0) - [7.9.0, 8.0) + [7.9.0, 10.0) [2.15.1, 2.16.0) [3.0.0, 4.0.0)