use of io.apiman.manager.api.gateway.GatewayAuthenticationException in project apiman by apiman.
the class OrganizationResourceImpl method getApiRegistry.
/**
* Gets the API registry.
* @param organizationId
* @param clientId
* @param version
* @throws ClientVersionNotFoundException
*/
private ApiRegistryBean getApiRegistry(String organizationId, String clientId, String version) throws ClientVersionNotFoundException {
// Try to get the client first - will throw a ClientVersionNotFoundException if not found.
ClientVersionBean clientVersion = getClientVersionInternal(organizationId, clientId, version);
Map<String, IGatewayLink> gatewayLinks = new HashMap<>();
Map<String, GatewayBean> gateways = new HashMap<>();
boolean txStarted = false;
try {
ApiRegistryBean apiRegistry = query.getApiRegistry(organizationId, clientId, version);
apiRegistry.setApiKey(clientVersion.getApikey());
List<ApiEntryBean> apis = apiRegistry.getApis();
storage.beginTx();
txStarted = true;
for (ApiEntryBean api : apis) {
String gatewayId = api.getGatewayId();
// Don't return the gateway id.
api.setGatewayId(null);
GatewayBean gateway = gateways.get(gatewayId);
if (gateway == null) {
gateway = storage.getGateway(gatewayId);
gateways.put(gatewayId, gateway);
}
IGatewayLink link = gatewayLinks.get(gatewayId);
if (link == null) {
link = gatewayLinkFactory.create(gateway);
gatewayLinks.put(gatewayId, link);
}
ApiEndpoint se = link.getApiEndpoint(api.getApiOrgId(), api.getApiId(), api.getApiVersion());
String apiEndpoint = se.getEndpoint();
api.setHttpEndpoint(apiEndpoint);
}
return apiRegistry;
} catch (StorageException | GatewayAuthenticationException e) {
throw new SystemErrorException(e);
} finally {
if (txStarted) {
storage.rollbackTx();
}
for (IGatewayLink link : gatewayLinks.values()) {
link.close();
}
}
}
use of io.apiman.manager.api.gateway.GatewayAuthenticationException 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;
}
use of io.apiman.manager.api.gateway.GatewayAuthenticationException in project apiman by apiman.
the class GatewayResourceImpl method test.
/**
* @see IGatewayResource#test(io.apiman.manager.api.beans.gateways.NewGatewayBean)
*/
@Override
public GatewayTestResultBean test(NewGatewayBean gatewayToTest) throws NotAuthorizedException {
securityContext.checkAdminPermissions();
GatewayTestResultBean rval = new GatewayTestResultBean();
try {
GatewayBean testGateway = new GatewayBean();
testGateway.setName(gatewayToTest.getName());
testGateway.setType(gatewayToTest.getType());
testGateway.setConfiguration(gatewayToTest.getConfiguration());
IGatewayLink gatewayLink = gatewayLinkFactory.create(testGateway);
SystemStatus status = gatewayLink.getStatus();
String detail = MAPPER.writer().writeValueAsString(status);
rval.setSuccess(true);
rval.setDetail(detail);
} catch (GatewayAuthenticationException e) {
rval.setSuccess(false);
// $NON-NLS-1$
rval.setDetail(Messages.i18n.format("GatewayResourceImpl.AuthenticationFailed"));
} catch (Exception e) {
rval.setSuccess(false);
rval.setDetail(e.getMessage());
}
return rval;
}
use of io.apiman.manager.api.gateway.GatewayAuthenticationException in project apiman by apiman.
the class GatewayClient method getApiEndpoint.
/**
* @see IApiResource#getApiEndpoint(java.lang.String, java.lang.String, java.lang.String)
*/
public ApiEndpoint getApiEndpoint(String organizationId, String apiId, String version) throws GatewayAuthenticationException {
InputStream is = null;
try {
@SuppressWarnings("nls") URI uri = new URI(this.endpoint + APIs + "/" + organizationId + "/" + apiId + "/" + version + "/endpoint");
HttpGet get = new HttpGet(uri);
HttpResponse response = httpClient.execute(get);
int actualStatusCode = response.getStatusLine().getStatusCode();
if (actualStatusCode == 401 || actualStatusCode == 403) {
throw new GatewayAuthenticationException();
}
if (actualStatusCode != 200) {
// $NON-NLS-1$
throw new RuntimeException("Failed to get the API endpoint: " + actualStatusCode);
}
is = response.getEntity().getContent();
return mapper.reader(ApiEndpoint.class).readValue(is);
} catch (GatewayAuthenticationException | RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(is);
}
}
use of io.apiman.manager.api.gateway.GatewayAuthenticationException in project apiman by apiman.
the class GatewayClient method register.
/**
* @see IClientResource#register(io.apiman.gateway.engine.beans.Client)
*/
public void register(Client client) throws RegistrationException, GatewayAuthenticationException {
try {
URI uri = new URI(this.endpoint + CLIENTS);
HttpPut put = new HttpPut(uri);
// $NON-NLS-1$ //$NON-NLS-2$
put.setHeader("Content-Type", "application/json; charset=utf-8");
String jsonPayload = mapper.writer().writeValueAsString(client);
HttpEntity entity = new StringEntity(jsonPayload);
put.setEntity(entity);
HttpResponse response = httpClient.execute(put);
int actualStatusCode = response.getStatusLine().getStatusCode();
if (actualStatusCode == 401 || actualStatusCode == 403) {
throw new GatewayAuthenticationException();
}
if (actualStatusCode == 500) {
// $NON-NLS-1$
Header[] headers = response.getHeaders("X-API-Gateway-Error");
if (headers != null && headers.length > 0) {
throw readRegistrationException(response);
}
}
if (actualStatusCode >= 300) {
// $NON-NLS-1$
throw new RuntimeException(Messages.i18n.format("GatewayClient.ClientRegistrationFailed", actualStatusCode));
}
} catch (GatewayAuthenticationException | RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations