use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method retrieveGatewayAPIDto.
public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environment, String tenantDomain, APIDTO apidto, String extractedFolderPath) throws APIManagementException, XMLStreamException, APITemplateException {
List<ClientCertificateDTO> clientCertificatesDTOList = ImportUtils.retrieveClientCertificates(extractedFolderPath);
List<SoapToRestMediationDto> soapToRestInMediationDtoList = ImportUtils.retrieveSoapToRestFlowMediations(extractedFolderPath, ImportUtils.IN);
List<SoapToRestMediationDto> soapToRestOutMediationDtoList = ImportUtils.retrieveSoapToRestFlowMediations(extractedFolderPath, ImportUtils.OUT);
JSONObject originalProperties = api.getAdditionalProperties();
// add new property for entires that has a __display suffix
JSONObject modifiedProperties = getModifiedProperties(originalProperties);
api.setAdditionalProperties(modifiedProperties);
APITemplateBuilder apiTemplateBuilder = TemplateBuilderUtil.getAPITemplateBuilder(api, tenantDomain, clientCertificatesDTOList, soapToRestInMediationDtoList, soapToRestOutMediationDtoList);
GatewayAPIDTO gatewaAPIDto = createAPIGatewayDTOtoPublishAPI(environment, api, apiTemplateBuilder, tenantDomain, extractedFolderPath, apidto, clientCertificatesDTOList);
// Reset the additional properties to the original values
if (originalProperties != null) {
api.setAdditionalProperties(originalProperties);
}
return gatewaAPIDto;
}
use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method createAPIGatewayDTOtoPublishAPI.
private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environment, API api, APITemplateBuilder builder, String tenantDomain, String extractedPath, APIDTO apidto, List<ClientCertificateDTO> clientCertificatesDTOList) throws APIManagementException, APITemplateException, XMLStreamException {
GatewayAPIDTO gatewayAPIDTO = new GatewayAPIDTO();
gatewayAPIDTO.setName(api.getId().getName());
gatewayAPIDTO.setVersion(api.getId().getVersion());
gatewayAPIDTO.setProvider(api.getId().getProviderName());
gatewayAPIDTO.setApiId(api.getUUID());
gatewayAPIDTO.setTenantDomain(tenantDomain);
gatewayAPIDTO.setKeyManagers(api.getKeyManagers());
String definition;
boolean isGraphQLSubscriptionAPI = false;
if (api.getType() != null && APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) {
// Build schema with additional info
gatewayAPIDTO.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(api.getUUID() + "_graphQL", gatewayAPIDTO.getLocalEntriesToBeRemove()));
GatewayContentDTO graphqlLocalEntry = new GatewayContentDTO();
graphqlLocalEntry.setName(api.getUUID() + "_graphQL");
graphqlLocalEntry.setContent("<localEntry key=\"" + api.getUUID() + "_graphQL" + "\">" + "<![CDATA[" + api.getGraphQLSchema() + "]]>" + "</localEntry>");
gatewayAPIDTO.setLocalEntriesToBeAdd(addGatewayContentToList(graphqlLocalEntry, gatewayAPIDTO.getLocalEntriesToBeAdd()));
gatewayAPIDTO.setGraphQLSchema(api.getGraphQLSchema());
Set<URITemplate> uriTemplates = new HashSet<>();
URITemplate template = new URITemplate();
template.setAuthType("Any");
template.setHTTPVerb("POST");
template.setHttpVerbs("POST");
template.setUriTemplate("/*");
uriTemplates.add(template);
api.setUriTemplates(uriTemplates);
GraphQLSchemaDefinition graphql = new GraphQLSchemaDefinition();
if (graphql.isSubscriptionAvailable(api.getGraphQLSchema())) {
isGraphQLSubscriptionAPI = true;
// if subscriptions are available add new URI template with wild card resource without http verb.
template = new URITemplate();
template.setUriTemplate("/*");
uriTemplates.add(template);
api.setUriTemplates(uriTemplates);
api.setEndpointConfig(populateSubscriptionEndpointConfig(api.getEndpointConfig()));
addGqlWebSocketTopicMappings(api);
}
} else if (api.getType() != null && (APIConstants.APITransportType.HTTP.toString().equals(api.getType()) || APIConstants.API_TYPE_SOAP.equals(api.getType()) || APIConstants.API_TYPE_SOAPTOREST.equals(api.getType()))) {
definition = api.getSwaggerDefinition();
gatewayAPIDTO.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(api.getUUID(), gatewayAPIDTO.getLocalEntriesToBeRemove()));
GatewayContentDTO apiLocalEntry = new GatewayContentDTO();
apiLocalEntry.setName(api.getUUID());
apiLocalEntry.setContent("<localEntry key=\"" + api.getUUID() + "\">" + definition.replaceAll("&(?!amp;)", "&").replaceAll("<", "<").replaceAll(">", ">") + "</localEntry>");
gatewayAPIDTO.setLocalEntriesToBeAdd(addGatewayContentToList(apiLocalEntry, gatewayAPIDTO.getLocalEntriesToBeAdd()));
} else if (api.getType() != null && (APIConstants.APITransportType.WS.toString().equals(api.getType()) || APIConstants.APITransportType.SSE.toString().equals(api.getType()) || APIConstants.APITransportType.WEBSUB.toString().equals(api.getType()))) {
gatewayAPIDTO.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(api.getUUID(), gatewayAPIDTO.getLocalEntriesToBeRemove()));
definition = api.getAsyncApiDefinition();
GatewayContentDTO apiLocalEntry = new GatewayContentDTO();
apiLocalEntry.setName(api.getUUID());
apiLocalEntry.setContent("<localEntry key=\"" + api.getUUID() + "\">" + definition.replaceAll("&(?!amp;)", "&").replaceAll("<", "<").replaceAll(">", ">") + "</localEntry>");
gatewayAPIDTO.setLocalEntriesToBeAdd(addGatewayContentToList(apiLocalEntry, gatewayAPIDTO.getLocalEntriesToBeAdd()));
}
if ((APIConstants.GATEWAY_ENV_TYPE_PRODUCTION.equals(environment.getType()) && !APIUtil.isProductionEndpointsExists(api.getEndpointConfig())) || (APIConstants.GATEWAY_ENV_TYPE_SANDBOX.equals(environment.getType()) && !APIUtil.isSandboxEndpointsExists(api.getEndpointConfig()))) {
if (log.isDebugEnabled()) {
log.debug("Not adding API to environment " + environment.getName() + " since its endpoint URL " + "cannot be found");
}
return null;
}
GatewayUtils.setCustomSequencesToBeRemoved(api, gatewayAPIDTO);
setAPIFaultSequencesToBeAdded(api, gatewayAPIDTO, extractedPath, apidto);
setCustomSequencesToBeAdded(api, gatewayAPIDTO, extractedPath, apidto);
setClientCertificatesToBeAdded(tenantDomain, gatewayAPIDTO, clientCertificatesDTOList);
boolean isWsApi = APIConstants.APITransportType.WS.toString().equals(api.getType());
if (isWsApi) {
addWebsocketTopicMappings(api, apidto);
}
// Add the API
if (APIConstants.IMPLEMENTATION_TYPE_INLINE.equalsIgnoreCase(api.getImplementation())) {
String prototypeScriptAPI = builder.getConfigStringForPrototypeScriptAPI(environment);
gatewayAPIDTO.setApiDefinition(prototypeScriptAPI);
} else if (APIConstants.IMPLEMENTATION_TYPE_ENDPOINT.equalsIgnoreCase(api.getImplementation())) {
String apiConfig = builder.getConfigStringForTemplate(environment);
gatewayAPIDTO.setApiDefinition(apiConfig);
org.json.JSONObject endpointConfig = new org.json.JSONObject(api.getEndpointConfig());
if (!endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).equals(APIConstants.ENDPOINT_TYPE_AWSLAMBDA)) {
if (!isWsApi) {
addEndpoints(api, builder, gatewayAPIDTO);
}
if (isWsApi || isGraphQLSubscriptionAPI) {
addWebSocketResourceEndpoints(api, builder, gatewayAPIDTO);
}
}
}
setSecureVaultPropertyToBeAdded(null, api, gatewayAPIDTO);
return gatewayAPIDTO;
}
use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method getAPITemplateBuilder.
public static APITemplateBuilderImpl getAPITemplateBuilder(APIProduct apiProduct, String tenantDomain, List<ClientCertificateDTO> clientCertificateDTOS, Map<String, APIDTO> associatedAPIMap) throws APIManagementException {
int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
APITemplateBuilderImpl vtb = new APITemplateBuilderImpl(apiProduct, associatedAPIMap);
Map<String, String> latencyStatsProperties = new HashMap<String, String>();
latencyStatsProperties.put(APIConstants.API_UUID, apiProduct.getUuid());
if (!APIUtil.isStreamingApi(apiProduct)) {
vtb.addHandler("org.wso2.carbon.apimgt.gateway.handlers.common.APIMgtLatencyStatsHandler", latencyStatsProperties);
}
Map<String, String> corsProperties = new HashMap<>();
corsProperties.put(APIConstants.CORSHeaders.IMPLEMENTATION_TYPE_HANDLER_VALUE, APIConstants.IMPLEMENTATION_TYPE_ENDPOINT);
// Get authorization header from the API object or from the tenant registry
String authorizationHeader;
if (!StringUtils.isBlank(apiProduct.getAuthorizationHeader())) {
authorizationHeader = apiProduct.getAuthorizationHeader();
} else {
// Retrieves the auth configuration from tenant registry or api-manager.xml if not available
// in tenant registry
authorizationHeader = APIUtil.getOAuthConfiguration(tenantDomain, APIConstants.AUTHORIZATION_HEADER);
}
if (!StringUtils.isBlank(authorizationHeader)) {
corsProperties.put(APIConstants.AUTHORIZATION_HEADER, authorizationHeader);
}
if (apiProduct.getCorsConfiguration() != null && apiProduct.getCorsConfiguration().isCorsConfigurationEnabled()) {
CORSConfiguration corsConfiguration = apiProduct.getCorsConfiguration();
if (corsConfiguration.getAccessControlAllowHeaders() != null) {
StringBuilder allowHeaders = new StringBuilder();
for (String header : corsConfiguration.getAccessControlAllowHeaders()) {
allowHeaders.append(header).append(',');
}
if (allowHeaders.length() != 0) {
allowHeaders.deleteCharAt(allowHeaders.length() - 1);
corsProperties.put(APIConstants.CORSHeaders.ALLOW_HEADERS_HANDLER_VALUE, allowHeaders.toString());
}
}
if (corsConfiguration.getAccessControlAllowOrigins() != null) {
StringBuilder allowOrigins = new StringBuilder();
for (String origin : corsConfiguration.getAccessControlAllowOrigins()) {
allowOrigins.append(origin).append(',');
}
if (allowOrigins.length() != 0) {
allowOrigins.deleteCharAt(allowOrigins.length() - 1);
corsProperties.put(APIConstants.CORSHeaders.ALLOW_ORIGIN_HANDLER_VALUE, allowOrigins.toString());
}
}
if (corsConfiguration.getAccessControlAllowMethods() != null) {
StringBuilder allowedMethods = new StringBuilder();
for (String methods : corsConfiguration.getAccessControlAllowMethods()) {
allowedMethods.append(methods).append(',');
}
if (allowedMethods.length() != 0) {
allowedMethods.deleteCharAt(allowedMethods.length() - 1);
corsProperties.put(APIConstants.CORSHeaders.ALLOW_METHODS_HANDLER_VALUE, allowedMethods.toString());
}
}
if (corsConfiguration.isAccessControlAllowCredentials()) {
corsProperties.put(APIConstants.CORSHeaders.ALLOW_CREDENTIALS_HANDLER_VALUE, String.valueOf(corsConfiguration.isAccessControlAllowCredentials()));
}
vtb.addHandler("org.wso2.carbon.apimgt.gateway.handlers.security.CORSRequestHandler", corsProperties);
} else if (APIUtil.isCORSEnabled()) {
vtb.addHandler("org.wso2.carbon.apimgt.gateway.handlers.security.CORSRequestHandler", corsProperties);
}
vtb.addHandler("org.wso2.carbon.apimgt.gateway.handlers.common.APIStatusHandler", Collections.emptyMap());
Map<String, String> clientCertificateObject = null;
CertificateMgtUtils certificateMgtUtils = CertificateMgtUtils.getInstance();
if (clientCertificateDTOS != null) {
clientCertificateObject = new HashMap<>();
for (ClientCertificateDTO clientCertificateDTO : clientCertificateDTOS) {
clientCertificateObject.put(certificateMgtUtils.getUniqueIdentifierOfCertificate(clientCertificateDTO.getCertificate()), clientCertificateDTO.getTierName());
}
}
Map<String, String> authProperties = new HashMap<String, String>();
if (!StringUtils.isBlank(authorizationHeader)) {
authProperties.put(APIConstants.AUTHORIZATION_HEADER, authorizationHeader);
}
String apiSecurity = apiProduct.getApiSecurity();
String apiLevelPolicy = apiProduct.getProductLevelPolicy();
authProperties.put(APIConstants.API_SECURITY, apiSecurity);
authProperties.put(APIConstants.API_LEVEL_POLICY, apiLevelPolicy);
if (clientCertificateObject != null) {
authProperties.put(APIConstants.CERTIFICATE_INFORMATION, clientCertificateObject.toString());
}
// Get RemoveHeaderFromOutMessage from tenant registry or api-manager.xml
String removeHeaderFromOutMessage = APIUtil.getOAuthConfiguration(tenantDomain, APIConstants.REMOVE_OAUTH_HEADER_FROM_OUT_MESSAGE);
if (!StringUtils.isBlank(removeHeaderFromOutMessage)) {
authProperties.put(APIConstants.REMOVE_OAUTH_HEADER_FROM_OUT_MESSAGE, removeHeaderFromOutMessage);
} else {
authProperties.put(APIConstants.REMOVE_OAUTH_HEADER_FROM_OUT_MESSAGE, APIConstants.REMOVE_OAUTH_HEADER_FROM_OUT_MESSAGE_DEFAULT);
}
authProperties.put("apiType", APIConstants.ApiTypes.PRODUCT_API.name());
vtb.addHandler("org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler", authProperties);
Map<String, String> properties = new HashMap<String, String>();
if (apiProduct.getProductionMaxTps() != null) {
properties.put("productionMaxCount", apiProduct.getProductionMaxTps());
}
if (apiProduct.getSandboxMaxTps() != null) {
properties.put("sandboxMaxCount", apiProduct.getSandboxMaxTps());
}
if (!APIUtil.isStreamingApi(apiProduct)) {
vtb.addHandler("org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler", properties);
properties = new HashMap<String, String>();
properties.put("configKey", APIConstants.GA_CONF_KEY);
vtb.addHandler("org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtGoogleAnalyticsTrackingHandler", properties);
String extensionHandlerPosition = getExtensionHandlerPosition(tenantDomain);
if ("top".equalsIgnoreCase(extensionHandlerPosition)) {
vtb.addHandlerPriority("org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler", Collections.emptyMap(), 2);
} else {
vtb.addHandler("org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler", Collections.emptyMap());
}
}
return vtb;
}
use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method retrieveGatewayAPIDto.
public static GatewayAPIDTO retrieveGatewayAPIDto(APIProduct apiProduct, Environment environment, String tenantDomain, String extractedFolderPath) throws APIManagementException, XMLStreamException, APITemplateException {
List<ClientCertificateDTO> clientCertificatesDTOList = ImportUtils.retrieveClientCertificates(extractedFolderPath);
Map<String, APIDTO> apidtoMap = retrieveAssociatedApis(extractedFolderPath);
Map<String, APIDTO> associatedAPIsMap = convertAPIIdToDto(apidtoMap.values());
for (APIProductResource productResource : apiProduct.getProductResources()) {
String apiId = productResource.getApiId();
APIDTO apidto = associatedAPIsMap.get(apiId);
if (apidto != null) {
API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider());
productResource.setApiIdentifier(api.getId());
if (api.isAdvertiseOnly()) {
productResource.setEndpointConfig(APIUtil.generateEndpointConfigForAdvertiseOnlyApi(api));
} else {
productResource.setEndpointConfig(api.getEndpointConfig());
}
if (StringUtils.isNotEmpty(api.getInSequence())) {
String sequenceName = APIUtil.getSequenceExtensionName(apiProduct.getId().getName(), apiProduct.getId().getVersion()).concat("--").concat(productResource.getApiId()) + APIConstants.API_CUSTOM_SEQ_IN_EXT;
productResource.setInSequenceName(sequenceName);
}
if (StringUtils.isNotEmpty(api.getOutSequence())) {
String sequenceName = APIUtil.getSequenceExtensionName(apiProduct.getId().getName(), apiProduct.getId().getVersion()).concat("--").concat(productResource.getApiId()) + APIConstants.API_CUSTOM_SEQ_OUT_EXT;
productResource.setOutSequenceName(sequenceName);
}
if (StringUtils.isNotEmpty(api.getFaultSequence())) {
String sequenceName = APIUtil.getSequenceExtensionName(apiProduct.getId().getName(), apiProduct.getId().getVersion()).concat("--").concat(productResource.getApiId()) + APIConstants.API_CUSTOM_SEQ_FAULT_EXT;
productResource.setFaultSequenceName(sequenceName);
}
productResource.setProductIdentifier(apiProduct.getId());
productResource.setEndpointSecurityMap(APIUtil.setEndpointSecurityForAPIProduct(api));
}
}
APITemplateBuilder apiTemplateBuilder = TemplateBuilderUtil.getAPITemplateBuilder(apiProduct, tenantDomain, clientCertificatesDTOList, convertAPIIdToDto(associatedAPIsMap.values()));
return createAPIGatewayDTOtoPublishAPI(environment, apiProduct, apiTemplateBuilder, tenantDomain, apidtoMap, clientCertificatesDTOList);
}
use of org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method setClientCertificatesToBeAdded.
/**
* To deploy client certificate in given API environment.
*
* @param tenantDomain Tenant domain.
* @param clientCertificatesDTOList
*/
private static void setClientCertificatesToBeAdded(String tenantDomain, GatewayAPIDTO gatewayAPIDTO, List<ClientCertificateDTO> clientCertificatesDTOList) {
int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
if (clientCertificatesDTOList != null) {
for (ClientCertificateDTO clientCertificateDTO : clientCertificatesDTOList) {
GatewayContentDTO clientCertificate = new GatewayContentDTO();
clientCertificate.setName(clientCertificateDTO.getAlias() + "_" + tenantId);
clientCertificate.setContent(clientCertificateDTO.getCertificate());
gatewayAPIDTO.setClientCertificatesToBeAdd(addGatewayContentToList(clientCertificate, gatewayAPIDTO.getClientCertificatesToBeAdd()));
}
}
}
Aggregations