/** * 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(); }
/** * 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(); }
/** * @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) { throw new ClientNotFoundException(Messages.i18n.format("InMemoryRegistry.ClientNotFound")); //$NON-NLS-1$ } } else { getMap().remove(oldClient.getApiKey()); } } }
/** * @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) { throw new ClientNotFoundException(Messages.i18n.format("InMemoryRegistry.ClientNotFound")); //$NON-NLS-1$ } } else { getMap().remove(oldClient.getApiKey()); } } }
/** * @see io.apiman.gateway.api.rest.contract.IClientResource#register(io.apiman.gateway.engine.beans.Client) */ @Override public void register(Client client) throws RegistrationException, NotAuthorizedException { if (client.getApiKey() == null) { throw new RegistrationException("Cannot Register Client: Missing API Key"); //$NON-NLS-1$ } final Set<Throwable> errorHolder = new HashSet<>(); final CountDownLatch latch = new CountDownLatch(1); // Register client; latch until result returned and evaluated getEngine().getRegistry().registerClient(client, latchedResultHandler(latch, errorHolder)); awaitOnLatch(latch, errorHolder); }
/** * @see io.apiman.gateway.api.rest.contract.IClientResource#register(io.apiman.gateway.engine.beans.Client) */ @Override public void register(Client client) throws RegistrationException, NotAuthorizedException { if (client.getApiKey() == null) { throw new RegistrationException("Cannot Register Client: Missing API Key"); //$NON-NLS-1$ } final Set<Throwable> errorHolder = new HashSet<>(); final CountDownLatch latch = new CountDownLatch(1); // Register client; latch until result returned and evaluated getEngine().getRegistry().registerClient(client, latchedResultHandler(latch, errorHolder)); awaitOnLatch(latch, errorHolder); }
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();
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();
/** * @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)); } }
/** * @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)); } }
/** * @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) { Exception error = null; synchronized (mutex) { // Validate the client first - we need to be able to resolve all the contracts. for (Contract contract : client.getContracts()) { String apiIdx = getApiIndex(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion()); if (!getMap().containsKey(apiIdx)) { error = new ApiNotFoundException(Messages.i18n.format("InMemoryRegistry.ApiNotFoundInOrg", //$NON-NLS-1$ contract.getApiId(), contract.getApiOrgId())); break; } } if (error == null) { // Unregister the client (if it exists) unregisterClientInternal(client, true); // Now, register the client. String clientIdx = getClientIndex(client); getMap().put(clientIdx, client); getMap().put(client.getApiKey(), client); handler.handle(AsyncResultImpl.create((Void) null)); } else { handler.handle(AsyncResultImpl.create(error, Void.class)); } } }
/** * @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) { Exception error = null; synchronized (mutex) { // Validate the client first - we need to be able to resolve all the contracts. for (Contract contract : client.getContracts()) { String apiIdx = getApiIndex(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion()); if (!getMap().containsKey(apiIdx)) { error = new ApiNotFoundException(Messages.i18n.format("InMemoryRegistry.ApiNotFoundInOrg", //$NON-NLS-1$ contract.getApiId(), contract.getApiOrgId())); break; } } if (error == null) { // Unregister the client (if it exists) unregisterClientInternal(client, true); // Now, register the client. String clientIdx = getClientIndex(client); getMap().put(clientIdx, client); getMap().put(client.getApiKey(), client); handler.handle(AsyncResultImpl.create((Void) null)); } else { handler.handle(AsyncResultImpl.create(error, Void.class)); } } }
@Override public void unregisterClient(Client client, IAsyncResultHandler<Void> resultHandler) { String clientIndex = getClientIndex(client); objectMap.get(clientIndex, handleSuccessfulResult(resultHandler, oldClientRaw -> { Client oldClient = (Client) oldClientRaw; if (oldClient == null) { Exception ex = new ClientNotFoundException(Messages.i18n.format("InMemoryRegistry.ClientNotFound")); resultHandler.handle(AsyncResultImpl.create(ex)); } else { Future<Object> future1 = Future.future(); Future<Object> future2 = Future.future(); objectMap.remove(clientIndex, future1.completer()); objectMap.remove(oldClient.getApiKey(), future2.completer()); CompositeFuture.all(future1, future2).setHandler(handleCompositeResult(resultHandler)); } })); }
@Override public void unregisterClient(Client client, IAsyncResultHandler<Void> resultHandler) { String clientIndex = getClientIndex(client); objectMap.get(clientIndex, handleSuccessfulResult(resultHandler, oldClientRaw -> { Client oldClient = (Client) oldClientRaw; if (oldClient == null) { Exception ex = new ClientNotFoundException(Messages.i18n.format("InMemoryRegistry.ClientNotFound")); resultHandler.handle(AsyncResultImpl.create(ex)); } else { Future<Object> future1 = Future.future(); Future<Object> future2 = Future.future(); objectMap.remove(clientIndex, future1.completer()); objectMap.remove(oldClient.getApiKey(), future2.completer()); CompositeFuture.all(future1, future2).setHandler(handleCompositeResult(resultHandler)); } })); }