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;
}
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();
}
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;
}
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;
}
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);
}
}
Aggregations