use of io.apiman.gateway.engine.beans.Client 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));
}
use of io.apiman.gateway.engine.beans.Client in project apiman by apiman.
the class InMemoryRegistry method unregisterClientInternal.
/**
* @param client
* @param silent
*/
protected void unregisterClientInternal(Client client, boolean silent) throws RegistrationException {
synchronized (mutex) {
String clientIdx = getClientIndex(client);
Client oldClient = (Client) getMap().remove(clientIdx);
if (oldClient == null) {
if (!silent) {
// $NON-NLS-1$
throw new ClientNotFoundException(Messages.i18n.format("InMemoryRegistry.ClientNotFound"));
}
} else {
getMap().remove(oldClient.getApiKey());
}
}
}
use of io.apiman.gateway.engine.beans.Client in project apiman by apiman.
the class InMemoryRegistry method getClient.
/**
* @see io.apiman.gateway.engine.IRegistry#getClient(java.lang.String, io.apiman.gateway.engine.async.IAsyncResultHandler)
*/
@Override
public void getClient(String apiKey, IAsyncResultHandler<Client> handler) {
Client client = getClientInternal(apiKey);
handler.handle(AsyncResultImpl.create(client));
}
use of io.apiman.gateway.engine.beans.Client in project apiman by apiman.
the class StorageImportDispatcher method registerClients.
/**
* Registers any clients that were imported in the "Registered" state.
* @throws StorageException
*/
private void registerClients() throws StorageException {
// $NON-NLS-1$
logger.info(Messages.i18n.format("StorageExporter.RegisteringClients"));
for (EntityInfo info : clientsToRegister) {
// $NON-NLS-1$
logger.info(Messages.i18n.format("StorageExporter.RegisteringClient", info));
ClientVersionBean versionBean = storage.getClientVersion(info.organizationId, info.id, info.version);
Iterator<ContractBean> contractBeans = storage.getAllContracts(info.organizationId, info.id, info.version);
Client client = new Client();
client.setOrganizationId(versionBean.getClient().getOrganization().getId());
client.setClientId(versionBean.getClient().getId());
client.setVersion(versionBean.getVersion());
client.setApiKey(versionBean.getApikey());
Set<Contract> contracts = new HashSet<>();
while (contractBeans.hasNext()) {
ContractBean contractBean = contractBeans.next();
EntityInfo apiInfo = new EntityInfo(contractBean.getApi().getApi().getOrganization().getId(), contractBean.getApi().getApi().getId(), contractBean.getApi().getVersion());
if (apisToPublish.contains(apiInfo)) {
Contract contract = new Contract();
contract.setPlan(contractBean.getPlan().getPlan().getId());
contract.setApiId(contractBean.getApi().getApi().getId());
contract.setApiOrgId(contractBean.getApi().getApi().getOrganization().getId());
contract.setApiVersion(contractBean.getApi().getVersion());
contract.getPolicies().addAll(aggregateContractPolicies(contractBean, info));
contracts.add(contract);
}
}
client.setContracts(contracts);
// Next, register the client with *all* relevant gateways. This is done by
// looking up all referenced apis and getting the gateway information for them.
// Each of those gateways must be told about the client.
Map<String, IGatewayLink> links = new HashMap<>();
for (Contract contract : client.getContracts()) {
ApiVersionBean svb = storage.getApiVersion(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion());
Set<ApiGatewayBean> gateways = svb.getGateways();
if (gateways == null) {
// $NON-NLS-1$
throw new PublishingException("No gateways specified for api: " + svb.getApi().getName());
}
for (ApiGatewayBean apiGatewayBean : gateways) {
String gatewayId = apiGatewayBean.getGatewayId();
if (!links.containsKey(gatewayId)) {
IGatewayLink gatewayLink = createGatewayLink(gatewayId);
links.put(gatewayId, gatewayLink);
}
}
}
for (IGatewayLink gatewayLink : links.values()) {
try {
gatewayLink.registerClient(client);
} catch (Exception e) {
throw new StorageException(e);
}
}
}
}
use of io.apiman.gateway.engine.beans.Client 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()));
}
});
}
Aggregations