Search in sources :

Example 6 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class FunctionDAOImpl method getUserDeployedFunctions.

/**
 * {@inheritDoc}
 */
@Override
public List<Function> getUserDeployedFunctions(String userName) throws APIMgtDAOException {
    if (userName == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    List<Function> functions = new ArrayList<>();
    final String sqlQuery = "SELECT FUNCTION_NAME, FUNCTION_URI " + "FROM AM_LAMBDA_FUNCTION " + "WHERE USER_NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, userName);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String functionName = resultSet.getString("FUNCTION_NAME");
                URI endpointURI = new URI(resultSet.getString("FUNCTION_URI"));
                functions.add(new Function(functionName, endpointURI));
            }
        } catch (URISyntaxException e) {
            throw new APIMgtDAOException("Not a URI", e);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error retrieving functions of user: " + userName, e);
    }
    return functions;
}
Also used : Function(org.wso2.carbon.apimgt.core.models.Function) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 7 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class FunctionDAOImpl method getUserFunctionsForEvent.

/**
 * {@inheritDoc}
 */
@Override
public List<Function> getUserFunctionsForEvent(String userName, Event event) throws APIMgtDAOException {
    if (userName == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    if (event == null) {
        throw new IllegalArgumentException("Event must not be null");
    }
    List<Function> functions = new ArrayList<>();
    final String sqlQuery = "SELECT FUNCTION_NAME, FUNCTION_URI " + "FROM AM_LAMBDA_FUNCTION JOIN AM_EVENT_FUNCTION_MAPPING " + "ON AM_LAMBDA_FUNCTION.FUNCTION_ID = AM_EVENT_FUNCTION_MAPPING.FUNCTION_ID " + "WHERE USER_NAME = ? AND EVENT = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, userName);
        preparedStatement.setString(2, event.getEventAsString());
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String functionName = resultSet.getString("FUNCTION_NAME");
                URI endpointURI = new URI(resultSet.getString("FUNCTION_URI"));
                functions.add(new Function(functionName, endpointURI));
            }
        } catch (URISyntaxException e) {
            throw new APIMgtDAOException("Not a URI", e);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error retrieving functions for event: " + event + " of user: " + userName, e);
    }
    return functions;
}
Also used : Function(org.wso2.carbon.apimgt.core.models.Function) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 8 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class FunctionDAOImpl method deleteEventFunctionMapping.

/**
 * {@inheritDoc}
 */
@Override
public void deleteEventFunctionMapping(String userName, Event event, String functionName) throws APIMgtDAOException {
    if (userName == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    if (event == null) {
        throw new IllegalArgumentException("Event must not be null");
    }
    if (functionName == null) {
        throw new IllegalArgumentException("Function name must not be null");
    }
    final String sqlQuery = "DELETE FROM AM_EVENT_FUNCTION_MAPPING " + "WHERE EVENT = ? AND FUNCTION_ID = " + "(SELECT FUNCTION_ID " + "FROM AM_LAMBDA_FUNCTION " + "WHERE USER_NAME = ? AND FUNCTION_NAME = ?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, event.getEventAsString());
        preparedStatement.setString(2, userName);
        preparedStatement.setString(3, functionName);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error deleting event function mapping -event: " + event + " -function: " + functionName + " -user: " + userName, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 9 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class FunctionDAOImpl method getTriggersForUserFunction.

/**
 * {@inheritDoc}
 */
@Override
public List<Event> getTriggersForUserFunction(String userName, String functionName) throws APIMgtDAOException {
    if (userName == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    if (functionName == null) {
        throw new IllegalArgumentException("Function name must not be null");
    }
    List<Event> events = new ArrayList<>();
    final String sqlQuery = "SELECT EVENT " + "FROM AM_LAMBDA_FUNCTION JOIN AM_EVENT_FUNCTION_MAPPING " + "ON AM_LAMBDA_FUNCTION.FUNCTION_ID = AM_EVENT_FUNCTION_MAPPING.FUNCTION_ID " + "WHERE USER_NAME = ? AND FUNCTION_NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, userName);
        preparedStatement.setString(2, functionName);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                events.add(Event.valueOf(resultSet.getString("EVENT")));
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error retrieving triggers for function: " + functionName + " of user: " + userName, e);
    }
    return events;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Event(org.wso2.carbon.apimgt.core.models.Event) PreparedStatement(java.sql.PreparedStatement)

Example 10 with User

use of org.wso2.broker.core.security.authentication.user.User in project carbon-apimgt by wso2.

the class APIPublisherImpl method searchAPIs.

/**
 * @param limit  Limit
 * @param offset Offset
 * @param query  Search query
 * @return List of APIS.
 * @throws APIManagementException If failed to formatApiSearch APIs.
 */
@Override
public List<API> searchAPIs(Integer limit, Integer offset, String query) throws APIManagementException {
    List<API> apiResults;
    String user = getUsername();
    Set<String> roles = new HashSet<>();
    try {
        // TODO: Need to validate users roles against results returned
        if (!"admin".equals(user)) {
            // Whenever call identity provider should convert pseudo name to actual name
            String userId = getIdentityProvider().getIdOfUser(user);
            roles = new HashSet<>(getIdentityProvider().getRoleIdsOfUser(userId));
        }
        if (query != null && !query.isEmpty()) {
            String[] attributes = query.split(ATTRIBUTE_DELIMITER);
            Map<String, String> attributeMap = new HashMap<>();
            boolean isFullTextSearch = false;
            String searchAttribute, searchValue;
            if (!query.contains(KEY_VALUE_DELIMITER)) {
                isFullTextSearch = true;
            } else {
                log.debug("Search query: " + query);
                for (String attribute : attributes) {
                    searchAttribute = attribute.split(KEY_VALUE_DELIMITER)[0];
                    searchValue = attribute.split(KEY_VALUE_DELIMITER)[1];
                    log.debug(searchAttribute + KEY_VALUE_DELIMITER + searchValue);
                    attributeMap.put(searchAttribute, searchValue);
                }
            }
            if (isFullTextSearch) {
                apiResults = getApiDAO().searchAPIs(roles, user, query, offset, limit);
            } else {
                log.debug("Attributes:", attributeMap.toString());
                apiResults = getApiDAO().attributeSearchAPIs(roles, user, attributeMap, offset, limit);
            }
        } else {
            apiResults = getApiDAO().getAPIs(roles, user);
        }
        return apiResults;
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while Searching the API with query " + query;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (IdentityProviderException e) {
        String errorMsg = "Error occurred while calling SCIM endpoint to retrieve user " + user + "'s information";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) HashMap(java.util.HashMap) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) HashSet(java.util.HashSet)

Aggregations

Test (org.junit.Test)225 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)223 Response (javax.ws.rs.core.Response)215 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)130 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)114 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)112 Request (org.wso2.msf4j.Request)105 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)98 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)88 Test (org.testng.annotations.Test)74 API (org.wso2.carbon.apimgt.core.models.API)73 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)65 ArrayList (java.util.ArrayList)55 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)44 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)41 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)37 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)35 HashMap (java.util.HashMap)34 LifecycleState (org.wso2.carbon.lcm.core.impl.LifecycleState)28 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)26