Search in sources :

Example 1 with RegistrationException

use of io.apiman.gateway.engine.beans.exceptions.RegistrationException in project apiman by apiman.

the class ClientResourceImpl method register.

/**
 * @see IClientResource#register(io.apiman.gateway.engine.beans.Client)
 */
@Override
public void register(Client client) throws RegistrationException, NotAuthorizedException {
    if (client.getApiKey() == null) {
        // $NON-NLS-1$
        throw new RegistrationException("Cannot Register Client: Missing API Key");
    }
    final Set<Throwable> errorHolder = new HashSet<>();
    final CountDownLatch latch = new CountDownLatch(1);
    // Register client; latch until result returned and evaluated
    getEngine().getRegistry().registerClient(client, latchedResultHandler(latch, errorHolder));
    awaitOnLatch(latch, errorHolder);
}
Also used : RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet)

Example 2 with RegistrationException

use of io.apiman.gateway.engine.beans.exceptions.RegistrationException 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);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) GatewayEndpoint(io.apiman.gateway.engine.beans.GatewayEndpoint) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header)

Example 3 with RegistrationException

use of io.apiman.gateway.engine.beans.exceptions.RegistrationException in project apiman by apiman.

the class GatewayClient method retire.

/**
 * @see IApiResource#retire(java.lang.String, java.lang.String, java.lang.String)
 */
public void retire(String organizationId, String apiId, String version) throws RegistrationException, GatewayAuthenticationException {
    try {
        @SuppressWarnings("nls") URI uri = new URI(this.endpoint + APIs + "/" + organizationId + "/" + apiId + "/" + version);
        HttpDelete put = new HttpDelete(uri);
        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) {
                PublishingException pe = readPublishingException(response);
                throw pe;
            }
        }
        if (actualStatusCode >= 300) {
            // $NON-NLS-1$
            throw new Exception(Messages.i18n.format("GatewayClient.ApiRetiringFailed", actualStatusCode));
        }
    } catch (PublishingException | GatewayAuthenticationException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) URI(java.net.URI) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) GatewayEndpoint(io.apiman.gateway.engine.beans.GatewayEndpoint) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException)

Example 4 with RegistrationException

use of io.apiman.gateway.engine.beans.exceptions.RegistrationException in project apiman by apiman.

the class ApiResourceImpl method retire.

@Override
public void retire(String organizationId, String apiId, String version) throws RegistrationException, NotAuthorizedException {
    Api api = new Api();
    api.setOrganizationId(organizationId);
    api.setApiId(apiId);
    api.setVersion(version);
    registry.retireApi(api, (IAsyncResultHandler<Void>) result -> {
        if (result.isError()) {
            throwError(result.getError());
        }
    });
}
Also used : EndpointHelper(io.apiman.gateway.platforms.vertx3.helpers.EndpointHelper) IPolicy(io.apiman.gateway.engine.policy.IPolicy) ApiEndpoint(io.apiman.gateway.engine.beans.ApiEndpoint) VertxEngineConfig(io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig) NotAuthorizedException(io.apiman.gateway.api.rest.exceptions.NotAuthorizedException) Client(io.apiman.gateway.engine.beans.Client) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) IPolicyProbe(io.apiman.gateway.engine.policy.IPolicyProbe) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) Status(javax.ws.rs.core.Response.Status) ApimanLoggerFactory(io.apiman.common.logging.ApimanLoggerFactory) ProbeContext(io.apiman.gateway.engine.policy.ProbeContext) IPolicyProbeResponse(io.apiman.gateway.engine.beans.IPolicyProbeResponse) Policy(io.apiman.gateway.engine.beans.Policy) RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException) IApiResource(io.apiman.gateway.api.rest.IApiResource) AsyncResponse(javax.ws.rs.container.AsyncResponse) PolicyContextImpl(io.apiman.gateway.engine.policy.PolicyContextImpl) ProbeRegistry(io.apiman.gateway.engine.policies.probe.ProbeRegistry) Suspended(javax.ws.rs.container.Suspended) Api(io.apiman.gateway.engine.beans.Api) IApimanLogger(io.apiman.common.logging.IApimanLogger) IEngine(io.apiman.gateway.engine.IEngine) Response(javax.ws.rs.core.Response) IRegistry(io.apiman.gateway.engine.IRegistry) ApiContract(io.apiman.gateway.engine.beans.ApiContract) IPolicyFactory(io.apiman.gateway.engine.policy.IPolicyFactory) Api(io.apiman.gateway.engine.beans.Api)

Example 5 with RegistrationException

use of io.apiman.gateway.engine.beans.exceptions.RegistrationException in project apiman by apiman.

the class JdbcRegistry method validateContract.

/**
 * Ensures that the api referenced by the Contract actually exists (is published).
 * @param contract
 * @param connection
 * @throws RegistrationException
 */
private void validateContract(final Contract contract, Connection connection) throws RegistrationException {
    QueryRunner run = new QueryRunner();
    try {
        Api api = // $NON-NLS-1$
        run.query(// $NON-NLS-1$
        connection, // $NON-NLS-1$
        "SELECT bean FROM gw_apis WHERE org_id = ? AND id = ? AND version = ?", Handlers.API_HANDLER, contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion());
        if (api == null) {
            String apiId = contract.getApiId();
            String orgId = contract.getApiOrgId();
            // $NON-NLS-1$
            throw new ApiNotFoundException(Messages.i18n.format("JdbcRegistry.ApiNotFoundInOrg", apiId, orgId));
        }
    } catch (SQLException e) {
        // $NON-NLS-1$
        throw new RegistrationException(Messages.i18n.format("JdbcRegistry.ErrorValidatingApp"), e);
    }
}
Also used : RegistrationException(io.apiman.gateway.engine.beans.exceptions.RegistrationException) SQLException(java.sql.SQLException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) Api(io.apiman.gateway.engine.beans.Api) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Aggregations

RegistrationException (io.apiman.gateway.engine.beans.exceptions.RegistrationException)9 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)5 ApiEndpoint (io.apiman.gateway.engine.beans.ApiEndpoint)4 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)4 GatewayEndpoint (io.apiman.gateway.engine.beans.GatewayEndpoint)3 URI (java.net.URI)3 Header (org.apache.http.Header)3 HttpResponse (org.apache.http.HttpResponse)3 Api (io.apiman.gateway.engine.beans.Api)2 ApiNotFoundException (io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException)2 IOException (java.io.IOException)2 HttpDelete (org.apache.http.client.methods.HttpDelete)2 ApimanLoggerFactory (io.apiman.common.logging.ApimanLoggerFactory)1 IApimanLogger (io.apiman.common.logging.IApimanLogger)1 IApiResource (io.apiman.gateway.api.rest.IApiResource)1 GatewayApiErrorBean (io.apiman.gateway.api.rest.exceptions.GatewayApiErrorBean)1 NotAuthorizedException (io.apiman.gateway.api.rest.exceptions.NotAuthorizedException)1 IEngine (io.apiman.gateway.engine.IEngine)1 IRegistry (io.apiman.gateway.engine.IRegistry)1 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)1