Search in sources :

Example 6 with KeyManagementException

use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.

the class APIPublisherImpl method deleteAPI.

/**
 * Delete an API
 *
 * @param identifier UUID of the API.
 * @throws APIManagementException if failed to remove the API
 */
@Override
public void deleteAPI(String identifier) throws APIManagementException {
    APIGateway gateway = getApiGateway();
    try {
        if (getAPISubscriptionCountByAPI(identifier) == 0) {
            API api = getAPIbyUUID(identifier);
            // Checks whether the user has required permissions to delete the API
            verifyUserPermissionsToDeleteAPI(getUsername(), api);
            String apiWfStatus = api.getWorkflowStatus();
            API.APIBuilder apiBuilder = new API.APIBuilder(api);
            // Delete API in gateway
            gateway.deleteAPI(api);
            if (log.isDebugEnabled()) {
                log.debug("API : " + api.getName() + " has been successfully removed from the gateway");
            }
            if (!getApiDAO().isAPIVersionsExist(api.getName())) {
                Map<String, String> scopeMap = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(getApiSwaggerDefinition(identifier));
                for (String scope : scopeMap.keySet()) {
                    try {
                        getKeyManager().deleteScope(scope);
                    } catch (KeyManagementException e) {
                        // We ignore the error due to if scope get deleted from other end that will affect to delete
                        // api.
                        log.warn("Scope couldn't delete from Key Manager", e);
                    }
                }
            }
            getApiDAO().deleteAPI(identifier);
            // Deleting API specific endpoints
            if (api.getEndpoint() != null) {
                for (Map.Entry<String, Endpoint> entry : api.getEndpoint().entrySet()) {
                    Endpoint endpoint = entry.getValue();
                    if (APIMgtConstants.API_SPECIFIC_ENDPOINT.equals(endpoint.getApplicableLevel())) {
                        deleteEndpoint(endpoint.getId());
                    }
                }
            }
            getApiLifecycleManager().removeLifecycle(apiBuilder.getLifecycleInstanceId());
            APIUtils.logDebug("API with id " + identifier + " was deleted successfully.", log);
            if (APILCWorkflowStatus.PENDING.toString().equals(apiWfStatus)) {
                cleanupPendingTaskForAPIStateChange(identifier);
            }
            // 'API_M Functions' related code
            // Create a payload with event specific details
            Map<String, String> eventPayload = new HashMap<>();
            eventPayload.put(APIMgtConstants.FunctionsConstants.API_ID, api.getId());
            eventPayload.put(APIMgtConstants.FunctionsConstants.API_NAME, api.getName());
            eventPayload.put(APIMgtConstants.FunctionsConstants.API_VERSION, api.getVersion());
            eventPayload.put(APIMgtConstants.FunctionsConstants.API_PROVIDER, api.getProvider());
            eventPayload.put(APIMgtConstants.FunctionsConstants.API_DESCRIPTION, api.getDescription());
            // This will notify all the EventObservers(Asynchronous)
            ObserverNotifier observerNotifier = new ObserverNotifier(Event.API_DELETION, getUsername(), ZonedDateTime.now(ZoneOffset.UTC), eventPayload, this);
            ObserverNotifierThreadPool.getInstance().executeTask(observerNotifier);
        } else {
            throw new ApiDeleteFailureException("API with " + identifier + " already have subscriptions");
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while deleting the API with id " + identifier;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (LifecycleException e) {
        String errorMsg = "Error occurred while Disassociating the API with Lifecycle id " + identifier;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
    } catch (GatewayException e) {
        String message = "Error occurred while deleting API with id - " + identifier + " from gateway";
        log.error(message, e);
        throw new APIManagementException(message, ExceptionCodes.GATEWAY_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) LifecycleException(org.wso2.carbon.lcm.core.exception.LifecycleException) ApiDeleteFailureException(org.wso2.carbon.apimgt.core.exception.ApiDeleteFailureException) HashMap(java.util.HashMap) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with KeyManagementException

use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.

the class DefaultScopeRegistrationImplTest method testUpdateScopeWhileAuthorizationServerThrowsInternalError.

@Test
public void testUpdateScopeWhileAuthorizationServerThrowsInternalError() {
    DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
    DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
    ScopeInfo scopeInfo = new ScopeInfo();
    scopeInfo.setDescription("cde");
    scopeInfo.setDisplayName("abc");
    Scope scope = new Scope();
    scope.setName("abc");
    scope.setDescription("cde");
    Mockito.when(defaultScopeRegistrationServiceStub.updateScope(Mockito.any(ScopeInfo.class), Mockito.anyString())).thenReturn(Response.builder().status(500).headers(new HashMap<>()).build());
    try {
        defaultScopeRegistration.updateScope(scope);
    } catch (KeyManagementException e) {
        Assert.assertEquals(e.getErrorHandler().getErrorCode(), 900967);
    }
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) ScopeInfo(org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException) Test(org.testng.annotations.Test)

Example 8 with KeyManagementException

use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.

the class DefaultScopeRegistrationImplTest method testRetrievescope.

@Test
public void testRetrievescope() throws KeyManagementException {
    DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
    DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
    ScopeInfo scopeInfo = new ScopeInfo();
    scopeInfo.setName("abc");
    scopeInfo.setDescription("cde");
    Mockito.when(defaultScopeRegistrationServiceStub.getScopeByName(scopeInfo.getName())).thenReturn(Response.builder().status(200).headers(new HashMap<>()).body(new Gson().toJson(scopeInfo), feign.Util.UTF_8).build());
    Scope retrievedScope = defaultScopeRegistration.getScopeByName(scopeInfo.getName());
    Assert.assertEquals(scopeInfo.getName(), retrievedScope.getName());
    Assert.assertEquals(scopeInfo.getDescription(), retrievedScope.getDescription());
    Assert.assertEquals(Collections.emptyList(), retrievedScope.getBindings());
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) ScopeInfo(org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo) Test(org.testng.annotations.Test)

Example 9 with KeyManagementException

use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.

the class DefaultScopeRegistrationImplTest method testRetrieveScopeWithBindings.

@Test
public void testRetrieveScopeWithBindings() throws KeyManagementException {
    DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
    DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
    ScopeInfo scopeInfo = new ScopeInfo();
    scopeInfo.setName("abc");
    scopeInfo.setDescription("cde");
    List<String> list = new ArrayList<>();
    list.add("apim:api_create");
    scopeInfo.setBindings(list);
    Mockito.when(defaultScopeRegistrationServiceStub.getScopeByName(scopeInfo.getName())).thenReturn(Response.builder().status(200).headers(new HashMap<>()).body(new Gson().toJson(scopeInfo), feign.Util.UTF_8).build());
    Scope retrievedScope = defaultScopeRegistration.getScopeByName(scopeInfo.getName());
    Assert.assertEquals(scopeInfo.getName(), retrievedScope.getName());
    Assert.assertEquals(scopeInfo.getDescription(), retrievedScope.getDescription());
    Assert.assertEquals(list, retrievedScope.getBindings());
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) ScopeInfo(org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo) Test(org.testng.annotations.Test)

Example 10 with KeyManagementException

use of org.wso2.carbon.apimgt.core.exception.KeyManagementException in project carbon-apimgt by wso2.

the class DefaultScopeRegistrationImplTest method testRegisterScope.

@Test
public void testRegisterScope() throws KeyManagementException {
    DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
    DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
    Scope scopeInfo = new Scope();
    scopeInfo.setName("abc");
    scopeInfo.setDescription("cde");
    Mockito.when(defaultScopeRegistrationServiceStub.registerScope(getScopeInfo(scopeInfo))).thenReturn(Response.builder().status(201).headers(new HashMap<>()).body(new Gson().toJson(scopeInfo), feign.Util.UTF_8).build());
    Scope scope = new Scope();
    scope.setName("abc");
    scope.setDescription("cde");
    Assert.assertTrue(defaultScopeRegistration.registerScope(scope));
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) Test(org.testng.annotations.Test)

Aggregations

KeyManagementException (org.wso2.carbon.apimgt.core.exception.KeyManagementException)24 Response (feign.Response)16 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)11 OAuth2IntrospectionResponse (org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse)11 Gson (com.google.gson.Gson)10 ScopeInfo (org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo)9 DCRMServiceStub (org.wso2.carbon.apimgt.core.auth.DCRMServiceStub)7 OAuth2ServiceStubs (org.wso2.carbon.apimgt.core.auth.OAuth2ServiceStubs)7 ScopeRegistration (org.wso2.carbon.apimgt.core.auth.ScopeRegistration)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 OAuthApplicationInfo (org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)6 Scope (org.wso2.carbon.apimgt.core.models.Scope)6 DCRClientInfo (org.wso2.carbon.apimgt.core.auth.dto.DCRClientInfo)5 AccessTokenInfo (org.wso2.carbon.apimgt.core.models.AccessTokenInfo)5 GsonDecoder (feign.gson.GsonDecoder)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 AccessTokenRequest (org.wso2.carbon.apimgt.core.models.AccessTokenRequest)3 DCRError (org.wso2.carbon.apimgt.core.auth.dto.DCRError)2