use of org.wso2.carbon.apimgt.api.doc.model.Operation in project carbon-apimgt by wso2.
the class OAS2Parser method removeExamplesFromSwagger.
/**
* Remove x-wso2-examples from all the paths from the swagger.
*
* @param swaggerString Swagger as String
*/
public String removeExamplesFromSwagger(String swaggerString) throws APIManagementException {
try {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(swaggerString);
swagger.getPaths().values().forEach(path -> {
path.getOperations().forEach(operation -> {
if (operation.getVendorExtensions().keySet().contains(APIConstants.SWAGGER_X_EXAMPLES)) {
operation.getVendorExtensions().remove(APIConstants.SWAGGER_X_EXAMPLES);
}
});
});
return Yaml.pretty().writeValueAsString(swagger);
} catch (JsonProcessingException e) {
throw new APIManagementException("Error while removing examples from OpenAPI definition", e, ExceptionCodes.ERROR_REMOVING_EXAMPLES);
}
}
use of org.wso2.carbon.apimgt.api.doc.model.Operation in project carbon-apimgt by wso2.
the class APIDefinitionFromOpenAPISpec method validateScopesFromSwagger.
/**
* Called using the jaggery api. Checks if the swagger contains valid api scopes.
*
* @param swagger Swagger definition
* @return true if the scope definition is valid
* @throws APIManagementException
*/
public Boolean validateScopesFromSwagger(String swagger) throws APIManagementException {
try {
Set<Scope> scopes = getScopes(swagger);
JSONParser parser = new JSONParser();
JSONObject swaggerJson;
swaggerJson = (JSONObject) parser.parse(swagger);
if (swaggerJson.get("paths") != null) {
JSONObject paths = (JSONObject) swaggerJson.get("paths");
for (Object uriTempKey : paths.keySet()) {
String uriTemp = (String) uriTempKey;
// if url template is a custom attribute "^x-" ignore.
if (uriTemp.startsWith("x-") || uriTemp.startsWith("X-")) {
continue;
}
JSONObject path = (JSONObject) paths.get(uriTemp);
// See field types supported by "Path Item Object" in swagger spec.
if (path.containsKey("$ref")) {
continue;
}
for (Object httpVerbKey : path.keySet()) {
String httpVerb = (String) httpVerbKey;
JSONObject operation = (JSONObject) path.get(httpVerb);
String operationScope = (String) operation.get(APIConstants.SWAGGER_X_SCOPE);
Scope scope = APIUtil.findScopeByKey(scopes, operationScope);
if (scope == null && operationScope != null) {
return false;
}
}
}
}
return true;
} catch (APIManagementException e) {
handleException("Error when validating scopes", e);
return false;
} catch (ParseException e) {
handleException("Error when validating scopes", e);
return false;
}
}
use of org.wso2.carbon.apimgt.api.doc.model.Operation in project carbon-apimgt by wso2.
the class AsyncApiParser method buildURITemplate.
private URITemplate buildURITemplate(String target, String verb, Aai20Operation operation, Set<Scope> scopes, Aai20ChannelItem channel) throws APIManagementException {
URITemplate template = new URITemplate();
template.setHTTPVerb(verb);
template.setHttpVerbs(verb);
template.setUriTemplate(target);
Extension authTypeExtension = channel.getExtension(APIConstants.SWAGGER_X_AUTH_TYPE);
if (authTypeExtension != null && authTypeExtension.value instanceof String) {
template.setAuthType(authTypeExtension.value.toString());
}
List<String> opScopes = getScopeOfOperations(operation);
if (!opScopes.isEmpty()) {
if (opScopes.size() == 1) {
String firstScope = opScopes.get(0);
Scope scope = APIUtil.findScopeByKey(scopes, firstScope);
if (scope == null) {
throw new APIManagementException("Scope '" + firstScope + "' not found.");
}
template.setScope(scope);
template.setScopes(scope);
} else {
for (String scopeName : opScopes) {
Scope scope = APIUtil.findScopeByKey(scopes, scopeName);
if (scope == null) {
throw new APIManagementException("Resource Scope '" + scopeName + "' not found.");
}
template.setScopes(scope);
}
}
}
return template;
}
use of org.wso2.carbon.apimgt.api.doc.model.Operation in project carbon-apimgt by wso2.
the class ApiMgtDAO method getOperationPoliciesOfURITemplate.
/**
* Get operation polycies attached to the resource identified by the url mapping ID
*
* @param urlMappingId URL Mapping ID of the resource
* @return
* @throws SQLException
* @throws APIManagementException
*/
private List<OperationPolicy> getOperationPoliciesOfURITemplate(int urlMappingId) throws SQLException, APIManagementException {
List<OperationPolicy> operationPolicies = new ArrayList<>();
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICIES_BY_URI_TEMPLATE_ID)) {
ps.setInt(1, urlMappingId);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
OperationPolicy policy = populateOperationPolicyWithRS(rs);
operationPolicies.add(policy);
}
}
}
return operationPolicies;
}
use of org.wso2.carbon.apimgt.api.doc.model.Operation in project carbon-apimgt by wso2.
the class ApiMgtDAO method cloneOperationPolicy.
/**
* Clone an operation policy to the API. This method is used to clone policy to a newly created api version.
* Cloning a common policy to API.
* Cloning a dependent policy of a product
* Each of these scenarios, original APIs' policy ID will be recorded as the cloned policy ID.
*
* @param apiUUID UUID of the API
* @param operationPolicyData
* @return cloned policyID
* @throws APIManagementException
*/
public String cloneOperationPolicy(String apiUUID, OperationPolicyData operationPolicyData) throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
String policyId = addAPISpecificOperationPolicy(connection, apiUUID, null, operationPolicyData, operationPolicyData.getClonedCommonPolicyId());
connection.commit();
return policyId;
} catch (SQLException e) {
connection.rollback();
throw e;
}
} catch (SQLException e) {
throw new APIManagementException("Error while cloning Operation policies", e);
}
}
Aggregations