use of io.apiman.manager.api.beans.contracts.ContractBean in project apiman by apiman.
the class EsMarshalling method unmarshallContract.
/**
* Unmarshals the given map source into a bean.
* @param source the source
* @return the contract
*/
public static ContractBean unmarshallContract(Map<String, Object> source) {
if (source == null) {
return null;
}
ContractBean bean = new ContractBean();
bean.setId(asLong(source.get("id")));
bean.setCreatedBy(asString(source.get("createdBy")));
bean.setCreatedOn(asDate(source.get("createdOn")));
postMarshall(bean);
return bean;
}
use of io.apiman.manager.api.beans.contracts.ContractBean in project apiman by apiman.
the class OrganizationResourceImpl method createContract.
/**
* @see IOrganizationResource#createContract(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.contracts.NewContractBean)
*/
@Override
public ContractBean createContract(String organizationId, String clientId, String version, NewContractBean bean) throws OrganizationNotFoundException, ClientNotFoundException, ApiNotFoundException, PlanNotFoundException, ContractAlreadyExistsException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.clientEdit, organizationId);
try {
storage.beginTx();
ContractBean contract = createContractInternal(organizationId, clientId, version, bean);
storage.commitTx();
// $NON-NLS-1$
log.debug(String.format("Created new contract %s: %s", contract.getId(), contract));
return contract;
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
// reduce overhead on the typical happy path.
if (contractAlreadyExists(organizationId, clientId, version, bean)) {
throw ExceptionFactory.contractAlreadyExistsException();
} else {
throw new SystemErrorException(e);
}
}
}
use of io.apiman.manager.api.beans.contracts.ContractBean in project apiman by apiman.
the class OrganizationResourceImpl method deleteContract.
/**
* @see IOrganizationResource#deleteContract(java.lang.String, java.lang.String, java.lang.String, java.lang.Long)
*/
@Override
public void deleteContract(String organizationId, String clientId, String version, Long contractId) throws ClientNotFoundException, ContractNotFoundException, NotAuthorizedException, InvalidClientStatusException {
securityContext.checkPermissions(PermissionType.clientEdit, organizationId);
try {
storage.beginTx();
ContractBean contract = storage.getContract(contractId);
if (contract == null) {
throw ExceptionFactory.contractNotFoundException(contractId);
}
if (!contract.getClient().getClient().getOrganization().getId().equals(organizationId)) {
throw ExceptionFactory.contractNotFoundException(contractId);
}
if (!contract.getClient().getClient().getId().equals(clientId)) {
throw ExceptionFactory.contractNotFoundException(contractId);
}
if (!contract.getClient().getVersion().equals(version)) {
throw ExceptionFactory.contractNotFoundException(contractId);
}
if (contract.getClient().getStatus() == ClientStatus.Retired) {
throw ExceptionFactory.invalidClientStatusException();
}
storage.deleteContract(contract);
storage.createAuditEntry(AuditUtils.contractBrokenFromClient(contract, securityContext));
storage.createAuditEntry(AuditUtils.contractBrokenToApi(contract, securityContext));
// Update the version with new meta-data (e.g. modified-by)
ClientVersionBean clientV = getClientVersionFromStorage(organizationId, clientId, version);
clientV.setModifiedBy(securityContext.getCurrentUser());
clientV.setModifiedOn(new Date());
storage.updateClientVersion(clientV);
storage.commitTx();
// $NON-NLS-1$
log.debug(String.format("Deleted contract: %s", contract));
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.contracts.ContractBean in project apiman by apiman.
the class OrganizationResourceImpl method getContract.
/**
* @see IOrganizationResource#getContract(java.lang.String, java.lang.String, java.lang.String, java.lang.Long)
*/
@Override
public ContractBean getContract(String organizationId, String clientId, String version, Long contractId) throws ClientNotFoundException, ContractNotFoundException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.clientView, organizationId);
try {
storage.beginTx();
ContractBean contract = storage.getContract(contractId);
if (contract == null)
throw ExceptionFactory.contractNotFoundException(contractId);
storage.commitTx();
// $NON-NLS-1$
log.debug(String.format("Got contract %s: %s", contract.getId(), contract));
return contract;
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.contracts.ContractBean in project apiman by apiman.
the class ContractService method probePolicy.
// TODO make properly optimised query for this
public List<IPolicyProbeResponse> probePolicy(Long contractId, long policyId, String rawPayload) throws ClientNotFoundException, ContractNotFoundException {
ContractBean contract = getContract(contractId);
ApiVersionBean avb = contract.getApi();
OrganizationBean apiOrg = avb.getApi().getOrganization();
String apiKey = contract.getClient().getApikey();
Set<String> gatewayIds = contract.getApi().getGateways().stream().map(ApiGatewayBean::getGatewayId).collect(Collectors.toSet());
if (gatewayIds.size() == 0) {
return List.of();
}
List<PolicyBean> policyChain = aggregateContractPolicies(contract);
int idxFound = -1;
for (int i = 0, policyChainSize = policyChain.size(); i < policyChainSize; i++) {
PolicyBean policy = policyChain.get(i);
if (policy.getId().equals(policyId)) {
idxFound = i;
}
}
if (idxFound == -1) {
throw new IllegalArgumentException("Provided policy ID not found in contract " + policyId);
}
List<GatewayBean> gateways = tryAction(() -> storage.getGateways(gatewayIds));
LOGGER.debug("Gateways for contract {0}: {1}", contractId, gateways);
List<IPolicyProbeResponse> probeResponses = new ArrayList<>(gateways.size());
for (GatewayBean gateway : gateways) {
IGatewayLink link = gatewayLinkFactory.create(gateway);
try {
probeResponses.add(link.probe(apiOrg.getId(), avb.getApi().getId(), avb.getVersion(), idxFound, apiKey, rawPayload));
} catch (GatewayAuthenticationException e) {
throw new SystemErrorException(e);
}
}
LOGGER.debug("Probe responses for contract {0}: {1}", contractId, probeResponses);
return probeResponses;
}
Aggregations