Search in sources :

Example 1 with APIManagementException

use of org.wso2.carbon.apimgt.api.APIManagementException in project product-apim by wso2.

the class TestUtil method generateClient.

public static DCRClientInfo generateClient() throws APIManagementException {
    DCRClientInfo dcrClientInfo = new DCRClientInfo();
    dcrClientInfo.setClientName("apim-integration-test");
    dcrClientInfo.setGrantTypes(Arrays.asList(new String[] { "password", "client_credentials" }));
    try {
        Response response = DCRMServiceStubFactory.getDCRMServiceStub(DYNAMIC_CLIENT_REGISTRATION_ENDPOINT, username, password, "wso2carbon").registerApplication(dcrClientInfo);
        DCRClientInfo dcrClientInfoResponse = (DCRClientInfo) new GsonDecoder().decode(response, DCRClientInfo.class);
        clientId = dcrClientInfoResponse.getClientId();
        clientSecret = dcrClientInfoResponse.getClientSecret();
        return dcrClientInfoResponse;
    } catch (APIManagementException | IOException e) {
        logger.error("Couldn't create client", e);
        throw new APIManagementException("Couldn't create client", e);
    }
}
Also used : Response(feign.Response) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) DCRClientInfo(org.wso2.carbon.apimgt.core.auth.dto.DCRClientInfo)

Example 2 with APIManagementException

use of org.wso2.carbon.apimgt.api.APIManagementException in project product-apim by wso2.

the class TestUtil method generateToken.

private static void generateToken(String username, String password, String scopes) throws APIManagementException {
    if (StringUtils.isEmpty(clientId) | StringUtils.isEmpty(clientSecret)) {
        generateClient();
    }
    OAuth2ServiceStubs.TokenServiceStub tokenServiceStub = getOauth2Client();
    Response response = tokenServiceStub.generatePasswordGrantAccessToken(username, password, scopes, -1, clientId, clientSecret);
    if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
        // 200 - Success
        logger.debug("A new access token is successfully generated.");
        try {
            OAuth2TokenInfo oAuth2TokenInfo = (OAuth2TokenInfo) new GsonDecoder().decode(response, OAuth2TokenInfo.class);
            accessTokenInfo = new TokenInfo(oAuth2TokenInfo.getAccessToken(), System.currentTimeMillis() + oAuth2TokenInfo.getExpiresIn());
        } catch (IOException e) {
            throw new KeyManagementException("Error occurred while parsing token response", e, ExceptionCodes.ACCESS_TOKEN_GENERATION_FAILED);
        }
    }
}
Also used : Response(feign.Response) GsonDecoder(feign.gson.GsonDecoder) OAuth2TokenInfo(org.wso2.carbon.apimgt.core.auth.dto.OAuth2TokenInfo) IOException(java.io.IOException) OAuth2ServiceStubs(org.wso2.carbon.apimgt.core.auth.OAuth2ServiceStubs) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException) OAuth2TokenInfo(org.wso2.carbon.apimgt.core.auth.dto.OAuth2TokenInfo)

Example 3 with APIManagementException

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

the class APIPublisherImpl method removePendingLifecycleWorkflowTaskForAPI.

/**
 * {@inheritDoc}
 */
@Override
public void removePendingLifecycleWorkflowTaskForAPI(String apiId) throws APIManagementException {
    try {
        API api = getApiDAO().getAPI(apiId);
        if (APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
            // change the state back
            getApiDAO().updateAPIWorkflowStatus(apiId, APILCWorkflowStatus.APPROVED);
            // call executor's cleanup task
            cleanupPendingTaskForAPIStateChange(apiId);
        } else {
            String msg = "API does not have a pending lifecycle state change.";
            log.error(msg);
            throw new APIManagementException(msg, ExceptionCodes.WORKFLOW_NO_PENDING_TASK);
        }
    } catch (APIMgtDAOException e) {
        String msg = "Error occurred while changing api lifecycle workflow status";
        log.error(msg, e);
        throw new APIManagementException(msg, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API)

Example 4 with APIManagementException

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

the class APIPublisherImpl method updateEndpoint.

/**
 * Update and endpoint
 *
 * @param endpoint Endpoint object.
 * @throws APIManagementException If failed to update endpoint.
 */
@Override
public void updateEndpoint(Endpoint endpoint) throws APIManagementException {
    APIGateway gateway = getApiGateway();
    gateway.updateEndpoint(endpoint);
    String config = getGatewaySourceGenerator().getEndpointConfigStringFromTemplate(endpoint);
    Endpoint updatedEndpoint = new Endpoint.Builder(endpoint).config(config).build();
    try {
        getApiDAO().updateEndpoint(updatedEndpoint);
    } catch (APIMgtDAOException e) {
        String msg = "Failed to update Endpoint : " + endpoint.getName();
        log.error(msg, e);
        throw new APIManagementException(msg, e, e.getErrorHandler());
    }
// update endpoint config in gateway
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway)

Example 5 with APIManagementException

use of org.wso2.carbon.apimgt.api.APIManagementException 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

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1120 Test (org.junit.Test)458 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)445 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)433 ArrayList (java.util.ArrayList)407 HashMap (java.util.HashMap)376 Test (org.testng.annotations.Test)353 IOException (java.io.IOException)274 SQLException (java.sql.SQLException)262 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)233 API (org.wso2.carbon.apimgt.api.model.API)228 PreparedStatement (java.sql.PreparedStatement)223 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)223 Connection (java.sql.Connection)209 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)203 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)202 API (org.wso2.carbon.apimgt.core.models.API)200 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)199 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)188 Response (javax.ws.rs.core.Response)183