use of io.apiman.manager.api.gateway.IGatewayLink in project apiman by apiman.
the class ApiService method getApiVersionEndpointInfoFromStorage.
private ApiVersionEndpointSummaryBean getApiVersionEndpointInfoFromStorage(ApiVersionBean apiVersion, String organizationId, String apiId, String version) throws GatewayNotFoundException, GatewayAuthenticationException, StorageException {
Set<ApiGatewayBean> gateways = apiVersion.getGateways();
if (gateways.isEmpty()) {
// $NON-NLS-1$
throw new SystemErrorException("No Gateways for published API!");
}
GatewayBean gateway = storage.getGateway(gateways.iterator().next().getGatewayId());
if (gateway == null) {
throw new GatewayNotFoundException();
} else {
// $NON-NLS-1$
LOGGER.debug(String.format("Got endpoint summary: %s", gateway));
}
IGatewayLink link = gatewayLinkFactory.create(gateway);
ApiEndpoint endpoint = link.getApiEndpoint(organizationId, apiId, version);
ApiVersionEndpointSummaryBean rval = new ApiVersionEndpointSummaryBean();
rval.setManagedEndpoint(endpoint.getEndpoint());
return rval;
}
use of io.apiman.manager.api.gateway.IGatewayLink in project apiman by apiman.
the class GatewayResourceImpl method getGatewayEndpoint.
/**
* @see IGatewayResource#getGatewayEndpoint(java.lang.String)
*/
public GatewayEndpointSummaryBean getGatewayEndpoint(String gatewayId) throws GatewayNotFoundException {
// No permission check is needed
try {
GatewayBean gateway = storage.getGateway(gatewayId);
if (gateway == null) {
throw ExceptionFactory.gatewayNotFoundException(gatewayId);
} else {
// $NON-NLS-1$
LOGGER.debug(String.format("Got endpoint summary: %s", gateway));
}
IGatewayLink link = gatewayLinkFactory.create(gateway);
GatewayEndpoint endpoint = link.getGatewayEndpoint();
GatewayEndpointSummaryBean gatewayEndpoint = new GatewayEndpointSummaryBean();
gatewayEndpoint.setEndpoint(endpoint.getEndpoint());
return gatewayEndpoint;
} catch (AbstractRestException e) {
throw e;
} catch (Exception e) {
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.gateway.IGatewayLink in project apiman by apiman.
the class OpenApi2 method rewrite.
@Override
public void rewrite(ProviderContext ctx, Document schema) throws StorageException, GatewayAuthenticationException {
// Prepare the data we need to extract to build the servers
ApiVersionBean avb = ctx.getAvb();
String orgId = avb.getApi().getOrganization().getId();
String apiId = avb.getApi().getId();
String apiVersion = avb.getVersion();
// Find IDs of all GWs this ApiVersion is published onto.
String firstGateway = avb.getGateways().stream().map(ApiGatewayBean::getGatewayId).findFirst().orElse("");
GatewayBean gateway = ctx.getStorage().getGateway(firstGateway);
IGatewayLink link = ctx.getGatewayLinkFactory().create(gateway);
String apiEndpoint = link.getApiEndpoint(orgId, apiId, apiVersion).getEndpoint();
URI apiEndpointUri = URI.create(apiEndpoint);
// We can guarantee it's an OAS2.x doc (aka. Swagger v2).
Oas20Document oas2 = (Oas20Document) schema;
if (apiEndpointUri.getPort() == -1) {
oas2.host = apiEndpointUri.getHost();
} else {
oas2.host = apiEndpointUri.getHost() + ":" + apiEndpointUri.getPort();
}
oas2.basePath = apiEndpointUri.getPath();
}
use of io.apiman.manager.api.gateway.IGatewayLink in project apiman by apiman.
the class OrganizationService method getApiRegistry.
/**
* Gets the API registry.
*/
public ApiRegistryBean getApiRegistry(String organizationId, String clientId, String version) throws ClientVersionNotFoundException {
// Try to get the client first - will throw a ClientVersionNotFoundException if not found.
ClientVersionBean clientVersion = clientService.getClientVersion(organizationId, clientId, version);
// TODO need to be careful with null on setGatewayId below
Map<String, IGatewayLink> gatewayLinks = new HashMap<>();
Map<String, GatewayBean> gateways = new HashMap<>();
try {
ApiRegistryBean apiRegistry = query.getApiRegistry(organizationId, clientId, version);
apiRegistry.setApiKey(clientVersion.getApikey());
List<ApiEntryBean> apis = apiRegistry.getApis();
for (ApiEntryBean api : apis) {
String gatewayId = api.getGatewayId();
// Don't return the gateway id.
api.setGatewayId(null);
GatewayBean gateway = gateways.get(gatewayId);
if (gateway == null) {
gateway = storage.getGateway(gatewayId);
gateways.put(gatewayId, gateway);
}
IGatewayLink link = gatewayLinks.get(gatewayId);
if (link == null) {
link = gatewayLinkFactory.create(gateway);
gatewayLinks.put(gatewayId, link);
}
ApiEndpoint se = link.getApiEndpoint(api.getApiOrgId(), api.getApiId(), api.getApiVersion());
String apiEndpoint = se.getEndpoint();
api.setHttpEndpoint(apiEndpoint);
}
return apiRegistry;
} catch (StorageException | GatewayAuthenticationException e) {
throw new SystemErrorException(e);
} finally {
for (IGatewayLink link : gatewayLinks.values()) {
link.close();
}
}
}
use of io.apiman.manager.api.gateway.IGatewayLink in project apiman by apiman.
the class ActionResourceImpl method unregisterClient.
/**
* De-registers an client that is currently registered with the gateway.
* @param action
*/
private void unregisterClient(ActionBean action) throws ActionException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.clientAdmin, action.getOrganizationId());
ClientVersionBean versionBean;
List<ContractSummaryBean> contractBeans;
try {
versionBean = orgs.getClientVersion(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
} catch (ClientVersionNotFoundException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"));
}
try {
contractBeans = query.getClientContracts(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
} catch (StorageException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"), e);
}
// Validate that it's ok to perform this action - client must be Ready.
if (versionBean.getStatus() != ClientStatus.Registered) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidClientStatus"));
}
Client client = new Client();
client.setOrganizationId(versionBean.getClient().getOrganization().getId());
client.setClientId(versionBean.getClient().getId());
client.setVersion(versionBean.getVersion());
// Each of those gateways must be told about the client.
try {
storage.beginTx();
Map<String, IGatewayLink> links = new HashMap<>();
for (ContractSummaryBean contractBean : contractBeans) {
ApiVersionBean svb = storage.getApiVersion(contractBean.getApiOrganizationId(), contractBean.getApiId(), contractBean.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) {
if (!links.containsKey(apiGatewayBean.getGatewayId())) {
IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
links.put(apiGatewayBean.getGatewayId(), gatewayLink);
}
}
}
storage.commitTx();
for (IGatewayLink gatewayLink : links.values()) {
gatewayLink.unregisterClient(client);
gatewayLink.close();
}
} catch (Exception e) {
storage.rollbackTx();
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("UnregisterError"), e);
}
versionBean.setStatus(ClientStatus.Retired);
versionBean.setRetiredOn(new Date());
try {
storage.beginTx();
storage.updateClientVersion(versionBean);
storage.createAuditEntry(AuditUtils.clientUnregistered(versionBean, securityContext));
storage.commitTx();
} catch (Exception e) {
storage.rollbackTx();
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("UnregisterError"), e);
}
log.debug(// $NON-NLS-1$
String.format(// $NON-NLS-1$
"Successfully registered Client %s on specified gateways: %s", versionBean.getClient().getName(), versionBean.getClient()));
}
Aggregations