use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.
the class GraphQLSchemaDefinition method policyDefinitionToJson.
/**
* Method to convert GraphqlComplexityInfo object to a JSONObject
*
* @param graphqlComplexityInfo GraphqlComplexityInfo object
* @return json object which contains the policy definition
*/
public JSONObject policyDefinitionToJson(GraphqlComplexityInfo graphqlComplexityInfo) {
JSONObject policyDefinition = new JSONObject();
HashMap<String, HashMap<String, Integer>> customComplexityMap = new HashMap<>();
List<CustomComplexityDetails> list = graphqlComplexityInfo.getList();
for (CustomComplexityDetails customComplexityDetails : list) {
String type = customComplexityDetails.getType();
String field = customComplexityDetails.getField();
int complexityValue = customComplexityDetails.getComplexityValue();
if (customComplexityMap.containsKey(type)) {
customComplexityMap.get(type).put(field, complexityValue);
} else {
HashMap<String, Integer> complexityValueMap = new HashMap<>();
complexityValueMap.put(field, complexityValue);
customComplexityMap.put(type, complexityValueMap);
}
}
Map<String, Map<String, Object>> customComplexityObject = new LinkedHashMap<>(customComplexityMap.size());
for (HashMap.Entry<String, HashMap<String, Integer>> entry : customComplexityMap.entrySet()) {
HashMap<String, Integer> fieldValueMap = entry.getValue();
String type = entry.getKey();
Map<String, Object> fieldValueObject = new LinkedHashMap<>(fieldValueMap.size());
for (HashMap.Entry<String, Integer> subEntry : fieldValueMap.entrySet()) {
String field = subEntry.getKey();
int complexityValue = subEntry.getValue();
fieldValueObject.put(field, complexityValue);
}
customComplexityObject.put(type, fieldValueObject);
}
policyDefinition.put(APIConstants.QUERY_ANALYSIS_COMPLEXITY, customComplexityObject);
return policyDefinition;
}
use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.
the class GraphqlQueryAnalysisMappingUtil method fromDTOtoValidatedGraphqlComplexityInfo.
/**
* Converts a GraphQLQueryComplexityInfo DTO object into a GraphqlComplexityInfo object. During this process a
* basic validation is done comparing with the types of the schema
*
* @param graphQLQueryComplexityInfoDTO GraphQLQueryComplexityInfoDTO object
* @param schema GraphQL Schema
* @return a new GraphqlComplexityInfo object corresponding to given GraphQLQueryComplexityInfoDTO object
*/
public static GraphqlComplexityInfo fromDTOtoValidatedGraphqlComplexityInfo(GraphQLQueryComplexityInfoDTO graphQLQueryComplexityInfoDTO, String schema) {
SchemaParser schemaParser = new SchemaParser();
Set<String> complexityInfoTypeSet = schemaParser.parse(schema).types().keySet();
GraphqlComplexityInfo graphqlComplexityInfo = new GraphqlComplexityInfo();
List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<CustomComplexityDetails>();
for (GraphQLCustomComplexityInfoDTO graphQLCustomComplexityInfoDTO : graphQLQueryComplexityInfoDTO.getList()) {
String complexityType = graphQLCustomComplexityInfoDTO.getType();
if (complexityInfoTypeSet.contains(complexityType)) {
CustomComplexityDetails customComplexityDetails = new CustomComplexityDetails();
customComplexityDetails.setType(complexityType);
customComplexityDetails.setField(graphQLCustomComplexityInfoDTO.getField());
customComplexityDetails.setComplexityValue(graphQLCustomComplexityInfoDTO.getComplexityValue());
customComplexityDetailsList.add(customComplexityDetails);
} else {
log.error("Complexity Type : " + complexityType + " is not included in the original schema. Hence " + "skipped.");
}
}
graphqlComplexityInfo.setList(customComplexityDetailsList);
return graphqlComplexityInfo;
}
use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.
the class SynapseArtifactGenerator method generateGatewayArtifact.
@Override
public RuntimeArtifactDto generateGatewayArtifact(List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList) throws APIManagementException {
RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
List<String> synapseArtifacts = new ArrayList<>();
for (APIRuntimeArtifactDto runTimeArtifact : apiRuntimeArtifactDtoList) {
if (runTimeArtifact.isFile()) {
String tenantDomain = runTimeArtifact.getTenantDomain();
String label = runTimeArtifact.getLabel();
Environment environment = APIUtil.getEnvironments(tenantDomain).get(label);
GatewayAPIDTO gatewayAPIDTO = null;
if (environment != null) {
try (InputStream artifact = (InputStream) runTimeArtifact.getArtifact()) {
File baseDirectory = CommonUtil.createTempDirectory(null);
try {
String extractedFolderPath = ImportUtils.getArchivePathOfExtractedDirectory(baseDirectory.getAbsolutePath(), artifact);
if (APIConstants.API_PRODUCT.equals(runTimeArtifact.getType())) {
APIProductDTO apiProductDTO = ImportUtils.retrieveAPIProductDto(extractedFolderPath);
apiProductDTO.setId(runTimeArtifact.getApiId());
APIProduct apiProduct = APIMappingUtil.fromDTOtoAPIProduct(apiProductDTO, apiProductDTO.getProvider());
APIDefinitionValidationResponse apiDefinitionValidationResponse = ImportUtils.retrieveValidatedSwaggerDefinitionFromArchive(extractedFolderPath);
apiProduct.setDefinition(apiDefinitionValidationResponse.getContent());
gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(apiProduct, environment, tenantDomain, extractedFolderPath);
} else {
APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath);
API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider());
api.setUUID(apidto.getId());
if (APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) {
APIDefinition parser = new OAS3Parser();
SwaggerData swaggerData = new SwaggerData(api);
String apiDefinition = parser.generateAPIDefinition(swaggerData);
api.setSwaggerDefinition(apiDefinition);
GraphqlComplexityInfo graphqlComplexityInfo = APIUtil.getComplexityDetails(api);
String graphqlSchema = ImportUtils.retrieveValidatedGraphqlSchemaFromArchive(extractedFolderPath);
api.setGraphQLSchema(graphqlSchema);
GraphQLSchemaDefinition graphQLSchemaDefinition = new GraphQLSchemaDefinition();
graphqlSchema = graphQLSchemaDefinition.buildSchemaWithAdditionalInfo(api, graphqlComplexityInfo);
api.setGraphQLSchema(graphqlSchema);
gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath);
} 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()) || APIConstants.APITransportType.WEBHOOK.toString().equals(api.getType()))) {
APIDefinitionValidationResponse apiDefinitionValidationResponse = ImportUtils.retrieveValidatedSwaggerDefinitionFromArchive(extractedFolderPath);
api.setSwaggerDefinition(apiDefinitionValidationResponse.getContent());
gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath, apiDefinitionValidationResponse);
} 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()))) {
APIDefinitionValidationResponse asyncApiDefinition = ImportUtils.retrieveValidatedAsyncApiDefinitionFromArchive(extractedFolderPath);
api.setAsyncApiDefinition(asyncApiDefinition.getContent());
gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDtoForStreamingAPI(api, environment, tenantDomain, apidto, extractedFolderPath);
}
}
if (gatewayAPIDTO != null) {
String content = new Gson().toJson(gatewayAPIDTO);
synapseArtifacts.add(content);
}
} finally {
FileUtils.deleteQuietly(baseDirectory);
}
} catch (Exception e) {
// only do error since we need to continue for other apis
log.error("Error while creating Synapse configurations", e);
}
}
}
}
runtimeArtifactDto.setFile(false);
runtimeArtifactDto.setArtifact(synapseArtifacts);
return runtimeArtifactDto;
}
use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.
the class ApiMgtDAO method getComplexityDetails.
/**
* Get custom complexity details for a particular API
*
* @param uuid API UUID to retrieve API ID
* @return info about the complexity details
* @throws APIManagementException
*/
public GraphqlComplexityInfo getComplexityDetails(String uuid) throws APIManagementException {
GraphqlComplexityInfo graphqlComplexityInfo = new GraphqlComplexityInfo();
String getCustomComplexityDetailsQuery = SQLConstants.GET_CUSTOM_COMPLEXITY_DETAILS_SQL;
List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<CustomComplexityDetails>();
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement getCustomComplexityDetails = conn.prepareStatement(getCustomComplexityDetailsQuery)) {
int apiId = getAPIID(uuid, conn);
getCustomComplexityDetails.setInt(1, apiId);
try (ResultSet rs1 = getCustomComplexityDetails.executeQuery()) {
while (rs1.next()) {
CustomComplexityDetails customComplexityDetails = new CustomComplexityDetails();
customComplexityDetails.setType(rs1.getString("TYPE"));
customComplexityDetails.setField(rs1.getString("FIELD"));
customComplexityDetails.setComplexityValue(rs1.getInt("COMPLEXITY_VALUE"));
customComplexityDetailsList.add(customComplexityDetails);
}
}
graphqlComplexityInfo.setList(customComplexityDetailsList);
} catch (SQLException ex) {
handleException("Error while retrieving custom complexity details: ", ex);
}
return graphqlComplexityInfo;
}
use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.
the class APIMgtDAOTest method getGraphqlComplexityInfoDetails.
private GraphqlComplexityInfo getGraphqlComplexityInfoDetails() {
GraphqlComplexityInfo graphqlComplexityInfo = new GraphqlComplexityInfo();
CustomComplexityDetails customComplexity1 = new CustomComplexityDetails();
customComplexity1.setType("type1");
customComplexity1.setField("field1");
customComplexity1.setComplexityValue(5);
CustomComplexityDetails customComplexity2 = new CustomComplexityDetails();
customComplexity2.setType("type1");
customComplexity2.setField("field2");
customComplexity2.setComplexityValue(4);
List<CustomComplexityDetails> list = new ArrayList<>();
list.add(customComplexity1);
list.add(customComplexity2);
graphqlComplexityInfo.setList(list);
return graphqlComplexityInfo;
}
Aggregations