Search in sources :

Example 1 with ClientNotFoundException

use of io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException in project apiman by apiman.

the class CachingEsRegistry method getContract.

/**
 * @see io.apiman.gateway.engine.jdbc.EsRegistry#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;
    try {
        synchronized (mutex) {
            client = getClient(apiKey);
            api = getApi(apiOrganizationId, apiId, apiVersion);
        }
        if (client == null) {
            // $NON-NLS-1$
            Exception error = new ClientNotFoundException(Messages.i18n.format("EsRegistry.NoClientForAPIKey", apiKey));
            handler.handle(AsyncResultImpl.create(error, ApiContract.class));
            return;
        }
        if (api == null) {
            throw new ApiRetiredException(// $NON-NLS-1$
            Messages.i18n.format(// $NON-NLS-1$
            "EsRegistry.ApiWasRetired", apiId, apiOrganizationId));
        }
        Contract matchedContract = null;
        for (Contract contract : client.getContracts()) {
            if (contract.matches(apiOrganizationId, apiId, apiVersion)) {
                matchedContract = contract;
                break;
            }
        }
        if (matchedContract == null) {
            throw new NoContractFoundException(// $NON-NLS-1$
            Messages.i18n.format(// $NON-NLS-1$
            "EsRegistry.NoContractFound", client.getClientId(), api.getApiId()));
        }
        ApiContract contract = new ApiContract(api, client, matchedContract.getPlan(), matchedContract.getPolicies());
        handler.handle(AsyncResultImpl.create(contract));
    } catch (Exception e) {
        handler.handle(AsyncResultImpl.create(e, ApiContract.class));
    }
}
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) ClientNotFoundException(io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException) ApiRetiredException(io.apiman.gateway.engine.beans.exceptions.ApiRetiredException) IOException(java.io.IOException) NoContractFoundException(io.apiman.gateway.engine.beans.exceptions.NoContractFoundException) ApiContract(io.apiman.gateway.engine.beans.ApiContract)

Example 2 with ClientNotFoundException

use of io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException in project apiman by apiman.

the class EsRegistry method lookupClient.

/**
 * Searches for a client by its orgid:clientId:version and returns it.
 * @param orgId the organization id
 * @param clientId the client id
 * @param version the version
 */
// Do beans need escaping or will that be done 'automatically'. Test it. Strings do, but probably only quotes?
@SuppressWarnings("nls")
private Client lookupClient(String orgId, String clientId, String version) {
    String query = "{" + "  \"query\": {" + "        \"bool\": {" + "            \"filter\": [{" + "                    \"term\": {" + // orgId
    "                        \"organizationId\": \"{{organizationId}}\" " + "                    }" + "          }," + "          {" + "                    \"term\": {" + // clientId
    "                        \"clientId\": \"{{clientId}}\" " + "                    }" + "          }," + "          {" + "                    \"term\": {" + // version
    "                        \"version\": \"{{version}}\" " + "          }" + "      }" + "            ]" + "    }" + "  }" + "}";
    SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest();
    searchTemplateRequest.setRequest(new SearchRequest(getIndexPrefixWithJoiner() + EsConstants.INDEX_CLIENTS));
    searchTemplateRequest.setScriptType(ScriptType.INLINE);
    searchTemplateRequest.setScript(query);
    Map<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("organizationId", orgId);
    scriptParams.put("clientId", clientId);
    scriptParams.put("version", version);
    searchTemplateRequest.setScriptParams(scriptParams);
    Client client;
    try {
        SearchTemplateResponse response = getClient().searchTemplate(searchTemplateRequest, RequestOptions.DEFAULT);
        SearchResponse searchResponse = response.getResponse();
        SearchHits hits = searchResponse.getHits();
        if (hits.getTotalHits().value == 0) {
            throw new IOException();
        }
        String sourceAsString = response.getResponse().getHits().getAt(0).getSourceAsString();
        client = JSON_MAPPER.readValue(sourceAsString, Client.class);
    } catch (IOException e) {
        // $NON-NLS-1$
        throw new ClientNotFoundException(Messages.i18n.format("EsRegistry.ClientNotFound"), e);
    }
    return client;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) ClientNotFoundException(io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException) HashMap(java.util.HashMap) SearchTemplateRequest(org.elasticsearch.script.mustache.SearchTemplateRequest) SearchTemplateResponse(org.elasticsearch.script.mustache.SearchTemplateResponse) SearchHits(org.elasticsearch.search.SearchHits) IOException(java.io.IOException) Client(io.apiman.gateway.engine.beans.Client) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with ClientNotFoundException

use of io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException in project apiman by apiman.

the class EsRegistry 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) {
    try {
        Client client = getClient(apiKey);
        Api api = getApi(getApiId(apiOrganizationId, apiId, apiVersion));
        if (client == null) {
            // $NON-NLS-1$
            Exception error = new ClientNotFoundException(Messages.i18n.format("EsRegistry.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$
            "EsRegistry.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$
            "EsRegistry.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));
    } catch (Exception e) {
        handler.handle(AsyncResultImpl.create(e, ApiContract.class));
    }
}
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) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) IOException(java.io.IOException) NoContractFoundException(io.apiman.gateway.engine.beans.exceptions.NoContractFoundException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) ApiContract(io.apiman.gateway.engine.beans.ApiContract)

Example 4 with ClientNotFoundException

use of io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException in project apiman by apiman.

the class SharedGlobalDataRegistry method getContract.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void getContract(String apiOrganizationId, String apiId, String apiVersion, String apiKey, IAsyncResultHandler<ApiContract> handler) {
    String apiIndex = getApiIndex(apiOrganizationId, apiId, apiVersion);
    Future apiFuture = Future.future();
    Future clientFuture = Future.future();
    objectMap.get(apiIndex, apiFuture.completer());
    objectMap.get(apiKey, clientFuture.completer());
    CompositeFuture.all(apiFuture, clientFuture).setHandler(compositeResult -> {
        if (compositeResult.succeeded()) {
            Api api = (Api) apiFuture.result();
            Client client = (Client) clientFuture.result();
            if (api == null) {
                Exception error = new ClientNotFoundException(Messages.i18n.format("InMemoryRegistry.NoClientForAPIKey", apiKey));
                handler.handle(AsyncResultImpl.create(error, ApiContract.class));
            } else if (client == null) {
                Exception error = new ApiRetiredException(Messages.i18n.format("InMemoryRegistry.ApiWasRetired", apiId, apiOrganizationId));
                handler.handle(AsyncResultImpl.create(error, ApiContract.class));
            } else {
                Optional<Contract> matchedOpt = client.getContracts().stream().filter(contract -> contract.matches(apiOrganizationId, apiId, apiVersion)).findFirst();
                if (matchedOpt.isPresent()) {
                    Contract contract = matchedOpt.get();
                    ApiContract apiContract = new ApiContract(api, client, contract.getPlan(), contract.getPolicies());
                    handler.handle(AsyncResultImpl.create(apiContract));
                } else {
                    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));
                }
            }
        } else {
            handler.handle(AsyncResultImpl.create(compositeResult.cause()));
        }
    });
}
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) Optional(java.util.Optional) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) 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) 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) ApiContract(io.apiman.gateway.engine.beans.ApiContract)

Example 5 with ClientNotFoundException

use of io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException in project apiman by apiman.

the class JdbcRegistry 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) {
    try {
        Client client = getClientInternal(apiKey);
        Api api = getApiInternal(apiOrganizationId, apiId, apiVersion);
        if (client == null) {
            // $NON-NLS-1$
            Exception error = new ClientNotFoundException(Messages.i18n.format("JdbcRegistry.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$
            "JdbcRegistry.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$
            "JdbcRegistry.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));
    } catch (Exception e) {
        handler.handle(AsyncResultImpl.create(e, ApiContract.class));
    }
}
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) ApiRetiredException(io.apiman.gateway.engine.beans.exceptions.ApiRetiredException) SQLException(java.sql.SQLException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException) ClientNotFoundException(io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NoContractFoundException(io.apiman.gateway.engine.beans.exceptions.NoContractFoundException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) ApiContract(io.apiman.gateway.engine.beans.ApiContract)

Aggregations

Client (io.apiman.gateway.engine.beans.Client)8 ClientNotFoundException (io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException)8 Api (io.apiman.gateway.engine.beans.Api)6 ApiContract (io.apiman.gateway.engine.beans.ApiContract)6 Contract (io.apiman.gateway.engine.beans.Contract)6 ApiRetiredException (io.apiman.gateway.engine.beans.exceptions.ApiRetiredException)6 NoContractFoundException (io.apiman.gateway.engine.beans.exceptions.NoContractFoundException)6 ApiNotFoundException (io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException)5 IOException (java.io.IOException)4 RegistrationException (io.apiman.gateway.engine.beans.exceptions.RegistrationException)3 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)2 CompositeFuture (io.vertx.core.CompositeFuture)2 Future (io.vertx.core.Future)2 Optional (java.util.Optional)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IEngineConfig (io.apiman.gateway.engine.IEngineConfig)1 IRegistry (io.apiman.gateway.engine.IRegistry)1 AsyncResultImpl (io.apiman.gateway.engine.async.AsyncResultImpl)1 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)1 Messages (io.apiman.gateway.engine.i18n.Messages)1