Search in sources :

Example 1 with Validate

use of org.wso2.micro.gateway.core.validation.Validate in project siddhi by wso2.

the class ValidateTestCase method validateTest1.

@Test
public void validateTest1() throws InterruptedException {
    log.info("validate test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('validateTest') " + "" + "define stream cseEventStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from cseEventStream[symbol is null] " + "select symbol, price " + "insert into outputStream;";
    siddhiManager.validateSiddhiApp(siddhiApp);
}
Also used : SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 2 with Validate

use of org.wso2.micro.gateway.core.validation.Validate in project carbon-apimgt by wso2.

the class PolicyDAOImpl method isValidApplication.

/**
 * validate the blocking application.
 *
 * @param appName name of the application
 * @param uuid    uuid of the application
 * @return return true/false depends of the success
 * @throws APIMgtDAOException if failed validating application
 */
private boolean isValidApplication(String appName, String uuid) throws APIMgtDAOException {
    String query = "SELECT 1 FROM AM_APPLICATION WHERE UUID = ? AND NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        connection.setAutoCommit(false);
        statement.setString(1, uuid);
        statement.setString(2, appName);
        try (ResultSet resultSet = statement.executeQuery()) {
            return resultSet.next();
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "checking if Block condition with Application Name " + appName + " , Application ID = " + uuid + " exists", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with Validate

use of org.wso2.micro.gateway.core.validation.Validate 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)

Example 4 with Validate

use of org.wso2.micro.gateway.core.validation.Validate in project carbon-apimgt by wso2.

the class APIPublisherImpl method createNewAPIVersion.

/**
 * Create a new version of the <code>api</code>, with version <code>newVersion</code>
 *
 * @param apiId      The API to be copied
 * @param newVersion The version of the new API
 * @return UUID of the newly created version.
 * @throws APIManagementException If an error occurs while trying to create
 *                                the new version of the API
 */
@Override
public String createNewAPIVersion(String apiId, String newVersion) throws APIManagementException {
    String newVersionedId;
    LifecycleState lifecycleState;
    // validate parameters
    if (StringUtils.isEmpty(newVersion)) {
        String errorMsg = "New API version cannot be empty";
        log.error(errorMsg);
        throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
    }
    if (StringUtils.isEmpty(apiId)) {
        String errorMsg = "API ID cannot be empty";
        log.error(errorMsg);
        throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
    }
    try {
        API api = getApiDAO().getAPI(apiId);
        if (api.getVersion().equals(newVersion)) {
            String errMsg = "New API version " + newVersion + " cannot be same as the previous version for " + "API " + api.getName();
            log.error(errMsg);
            throw new APIManagementException(errMsg, ExceptionCodes.API_ALREADY_EXISTS);
        }
        API.APIBuilder apiBuilder = new API.APIBuilder(api);
        apiBuilder.id(UUID.randomUUID().toString());
        apiBuilder.version(newVersion);
        apiBuilder.context(api.getContext().replace(api.getVersion(), newVersion));
        lifecycleState = getApiLifecycleManager().addLifecycle(APIMgtConstants.API_LIFECYCLE, getUsername());
        apiBuilder.associateLifecycle(lifecycleState);
        apiBuilder.copiedFromApiId(api.getId());
        if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
            apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
        }
        getApiDAO().addAPI(apiBuilder.build());
        newVersionedId = apiBuilder.getId();
        sendNotification(apiId, apiBuilder.getName(), newVersion);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Couldn't create new API version from " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (LifecycleException e) {
        String errorMsg = "Couldn't Associate  new API Lifecycle from " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
    }
    return newVersionedId;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) LifecycleException(org.wso2.carbon.lcm.core.exception.LifecycleException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) API(org.wso2.carbon.apimgt.core.models.API)

Example 5 with Validate

use of org.wso2.micro.gateway.core.validation.Validate in project carbon-apimgt by wso2.

the class APIStoreImpl method createNewCompositeApiVersion.

/**
 * {@inheritDoc}
 */
@Override
public String createNewCompositeApiVersion(String apiId, String newVersion) throws APIManagementException {
    // validate parameters
    if (StringUtils.isEmpty(newVersion)) {
        String errorMsg = "New API version cannot be empty";
        log.error(errorMsg);
        throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
    }
    if (StringUtils.isEmpty(apiId)) {
        String errorMsg = "API ID cannot be empty";
        log.error(errorMsg);
        throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
    }
    String newVersionedId;
    try {
        CompositeAPI api = getApiDAO().getCompositeAPI(apiId);
        if (api.getVersion().equals(newVersion)) {
            String errMsg = "New API version " + newVersion + " cannot be same as the previous version for " + "API " + api.getName();
            log.error(errMsg);
            throw new APIManagementException(errMsg, ExceptionCodes.API_ALREADY_EXISTS);
        }
        CompositeAPI.Builder apiBuilder = new CompositeAPI.Builder(api);
        apiBuilder.id(UUID.randomUUID().toString());
        apiBuilder.version(newVersion);
        apiBuilder.context(api.getContext().replace(api.getVersion(), newVersion));
        apiBuilder.copiedFromApiId(api.getId());
        if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
            apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
        }
        getApiDAO().addApplicationAssociatedAPI(apiBuilder.build());
        newVersionedId = apiBuilder.getId();
    } catch (APIMgtDAOException e) {
        String errorMsg = "Couldn't create new API version from " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    return newVersionedId;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI)

Aggregations

HashMap (java.util.HashMap)74 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)69 ArrayList (java.util.ArrayList)63 Test (org.testng.annotations.Test)53 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)38 Map (java.util.Map)36 IOException (java.io.IOException)32 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)26 List (java.util.List)24 HttpResponse (org.apache.http.HttpResponse)21 JSONObject (org.json.simple.JSONObject)19 APIInfo (org.wso2.carbon.apimgt.api.model.APIInfo)18 URISyntaxException (java.net.URISyntaxException)17 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)17 JSONObject (org.json.JSONObject)16 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)16 API (org.wso2.carbon.apimgt.api.model.API)16 API (org.wso2.carbon.apimgt.core.models.API)16 UserRecoveryData (org.wso2.carbon.identity.recovery.model.UserRecoveryData)15 LinkedHashMap (java.util.LinkedHashMap)14