use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.
the class ActionService method publishApi.
/**
* Publishes an API to the gateway.
*/
public void publishApi(String orgId, String apiId, String apiVersion) throws ActionException, NotAuthorizedException {
ApiVersionBean versionBean;
try {
versionBean = storage.getApiVersion(orgId, apiId, apiVersion);
} catch (ApiVersionNotFoundException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ApiNotFound"));
} catch (StorageException e) {
throw new RuntimeException(e);
}
// Validate that it's ok to perform this action - API must be Ready.
if (!versionBean.isPublicAPI() && versionBean.getStatus() != ApiStatus.Ready) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
}
if (versionBean.isPublicAPI()) {
if (versionBean.getStatus() == ApiStatus.Retired || versionBean.getStatus() == ApiStatus.Created) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
}
if (versionBean.getStatus() == ApiStatus.Published) {
Date modOn = versionBean.getModifiedOn();
Date publishedOn = versionBean.getPublishedOn();
int c = modOn.compareTo(publishedOn);
if (c <= 0) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ApiRePublishNotRequired"));
}
}
}
Api gatewayApi = new Api();
gatewayApi.setEndpoint(versionBean.getEndpoint());
gatewayApi.setEndpointType(versionBean.getEndpointType().toString());
if (versionBean.getEndpointContentType() != null) {
gatewayApi.setEndpointContentType(versionBean.getEndpointContentType().toString());
}
gatewayApi.setEndpointProperties(versionBean.getEndpointProperties());
gatewayApi.setOrganizationId(versionBean.getApi().getOrganization().getId());
gatewayApi.setApiId(versionBean.getApi().getId());
gatewayApi.setVersion(versionBean.getVersion());
gatewayApi.setPublicAPI(versionBean.isPublicAPI());
gatewayApi.setParsePayload(versionBean.isParsePayload());
gatewayApi.setKeysStrippingDisabled(versionBean.getDisableKeysStrip());
try {
if (versionBean.isPublicAPI()) {
List<Policy> policiesToPublish = new ArrayList<>();
List<PolicySummaryBean> apiPolicies = query.getPolicies(orgId, apiId, apiVersion, PolicyType.Api);
for (PolicySummaryBean policySummaryBean : apiPolicies) {
PolicyBean apiPolicy = storage.getPolicy(PolicyType.Api, orgId, apiId, apiVersion, policySummaryBean.getId());
Policy policyToPublish = new Policy();
policyToPublish.setPolicyJsonConfig(apiPolicy.getConfiguration());
policyToPublish.setPolicyImpl(apiPolicy.getDefinition().getPolicyImpl());
policiesToPublish.add(policyToPublish);
}
gatewayApi.setApiPolicies(policiesToPublish);
}
} catch (StorageException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
}
// Publish the API to all relevant gateways
try {
Set<ApiGatewayBean> gateways = versionBean.getGateways();
if (gateways == null) {
// $NON-NLS-1$
throw new PublishingException("No gateways specified for API!");
}
for (ApiGatewayBean apiGatewayBean : gateways) {
IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
gatewayLink.publishApi(gatewayApi);
gatewayLink.close();
}
versionBean.setStatus(ApiStatus.Published);
versionBean.setPublishedOn(new Date());
ApiBean api = storage.getApi(orgId, apiId);
if (api == null) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new PublishingException("Error: could not find API - " + orgId + "=>" + apiId);
}
if (api.getNumPublished() == null) {
api.setNumPublished(1);
} else {
api.setNumPublished(api.getNumPublished() + 1);
}
storage.updateApi(api);
storage.updateApiVersion(versionBean);
storage.createAuditEntry(AuditUtils.apiPublished(versionBean, securityContext));
} catch (Exception e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
}
LOGGER.debug(// $NON-NLS-1$
String.format(// $NON-NLS-1$
"Successfully published API %s on specified gateways: %s", versionBean.getApi().getName(), versionBean.getApi()));
}
use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.
the class ApiResourceImpl method getPolicy.
private void getPolicy(Api api, int policyIdx, String probeConfigRaw, AsyncResponse response) {
if (policyIdx < api.getApiPolicies().size()) {
// Get API policy by index
Policy policyConfig = api.getApiPolicies().get(policyIdx);
IPolicyFactory policyFactory = getEngine().getPolicyFactory();
// Load the policy (may not have been loaded yet, but is usually cached).
policyFactory.loadPolicy(policyConfig.getPolicyImpl(), policyLoad -> {
// Generate & load appropriate config for policy (is cached, so OK to do repeatedly).
IPolicy policy = policyLoad.getResult();
PolicyContextImpl policyContext = new PolicyContextImpl(getEngine().getComponentRegistry());
ProbeContext probeContext = buildProbeContext(api, null, null, api.getEndpointType());
// Probe it!
if (policy instanceof IPolicyProbe) {
IPolicyProbe<?, ?> policyWithProbe = (IPolicyProbe<?, ?>) policy;
policyWithProbe.probe(probeConfigRaw, policyConfig.getPolicyJsonConfig(), probeContext, policyContext, probeResult -> {
IPolicyProbeResponse probeResponse = probeResult.getResult();
LOGGER.debug("Probe response for config {0} -> {1}", probeConfigRaw, probeResponse);
response.resume(Response.ok(probeResponse).build());
});
} else {
response.resume(Response.status(Status.NOT_IMPLEMENTED.getStatusCode(), "Requested policy does not implement a policy probe").build());
}
});
} else {
response.resume(new IllegalArgumentException("Provided policy index out of bounds: " + policyIdx));
}
}
use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.
the class ApiResourceImpl method getPolicy.
private void getPolicy(ApiContract contract, int policyIdx, String probeConfigRaw, AsyncResponse response) {
if (policyIdx < contract.getPolicies().size()) {
// Get API policy by index
Policy policyConfig = contract.getPolicies().get(policyIdx);
IPolicyFactory policyFactory = getEngine().getPolicyFactory();
// Load the policy (may not have been loaded yet, but is usually cached).
policyFactory.loadPolicy(policyConfig.getPolicyImpl(), policyLoad -> {
// Generate & load appropriate config for policy (is cached, so OK to do repeatedly).
IPolicy policy = policyLoad.getResult();
PolicyContextImpl policyContext = new PolicyContextImpl(getEngine().getComponentRegistry());
Api api = contract.getApi();
Client client = contract.getClient();
ProbeContext probeContext = buildProbeContext(contract.getApi(), contract, client.getApiKey(), api.getEndpointType());
// Probe it!
if (policy instanceof IPolicyProbe) {
IPolicyProbe<?, ?> policyWithProbe = (IPolicyProbe<?, ?>) policy;
policyWithProbe.probe(probeConfigRaw, policyConfig.getPolicyJsonConfig(), probeContext, policyContext, probeResult -> {
IPolicyProbeResponse probeResponse = probeResult.getResult();
LOGGER.debug("Probe response for config {0} -> {1}", probeConfigRaw, probeResponse);
response.resume(Response.ok(ProbeRegistry.serialize(probeResponse)).build());
});
} else {
response.resume(Response.status(Status.NOT_IMPLEMENTED.getStatusCode(), "Requested policy does not implement a policy probe").build());
}
});
} else {
response.resume(new IllegalArgumentException("Provided policy index out of bounds: " + policyIdx));
}
}
use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.
the class ApiRequestExecutorImpl method resolvePropertyReplacements.
/**
* Resolve property replacements for list of policies
*/
private void resolvePropertyReplacements(List<Policy> apiPolicies) {
if (apiPolicies != null) {
for (Policy policy : apiPolicies) {
String config = policy.getPolicyJsonConfig();
config = resolveProperties(config);
policy.setPolicyJsonConfig(config);
}
}
}
use of io.apiman.gateway.engine.beans.Policy in project apiman by apiman.
the class ApiResourceImpl method getPolicy.
private void getPolicy(ApiContract contract, int policyIdx, String probeConfigRaw, AsyncResponse response) {
if (policyIdx < contract.getPolicies().size()) {
// Get API policy by index
Policy policyConfig = contract.getPolicies().get(policyIdx);
IPolicyFactory policyFactory = engine.getPolicyFactory();
// Load the policy (may not have been loaded yet, but is usually cached).
policyFactory.loadPolicy(policyConfig.getPolicyImpl(), policyLoad -> {
// Generate & load appropriate config for policy (is cached, so OK to do repeatedly).
IPolicy policy = policyLoad.getResult();
PolicyContextImpl policyContext = new PolicyContextImpl(engine.getComponentRegistry());
Api api = contract.getApi();
Client client = contract.getClient();
ProbeContext probeContext = buildProbeContext(contract.getApi(), contract, client.getApiKey(), api.getEndpointType());
// Probe it!
if (policy instanceof IPolicyProbe) {
IPolicyProbe<?, ?> policyWithProbe = (IPolicyProbe<?, ?>) policy;
policyWithProbe.probe(probeConfigRaw, policyConfig.getPolicyJsonConfig(), probeContext, policyContext, probeResult -> {
IPolicyProbeResponse probeResponse = probeResult.getResult();
LOGGER.debug("Probe response for config {0} -> {1}", probeConfigRaw, probeResponse);
response.resume(Response.ok(probeResponse).build());
});
} else {
response.resume(Response.status(Status.NOT_IMPLEMENTED.getStatusCode(), "Requested policy does not implement a policy probe").build());
}
});
} else {
response.resume(new IllegalArgumentException("Provided policy index out of bounds: " + policyIdx));
}
}
Aggregations