use of io.apiman.manager.api.beans.summary.GatewaySummaryBean in project apiman by apiman.
the class OrganizationResourceImpl method createApiVersion.
/**
* @see IOrganizationResource#createApiVersion(java.lang.String, java.lang.String, io.apiman.manager.api.beans.apis.NewApiVersionBean)
*/
@Override
public ApiVersionBean createApiVersion(String organizationId, String apiId, NewApiVersionBean bean) throws ApiNotFoundException, NotAuthorizedException, InvalidVersionException, ApiVersionAlreadyExistsException {
securityContext.checkPermissions(PermissionType.apiEdit, organizationId);
FieldValidator.validateVersion(bean.getVersion());
ApiVersionBean newVersion;
try {
GatewaySummaryBean gateway = getSingularGateway();
storage.beginTx();
ApiBean api = getApiFromStorage(organizationId, apiId);
if (storage.getApiVersion(organizationId, apiId, bean.getVersion()) != null) {
throw ExceptionFactory.apiVersionAlreadyExistsException(apiId, bean.getVersion());
}
newVersion = createApiVersionInternal(bean, api, gateway);
storage.commitTx();
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
if (bean.isClone() && bean.getCloneVersion() != null) {
try {
ApiVersionBean cloneSource = getApiVersion(organizationId, apiId, bean.getCloneVersion());
// Clone primary attributes of the API version unless those attributes
// were included in the NewApiVersionBean. In other words, information
// sent as part of the "create version" payload take precedence over the
// cloned attributes.
UpdateApiVersionBean updatedApi = new UpdateApiVersionBean();
if (bean.getEndpoint() == null) {
updatedApi.setEndpoint(cloneSource.getEndpoint());
}
if (bean.getEndpointType() == null) {
updatedApi.setEndpointType(cloneSource.getEndpointType());
}
if (bean.getEndpointContentType() == null) {
updatedApi.setEndpointContentType(cloneSource.getEndpointContentType());
}
updatedApi.setEndpointProperties(cloneSource.getEndpointProperties());
updatedApi.setGateways(cloneSource.getGateways());
if (bean.getPlans() == null) {
updatedApi.setPlans(cloneSource.getPlans());
}
if (bean.getPublicAPI() == null) {
updatedApi.setPublicAPI(cloneSource.isPublicAPI());
}
if (bean.getParsePayload() == null) {
updatedApi.setParsePayload(bean.getParsePayload());
}
newVersion = updateApiVersion(organizationId, apiId, bean.getVersion(), updatedApi);
if (bean.getDefinitionUrl() == null) {
// Clone the API definition document
InputStream definition = null;
try {
Response response = getApiDefinition(organizationId, apiId, bean.getCloneVersion());
definition = (InputStream) response.getEntity();
storeApiDefinition(organizationId, apiId, newVersion.getVersion(), cloneSource.getDefinitionType(), definition, cloneSource.getDefinitionUrl());
} catch (ApiDefinitionNotFoundException svnfe) {
// This is ok - it just means the API doesn't have one, so do nothing.
} catch (Exception sdnfe) {
// $NON-NLS-1$
log.error("Unable to create response", sdnfe);
} finally {
IOUtils.closeQuietly(definition);
}
}
// Clone all API policies
List<PolicySummaryBean> policies = listApiPolicies(organizationId, apiId, bean.getCloneVersion());
for (PolicySummaryBean policySummary : policies) {
PolicyBean policy = getApiPolicy(organizationId, apiId, bean.getCloneVersion(), policySummary.getId());
NewPolicyBean npb = new NewPolicyBean();
npb.setDefinitionId(policy.getDefinition().getId());
npb.setConfiguration(policy.getConfiguration());
createApiPolicy(organizationId, apiId, newVersion.getVersion(), npb);
}
} catch (Exception e) {
// TODO it's ok if the clone fails - we did our best
if (e != null) {
Throwable t = e;
e = (Exception) t;
}
}
}
return newVersion;
}
use of io.apiman.manager.api.beans.summary.GatewaySummaryBean in project apiman by apiman.
the class ApiService method createApi.
public ApiBeanDto createApi(String organizationId, NewApiBean bean) throws OrganizationNotFoundException, ApiAlreadyExistsException, NotAuthorizedException, InvalidNameException {
FieldValidator.validateName(bean.getName());
ApiBean newApi = new ApiBean();
newApi.setName(bean.getName());
newApi.setDescription(bean.getDescription());
newApi.setId(BeanUtils.idFromName(bean.getName()));
newApi.setCreatedOn(new Date());
newApi.setCreatedBy(securityContext.getCurrentUser());
newApi.setImage(bean.getImage());
newApi.setTags(tagMapper.toEntity(bean.getTags()));
return tryAction(() -> {
GatewaySummaryBean gateway = getSingularGateway();
OrganizationBean orgBean = organizationService.getOrg(organizationId);
if (storage.getApi(orgBean.getId(), newApi.getId()) != null) {
throw ExceptionFactory.apiAlreadyExistsException(bean.getName());
}
newApi.setOrganization(orgBean);
// Store/persist the new API
storage.createApi(newApi);
storage.createAuditEntry(AuditUtils.apiCreated(newApi, securityContext));
if (bean.getInitialVersion() != null) {
NewApiVersionBean newApiVersion = new NewApiVersionBean();
newApiVersion.setEndpoint(bean.getEndpoint());
newApiVersion.setEndpointType(bean.getEndpointType());
newApiVersion.setEndpointContentType(bean.getEndpointContentType());
newApiVersion.setPlans(bean.getPlans());
newApiVersion.setPublicAPI(bean.getPublicAPI());
newApiVersion.setParsePayload(bean.getParsePayload());
newApiVersion.setDisableKeysStrip(bean.getDisableKeysStrip());
newApiVersion.setVersion(bean.getInitialVersion());
newApiVersion.setDefinitionUrl(bean.getDefinitionUrl());
newApiVersion.setDefinitionType(bean.getDefinitionType());
newApiVersion.setExtendedDescription(bean.getExtendedDescription());
createApiVersionInternal(newApiVersion, newApi, gateway);
}
return apiMapper.toDto(newApi);
});
}
use of io.apiman.manager.api.beans.summary.GatewaySummaryBean in project apiman by apiman.
the class JpaStorage method listGateways.
/**
* {@inheritDoc}
*/
@Override
public List<GatewaySummaryBean> listGateways() throws StorageException {
try {
EntityManager entityManager = getActiveEntityManager();
String sql = "SELECT g.id, g.name, g.description, g.type" + " FROM gateways g" + " ORDER BY g.name ASC";
Query query = entityManager.createNativeQuery(sql);
List<Object[]> rows = query.getResultList();
List<GatewaySummaryBean> gateways = new ArrayList<>(rows.size());
for (Object[] row : rows) {
GatewaySummaryBean gateway = new GatewaySummaryBean();
gateway.setId(String.valueOf(row[0]));
gateway.setName(String.valueOf(row[1]));
gateway.setDescription(String.valueOf(row[2]));
gateway.setType(GatewayType.valueOf(String.valueOf(row[3])));
gateways.add(gateway);
}
return gateways;
} catch (Throwable t) {
LOGGER.error(t.getMessage(), t);
throw new StorageException(t);
}
}
use of io.apiman.manager.api.beans.summary.GatewaySummaryBean in project apiman by apiman.
the class ApiService method createApiVersion.
public ApiVersionBeanDto createApiVersion(String organizationId, String apiId, NewApiVersionBean newApiVersion) throws ApiNotFoundException, NotAuthorizedException, InvalidVersionException, ApiVersionAlreadyExistsException {
FieldValidator.validateVersion(newApiVersion.getVersion());
ApiVersionBean newVersion = tryAction(() -> {
GatewaySummaryBean gateway = getSingularGateway();
ApiBean api = getApiFromStorage(organizationId, apiId);
if (storage.getApiVersion(organizationId, apiId, newApiVersion.getVersion()) != null) {
throw ExceptionFactory.apiVersionAlreadyExistsException(apiId, newApiVersion.getVersion());
}
return createApiVersionInternal(newApiVersion, api, gateway);
});
if (newApiVersion.isClone() && newApiVersion.getCloneVersion() != null) {
try {
ApiVersionBean cloneSource = storage.getApiVersion(organizationId, apiId, newApiVersion.getCloneVersion());
// storage.flush();
// Clone primary attributes of the API version unless those attributes
// were included in the NewApiVersionBean. In other words, information
// sent as part of the "create version" payload take precedence over the
// cloned attributes.
UpdateApiVersionBean updatedApi = new UpdateApiVersionBean();
if (newApiVersion.getEndpoint() == null) {
updatedApi.setEndpoint(cloneSource.getEndpoint());
}
if (newApiVersion.getEndpointType() == null) {
updatedApi.setEndpointType(cloneSource.getEndpointType());
}
if (newApiVersion.getEndpointContentType() == null) {
updatedApi.setEndpointContentType(cloneSource.getEndpointContentType());
}
updatedApi.setEndpointProperties(cloneSource.getEndpointProperties());
updatedApi.setGateways(cloneSource.getGateways());
if (newApiVersion.getPlans() == null) {
Set<UpdateApiPlanDto> plans = ApiVersionMapper.INSTANCE.toDto2(cloneSource.getPlans());
updatedApi.setPlans(plans);
}
if (newApiVersion.getPublicAPI() == null) {
updatedApi.setPublicAPI(cloneSource.isPublicAPI());
}
if (newApiVersion.getParsePayload() == null) {
updatedApi.setParsePayload(cloneSource.isParsePayload());
}
if (newApiVersion.getExtendedDescription() == null) {
updatedApi.setExtendedDescription(cloneSource.getExtendedDescription());
}
ApiVersionBean updated = updateApiVersion(newVersion, updatedApi);
if (newApiVersion.getDefinitionUrl() == null) {
// Clone the API definition document
InputStream definition = null;
try {
definition = getApiDefinition(organizationId, apiId, newApiVersion.getCloneVersion()).getDefinition();
setApiDefinition(organizationId, apiId, updated.getVersion(), cloneSource.getDefinitionType(), definition, cloneSource.getDefinitionUrl());
} catch (ApiDefinitionNotFoundException svnfe) {
// This is ok - it just means the API doesn't have one, so do nothing.
} catch (Exception sdnfe) {
// $NON-NLS-1$
LOGGER.error("Unable to create response", sdnfe);
} finally {
IOUtils.closeQuietly(definition);
}
}
// Clone all API policies
List<PolicySummaryBean> policies = listApiPolicies(organizationId, apiId, newApiVersion.getCloneVersion());
for (PolicySummaryBean policySummary : policies) {
PolicyBean policy = getApiPolicy(organizationId, apiId, newApiVersion.getCloneVersion(), policySummary.getId());
NewPolicyBean npb = new NewPolicyBean();
npb.setDefinitionId(policy.getDefinition().getId());
npb.setConfiguration(policy.getConfiguration());
createApiPolicy(organizationId, apiId, updated.getVersion(), npb);
}
} catch (Exception e) {
e.printStackTrace();
// TODO it's ok if the clone fails - we did our best
if (e != null) {
Throwable t = e;
e = (Exception) t;
}
}
}
return apiVersionMapper.toDto(newVersion);
}
use of io.apiman.manager.api.beans.summary.GatewaySummaryBean in project apiman by apiman.
the class ApiService method updateApiVersionInternal.
private ApiVersionBean updateApiVersionInternal(ApiVersionBean avb, UpdateApiVersionBean update) throws ApiVersionNotFoundException {
if (avb.getStatus() == ApiStatus.Retired) {
throw ExceptionFactory.invalidApiStatusException();
}
avb.setModifiedBy(securityContext.getCurrentUser());
avb.setModifiedOn(new Date());
EntityUpdatedData data = new EntityUpdatedData();
if (AuditUtils.valueChanged(avb.getPlans(), update.getPlans())) {
Set<ApiPlanBean> updateEntities = update.getPlans().stream().map(dto -> ApiPlanMapper.INSTANCE.fromDto(dto, avb)).collect(Collectors.toSet());
// $NON-NLS-1$
data.addChange("plans", AuditUtils.asString_ApiPlanBeans(avb.getPlans()), AuditUtils.asString_ApiPlanBeans(updateEntities));
if (update.getPlans() != null) {
// Work around: https://hibernate.atlassian.net/browse/HHH-3799
// Step 1: Set intersection
Set<UpdateApiPlanDto> existingAsDto = ApiPlanMapper.INSTANCE.toDto(avb.getPlans());
existingAsDto.retainAll(update.getPlans());
// Step 2: Upsert
Set<ApiPlanBean> mergedPlans = new HashSet<>();
for (ApiPlanBean updateApb : updateEntities) {
existingAsDto.stream().map(e -> ApiPlanMapper.INSTANCE.fromDto(e, avb)).filter(e -> e.equals(updateApb)).findAny().ifPresentOrElse(// Merge (existing element to be updated)
ep -> {
ApiPlanMapper.INSTANCE.merge(updateApb, ep);
mergedPlans.add(ep);
}, // Insert (new element)
() -> {
mergedPlans.add(updateApb);
});
}
avb.setPlans(mergedPlans);
tryAction(() -> storage.merge(avb));
}
}
if (AuditUtils.valueChanged(avb.getGateways(), update.getGateways())) {
// $NON-NLS-1$
data.addChange("gateways", AuditUtils.asString_ApiGatewayBeans(avb.getGateways()), AuditUtils.asString_ApiGatewayBeans(update.getGateways()));
if (avb.getGateways() == null) {
avb.setGateways(new HashSet<>());
}
avb.getGateways().clear();
avb.getGateways().addAll(update.getGateways());
}
if (AuditUtils.valueChanged(avb.getEndpoint(), update.getEndpoint())) {
// validate the endpoint is a URL
validateEndpoint(update.getEndpoint());
// $NON-NLS-1$
data.addChange("endpoint", avb.getEndpoint(), update.getEndpoint());
avb.setEndpoint(update.getEndpoint());
}
if (AuditUtils.valueChanged(avb.getEndpointType(), update.getEndpointType())) {
// $NON-NLS-1$
data.addChange("endpointType", avb.getEndpointType(), update.getEndpointType());
avb.setEndpointType(update.getEndpointType());
}
if (AuditUtils.valueChanged(avb.getEndpointContentType(), update.getEndpointContentType())) {
// $NON-NLS-1$
data.addChange("endpointContentType", avb.getEndpointContentType(), update.getEndpointContentType());
avb.setEndpointContentType(update.getEndpointContentType());
}
if (AuditUtils.valueChanged(avb.getEndpointProperties(), update.getEndpointProperties())) {
if (avb.getEndpointProperties() == null) {
avb.setEndpointProperties(new HashMap<>());
} else {
avb.getEndpointProperties().clear();
}
if (update.getEndpointProperties() != null) {
avb.getEndpointProperties().putAll(update.getEndpointProperties());
}
}
if (AuditUtils.valueChanged(avb.isPublicAPI(), update.getPublicAPI())) {
// $NON-NLS-1$
data.addChange("publicAPI", String.valueOf(avb.isPublicAPI()), String.valueOf(update.getPublicAPI()));
avb.setPublicAPI(update.getPublicAPI());
}
if (AuditUtils.valueChanged(avb.isParsePayload(), update.getParsePayload())) {
// $NON-NLS-1$
data.addChange("parsePayload", String.valueOf(avb.isParsePayload()), String.valueOf(update.getParsePayload()));
avb.setParsePayload(update.getParsePayload());
}
if (AuditUtils.valueChanged(avb.getDisableKeysStrip(), update.getDisableKeysStrip())) {
// $NON-NLS-1$
data.addChange("disableKeysStrip", String.valueOf(avb.getDisableKeysStrip()), String.valueOf(update.getDisableKeysStrip()));
avb.setDisableKeysStrip(update.getDisableKeysStrip());
}
if (AuditUtils.valueChanged(avb.getExtendedDescription(), update.getExtendedDescription())) {
// $NON-NLS-1$
data.addChange("extendedDescription", String.valueOf(avb.getExtendedDescription()), String.valueOf(update.getExtendedDescription()));
avb.setExtendedDescription(update.getExtendedDescription());
}
if (AuditUtils.valueChanged(avb.getDiscoverability(), update.getPublicDiscoverability())) {
// $NON-NLS-1$
data.addChange("discoverability", String.valueOf(avb.getDiscoverability()), String.valueOf(update.getPublicDiscoverability()));
avb.setDiscoverability(update.getPublicDiscoverability());
}
return tryAction(() -> {
if (avb.getGateways() == null || avb.getGateways().isEmpty()) {
GatewaySummaryBean gateway = getSingularGateway();
if (gateway != null && avb.getGateways() == null) {
avb.setGateways(new HashSet<>());
ApiGatewayBean sgb = new ApiGatewayBean();
sgb.setGatewayId(gateway.getId());
avb.getGateways().add(sgb);
}
}
if (avb.getStatus() != ApiStatus.Published) {
if (apiValidator.isReady(avb)) {
avb.setStatus(ApiStatus.Ready);
} else {
avb.setStatus(ApiStatus.Created);
}
} else {
if (!apiValidator.isReady(avb)) {
throw ExceptionFactory.invalidApiStatusException();
}
}
encryptEndpointProperties(avb);
// Ensure all the plans are in the right status (locked)
Set<ApiPlanBean> plans = avb.getPlans();
if (plans != null) {
for (ApiPlanBean splanBean : plans) {
String orgId = avb.getApi().getOrganization().getId();
PlanVersionBean pvb = storage.getPlanVersion(orgId, splanBean.getPlanId(), splanBean.getVersion());
if (pvb == null) {
// $NON-NLS-1$
throw new StorageException(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.updateApiVersion(avb);
storage.createAuditEntry(AuditUtils.apiVersionUpdated(avb, data, securityContext));
// $NON-NLS-1$
LOGGER.debug(String.format("Successfully updated API Version: %s", avb));
decryptEndpointProperties(avb);
return avb;
});
}
Aggregations