Search in sources :

Example 86 with Subscriber

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

the class AbstractApplicationRegistrationWorkflowExecutor method dogenerateKeysForApplication.

public static void dogenerateKeysForApplication(ApplicationRegistrationWorkflowDTO workflowDTO) throws APIManagementException {
    log.debug("Registering Application and creating an Access Token... ");
    Application application = workflowDTO.getApplication();
    Subscriber subscriber = application.getSubscriber();
    ApiMgtDAO dao = ApiMgtDAO.getInstance();
    if (subscriber == null || workflowDTO.getAllowedDomains() == null) {
        dao.populateAppRegistrationWorkflowDTO(workflowDTO);
    }
    try {
        // get new key manager
        // Here the default flow is set expecting an ID as the keymanager as this flow only involves new applications
        String keyManagerId = workflowDTO.getKeyManager();
        KeyManagerConfigurationDTO km = dao.getKeyManagerConfigurationByUUID(keyManagerId);
        String tenantDomain = km.getOrganization();
        String keyManagerName = km.getName();
        KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(tenantDomain, keyManagerName);
        if (keyManager == null) {
            throw new APIManagementException("Key Manager " + keyManagerName + " not configured");
        }
        workflowDTO.getAppInfoDTO().getOAuthApplicationInfo().setClientName(application.getName());
        // set applications attributes to the oAuthApplicationInfo
        workflowDTO.getAppInfoDTO().getOAuthApplicationInfo().putAllAppAttributes(application.getApplicationAttributes());
        // createApplication on oAuthorization server.
        OAuthApplicationInfo oAuthApplication = keyManager.createApplication(workflowDTO.getAppInfoDTO());
        // update associateApplication
        ApplicationUtils.updateOAuthAppAssociation(application, workflowDTO.getKeyType(), oAuthApplication, keyManagerId);
        // change create application status in to completed.
        dao.updateApplicationRegistration(APIConstants.AppRegistrationStatus.REGISTRATION_COMPLETED, workflowDTO.getKeyType(), workflowDTO.getApplication().getId(), keyManagerId);
        workflowDTO.setApplicationInfo(oAuthApplication);
        AccessTokenInfo tokenInfo;
        Object enableTokenGeneration = keyManager.getKeyManagerConfiguration().getParameter(APIConstants.KeyManager.ENABLE_TOKEN_GENERATION);
        if (enableTokenGeneration != null && (Boolean) enableTokenGeneration && oAuthApplication.getJsonString().contains(APIConstants.GRANT_TYPE_CLIENT_CREDENTIALS)) {
            AccessTokenRequest tokenRequest = ApplicationUtils.createAccessTokenRequest(keyManager, oAuthApplication, null);
            tokenInfo = keyManager.getNewApplicationAccessToken(tokenRequest);
        } else {
            tokenInfo = new AccessTokenInfo();
            tokenInfo.setAccessToken("");
            tokenInfo.setValidityPeriod(0L);
            String[] noScopes = new String[] { "N/A" };
            tokenInfo.setScope(noScopes);
            oAuthApplication.addParameter("tokenScope", Arrays.toString(noScopes));
        }
        workflowDTO.setAccessTokenInfo(tokenInfo);
    } catch (Exception e) {
        APIUtil.handleException("Error occurred while executing SubscriberKeyMgtClient.", e);
    }
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) AccessTokenRequest(org.wso2.carbon.apimgt.api.model.AccessTokenRequest) Application(org.wso2.carbon.apimgt.api.model.Application) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 87 with Subscriber

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

the class SubscriptionUpdateApprovalWorkflowExecutor method execute.

/**
 * Execute the Application Creation workflow approval process.
 *
 * @param workflowDTO
 */
@Override
public WorkflowResponse execute(WorkflowDTO workflowDTO) throws WorkflowException {
    if (log.isDebugEnabled()) {
        log.debug("Executing Subscription Update Webservice Workflow.. ");
    }
    SubscriptionWorkflowDTO subsWorkflowDTO = (SubscriptionWorkflowDTO) workflowDTO;
    String message = "Approve API " + subsWorkflowDTO.getApiName() + " - " + subsWorkflowDTO.getApiVersion() + " subscription creation request from subscriber - " + subsWorkflowDTO.getSubscriber() + " for the application - " + subsWorkflowDTO.getApplicationName();
    workflowDTO.setWorkflowDescription(message);
    workflowDTO.setProperties("apiName", subsWorkflowDTO.getApiName());
    workflowDTO.setProperties("apiVersion", subsWorkflowDTO.getApiVersion());
    workflowDTO.setProperties("subscriber", subsWorkflowDTO.getSubscriber());
    workflowDTO.setProperties("applicationName", subsWorkflowDTO.getApplicationName());
    workflowDTO.setProperties("currentTier", subsWorkflowDTO.getTierName());
    workflowDTO.setProperties("requestedTier", subsWorkflowDTO.getRequestedTierName());
    super.execute(workflowDTO);
    return new GeneralWorkflowResponse();
}
Also used : SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)

Example 88 with Subscriber

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

the class ApplicationCreationWSWorkflowExecutorTest method testWorkflowExecuteFailWhenMessageProcessingFailed.

@Test
public void testWorkflowExecuteFailWhenMessageProcessingFailed() throws Exception {
    ApplicationWorkflowDTO workflowDTO = new ApplicationWorkflowDTO();
    PowerMockito.mockStatic(AXIOMUtil.class);
    PowerMockito.when(AXIOMUtil.stringToOM(Mockito.anyString())).thenThrow(new XMLStreamException("Error " + "converting String to OMElement"));
    Application application = new Application("TestAPP", new Subscriber(null));
    application.setTier("Gold");
    application.setCallbackUrl("www.wso2.com");
    application.setDescription("Description");
    workflowDTO.setApplication(application);
    workflowDTO.setTenantDomain("wso2");
    workflowDTO.setUserName("admin");
    workflowDTO.setCallbackUrl("http://localhost:8280/workflow-callback");
    workflowDTO.setWorkflowReference("1");
    workflowDTO.setExternalWorkflowReference(UUID.randomUUID().toString());
    PowerMockito.doNothing().when(apiMgtDAO).updateSubscriptionStatus(Integer.parseInt(workflowDTO.getWorkflowReference()), APIConstants.SubscriptionStatus.REJECTED);
    ServiceReferenceHolderMockCreator serviceRefMock = new ServiceReferenceHolderMockCreator(-1234);
    ServiceReferenceHolderMockCreator.initContextService();
    PowerMockito.whenNew(ServiceClient.class).withArguments(Mockito.any(ConfigurationContext.class), Mockito.any(AxisService.class)).thenReturn(serviceClient);
    try {
        applicationCreationWSWorkflowExecutor.execute(workflowDTO);
        Assert.fail("Unexpected WorkflowException occurred while executing Application creation ws workflow");
    } catch (WorkflowException e) {
        Assert.assertEquals(e.getMessage(), "Error converting String to OMElement");
    }
}
Also used : ServiceReferenceHolderMockCreator(org.wso2.carbon.apimgt.impl.ServiceReferenceHolderMockCreator) XMLStreamException(javax.xml.stream.XMLStreamException) ApplicationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Application(org.wso2.carbon.apimgt.api.model.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 89 with Subscriber

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

the class ApplicationCreationWSWorkflowExecutorTest method testWorkflowExecuteWithDifferentMediatype.

@Test
public void testWorkflowExecuteWithDifferentMediatype() throws Exception {
    ApplicationWorkflowDTO workflowDTO = new ApplicationWorkflowDTO();
    Application application = new Application("TestAPP", new Subscriber(null));
    application.setTier("Gold");
    application.setCallbackUrl("www.wso2.com");
    application.setDescription("Description");
    workflowDTO.setApplication(application);
    workflowDTO.setTenantDomain("wso2");
    workflowDTO.setUserName("admin");
    workflowDTO.setCallbackUrl("http://localhost:8280/workflow-callback");
    workflowDTO.setWorkflowReference("1");
    workflowDTO.setExternalWorkflowReference(UUID.randomUUID().toString());
    PowerMockito.doNothing().when(apiMgtDAO).updateSubscriptionStatus(Integer.parseInt(workflowDTO.getWorkflowReference()), APIConstants.SubscriptionStatus.REJECTED);
    ServiceReferenceHolderMockCreator serviceRefMock = new ServiceReferenceHolderMockCreator(-1234);
    ServiceReferenceHolderMockCreator.initContextService();
    PowerMockito.whenNew(ServiceClient.class).withArguments(Mockito.any(ConfigurationContext.class), Mockito.any(AxisService.class)).thenReturn(serviceClient);
    applicationCreationWSWorkflowExecutor.setContentType("application/xml");
    try {
        // shouldn't fail.
        Assert.assertNotNull(applicationCreationWSWorkflowExecutor.execute(workflowDTO));
    } catch (WorkflowException e) {
        Assert.fail("Unexpected WorkflowException occurred while executing Application creation ws workflow");
    }
}
Also used : ServiceReferenceHolderMockCreator(org.wso2.carbon.apimgt.impl.ServiceReferenceHolderMockCreator) ApplicationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Application(org.wso2.carbon.apimgt.api.model.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 90 with Subscriber

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

the class ApplicationRegistrationWSWorkflowExecutorTest method init.

@Before
public void init() throws Exception {
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    ConfigurationContextService configurationContextService = Mockito.mock(ConfigurationContextService.class);
    configurationContext = Mockito.mock(ConfigurationContext.class);
    PowerMockito.when(serviceReferenceHolder.getContextService()).thenReturn(configurationContextService);
    PowerMockito.when(configurationContextService.getClientConfigContext()).thenReturn(configurationContext);
    serviceClient = Mockito.mock(ServiceClient.class);
    PowerMockito.whenNew(ServiceClient.class).withAnyArguments().thenReturn(serviceClient);
    applicationRegistrationWSWorkflowExecutor = new ApplicationRegistrationWSWorkflowExecutor();
    apiMgtDAO = TestUtils.getApiMgtDAO();
    application = new Application("test", new Subscriber("testUser"));
    application.setCallbackUrl(callBaclURL);
    application.setTier("Unlimited");
    PowerMockito.mockStatic(KeyManagerHolder.class);
    keyManager = Mockito.mock(KeyManager.class);
    KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
    Mockito.when(keyManager.getKeyManagerConfiguration()).thenReturn(keyManagerConfiguration);
    PowerMockito.when(KeyManagerHolder.getKeyManagerInstance("carbon.super", "default")).thenReturn(keyManager);
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    PowerMockito.when(keyManager.createApplication((OAuthAppRequest) Mockito.anyObject())).thenReturn(oAuthApplicationInfo);
    OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
    oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
    workflowDTO = new ApplicationRegistrationWorkflowDTO();
    workflowDTO.setWorkflowReference("1");
    workflowDTO.setApplication(application);
    workflowDTO.setCallbackUrl(callBaclURL);
    workflowDTO.setTenantDomain("carbon.super");
    workflowDTO.setUserName("testUser");
    workflowDTO.setExternalWorkflowReference("testUser");
    workflowDTO.setKeyType("PRODUCTION");
    workflowDTO.setAppInfoDTO(oAuthAppRequest);
    KeyManagerConfigurationDTO kmConfigDTO = new KeyManagerConfigurationDTO();
    kmConfigDTO.setOrganization("carbon.super");
    kmConfigDTO.setName("default");
    PowerMockito.when(apiMgtDAO.getKeyManagerConfigurationByUUID("default")).thenReturn(kmConfigDTO);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) ApplicationRegistrationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) ServiceClient(org.apache.axis2.client.ServiceClient) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) Application(org.wso2.carbon.apimgt.api.model.Application) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) KeyManagerConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration) Before(org.junit.Before)

Aggregations

Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)98 Test (org.junit.Test)64 Application (org.wso2.carbon.apimgt.api.model.Application)63 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)60 PreparedStatement (java.sql.PreparedStatement)39 SQLException (java.sql.SQLException)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)39 ResultSet (java.sql.ResultSet)37 Connection (java.sql.Connection)31 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)25 Tier (org.wso2.carbon.apimgt.api.model.Tier)20 ArrayList (java.util.ArrayList)19 HashSet (java.util.HashSet)19 Date (java.util.Date)14 HashMap (java.util.HashMap)11 LinkedHashSet (java.util.LinkedHashSet)10 JSONObject (org.json.simple.JSONObject)10 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)10 TreeMap (java.util.TreeMap)9