Search in sources :

Example 6 with ApiPlanBean

use of io.apiman.manager.api.beans.apis.ApiPlanBean in project apiman by apiman.

the class ApiService method createApiVersionInternal.

/**
 * Creates an API version.
 */
protected ApiVersionBean createApiVersionInternal(NewApiVersionBean bean, ApiBean api, GatewaySummaryBean gateway) throws Exception {
    if (!BeanUtils.isValidVersion(bean.getVersion())) {
        // $NON-NLS-1$
        throw new StorageException("Invalid/illegal API version: " + bean.getVersion());
    }
    ApiVersionBean newVersion = new ApiVersionBean();
    newVersion.setVersion(bean.getVersion());
    newVersion.setCreatedBy(securityContext.getCurrentUser());
    newVersion.setCreatedOn(new Date());
    newVersion.setModifiedBy(securityContext.getCurrentUser());
    newVersion.setModifiedOn(new Date());
    newVersion.setStatus(ApiStatus.Created);
    newVersion.setApi(api);
    newVersion.setEndpoint(bean.getEndpoint());
    newVersion.setEndpointType(bean.getEndpointType());
    newVersion.setEndpointContentType(bean.getEndpointContentType());
    newVersion.setDefinitionUrl(bean.getDefinitionUrl());
    newVersion.setExtendedDescription(bean.getExtendedDescription());
    if (bean.getPublicAPI() != null) {
        newVersion.setPublicAPI(bean.getPublicAPI());
    }
    if (bean.getParsePayload() != null) {
        newVersion.setParsePayload(bean.getParsePayload());
    }
    if (bean.getDisableKeysStrip() != null) {
        newVersion.setDisableKeysStrip(bean.getDisableKeysStrip());
    }
    if (bean.getPlans() != null) {
        newVersion.setPlans(bean.getPlans());
    }
    if (bean.getDefinitionType() != null) {
        newVersion.setDefinitionType(bean.getDefinitionType());
    } else {
        newVersion.setDefinitionType(ApiDefinitionType.None);
    }
    if (gateway != null && newVersion.getGateways() == null) {
        newVersion.setGateways(new HashSet<>());
        ApiGatewayBean sgb = new ApiGatewayBean();
        sgb.setGatewayId(gateway.getId());
        newVersion.getGateways().add(sgb);
    }
    if (apiValidator.isReady(newVersion)) {
        newVersion.setStatus(ApiStatus.Ready);
    } else {
        newVersion.setStatus(ApiStatus.Created);
    }
    // Ensure all the plans are in the right status (locked)
    Set<ApiPlanBean> plans = newVersion.getPlans();
    if (plans != null) {
        for (ApiPlanBean splanBean : plans) {
            String orgId = newVersion.getApi().getOrganization().getId();
            PlanVersionBean pvb = storage.getPlanVersion(orgId, splanBean.getPlanId(), splanBean.getVersion());
            if (pvb == null) {
                throw new StorageException(// $NON-NLS-1$
                Messages.i18n.format("PlanVersionDoesNotExist", splanBean.getPlanId(), splanBean.getVersion()));
            }
            if (pvb.getStatus() != PlanStatus.Locked) {
                // $NON-NLS-1$
                throw new StorageException(Messages.i18n.format("PlanNotLocked", splanBean.getPlanId(), splanBean.getVersion()));
            }
        }
    }
    storage.createApiVersion(newVersion);
    if (bean.getDefinitionUrl() != null) {
        InputStream definition = null;
        try {
            definition = new URL(bean.getDefinitionUrl()).openStream();
            storage.updateApiDefinition(newVersion, definition);
        } catch (Exception e) {
            // $NON-NLS-1$
            LOGGER.error("Unable to store API definition from: " + bean.getDefinitionUrl(), e);
            // Set definition type silently to None
            newVersion.setDefinitionType(ApiDefinitionType.None);
            storage.updateApiVersion(newVersion);
        } finally {
            IOUtils.closeQuietly(definition);
        }
    }
    storage.createAuditEntry(AuditUtils.apiVersionCreated(newVersion, securityContext));
    return newVersion;
}
Also used : ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) InputStream(java.io.InputStream) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) NewApiVersionBean(io.apiman.manager.api.beans.apis.NewApiVersionBean) Date(java.util.Date) URL(java.net.URL) ApiVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) ApiAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) InvalidApiStatusException(io.apiman.manager.api.rest.exceptions.InvalidApiStatusException) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) MalformedURLException(java.net.MalformedURLException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean)

Example 7 with ApiPlanBean

use of io.apiman.manager.api.beans.apis.ApiPlanBean in project apiman by apiman.

the class AuditUtils method asString_ApiPlanBeans.

/**
 * Converts the list of plans to a string for display/comparison.
 * @param plans the plans
 * @return the plans as a string
 */
public static String asString_ApiPlanBeans(Set<ApiPlanBean> plans) {
    if (plans == null || plans.isEmpty()) {
        return "";
    }
    TreeSet<ApiPlanBean> sortedPlans = new TreeSet<>(new Comparator<ApiPlanBean>() {

        @Override
        public int compare(ApiPlanBean o1, ApiPlanBean o2) {
            // $NON-NLS-1$
            String p1 = o1.getPlanId() + ":" + o1.getVersion();
            // $NON-NLS-1$
            String p2 = o2.getPlanId() + ":" + o2.getVersion();
            return p1.compareTo(p2);
        }
    });
    sortedPlans.addAll(plans);
    StringBuilder builder = new StringBuilder();
    boolean first = true;
    if (plans != null) {
        for (ApiPlanBean plan : sortedPlans) {
            if (!first) {
                // $NON-NLS-1$
                builder.append(", ");
            }
            // $NON-NLS-1$
            builder.append(plan.getPlanId()).append(":").append(plan.getVersion());
            first = false;
        }
    }
    return builder.toString();
}
Also used : ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) TreeSet(java.util.TreeSet)

Example 8 with ApiPlanBean

use of io.apiman.manager.api.beans.apis.ApiPlanBean in project apiman by apiman.

the class EsStorage method getApiVersionPlans.

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getApiVersionPlans(java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public List<ApiPlanSummaryBean> getApiVersionPlans(String organizationId, String apiId, String version) throws StorageException {
    List<ApiPlanSummaryBean> rval = new ArrayList<>();
    ApiVersionBean versionBean = getApiVersion(organizationId, apiId, version);
    if (versionBean != null) {
        Set<ApiPlanBean> plans = versionBean.getPlans();
        if (plans != null) {
            for (ApiPlanBean spb : plans) {
                PlanBean planBean = getPlan(organizationId, spb.getPlanId());
                ApiPlanSummaryBean plan = new ApiPlanSummaryBean();
                plan.setPlanId(spb.getPlanId());
                plan.setVersion(spb.getVersion());
                plan.setPlanName(planBean.getName());
                plan.setPlanDescription(planBean.getDescription());
                rval.add(plan);
            }
        }
    }
    return rval;
}
Also used : ApiPlanSummaryBean(io.apiman.manager.api.beans.summary.ApiPlanSummaryBean) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) ArrayList(java.util.ArrayList) PlanBean(io.apiman.manager.api.beans.plans.PlanBean) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean)

Example 9 with ApiPlanBean

use of io.apiman.manager.api.beans.apis.ApiPlanBean in project apiman by apiman.

the class EsMarshalling method unmarshallApiVersion.

/**
 * Unmarshals the given map source into a bean.
 * @param source the source
 * @return the API version
 */
@SuppressWarnings("unchecked")
public static ApiVersionBean unmarshallApiVersion(Map<String, Object> source) {
    if (source == null) {
        return null;
    }
    ApiVersionBean bean = new ApiVersionBean();
    bean.setVersion(asString(source.get("version")));
    bean.setStatus(asEnum(source.get("status"), ApiStatus.class));
    bean.setCreatedBy(asString(source.get("createdBy")));
    bean.setCreatedOn(asDate(source.get("createdOn")));
    bean.setModifiedBy(asString(source.get("modifiedBy")));
    bean.setModifiedOn(asDate(source.get("modifiedOn")));
    bean.setPublishedOn(asDate(source.get("publishedOn")));
    bean.setRetiredOn(asDate(source.get("retiredOn")));
    bean.setEndpoint(asString(source.get("endpoint")));
    bean.setEndpointType(asEnum(source.get("endpointType"), EndpointType.class));
    bean.setEndpointContentType(asEnum(source.get("endpointContentType"), EndpointContentType.class));
    bean.setPublicAPI(asBoolean(source.get("publicAPI")));
    bean.setDefinitionType(asEnum(source.get("definitionType"), ApiDefinitionType.class));
    bean.setParsePayload(asBool(source.get("parsePayload")));
    bean.setDisableKeysStrip(asBool(source.get("disableKeysStrip")));
    bean.setDefinitionUrl(asString(source.get("definitionUrl")));
    bean.setGateways(new HashSet<>());
    List<Map<String, Object>> gateways = (List<Map<String, Object>>) source.get("gateways");
    if (gateways != null) {
        for (Map<String, Object> gatewayMap : gateways) {
            ApiGatewayBean gatewayBean = new ApiGatewayBean();
            gatewayBean.setGatewayId(asString(gatewayMap.get("gatewayId")));
            bean.getGateways().add(gatewayBean);
        }
    }
    bean.setPlans(new HashSet<>());
    List<Map<String, Object>> plans = (List<Map<String, Object>>) source.get("plans");
    if (plans != null) {
        for (Map<String, Object> planMap : plans) {
            ApiPlanBean planBean = new ApiPlanBean();
            planBean.setPlanId(asString(planMap.get("planId")));
            planBean.setVersion(asString(planMap.get("version")));
            bean.getPlans().add(planBean);
        }
    }
    Map<String, Object> endpointProperties = (Map<String, Object>) source.get("endpointProperties");
    if (endpointProperties != null) {
        bean.setEndpointProperties(new HashMap<>());
        for (Entry<String, Object> entry : endpointProperties.entrySet()) {
            bean.getEndpointProperties().put(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    postMarshall(bean);
    return bean;
}
Also used : ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) ApiStatus(io.apiman.manager.api.beans.apis.ApiStatus) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) EndpointType(io.apiman.manager.api.beans.apis.EndpointType) List(java.util.List) EndpointContentType(io.apiman.manager.api.beans.apis.EndpointContentType) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) Map(java.util.Map) HashMap(java.util.HashMap) ApiDefinitionType(io.apiman.manager.api.beans.apis.ApiDefinitionType)

Example 10 with ApiPlanBean

use of io.apiman.manager.api.beans.apis.ApiPlanBean in project apiman by apiman.

the class EsMarshalling method marshall.

/**
 * Marshals the given bean into the given map.
 * @param bean the bean
 * @return the content builder
 * @throws StorageException when a storage problem occurs while storing a bean
 */
public static XContentBuilder marshall(ApiVersionBean bean) throws StorageException {
    try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
        ApiBean api = bean.getApi();
        OrganizationBean org = api.getOrganization();
        preMarshall(bean);
        builder.startObject().field("organizationId", org.getId()).field("organizationName", org.getName()).field("apiId", api.getId()).field("apiName", api.getName()).field("apiDescription", api.getDescription()).field("version", bean.getVersion()).field("status", bean.getStatus()).field("createdBy", bean.getCreatedBy()).field("createdOn", bean.getCreatedOn().getTime()).field("modifiedBy", bean.getModifiedBy()).field("modifiedOn", bean.getModifiedOn().getTime()).field("publishedOn", bean.getPublishedOn() != null ? bean.getPublishedOn().getTime() : null).field("retiredOn", bean.getRetiredOn() != null ? bean.getRetiredOn().getTime() : null).field("publicAPI", bean.isPublicAPI()).field("endpoint", bean.getEndpoint()).field("endpointType", bean.getEndpointType()).field("endpointContentType", bean.getEndpointContentType()).field("parsePayload", bean.isParsePayload()).field("disableKeysStrip", bean.getDisableKeysStrip()).field("definitionType", bean.getDefinitionType()).field("definitionUrl", bean.getDefinitionUrl());
        Set<ApiGatewayBean> gateways = bean.getGateways();
        if (gateways != null) {
            builder.startArray("gateways");
            for (ApiGatewayBean gateway : gateways) {
                builder.startObject().field("gatewayId", gateway.getGatewayId()).endObject();
            }
            builder.endArray();
        }
        Set<ApiPlanBean> plans = bean.getPlans();
        if (plans != null) {
            builder.startArray("plans");
            for (ApiPlanBean plan : plans) {
                builder.startObject().field("planId", plan.getPlanId()).field("version", plan.getVersion()).endObject();
            }
            builder.endArray();
        }
        Map<String, String> endpointProperties = bean.getEndpointProperties();
        if (endpointProperties != null) {
            builder.startObject("endpointProperties");
            for (Entry<String, String> property : endpointProperties.entrySet()) {
                builder.field(property.getKey(), property.getValue());
            }
            builder.endObject();
        }
        builder.endObject();
        postMarshall(bean);
        return builder;
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
Also used : ApiBean(io.apiman.manager.api.beans.apis.ApiBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

ApiPlanBean (io.apiman.manager.api.beans.apis.ApiPlanBean)13 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)11 NewApiVersionBean (io.apiman.manager.api.beans.apis.NewApiVersionBean)7 UpdateApiVersionBean (io.apiman.manager.api.beans.apis.UpdateApiVersionBean)7 PlanVersionBean (io.apiman.manager.api.beans.plans.PlanVersionBean)7 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)6 StorageException (io.apiman.manager.api.core.exceptions.StorageException)6 Date (java.util.Date)6 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)5 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)4 ApiAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException)4 ApiDefinitionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)4 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)4 ApiVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException)4 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)4 EntityStillActiveException (io.apiman.manager.api.rest.exceptions.EntityStillActiveException)4 GatewayNotFoundException (io.apiman.manager.api.rest.exceptions.GatewayNotFoundException)4 InvalidApiStatusException (io.apiman.manager.api.rest.exceptions.InvalidApiStatusException)4 InvalidNameException (io.apiman.manager.api.rest.exceptions.InvalidNameException)4 InvalidParameterException (io.apiman.manager.api.rest.exceptions.InvalidParameterException)4