Search in sources :

Example 26 with APIDefinitionFromSwagger20

use of org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20 in project carbon-apimgt by wso2.

the class APIStoreImpl method addCompositeApiFromDefinition.

/**
 * {@inheritDoc}
 */
@Override
public String addCompositeApiFromDefinition(InputStream apiDefinition) throws APIManagementException {
    try {
        String apiDefinitionString = IOUtils.toString(apiDefinition);
        CompositeAPI.Builder apiBuilder = apiDefinitionFromSwagger20.generateCompositeApiFromSwaggerResource(getUsername(), apiDefinitionString);
        apiBuilder.apiDefinition(apiDefinitionString);
        addCompositeApi(apiBuilder);
        return apiBuilder.getId();
    } catch (IOException e) {
        throw new APIManagementException("Couldn't Generate ApiDefinition from file", ExceptionCodes.API_DEFINITION_MALFORMED);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IOException(java.io.IOException)

Example 27 with APIDefinitionFromSwagger20

use of org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20 in project carbon-apimgt by wso2.

the class AuthenticatorService method getApplicationScopes.

/**
 * This method returns the scopes for a given application.
 *
 * @param appName Name the application
 * @return scopes - A space separated list of scope keys
 * @throws APIManagementException When retrieving scopes from swagger definition fails
 */
private String getApplicationScopes(String appName) throws APIManagementException {
    String scopes;
    String applicationRestAPI = null;
    if (AuthenticatorConstants.STORE_APPLICATION.equals(appName)) {
        applicationRestAPI = RestApiUtil.getStoreRestAPIResource();
    } else if (AuthenticatorConstants.PUBLISHER_APPLICATION.equals(appName)) {
        applicationRestAPI = RestApiUtil.getPublisherRestAPIResource();
    } else if (AuthenticatorConstants.ADMIN_APPLICATION.equals(appName)) {
        applicationRestAPI = RestApiUtil.getAdminRestAPIResource();
    }
    try {
        if (applicationRestAPI != null) {
            APIDefinition apiDefinitionFromSwagger20 = new APIDefinitionFromSwagger20();
            Map<String, Scope> applicationScopesMap = apiDefinitionFromSwagger20.getScopesFromSecurityDefinitionForWebApps(applicationRestAPI);
            scopes = String.join(" ", applicationScopesMap.keySet());
            // Set openid scope
            if (StringUtils.isEmpty(scopes)) {
                scopes = KeyManagerConstants.OPEN_ID_CONNECT_SCOPE;
            } else {
                scopes = scopes + " " + KeyManagerConstants.OPEN_ID_CONNECT_SCOPE;
            }
        } else {
            String errorMsg = "Error while getting application rest API resource.";
            log.error(errorMsg, ExceptionCodes.INTERNAL_ERROR);
            throw new APIManagementException(errorMsg, ExceptionCodes.INTERNAL_ERROR);
        }
    } catch (APIManagementException e) {
        String errorMsg = "Error while reading scopes from swagger definition.";
        log.error(errorMsg, e, ExceptionCodes.INTERNAL_ERROR);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.INTERNAL_ERROR);
    }
    return scopes;
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.core.api.APIDefinition) APIDefinitionFromSwagger20(org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)15 HashMap (java.util.HashMap)12 API (org.wso2.carbon.apimgt.core.models.API)12 Test (org.testng.annotations.Test)10 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)9 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)8 Scope (org.wso2.carbon.apimgt.core.models.Scope)7 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)5 IOException (java.io.IOException)4 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)4 GatewayException (org.wso2.carbon.apimgt.core.exception.GatewayException)4 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)4 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)4 LocalDateTime (java.time.LocalDateTime)3 Map (java.util.Map)3 ParseException (org.json.simple.parser.ParseException)3 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)3 APIDefinitionFromSwagger20 (org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20)3 LifecycleException (org.wso2.carbon.lcm.core.exception.LifecycleException)3 FileInputStream (java.io.FileInputStream)2