Search in sources :

Example 41 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition 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, APIDefinitionValidationResponse apiDefinitionValidationResponse) throws APIManagementException, XMLStreamException, APITemplateException, CertificateManagementException {
    if (apiDefinitionValidationResponse.isValid()) {
        APIDefinition parser = apiDefinitionValidationResponse.getParser();
        String definition = apiDefinitionValidationResponse.getJsonContent();
        if (parser != null) {
            Set<URITemplate> uriTemplates = parser.getURITemplates(definition);
            for (URITemplate uriTemplate : uriTemplates) {
                for (URITemplate template : api.getUriTemplates()) {
                    if (template.getHTTPVerb().equalsIgnoreCase(uriTemplate.getHTTPVerb()) && template.getUriTemplate().equals(uriTemplate.getUriTemplate())) {
                        template.setMediationScript(uriTemplate.getMediationScript());
                        template.setMediationScripts(uriTemplate.getHTTPVerb(), uriTemplate.getMediationScript());
                        template.setAmznResourceName(uriTemplate.getAmznResourceName());
                        template.setAmznResourceTimeout(uriTemplate.getAmznResourceTimeout());
                        break;
                    }
                }
            }
        }
    }
    return retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath);
}
Also used : APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Example 42 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.

the class RestApiUtil method getAdminAPIAppResourceMapping.

/**
 * This is static method to return URI Templates map of API Admin REST API.
 * This content need to load only one time and keep it in memory as content will not change
 * during runtime.
 *
 * @return URITemplate set associated with API Manager Admin REST API
 */
public static Set<URITemplate> getAdminAPIAppResourceMapping(String version) {
    API api = new API(new APIIdentifier(RestApiConstants.REST_API_PROVIDER, RestApiConstants.REST_API_ADMIN_CONTEXT, RestApiConstants.REST_API_ADMIN_VERSION_0));
    if (adminAPIResourceMappings != null) {
        return adminAPIResourceMappings;
    } else {
        try {
            String definition;
            if (RestApiConstants.REST_API_ADMIN_VERSION_0.equals(version)) {
                definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream("/admin-api.json"), "UTF-8");
            } else {
                definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream("/admin-api.yaml"), "UTF-8");
            }
            APIDefinition oasParser = OASParserUtil.getOASParser(definition);
            // Get URL templates from swagger content we created
            adminAPIResourceMappings = oasParser.getURITemplates(definition);
        } catch (APIManagementException e) {
            log.error("Error while reading resource mappings for API: " + api.getId().getApiName(), e);
        } catch (IOException e) {
            log.error("Error while reading the swagger definition for API: " + api.getId().getApiName(), e);
        }
        return adminAPIResourceMappings;
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) IOException(java.io.IOException)

Example 43 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.

the class SolaceAdminApis method registerAPI.

/**
 * Register API in Solace using AsyncAPI Definition
 *
 * @param organization  name of the Organization
 * @param title         name of the Solace API
 * @param apiDefinition Async definition of the Solace API
 * @return CloseableHttpResponse of the PUT call
 */
public CloseableHttpResponse registerAPI(String organization, String title, String apiDefinition) {
    URL serviceEndpointURL = new URL(baseUrl);
    HttpClient httpClient = APIUtil.getHttpClient(serviceEndpointURL.getPort(), serviceEndpointURL.getProtocol());
    HttpPut httpPut = new HttpPut(baseUrl + "/" + organization + "/apis/" + title);
    httpPut.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedCredentials());
    httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
    JsonNode jsonNodeTree;
    String jsonAsYaml = null;
    // convert json to yaml
    try {
        jsonNodeTree = new ObjectMapper().readTree(apiDefinition);
        jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);
    } catch (JsonProcessingException e) {
        log.error(e.getMessage());
    }
    // add definition to request body
    if (jsonAsYaml != null) {
        StringEntity params = null;
        try {
            params = new StringEntity(jsonAsYaml);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage());
        }
        httpPut.setEntity(params);
        try {
            return APIUtil.executeHTTPRequest(httpPut, httpClient);
        } catch (IOException | APIManagementException e) {
            log.error(e.getMessage());
        }
    }
    return null;
}
Also used : YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) URL(org.apache.axis2.util.URL) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HttpClient(org.apache.http.client.HttpClient) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 44 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.

the class SettingsMappingUtil method GetScopeList.

private List<String> GetScopeList() throws APIManagementException {
    String definition = null;
    try {
        definition = IOUtils.toString(RestApiUtil.class.getResourceAsStream("/devportal-api.yaml"), "UTF-8");
    } catch (IOException e) {
        log.error("Error while reading the swagger definition", e);
    }
    APIDefinition oasParser = OASParserUtil.getOASParser(definition);
    Set<Scope> scopeSet = oasParser.getScopes(definition);
    List<String> scopeList = new ArrayList<>();
    for (Scope entry : scopeSet) {
        scopeList.add(entry.getKey());
    }
    return scopeList;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 45 with APIDefinition

use of org.wso2.carbon.apimgt.api.doc.model.APIDefinition in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method generateSOAPAPIDefinition.

/**
 * Add soap parameters to the default soap api resource.
 *
 * @param apiDefinition The API definition string.
 * @return Modified api definition.
 */
private String generateSOAPAPIDefinition(String apiDefinition) throws APIManagementException {
    JSONParser jsonParser = new JSONParser();
    JSONObject apiJson;
    JSONObject paths;
    try {
        apiJson = (JSONObject) jsonParser.parse(apiDefinition);
        paths = (JSONObject) jsonParser.parse(RestApiPublisherUtils.getSOAPOperation());
        apiJson.replace("paths", paths);
        return apiJson.toJSONString();
    } catch (ParseException e) {
        throw new APIManagementException("Error while parsing the api definition.", e);
    }
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)32 HashMap (java.util.HashMap)30 APIDefinition (org.wso2.carbon.apimgt.api.APIDefinition)30 API (org.wso2.carbon.apimgt.core.models.API)30 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)25 ArrayList (java.util.ArrayList)23 API (org.wso2.carbon.apimgt.api.model.API)20 Map (java.util.Map)19 IOException (java.io.IOException)18 Test (org.testng.annotations.Test)18 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)18 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)16 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)15 Scope (org.wso2.carbon.apimgt.api.model.Scope)15 HashSet (java.util.HashSet)13 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)13 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)12 SwaggerData (org.wso2.carbon.apimgt.api.model.SwaggerData)12 BusinessInformation (org.wso2.carbon.apimgt.core.models.BusinessInformation)12 CorsConfiguration (org.wso2.carbon.apimgt.core.models.CorsConfiguration)12