/** * Generates an in-memory key for an client, used to index the client for later quick * retrieval. * @param client an client * @return a client key */ private String getClientIndex(Client client) { return getClientIndex(client.getOrganizationId(), client.getClientId(), client.getVersion()); }
/** * @see io.apiman.gateway.engine.IRegistry#registerClient(io.apiman.gateway.engine.beans.Client, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void registerClient(Client client, IAsyncResultHandler<Void> handler) { Set<Contract> contracts = client.getContracts(); if (contracts != null) { for (Contract contract : contracts) { List<Policy> policies = contract.getPolicies(); encryptPolicies(client.getOrganizationId(), client.getClientId(), client.getVersion(), EntityType.ClientApp, policies); } } delegate.registerClient(client, handler); if (contracts != null) { for (Contract contract : contracts) { List<Policy> policies = contract.getPolicies(); decryptPolicies(client.getOrganizationId(), client.getClientId(), client.getVersion(), EntityType.ClientApp, policies); } } }
/** * @see io.apiman.gateway.engine.IRegistry#registerClient(io.apiman.gateway.engine.beans.Client, io.apiman.gateway.engine.async.IAsyncResultHandler) */ @Override public void registerClient(Client client, IAsyncResultHandler<Void> handler) { Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false); QueryRunner run = new QueryRunner(); // Validate the client and populate the api map with apis found during validation. validateClient(client, conn); // Remove any old data first, then (re)insert run.update(conn, "DELETE FROM gw_clients WHERE org_id = ? AND id = ? AND version = ?", //$NON-NLS-1$ client.getOrganizationId(), client.getClientId(), client.getVersion()); String bean = mapper.writeValueAsString(client); run.update(conn, "INSERT INTO gw_clients (api_key, org_id, id, version, bean) VALUES (?, ?, ?, ?, ?)", //$NON-NLS-1$ client.getApiKey(), client.getOrganizationId(), client.getClientId(), client.getVersion(), bean); DbUtils.commitAndClose(conn); handler.handle(AsyncResultImpl.create((Void) null)); } catch (Exception re) { DbUtils.rollbackAndCloseQuietly(conn); handler.handle(AsyncResultImpl.create(re, Void.class)); } }
@Override public void unregister(String organizationId, String clientId, String version) throws RegistrationException, NotAuthorizedException { Client client = new Client(); client.setOrganizationId(organizationId); client.setClientId(clientId); client.setVersion(version); registry.unregisterClient(client, handlerWithEmptyResult()); }
@Override public void listClients(String organizationId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) { // For now, ignore paging, but it's there for future. Would need to ensure stable ordering. List<String> res = map.entrySet().stream() .map(Map.Entry::getValue) .filter(entity -> entity instanceof Client) .map(entity -> (Client) entity) .filter(client -> client.getOrganizationId().equals(organizationId)) .map(client -> client.getClientId()) .distinct() .collect(Collectors.toList()); handler.handle(AsyncResultImpl.create(res)); }
handler.handle(AsyncResultImpl.create(error, ApiContract.class)); } else { Optional<Contract> matchedOpt = client.getContracts().stream() .filter(contract -> contract.matches(apiOrganizationId, apiId, apiVersion)) .findFirst(); } else { Exception error = new NoContractFoundException(Messages.i18n.format("InMemoryRegistry.NoContractFound", //$NON-NLS-1$ client.getClientId(), api.getApiId())); handler.handle(AsyncResultImpl.create(error, ApiContract.class));
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()); client.setContracts(contracts); for (Contract contract : client.getContracts()) { ApiVersionBean svb = storage.getApiVersion(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion()); Set<ApiGatewayBean> gateways = svb.getGateways();
@SuppressWarnings("rawtypes") // CompositeFuture.all(list) requires raw futures. @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); objectMap.putIfAbsent(client.getApiKey(), client, putNewApiKeyFuture.completer()); objectMap.replace(client.getApiKey(), client, replaceClientFuture.completer()); } else { objectMap.putIfAbsent(clientIndex, client, replaceClientFuture.completer()); if (oldClientRaw != null && !oldClient.getApiKey().equals(client.getApiKey())) { objectMap.remove(oldClient.getApiKey(), endFuture.completer()); } else { endFuture.complete();
/** * Generates a valid document ID for the client - used to index the client in ES. * @param client * @return an id */ protected String getClientId(Client client) { return client.getApiKey(); }
/** * Validate that the client should be registered. * @param client */ private void validateClient(Client client) throws RegistrationException { Set<Contract> contracts = client.getContracts(); if (contracts.isEmpty()) { throw new NoContractFoundException(Messages.i18n.format("ESRegistry.NoContracts")); //$NON-NLS-1$ } for (Contract contract : contracts) { validateContract(contract); } }
/** * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((organizationId == null) ? 0 : organizationId.hashCode()); result = prime * result + ((getClientId() == null) ? 0 : getClientId().hashCode()); result = prime * result + ((version == null) ? 0 : version.hashCode()); return result; }
@Override public void listOrgs(IAsyncResultHandler<List<String>> handler) { // TODO: We should track set of OrgId -> AtomicCounter if this API has meaningfully high usage. List<String> res = map.entrySet().stream() .map(Map.Entry::getValue) .map(elem -> { if (elem instanceof Api) { return ((Api) elem).getOrganizationId(); } else { return ((Client) elem).getOrganizationId(); } }) .distinct() .collect(Collectors.toList()); handler.handle(AsyncResultImpl.create(res)); }
@Override public void unregister(String organizationId, String clientId, String version, AsyncResponse response) throws RegistrationException, NotAuthorizedException { Client client = new Client(); client.setOrganizationId(organizationId); client.setClientId(clientId); client.setVersion(version); registry.unregisterClient(client, handlerWithEmptyResult(response)); }
handler.handle(AsyncResultImpl.create(error, ApiContract.class)); } else { Optional<Contract> matchedOpt = client.getContracts().stream() .filter(contract -> contract.matches(apiOrganizationId, apiId, apiVersion)) .findFirst(); } else { Exception error = new NoContractFoundException(Messages.i18n.format("InMemoryRegistry.NoContractFound", //$NON-NLS-1$ client.getClientId(), api.getApiId())); handler.handle(AsyncResultImpl.create(error, ApiContract.class));
Client client = new Client(); client.setOrganizationId(versionBean.getClient().getOrganization().getId()); client.setClientId(versionBean.getClient().getId()); client.setVersion(versionBean.getVersion()); client.setApiKey(versionBean.getApikey()); contracts.add(contract); client.setContracts(contracts); storage.beginTx(); 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();
@Override public void listClients(String organizationId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) { // For now, ignore paging, but it's there for future. Would need to ensure stable ordering. List<String> res = map.entrySet().stream() .map(Map.Entry::getValue) .filter(entity -> entity instanceof Client) .map(entity -> (Client) entity) .filter(client -> client.getOrganizationId().equals(organizationId)) .map(client -> client.getClientId()) .distinct() .collect(Collectors.toList()); handler.handle(AsyncResultImpl.create(res)); }
@SuppressWarnings("rawtypes") // CompositeFuture.all(list) requires raw futures. @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); objectMap.putIfAbsent(client.getApiKey(), client, putNewApiKeyFuture.completer()); objectMap.replace(client.getApiKey(), client, replaceClientFuture.completer()); } else { objectMap.putIfAbsent(clientIndex, client, replaceClientFuture.completer()); if (oldClientRaw != null && !oldClient.getApiKey().equals(client.getApiKey())) { objectMap.remove(oldClient.getApiKey(), endFuture.completer()); } else { endFuture.complete();
/** * Generates a valid document ID for the client - used to index the client in ES. * @param client * @return an id */ protected String getClientId(Client client) { return client.getApiKey(); }
/** * Validate that the client should be registered. * @param client */ private void validateClient(Client client) throws RegistrationException { Set<Contract> contracts = client.getContracts(); if (contracts.isEmpty()) { throw new NoContractFoundException(Messages.i18n.format("ESRegistry.NoContracts")); //$NON-NLS-1$ } for (Contract contract : contracts) { validateContract(contract); } }
/** * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Client other = (Client) obj; if (organizationId == null) { if (other.organizationId != null) return false; } else if (!organizationId.equals(other.organizationId)) return false; if (getClientId() == null) { if (other.getClientId() != null) return false; } else if (!getClientId().equals(other.getClientId())) return false; if (version == null) { if (other.version != null) return false; } else if (!version.equals(other.version)) return false; return true; }