Search in sources :

Example 1 with ApiEntryBean

use of io.apiman.manager.api.beans.summary.ApiEntryBean in project apiman by apiman.

the class OrganizationResourceImpl method getApiRegistry.

/**
 * Gets the API registry.
 * @param organizationId
 * @param clientId
 * @param version
 * @throws ClientVersionNotFoundException
 */
private 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 = getClientVersionInternal(organizationId, clientId, version);
    Map<String, IGatewayLink> gatewayLinks = new HashMap<>();
    Map<String, GatewayBean> gateways = new HashMap<>();
    boolean txStarted = false;
    try {
        ApiRegistryBean apiRegistry = query.getApiRegistry(organizationId, clientId, version);
        apiRegistry.setApiKey(clientVersion.getApikey());
        List<ApiEntryBean> apis = apiRegistry.getApis();
        storage.beginTx();
        txStarted = true;
        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 {
        if (txStarted) {
            storage.rollbackTx();
        }
        for (IGatewayLink link : gatewayLinks.values()) {
            link.close();
        }
    }
}
Also used : NewClientVersionBean(io.apiman.manager.api.beans.clients.NewClientVersionBean) 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) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) GatewayBean(io.apiman.manager.api.beans.gateways.GatewayBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 2 with ApiEntryBean

use of io.apiman.manager.api.beans.summary.ApiEntryBean in project apiman by apiman.

the class JpaStorage method getApiRegistry.

/**
 * {@inheritDoc}
 */
@Override
public ApiRegistryBean getApiRegistry(String organizationId, String clientId, String version) throws StorageException {
    ApiRegistryBean rval = new ApiRegistryBean();
    try {
        EntityManager entityManager = getActiveEntityManager();
        String jpql = "SELECT c from ContractBean c " + "  JOIN c.client clientv " + "  JOIN clientv.client client " + "  JOIN client.organization aorg" + " WHERE client.id = :clientId " + "   AND aorg.id = :orgId " + "   AND clientv.version = :version " + " ORDER BY c.id ASC";
        TypedQuery<ContractBean> query = entityManager.createQuery(jpql, ContractBean.class).setParameter("orgId", organizationId).setParameter("clientId", clientId).setParameter("version", version);
        List<ContractBean> contracts = query.getResultList();
        for (ContractBean contractBean : contracts) {
            ApiVersionBean svb = contractBean.getApi();
            ApiBean api = svb.getApi();
            PlanBean plan = contractBean.getPlan().getPlan();
            OrganizationBean apiOrg = api.getOrganization();
            ApiEntryBean entry = new ApiEntryBean();
            entry.setApiId(api.getId());
            entry.setApiName(api.getName());
            entry.setApiOrgId(apiOrg.getId());
            entry.setApiOrgName(apiOrg.getName());
            entry.setApiVersion(svb.getVersion());
            entry.setPlanId(plan.getId());
            entry.setPlanName(plan.getName());
            entry.setPlanVersion(contractBean.getPlan().getVersion());
            Set<ApiGatewayBean> gateways = svb.getGateways();
            if (gateways != null && !gateways.isEmpty()) {
                ApiGatewayBean sgb = gateways.iterator().next();
                entry.setGatewayId(sgb.getGatewayId());
            }
            rval.getApis().add(entry);
        }
    } catch (Throwable t) {
        LOGGER.error(t.getMessage(), t);
        throw new StorageException(t);
    }
    return rval;
}
Also used : ApiRegistryBean(io.apiman.manager.api.beans.summary.ApiRegistryBean) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) PlanBean(io.apiman.manager.api.beans.plans.PlanBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) EntityManager(javax.persistence.EntityManager) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) ApiEntryBean(io.apiman.manager.api.beans.summary.ApiEntryBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 3 with ApiEntryBean

use of io.apiman.manager.api.beans.summary.ApiEntryBean 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 4 with ApiEntryBean

use of io.apiman.manager.api.beans.summary.ApiEntryBean in project apiman by apiman.

the class EsStorage method getApiRegistry.

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getApiRegistry(java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public ApiRegistryBean getApiRegistry(String organizationId, String clientId, String version) throws StorageException {
    @SuppressWarnings("nls") BoolQueryBuilder query = QueryBuilders.boolQuery();
    List<QueryBuilder> filter = query.filter();
    filter.add(QueryBuilders.termQuery("clientOrganizationId", organizationId));
    filter.add(QueryBuilders.termQuery("clientId", clientId));
    filter.add(QueryBuilders.termQuery("clientVersion", version));
    @SuppressWarnings("nls") SearchSourceBuilder builder = new SearchSourceBuilder().query(query).sort(new FieldSortBuilder("id").order(SortOrder.ASC)).size(500);
    List<SearchHit> hits = listEntities(INDEX_MANAGER_POSTFIX_CONTRACT, builder);
    ApiRegistryBean registry = new ApiRegistryBean();
    for (SearchHit hit : hits) {
        ApiEntryBean bean = EsMarshalling.unmarshallApiEntry(hit.getSourceAsMap());
        ApiVersionBean svb = getApiVersion(bean.getApiOrgId(), bean.getApiId(), bean.getApiVersion());
        Set<ApiGatewayBean> gateways = svb.getGateways();
        if (gateways != null && !gateways.isEmpty()) {
            ApiGatewayBean sgb = gateways.iterator().next();
            bean.setGatewayId(sgb.getGatewayId());
        }
        registry.getApis().add(bean);
    }
    return registry;
}
Also used : ApiRegistryBean(io.apiman.manager.api.beans.summary.ApiRegistryBean) SearchHit(org.elasticsearch.search.SearchHit) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ApiEntryBean(io.apiman.manager.api.beans.summary.ApiEntryBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Example 5 with ApiEntryBean

use of io.apiman.manager.api.beans.summary.ApiEntryBean in project apiman by apiman.

the class EsMarshalling method unmarshallApiEntry.

/**
 * Unmarshals the given map source into a bean.
 * @param source the source
 * @return the api entry
 */
public static ApiEntryBean unmarshallApiEntry(Map<String, Object> source) {
    if (source == null) {
        return null;
    }
    ApiEntryBean bean = new ApiEntryBean();
    bean.setApiOrgId(asString(source.get("apiOrganizationId")));
    bean.setApiOrgName(asString(source.get("apiOrganizationName")));
    bean.setApiId(asString(source.get("apiId")));
    bean.setApiName(asString(source.get("apiName")));
    bean.setApiVersion(asString(source.get("apiVersion")));
    bean.setPlanName(asString(source.get("planName")));
    bean.setPlanId(asString(source.get("planId")));
    bean.setPlanVersion(asString(source.get("planVersion")));
    postMarshall(bean);
    return bean;
}
Also used : ApiEntryBean(io.apiman.manager.api.beans.summary.ApiEntryBean)

Aggregations

ApiEntryBean (io.apiman.manager.api.beans.summary.ApiEntryBean)5 ApiRegistryBean (io.apiman.manager.api.beans.summary.ApiRegistryBean)4 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)3 StorageException (io.apiman.manager.api.core.exceptions.StorageException)3 ApiEndpoint (io.apiman.gateway.engine.beans.ApiEndpoint)2 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)2 ClientVersionBean (io.apiman.manager.api.beans.clients.ClientVersionBean)2 GatewayBean (io.apiman.manager.api.beans.gateways.GatewayBean)2 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)2 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)2 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)2 HashMap (java.util.HashMap)2 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)1 ApiPlanBean (io.apiman.manager.api.beans.apis.ApiPlanBean)1 NewClientVersionBean (io.apiman.manager.api.beans.clients.NewClientVersionBean)1 ContractBean (io.apiman.manager.api.beans.contracts.ContractBean)1 OrganizationBean (io.apiman.manager.api.beans.orgs.OrganizationBean)1 PlanBean (io.apiman.manager.api.beans.plans.PlanBean)1 EntityManager (javax.persistence.EntityManager)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1