Search in sources :

Example 6 with KeyMgtConfigurations

use of org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdScopesPost.

@Override
public Response apisApiIdScopesPost(String apiId, ScopeDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        KeyMgtConfigurations keyManagerConfiguration = APIMConfigurationService.getInstance().getApimConfigurations().getKeyManagerConfigs();
        if (body.getBindings() != null && StringUtils.isNotEmpty(body.getBindings().getType())) {
            if (!keyManagerConfiguration.getScopeBindingType().equalsIgnoreCase(body.getBindings().getType())) {
                String message = "binding type not valid";
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(message, 900313L, message);
                return Response.status(Response.Status.PRECONDITION_FAILED).entity(errorDTO).build();
            }
        }
        Scope scope = MappingUtil.toScope(body);
        apiPublisher.addScopeToTheApi(apiId, scope);
        URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + "/scopes/" + scope.getName());
        return Response.created(location).entity(body).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while creating scope" + body.getName();
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        paramList.put(APIMgtConstants.ExceptionsConstants.SCOPE_NAME, body.getName());
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving source URI location of " + body.getName();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
        log.error(errorMessage, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
    }
}
Also used : KeyMgtConfigurations(org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations) Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

KeyMgtConfigurations (org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations)6 HashMap (java.util.HashMap)4 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)2 Scope (org.wso2.carbon.apimgt.core.models.Scope)2 Swagger (io.swagger.models.Swagger)1 ApiKeyAuthDefinition (io.swagger.models.auth.ApiKeyAuthDefinition)1 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)1 SwaggerParser (io.swagger.parser.SwaggerParser)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.testng.annotations.Test)1 APIMConfigurations (org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations)1 APIDefinitionFromSwagger20 (org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20)1 WSO2ISKeyManagerImpl (org.wso2.carbon.apimgt.core.impl.WSO2ISKeyManagerImpl)1 ScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.ScopeDTO)1 ConfigProvider (org.wso2.carbon.config.provider.ConfigProvider)1