use of io.apiman.manager.api.gateway.IGatewayLink in project apiman by apiman.
the class StorageImportDispatcher method createGatewayLink.
/**
* Creates a gateway link given a gateway id.
* @param gatewayId
*/
private IGatewayLink createGatewayLink(String gatewayId) throws StorageException {
if (gatewayLinkCache.containsKey(gatewayId)) {
return gatewayLinkCache.get(gatewayId);
}
try {
GatewayBean gateway = storage.getGateway(gatewayId);
if (gateway == null) {
// $NON-NLS-1$
throw new Exception("Gateway not found: " + gatewayId);
}
IGatewayLink link = gatewayLinks.create(gateway);
gatewayLinkCache.put(gatewayId, link);
return link;
} catch (Exception e) {
throw new StorageException(e);
}
}
use of io.apiman.manager.api.gateway.IGatewayLink in project apiman by apiman.
the class StorageImportDispatcher method registerClients.
/**
* Registers any clients that were imported in the "Registered" state.
* @throws StorageException
*/
private void registerClients() throws StorageException {
// $NON-NLS-1$
logger.info(Messages.i18n.format("StorageExporter.RegisteringClients"));
for (EntityInfo info : clientsToRegister) {
// $NON-NLS-1$
logger.info(Messages.i18n.format("StorageExporter.RegisteringClient", info));
ClientVersionBean versionBean = storage.getClientVersion(info.organizationId, info.id, info.version);
Iterator<ContractBean> contractBeans = storage.getAllContracts(info.organizationId, info.id, info.version);
Client client = new Client();
client.setOrganizationId(versionBean.getClient().getOrganization().getId());
client.setClientId(versionBean.getClient().getId());
client.setVersion(versionBean.getVersion());
client.setApiKey(versionBean.getApikey());
Set<Contract> contracts = new HashSet<>();
while (contractBeans.hasNext()) {
ContractBean contractBean = contractBeans.next();
EntityInfo apiInfo = new EntityInfo(contractBean.getApi().getApi().getOrganization().getId(), contractBean.getApi().getApi().getId(), contractBean.getApi().getVersion());
if (apisToPublish.contains(apiInfo)) {
Contract contract = new Contract();
contract.setPlan(contractBean.getPlan().getPlan().getId());
contract.setApiId(contractBean.getApi().getApi().getId());
contract.setApiOrgId(contractBean.getApi().getApi().getOrganization().getId());
contract.setApiVersion(contractBean.getApi().getVersion());
contract.getPolicies().addAll(aggregateContractPolicies(contractBean, info));
contracts.add(contract);
}
}
client.setContracts(contracts);
// Next, register the client with *all* relevant gateways. This is done by
// looking up all referenced apis and getting the gateway information for them.
// Each of those gateways must be told about the client.
Map<String, IGatewayLink> links = new HashMap<>();
for (Contract contract : client.getContracts()) {
ApiVersionBean svb = storage.getApiVersion(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion());
Set<ApiGatewayBean> gateways = svb.getGateways();
if (gateways == null) {
// $NON-NLS-1$
throw new PublishingException("No gateways specified for api: " + svb.getApi().getName());
}
for (ApiGatewayBean apiGatewayBean : gateways) {
String gatewayId = apiGatewayBean.getGatewayId();
if (!links.containsKey(gatewayId)) {
IGatewayLink gatewayLink = createGatewayLink(gatewayId);
links.put(gatewayId, gatewayLink);
}
}
}
for (IGatewayLink gatewayLink : links.values()) {
try {
gatewayLink.registerClient(client);
} catch (Exception e) {
throw new StorageException(e);
}
}
}
}
use of io.apiman.manager.api.gateway.IGatewayLink in project apiman by apiman.
the class WsdlRewriter method rewrite.
@Override
public String rewrite(ProviderContext ctx, InputStream wsdlStream, ApiDefinitionType apiDefinitionType) throws IOException, StorageException, GatewayAuthenticationException, Exception {
IStorage storage = ctx.getStorage();
ApiVersionBean avb = ctx.getAvb();
String orgId = avb.getApi().getOrganization().getId();
String apiId = avb.getApi().getId();
String apiVersion = avb.getVersion();
Document document = readWsdlInputStream(wsdlStream);
// Collection of all SOAP binding addresses
List<Element> allSoapAddresses = new LinkedList<>();
for (String soapNamespace : SOAP_NAMESPACES) {
NodeList soapAddresses = document.getDocumentElement().getElementsByTagNameNS(soapNamespace, SOAP_ADDRESS);
if (soapAddresses.getLength() > 0) {
for (int j = 0; j < soapAddresses.getLength(); j++) {
allSoapAddresses.add((Element) soapAddresses.item(j));
}
}
}
// Find IDs of all GWs this ApiVersion is published onto.
String firstGateway = avb.getGateways().stream().map(ApiGatewayBean::getGatewayId).findFirst().orElse("");
GatewayBean gateway = storage.getGateway(firstGateway);
IGatewayLink link = ctx.getGatewayLinkFactory().create(gateway);
// Go through the addresses we've found (if any) and update the 'location' attribute if needed.
String endpoint = link.getApiEndpoint(orgId, apiId, apiVersion).getEndpoint();
for (Element addressElem : allSoapAddresses) {
String location = addressElem.getAttribute(LOCATION);
if (!location.equals(endpoint)) {
addressElem.setAttribute(LOCATION, endpoint);
}
}
// Convert document back to string and update in storage.
return xmlDocumentToString(document);
}
use of io.apiman.manager.api.gateway.IGatewayLink 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.manager.api.gateway.IGatewayLink in project apiman by apiman.
the class ActionService method registerClient.
/**
* Registers a client (along with all of its contracts) to the gateway.
*/
public void registerClient(String orgId, String clientId, String clientVersion) throws ActionException, NotAuthorizedException {
ClientVersionBean versionBean;
List<ContractSummaryBean> contractBeans;
try {
versionBean = clientAppService.getClientVersion(orgId, clientId, clientVersion);
} catch (ClientVersionNotFoundException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("clientVersionDoesNotExist", clientId, clientVersion));
}
try {
contractBeans = query.getClientContracts(orgId, clientId, clientVersion);
// Any awaiting approval then don't let them republish.
List<ContractSummaryBean> awaitingApproval = contractBeans.stream().filter(f -> f.getStatus() == ContractStatus.AwaitingApproval).collect(Collectors.toList());
if (!awaitingApproval.isEmpty()) {
throw ExceptionFactory.contractNotYetApprovedException(awaitingApproval);
}
} catch (StorageException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"), e);
}
boolean isReregister = false;
// Validate that it's ok to perform this action
if (versionBean.getStatus() == ClientStatus.Registered) {
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("ClientReRegisterNotRequired"));
}
isReregister = true;
}
Client client = new Client();
client.setOrganizationId(versionBean.getClient().getOrganization().getId());
client.setClientId(versionBean.getClient().getId());
client.setVersion(versionBean.getVersion());
client.setApiKey(versionBean.getApikey());
Set<Contract> contracts = new HashSet<>();
for (ContractSummaryBean contractBean : contractBeans) {
Contract contract = new Contract();
contract.setPlan(contractBean.getPlanId());
contract.setApiId(contractBean.getApiId());
contract.setApiOrgId(contractBean.getApiOrganizationId());
contract.setApiVersion(contractBean.getApiVersion());
contract.getPolicies().addAll(contractService.aggregateContractPolicies(contractBean));
contracts.add(contract);
}
client.setContracts(contracts);
// Each of those gateways must be told about the client.
try {
Map<String, IGatewayLink> links = new HashMap<>();
for (Contract contract : client.getContracts()) {
ApiVersionBean svb = storage.getApiVersion(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion());
Set<ApiGatewayBean> gateways = svb.getGateways();
if (gateways == null) {
// $NON-NLS-1$
throw new PublishingException("No gateways specified for API: " + svb.getApi().getName());
}
for (ApiGatewayBean apiGatewayBean : gateways) {
if (!links.containsKey(apiGatewayBean.getGatewayId())) {
IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
links.put(apiGatewayBean.getGatewayId(), gatewayLink);
}
}
}
if (isReregister) {
// Once we figure out which gateways to register with, make sure we also "unregister"
// the client app from all other gateways. This is necessary because we may have broken
// contracts we previously had on APIs that are published to other gateways. And thus
// it's possible we need to remove a contract from a Gateway that is not otherwise/currently
// referenced.
//
// This is a fix for: https://issues.jboss.org/browse/APIMAN-895
Iterator<GatewayBean> gateways = storage.getAllGateways();
while (gateways.hasNext()) {
GatewayBean gbean = gateways.next();
if (!links.containsKey(gbean.getId())) {
IGatewayLink gatewayLink = createGatewayLink(gbean.getId());
try {
gatewayLink.unregisterClient(client);
} catch (Exception e) {
// We need to catch the error, but ignore it,
// in the event that the gateway is invalid.
}
gatewayLink.close();
}
}
}
for (IGatewayLink gatewayLink : links.values()) {
gatewayLink.registerClient(client);
gatewayLink.close();
}
} catch (Exception e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
}
ClientStatus oldStatus = versionBean.getStatus();
versionBean.setStatus(ClientStatus.Registered);
versionBean.setPublishedOn(new Date());
try {
storage.updateClientVersion(versionBean);
storage.createAuditEntry(AuditUtils.clientRegistered(versionBean, securityContext));
clientAppService.fireClientStatusChangeEvent(versionBean, oldStatus);
} catch (Exception e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
}
LOGGER.debug(// $NON-NLS-1$
String.format(// $NON-NLS-1$
"Successfully registered Client %s on specified gateways: %s", versionBean.getClient().getName(), versionBean.getClient()));
}
Aggregations