use of org.wso2.carbon.apimgt.api.model.APIKey in project carbon-apimgt by wso2.
the class ApiMgtDAO method getKeyMappingFromApplicationIdAndKeyMappingId.
public APIKey getKeyMappingFromApplicationIdAndKeyMappingId(int applicationId, String keyMappingId) throws APIManagementException {
final String query = "SELECT UUID,CONSUMER_KEY,KEY_MANAGER,KEY_TYPE,STATE,CREATE_MODE FROM " + "AM_APPLICATION_KEY_MAPPING WHERE APPLICATION_ID=? AND UUID = ?";
Set<APIKey> apiKeyList = new HashSet<>();
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setInt(1, applicationId);
preparedStatement.setString(2, keyMappingId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
APIKey apiKey = new APIKey();
apiKey.setMappingId(resultSet.getString("UUID"));
apiKey.setConsumerKey(resultSet.getString("CONSUMER_KEY"));
apiKey.setKeyManager(resultSet.getString("KEY_MANAGER"));
apiKey.setType(resultSet.getString("KEY_TYPE"));
apiKey.setState(resultSet.getString("STATE"));
String createMode = resultSet.getString("CREATE_MODE");
if (StringUtils.isEmpty(createMode)) {
createMode = APIConstants.OAuthAppMode.CREATED.name();
}
apiKey.setCreateMode(createMode);
return apiKey;
}
}
} catch (SQLException e) {
throw new APIManagementException("Error while Retrieving Key Mapping ", e);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIKey in project carbon-apimgt by wso2.
the class ApiMgtDAO method getKeyMappingsFromApplicationId.
public Set<APIKey> getKeyMappingsFromApplicationId(int applicationId) throws APIManagementException {
Set<APIKey> apiKeyList = new HashSet<>();
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(SQLConstants.GET_KEY_MAPPING_INFO_FROM_APP_ID)) {
preparedStatement.setInt(1, applicationId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
APIKey apiKey = new APIKey();
apiKey.setMappingId(resultSet.getString("UUID"));
apiKey.setConsumerKey(resultSet.getString("CONSUMER_KEY"));
apiKey.setKeyManager(resultSet.getString("KEY_MANAGER"));
apiKey.setType(resultSet.getString("KEY_TYPE"));
apiKey.setState(resultSet.getString("STATE"));
String createMode = resultSet.getString("CREATE_MODE");
if (StringUtils.isEmpty(createMode)) {
createMode = APIConstants.OAuthAppMode.CREATED.name();
}
apiKey.setCreateMode(createMode);
try (InputStream appInfo = resultSet.getBinaryStream("APP_INFO")) {
if (appInfo != null) {
apiKey.setAppMetaData(IOUtils.toString(appInfo));
}
} catch (IOException e) {
log.error("Error while retrieving metadata", e);
}
apiKeyList.add(apiKey);
}
}
} catch (SQLException e) {
throw new APIManagementException("Error while Retrieving Key Mappings ", e);
}
return apiKeyList;
}
use of org.wso2.carbon.apimgt.api.model.APIKey in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetSubscribedIdentifiers.
@Test
public void testGetSubscribedIdentifiers() throws APIManagementException {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
Set<SubscribedAPI> originalSubscribedAPIs = new HashSet<>();
SubscribedAPI subscribedAPI = Mockito.mock(SubscribedAPI.class);
originalSubscribedAPIs.add(subscribedAPI);
Subscriber subscriber = new Subscriber("Subscriber");
APIIdentifier apiId1 = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
Tier tier = Mockito.mock(Tier.class);
when(apiMgtDAO.getSubscribedAPIs("testorg", subscriber, "testID")).thenReturn(originalSubscribedAPIs);
when(subscribedAPI.getTier()).thenReturn(tier);
when(tier.getName()).thenReturn("tier");
when(subscribedAPI.getApiId()).thenReturn(apiId1);
Application app = Mockito.mock(Application.class);
when(app.getId()).thenReturn(1);
when(subscribedAPI.getApplication()).thenReturn(app);
Set<APIKey> apiKeys = new HashSet<>();
APIKey apiKey = new APIKey();
apiKey.setType("Production");
apiKeys.add(apiKey);
Mockito.when(apiMgtDAO.getKeyMappingsFromApplicationId(Mockito.anyInt())).thenReturn(apiKeys);
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
accessTokenInfo.setAccessToken(UUID.randomUUID().toString());
Mockito.when(keyManager.getAccessTokenByConsumerKey(Mockito.anyString())).thenReturn(accessTokenInfo);
assertNotNull(apiConsumer.getSubscribedIdentifiers(subscriber, apiId1, "testID", "testorg"));
}
use of org.wso2.carbon.apimgt.api.model.APIKey in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetApplicationKeys.
@Test
public void testGetApplicationKeys() throws APIManagementException {
APIKey apiKey1 = new APIKey();
apiKey1.setConsumerKey(UUID.randomUUID().toString());
apiKey1.setType(APIConstants.API_KEY_TYPE_PRODUCTION);
apiKey1.setState(UUID.randomUUID().toString());
APIKey apiKey2 = new APIKey();
apiKey2.setConsumerKey(UUID.randomUUID().toString());
apiKey2.setType(APIConstants.API_KEY_TYPE_SANDBOX);
apiKey2.setState(UUID.randomUUID().toString());
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
Map<String, String> consumerKeyMap = new HashMap<>();
consumerKeyMap.put("default", apiKey1.getConsumerKey());
Set<APIKey> apiKeys = new HashSet<>();
apiKeys.add(apiKey1);
apiKeys.add(apiKey2);
Mockito.when(apiMgtDAO.getKeyMappingsFromApplicationId(Mockito.anyInt())).thenReturn(apiKeys);
Mockito.when(apiMgtDAO.getConsumerkeyByApplicationIdAndKeyType(Mockito.anyInt(), Mockito.anyString())).thenReturn(consumerKeyMap, consumerKeyMap);
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
accessTokenInfo.setAccessToken(UUID.randomUUID().toString());
Mockito.when(keyManager.getAccessTokenByConsumerKey(Mockito.anyString())).thenReturn(accessTokenInfo);
Mockito.when(keyManagerConfigurationDTO.isEnabled()).thenReturn(true);
assertNotNull(apiConsumer.getApplicationKeys(1));
assertEquals(apiConsumer.getApplicationKeys(1).size(), 2);
assertNotNull(apiConsumer.getApplicationKeys(1).iterator().next().getAccessToken());
}
use of org.wso2.carbon.apimgt.api.model.APIKey in project carbon-apimgt by wso2.
the class APIConsumerImpl method cleanUpApplicationRegistrationByApplicationIdAndKeyMappingId.
@Override
public void cleanUpApplicationRegistrationByApplicationIdAndKeyMappingId(int applicationId, String keyMappingId) throws APIManagementException {
APIKey apiKey = apiMgtDAO.getKeyMappingFromApplicationIdAndKeyMappingId(applicationId, keyMappingId);
if (apiKey != null) {
apiMgtDAO.deleteApplicationRegistration(applicationId, apiKey.getType(), apiKey.getKeyManager());
apiMgtDAO.deleteApplicationKeyMappingByMappingId(keyMappingId);
}
}
Aggregations