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));
}
}
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;
}
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;
}
}
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));
}
}
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());
});
}
Aggregations