use of org.wso2.carbon.apimgt.core.dao.FunctionDAO in project carbon-apimgt by wso2.
the class DAOFactory method getAnalyticsDAO.
/**
* To get the AnalyticsDao object. Depends on different vendors.
*
* @return AnalyticsDAO object
* @throws APIMgtDAOException if error during getting analytics database connection
*/
public static AnalyticsDAO getAnalyticsDAO() throws APIMgtDAOException {
AnalyticsDAO analyticsDAO;
boolean isAnalyticsEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getAnalyticsConfigurations().isEnabled();
if (isAnalyticsEnabled) {
try (Connection connection = DAOUtil.getAnalyticsConnection()) {
analyticsDAO = getAnalyticsDaoImplForVendor(connection);
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
}
} else {
// if analytics is not enabled create a normal AMDB data connection to check db driver
try (Connection connection = DAOUtil.getConnection()) {
analyticsDAO = getAnalyticsDaoImplForVendor(connection);
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
}
}
return analyticsDAO;
}
use of org.wso2.carbon.apimgt.core.dao.FunctionDAO in project carbon-apimgt by wso2.
the class DAOFactory method getFunctionDAO.
/**
* To get the FunctionDAO object. Depends on different vendors.
*
* @return FunctionDAO object
* @throws APIMgtDAOException In case of unhandled DB type or SQLException
*/
public static FunctionDAO getFunctionDAO() throws APIMgtDAOException {
FunctionDAO functionDAO = null;
try (Connection connection = DAOUtil.getConnection()) {
String driverName = connection.getMetaData().getDriverName();
if (driverName.contains(MYSQL) || driverName.contains(H2)) {
functionDAO = new FunctionDAOImpl();
} else if (driverName.contains(DB2)) {
} else if (driverName.contains(MS_SQL) || driverName.contains(MICROSOFT)) {
functionDAO = new FunctionDAOImpl();
} else if (driverName.contains(POSTGRE)) {
functionDAO = new FunctionDAOImpl();
} else if (driverName.contains(ORACLE)) {
functionDAO = new FunctionDAOImpl();
} else {
throw new APIMgtDAOException("Unhandled DB driver: " + driverName + " detected", ExceptionCodes.APIM_DAO_EXCEPTION);
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
}
setup();
return functionDAO;
}
use of org.wso2.carbon.apimgt.core.dao.FunctionDAO in project carbon-apimgt by wso2.
the class FunctionTrigger method captureEvent.
/**
* Used to observe all the {@link org.wso2.carbon.apimgt.core.models.Event} occurrences
* in an {@link org.wso2.carbon.apimgt.core.api.APIMObservable} object and trigger corresponding function that is
* mapped to the particular {@link org.wso2.carbon.apimgt.core.models.Event} occurred.
* <p>
* This is a specific implementation for
* {@link org.wso2.carbon.apimgt.core.api.EventObserver#captureEvent(Event, String, ZonedDateTime, Map)} method,
* provided by {@link org.wso2.carbon.apimgt.core.impl.FunctionTrigger} which implements
* {@link org.wso2.carbon.apimgt.core.api.EventObserver} interface.
* <p>
* {@inheritDoc}
*
* @see org.wso2.carbon.apimgt.core.impl.EventLogger#captureEvent(Event, String, ZonedDateTime, Map)
*/
@Override
public void captureEvent(Event event, String username, ZonedDateTime eventTime, Map<String, String> metadata) {
List<Function> functions = null;
String jsonPayload = null;
if (event == null) {
throw new IllegalArgumentException("Event must not be null");
}
if (username == null) {
throw new IllegalArgumentException("Username must not be null");
}
if (eventTime == null) {
throw new IllegalArgumentException("Event_time must not be null");
}
if (metadata == null) {
throw new IllegalArgumentException("Payload must not be null");
}
// Add general attributes to payload
metadata.put(APIMgtConstants.FunctionsConstants.EVENT, event.getEventAsString());
metadata.put(APIMgtConstants.FunctionsConstants.COMPONENT, event.getComponent().getComponentAsString());
metadata.put(APIMgtConstants.FunctionsConstants.USERNAME, username);
metadata.put(APIMgtConstants.FunctionsConstants.EVENT_TIME, eventTime.toString());
try {
functions = functionDAO.getUserFunctionsForEvent(username, event);
jsonPayload = new Gson().toJson(metadata);
} catch (APIMgtDAOException e) {
String message = "Error loading functions for event from DB: -event: " + event + " -Username: " + username;
log.error(message, new APIManagementException("Problem invoking 'getUserFunctionsForEvent' method in " + "'FunctionDAO' ", e, ExceptionCodes.APIMGT_DAO_EXCEPTION));
}
if (functions != null && !functions.isEmpty()) {
for (Function function : functions) {
HttpResponse response = null;
try {
response = restCallUtil.postRequest(function.getEndpointURI(), null, null, Entity.json(jsonPayload), MediaType.APPLICATION_JSON_TYPE, Collections.EMPTY_MAP);
} catch (APIManagementException e) {
log.error("Failed to make http request: -function: " + function.getName() + " -endpoint URI: " + function.getEndpointURI() + " -event: " + event + " -Username: " + username, e);
}
if (response != null) {
int responseStatusCode = response.getResponseCode();
// Benefit of integer division used to ensure all possible success response codes covered
if (responseStatusCode / 100 == 2) {
log.info("Function successfully invoked: " + function.getName() + " -event: " + event + " -Username: " + username + " -Response code: " + responseStatusCode);
} else {
log.error("Problem invoking function: " + function.getName() + " -event: " + event + " -Username: " + username + " -Response code: " + responseStatusCode);
}
}
}
}
}
use of org.wso2.carbon.apimgt.core.dao.FunctionDAO in project carbon-apimgt by wso2.
the class FunctionDAOImplIT method testUpdateGetUserDeployedFunctions.
@Test
public void testUpdateGetUserDeployedFunctions() throws Exception {
FunctionDAO functionDAO = DAOFactory.getFunctionDAO();
// validate getUserDeployedFunctions() args
try {
functionDAO.getUserDeployedFunctions(null);
Assert.fail("Expected IllegalArgumentException when username is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Username must not be null"));
}
Function function1 = SampleTestObjectCreator.createDefaultFunction();
Function function2 = SampleTestObjectCreator.createAlternativeFunction();
Function function3 = SampleTestObjectCreator.createAlternativeFunction2();
// Validate updateUserDeployedFunctions() args
try {
functionDAO.updateUserDeployedFunctions(null, Arrays.asList(function1, function2));
Assert.fail("Expected IllegalArgumentException when username is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Username must not be null"));
}
try {
functionDAO.updateUserDeployedFunctions(ADMIN, null);
Assert.fail("Expected IllegalArgumentException when function list is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Functions(List) must not be null"));
}
functionDAO.updateUserDeployedFunctions(ADMIN, Arrays.asList(function1, function2));
List<Function> functionListFromDB = functionDAO.getUserDeployedFunctions(ADMIN);
Assert.assertEquals(functionListFromDB.size(), 2);
Assert.assertTrue(functionListFromDB.contains(function1));
Assert.assertTrue(functionListFromDB.contains(function2));
functionDAO.updateUserDeployedFunctions(ADMIN, Arrays.asList(function1, function3));
List<Function> functionListFromDBUpdated = functionDAO.getUserDeployedFunctions(ADMIN);
Assert.assertEquals(functionListFromDBUpdated.size(), 2);
Assert.assertTrue(functionListFromDBUpdated.contains(function1));
Assert.assertTrue(functionListFromDBUpdated.contains(function3));
Assert.assertFalse(functionListFromDBUpdated.contains(function2));
}
use of org.wso2.carbon.apimgt.core.dao.FunctionDAO in project carbon-apimgt by wso2.
the class FunctionDAOImplIT method testAddGetDeleteEventFunctionMapping.
@Test
public void testAddGetDeleteEventFunctionMapping() throws Exception {
FunctionDAO functionDAO = DAOFactory.getFunctionDAO();
Function function1 = SampleTestObjectCreator.createDefaultFunction();
Function function2 = SampleTestObjectCreator.createAlternativeFunction();
Function function3 = SampleTestObjectCreator.createAlternativeFunction2();
functionDAO.updateUserDeployedFunctions(ADMIN, Arrays.asList(function1, function2, function3));
try {
functionDAO.addEventFunctionMapping(null, Event.API_CREATION, function1.getName());
Assert.fail("Expected IllegalArgumentException when username is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Username must not be null"));
}
try {
functionDAO.addEventFunctionMapping(ADMIN, null, function1.getName());
Assert.fail("Expected IllegalArgumentException when Event is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Event must not be null"));
}
try {
functionDAO.addEventFunctionMapping(ADMIN, Event.API_CREATION, null);
Assert.fail("Expected IllegalArgumentException when function name is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Function name must not be null"));
}
functionDAO.addEventFunctionMapping(ADMIN, Event.API_CREATION, function1.getName());
functionDAO.addEventFunctionMapping(ADMIN, Event.API_UPDATE, function1.getName());
functionDAO.addEventFunctionMapping(ADMIN, Event.APP_CREATION, function2.getName());
functionDAO.addEventFunctionMapping(ADMIN, Event.APP_MODIFICATION, function2.getName());
functionDAO.addEventFunctionMapping(ADMIN, Event.API_CREATION, function3.getName());
functionDAO.addEventFunctionMapping(ADMIN, Event.APP_CREATION, function3.getName());
// Validate getTriggersForUserFunction() args
try {
functionDAO.getTriggersForUserFunction(null, function3.getName());
Assert.fail("Expected IllegalArgumentException when username is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Username must not be null"));
}
try {
functionDAO.getTriggersForUserFunction(ADMIN, null);
Assert.fail("Expected IllegalArgumentException when function name is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Function name must not be null"));
}
// Check events for functions
List<Event> eventsFromDB = functionDAO.getTriggersForUserFunction(ADMIN, function3.getName());
Assert.assertEquals(eventsFromDB.size(), 2);
Assert.assertTrue(eventsFromDB.get(0).equals(Event.API_CREATION) || eventsFromDB.get(0).equals(Event.APP_CREATION));
Assert.assertTrue(eventsFromDB.get(1).equals(Event.API_CREATION) || eventsFromDB.get(1).equals(Event.APP_CREATION));
// Validate getUserFunctionsForEvent() args
try {
functionDAO.getUserFunctionsForEvent(null, Event.API_UPDATE);
Assert.fail("Expected IllegalArgumentException when username is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Username must not be null"));
}
try {
functionDAO.getUserFunctionsForEvent(ADMIN, null);
Assert.fail("Expected IllegalArgumentException when Event is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Event must not be null"));
}
// Check functions for events
List<Function> functionsForAPIUpdateFromDB = functionDAO.getUserFunctionsForEvent(ADMIN, Event.API_UPDATE);
Assert.assertEquals(functionsForAPIUpdateFromDB.size(), 1);
Assert.assertTrue(functionsForAPIUpdateFromDB.get(0).equals(function1));
List<Function> functionsForAPICreateFromDB = functionDAO.getUserFunctionsForEvent(ADMIN, Event.API_CREATION);
Assert.assertEquals(functionsForAPICreateFromDB.size(), 2);
Assert.assertTrue(functionsForAPICreateFromDB.get(0).equals(function1) || functionsForAPICreateFromDB.get(0).equals(function3));
Assert.assertTrue(functionsForAPICreateFromDB.get(1).equals(function1) || functionsForAPICreateFromDB.get(1).equals(function3));
// Validate deleteEventFunctionMapping() args
try {
functionDAO.deleteEventFunctionMapping(null, Event.API_CREATION, function1.getName());
Assert.fail("Expected IllegalArgumentException when username is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Username must not be null"));
}
try {
functionDAO.deleteEventFunctionMapping(ADMIN, null, function1.getName());
Assert.fail("Expected IllegalArgumentException when Event is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Event must not be null"));
}
try {
functionDAO.deleteEventFunctionMapping(ADMIN, Event.API_CREATION, null);
Assert.fail("Expected IllegalArgumentException when function name is null.");
} catch (IllegalArgumentException e) {
Assert.assertTrue(e.getMessage().contains("Function name must not be null"));
}
// Check deletion
functionDAO.deleteEventFunctionMapping(ADMIN, Event.API_CREATION, function3.getName());
List<Function> functionsForAPICreateFromDBAfterDeletion = functionDAO.getUserFunctionsForEvent(ADMIN, Event.API_CREATION);
Assert.assertEquals(functionsForAPICreateFromDBAfterDeletion.size(), 1);
Assert.assertEquals(functionsForAPICreateFromDBAfterDeletion.get(0), function1);
}
Aggregations