Search in sources :

Example 21 with Api

use of io.apiman.gateway.engine.beans.Api in project apiman by apiman.

the class JdbcRegistry method validateContract.

/**
 * Ensures that the api referenced by the Contract actually exists (is published).
 * @param contract
 * @param connection
 * @throws RegistrationException
 */
private void validateContract(final Contract contract, Connection connection) throws RegistrationException {
    QueryRunner run = new QueryRunner();
    try {
        Api api = // $NON-NLS-1$
        run.query(// $NON-NLS-1$
        connection, // $NON-NLS-1$
        "SELECT bean FROM gw_apis WHERE org_id = ? AND id = ? AND version = ?", Handlers.API_HANDLER, contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion());
        if (api == null) {
            String apiId = contract.getApiId();
            String orgId = contract.getApiOrgId();
            // $NON-NLS-1$
            throw new ApiNotFoundException(Messages.i18n.format("JdbcRegistry.ApiNotFoundInOrg", apiId, orgId));
        }
    } catch (SQLException e) {
        // $NON-NLS-1$
        throw new RegistrationException(Messages.i18n.format("JdbcRegistry.ErrorValidatingApp"), e);
    }
}
Also used : RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException) SQLException(java.sql.SQLException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) Api(io.apiman.gateway.engine.beans.Api) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 22 with Api

use of io.apiman.gateway.engine.beans.Api in project apiman by apiman.

the class BucketFactory method bucketId.

/**
 * Creates the ID of the rate bucket to use.  The ID is composed differently
 * depending on the configuration of the policy.
 */
public String bucketId(RateLimitingConfig config, BucketIdBuilderContext context) {
    Api api = context.getApi();
    StringBuilder builder = new StringBuilder();
    // Public API in this branch
    if (context.getContract() == null) {
        // $NON-NLS-1$
        builder.append("PUBLIC||");
        // $NON-NLS-1$
        builder.append("||");
        builder.append(api.getOrganizationId());
        // $NON-NLS-1$
        builder.append("||");
        builder.append(api.getApiId());
        // $NON-NLS-1$
        builder.append("||");
        builder.append(api.getVersion());
        if (config.getGranularity() == RateLimitingGranularity.User) {
            String user = context.getUserSupplier().get();
            // $NON-NLS-1$
            builder.append("||");
            builder.append(user);
        } else if (config.getGranularity() == RateLimitingGranularity.Ip) {
            // $NON-NLS-1$
            builder.append("||");
            builder.append(context.getRemoteAddr());
        } else if (config.getGranularity() == RateLimitingGranularity.Api) {
        } else {
            return NO_CLIENT_AVAILABLE;
        }
    } else {
        // Have a fully valid contract in this branch.
        ApiContract contract = context.getContract();
        Client client = contract.getClient();
        String apiKey = client.getApiKey();
        builder.append(apiKey);
        if (config.getGranularity() == RateLimitingGranularity.User) {
            String user = context.getUserSupplier().get();
            if (user == null) {
                return NO_USER_AVAILABLE;
            } else {
                // $NON-NLS-1$
                builder.append("||USER||");
                builder.append(client.getOrganizationId());
                // $NON-NLS-1$
                builder.append("||");
                builder.append(client.getClientId());
                // $NON-NLS-1$
                builder.append("||");
                builder.append(user);
            }
        } else if (config.getGranularity() == RateLimitingGranularity.Client) {
            builder.append(apiKey);
            // $NON-NLS-1$
            builder.append("||APP||");
            builder.append(client.getOrganizationId());
            // $NON-NLS-1$
            builder.append("||");
            builder.append(client.getClientId());
        } else if (config.getGranularity() == RateLimitingGranularity.Ip) {
            builder.append(apiKey);
            // $NON-NLS-1$
            builder.append("||IP||");
            builder.append(client.getOrganizationId());
            // $NON-NLS-1$
            builder.append("||");
            builder.append(context.getRemoteAddr());
        } else {
            builder.append(apiKey);
            // $NON-NLS-1$
            builder.append("||SERVICE||");
            builder.append(api.getOrganizationId());
            // $NON-NLS-1$
            builder.append("||");
            builder.append(api.getApiId());
        }
    }
    return builder.toString();
}
Also used : Api(io.apiman.gateway.engine.beans.Api) Client(io.apiman.gateway.engine.beans.Client) ApiContract(io.apiman.gateway.engine.beans.ApiContract)

Example 23 with Api

use of io.apiman.gateway.engine.beans.Api in project apiman by apiman.

the class InMemoryRegistry method retireApi.

/**
 * @see io.apiman.gateway.engine.IRegistry#retireApi(io.apiman.gateway.engine.beans.Api, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void retireApi(Api api, IAsyncResultHandler<Void> handler) {
    String apiIdx = getApiIndex(api);
    Exception error = null;
    synchronized (mutex) {
        Api removedApi = (Api) getMap().remove(apiIdx);
        if (removedApi == null) {
            // $NON-NLS-1$
            error = new ApiNotFoundException(Messages.i18n.format("InMemoryRegistry.ApiNotFound"));
        }
    }
    if (error == null) {
        handler.handle(AsyncResultImpl.create((Void) null));
    } else {
        handler.handle(AsyncResultImpl.create(error, Void.class));
    }
}
Also used : ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) Api(io.apiman.gateway.engine.beans.Api) RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException) ClientNotFoundException(io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException) ApiRetiredException(io.apiman.gateway.engine.beans.exceptions.ApiRetiredException) NoContractFoundException(io.apiman.gateway.engine.beans.exceptions.NoContractFoundException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException)

Example 24 with Api

use of io.apiman.gateway.engine.beans.Api in project apiman by apiman.

the class InMemoryRegistry method getContract.

/**
 * @see io.apiman.gateway.engine.IRegistry#getContract(java.lang.String, java.lang.String, java.lang.String, java.lang.String, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void getContract(String apiOrganizationId, String apiId, String apiVersion, String apiKey, IAsyncResultHandler<ApiContract> handler) {
    Client client = null;
    Api api = null;
    String apiIdx = getApiIndex(apiOrganizationId, apiId, apiVersion);
    synchronized (mutex) {
        client = (Client) getMap().get(apiKey);
        api = (Api) getMap().get(apiIdx);
    }
    if (client == null) {
        // $NON-NLS-1$
        Exception error = new ClientNotFoundException(Messages.i18n.format("InMemoryRegistry.NoClientForAPIKey", apiKey));
        handler.handle(AsyncResultImpl.create(error, ApiContract.class));
        return;
    }
    if (api == null) {
        Exception error = new ApiRetiredException(// $NON-NLS-1$
        Messages.i18n.format(// $NON-NLS-1$
        "InMemoryRegistry.ApiWasRetired", apiId, apiOrganizationId));
        handler.handle(AsyncResultImpl.create(error, ApiContract.class));
        return;
    }
    Contract matchedContract = null;
    for (Contract contract : client.getContracts()) {
        if (contract.matches(apiOrganizationId, apiId, apiVersion)) {
            matchedContract = contract;
            break;
        }
    }
    if (matchedContract == null) {
        Exception error = new NoContractFoundException(// $NON-NLS-1$
        Messages.i18n.format(// $NON-NLS-1$
        "InMemoryRegistry.NoContractFound", client.getClientId(), api.getApiId()));
        handler.handle(AsyncResultImpl.create(error, ApiContract.class));
        return;
    }
    ApiContract contract = new ApiContract(api, client, matchedContract.getPlan(), matchedContract.getPolicies());
    handler.handle(AsyncResultImpl.create(contract));
}
Also used : NoContractFoundException(io.apiman.gateway.engine.beans.exceptions.NoContractFoundException) ApiRetiredException(io.apiman.gateway.engine.beans.exceptions.ApiRetiredException) ClientNotFoundException(io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException) Api(io.apiman.gateway.engine.beans.Api) Client(io.apiman.gateway.engine.beans.Client) Contract(io.apiman.gateway.engine.beans.Contract) ApiContract(io.apiman.gateway.engine.beans.ApiContract) RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException) ClientNotFoundException(io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException) ApiRetiredException(io.apiman.gateway.engine.beans.exceptions.ApiRetiredException) NoContractFoundException(io.apiman.gateway.engine.beans.exceptions.NoContractFoundException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) ApiContract(io.apiman.gateway.engine.beans.ApiContract)

Example 25 with Api

use of io.apiman.gateway.engine.beans.Api in project apiman by apiman.

the class SharedGlobalDataRegistry method registerClient.

// CompositeFuture.all(list) requires raw futures.
@SuppressWarnings("rawtypes")
@Override
public void registerClient(Client client, IAsyncResultHandler<Void> resultHandler) {
    List<Future> futures = new ArrayList<>(client.getContracts().size());
    List<Contract> contracts = new ArrayList<>(client.getContracts());
    String clientIndex = getClientIndex(client);
    // Future for each contract and execute get.
    for (Contract contract : contracts) {
        Future future = Future.future();
        futures.add(future);
        String apiIndex = getApiIndex(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion());
        objectMap.get(apiIndex, future.completer());
    }
    CompositeFuture.all(futures).setHandler(compositeResult -> {
        if (compositeResult.succeeded()) {
            // If any contract didn't correspond to a stored API.
            Contract failedContract = null;
            for (int i = 0; i < futures.size(); i++) {
                if (futures.get(i).result() == null) {
                    failedContract = contracts.get(0);
                    break;
                }
            }
            // If we found an invalid contract.
            if (failedContract != null) {
                Exception ex = new ApiNotFoundException(Messages.i18n.format("InMemoryRegistry.ApiNotFoundInOrg", failedContract.getApiId(), failedContract.getApiOrgId()));
                resultHandler.handle(AsyncResultImpl.create(ex));
            } else {
                Future<Object> putNewApiKeyFuture = Future.future();
                Future<Object> endFuture = Future.future();
                // Order: Create new API Key reference; Replace old ID -> API mapping; Delete old key reference)
                // This should ensure no breaking/irreconcilable behaviour.
                objectMap.putIfAbsent(client.getApiKey(), client, putNewApiKeyFuture.completer());
                // Replace API Key reference
                putNewApiKeyFuture.compose(clientWithSameApiKey -> {
                    Future<Object> replaceClientFuture = Future.future();
                    // only in hard-coded tests. Generally sameKeyReplace will be null.
                    if (clientWithSameApiKey != null) {
                        // System.err.println("!!!!! Same API Key -- Replacing. Must not delete later. !!!!!!");
                        objectMap.replace(client.getApiKey(), client, replaceClientFuture.completer());
                    } else {
                        objectMap.putIfAbsent(clientIndex, client, replaceClientFuture.completer());
                    }
                    return replaceClientFuture;
                // Remove old API key reference
                }).compose(oldClientRaw -> {
                    Client oldClient = (Client) oldClientRaw;
                    if (oldClientRaw != null && !oldClient.getApiKey().equals(client.getApiKey())) {
                        objectMap.remove(oldClient.getApiKey(), endFuture.completer());
                    } else {
                        endFuture.complete();
                    }
                }, endFuture).setHandler(handleResult(resultHandler));
            }
        } else {
            resultHandler.handle(AsyncResultImpl.create(compositeResult.cause()));
        }
    });
}
Also used : ApiRetiredException(io.apiman.gateway.engine.beans.exceptions.ApiRetiredException) AsyncResultImpl(io.apiman.gateway.engine.async.AsyncResultImpl) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) Client(io.apiman.gateway.engine.beans.Client) CompositeFuture(io.vertx.core.CompositeFuture) AsyncMap(io.vertx.core.shareddata.AsyncMap) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) Logger(io.vertx.core.logging.Logger) Contract(io.apiman.gateway.engine.beans.Contract) ClientNotFoundException(io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException) Vertx(io.vertx.core.Vertx) Future(io.vertx.core.Future) Api(io.apiman.gateway.engine.beans.Api) NoContractFoundException(io.apiman.gateway.engine.beans.exceptions.NoContractFoundException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) IEngineConfig(io.apiman.gateway.engine.IEngineConfig) List(java.util.List) Messages(io.apiman.gateway.engine.i18n.Messages) IRegistry(io.apiman.gateway.engine.IRegistry) ApiContract(io.apiman.gateway.engine.beans.ApiContract) Optional(java.util.Optional) Handler(io.vertx.core.Handler) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) ArrayList(java.util.ArrayList) ApiRetiredException(io.apiman.gateway.engine.beans.exceptions.ApiRetiredException) ClientNotFoundException(io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException) NoContractFoundException(io.apiman.gateway.engine.beans.exceptions.NoContractFoundException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) Client(io.apiman.gateway.engine.beans.Client) Contract(io.apiman.gateway.engine.beans.Contract) ApiContract(io.apiman.gateway.engine.beans.ApiContract)

Aggregations

Api (io.apiman.gateway.engine.beans.Api)41 Client (io.apiman.gateway.engine.beans.Client)16 ApiContract (io.apiman.gateway.engine.beans.ApiContract)11 Contract (io.apiman.gateway.engine.beans.Contract)10 Policy (io.apiman.gateway.engine.beans.Policy)10 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)10 ApiNotFoundException (io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException)8 ApiRetiredException (io.apiman.gateway.engine.beans.exceptions.ApiRetiredException)8 NoContractFoundException (io.apiman.gateway.engine.beans.exceptions.NoContractFoundException)8 ClientNotFoundException (io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException)7 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)7 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)7 StorageException (io.apiman.manager.api.core.exceptions.StorageException)7 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)7 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 RegistrationException (io.apiman.gateway.engine.beans.exceptions.RegistrationException)6 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)6 ActionException (io.apiman.manager.api.rest.exceptions.ActionException)6 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)6