Search in sources :

Example 16 with IGatewayLink

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;
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ApiVersionEndpointSummaryBean(io.apiman.manager.api.beans.summary.ApiVersionEndpointSummaryBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink)

Example 17 with IGatewayLink

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);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) GatewayEndpointSummaryBean(io.apiman.manager.api.beans.summary.GatewayEndpointSummaryBean) NewGatewayBean(io.apiman.manager.api.beans.gateways.NewGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) UpdateGatewayBean(io.apiman.manager.api.beans.gateways.UpdateGatewayBean) GatewayEndpoint(io.apiman.gateway.engine.beans.GatewayEndpoint) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) GatewayAlreadyExistsException(io.apiman.manager.api.rest.exceptions.GatewayAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException)

Example 18 with IGatewayLink

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();
}
Also used : ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) Oas20Document(io.apicurio.datamodels.openapi.v2.models.Oas20Document) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) URI(java.net.URI) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink)

Example 19 with IGatewayLink

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();
        }
    }
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ApiRegistryBean(io.apiman.manager.api.beans.summary.ApiRegistryBean) HashMap(java.util.HashMap) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) ApiEntryBean(io.apiman.manager.api.beans.summary.ApiEntryBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 20 with IGatewayLink

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()));
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) HashMap(java.util.HashMap) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) ActionException(io.apiman.manager.api.rest.exceptions.ActionException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) Date(java.util.Date) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) Client(io.apiman.gateway.engine.beans.Client) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Aggregations

IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)23 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)19 StorageException (io.apiman.manager.api.core.exceptions.StorageException)17 GatewayBean (io.apiman.manager.api.beans.gateways.GatewayBean)15 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)14 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)13 GatewayNotFoundException (io.apiman.manager.api.rest.exceptions.GatewayNotFoundException)13 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)11 ActionException (io.apiman.manager.api.rest.exceptions.ActionException)9 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)9 ClientVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException)9 PlanVersionNotFoundException (io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException)9 Date (java.util.Date)8 Api (io.apiman.gateway.engine.beans.Api)7 ClientVersionBean (io.apiman.manager.api.beans.clients.ClientVersionBean)7 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)7 HashMap (java.util.HashMap)7 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)6 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)6 ArrayList (java.util.ArrayList)6