Search in sources :

Example 6 with Api

use of io.apiman.gateway.engine.beans.Api 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 7 with Api

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

the class CachingEsRegistry method getApi.

/**
 * Gets the api either from the cache or from ES.
 * @param orgId
 * @param apiId
 * @param version
 */
protected Api getApi(String orgId, String apiId, String version) throws IOException {
    String apiIdx = getApiIdx(orgId, apiId, version);
    Api api;
    synchronized (mutex) {
        api = apiCache.get(apiIdx);
    }
    if (api == null) {
        api = super.getApi(getApiId(orgId, apiId, version));
        synchronized (mutex) {
            if (api != null) {
                apiCache.put(apiIdx, api);
            }
        }
    }
    return api;
}
Also used : Api(io.apiman.gateway.engine.beans.Api)

Example 8 with Api

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

the class EsRegistry method getApi.

/**
 * Gets the api synchronously.
 * @param id the api id
 * @throws IOException
 */
protected Api getApi(String id) throws IOException {
    GetRequest getRequest = new GetRequest(getIndexPrefixWithJoiner() + EsConstants.INDEX_APIS, id);
    GetResponse result = getClient().get(getRequest, RequestOptions.DEFAULT);
    if (result.isExists()) {
        Api api = JSON_MAPPER.readValue(result.getSourceAsString(), Api.class);
        return api;
    } else {
        return null;
    }
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) Api(io.apiman.gateway.engine.beans.Api) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 9 with Api

use of io.apiman.gateway.engine.beans.Api 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 10 with Api

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

the class LocalFileRegistryTest method readWriteFileEmptyRegistry.

@Test
public void readWriteFileEmptyRegistry() throws Exception {
    final File registryFile = Files.createTempFile("file-registry", ".json").toFile();
    // don't start with a saved registry - just use the filename
    // noinspection ResultOfMethodCallIgnored
    registryFile.delete();
    final Map<String, String> config = new HashMap<String, String>() {

        {
            put(CONFIG_REGISTRY_PATH, registryFile.getAbsolutePath());
        }
    };
    final LocalFileRegistry registry = new LocalFileRegistry(config);
    assertTrue("The map should be empty", registry.getMap().isEmpty());
    final Api api = new Api() {

        {
            setApiId("apiA");
            setApiPolicies(Collections.emptyList());
            setEndpoint("http://example.com");
            setEndpointType("REST");
            setOrganizationId("org");
            setPublicAPI(true);
            setVersion("1.0");
        }
    };
    registry.publishApi(api, result -> {
        assertTrue("Publish should be successful", result.isSuccess());
        assertFalse("The map should not be empty", registry.getMap().isEmpty());
    });
    // clear and re-read from file
    registry.clear();
    registry.getApi("org", "apiA", "1.0", result -> {
        assertTrue(result.isSuccess());
        assertNotNull(result.getResult());
        assertEquals("apiA", result.getResult().getApiId());
    });
}
Also used : HashMap(java.util.HashMap) Api(io.apiman.gateway.engine.beans.Api) File(java.io.File) Test(org.junit.Test)

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