Search in sources :

Example 1 with ApiLoaderInterceptor

use of io.gravitee.gateway.standalone.ApiLoaderInterceptor in project gravitee-gateway by gravitee-io.

the class ApiDeployerStatement method loadApi.

private Api loadApi(String apiDescriptorPath) throws Exception {
    URL jsonFile = ApiDeployerStatement.class.getResource(apiDescriptorPath);
    Api api = new GraviteeMapper().readValue(jsonFile, Api.class);
    if (ApiLoaderInterceptor.class.isAssignableFrom(target.getClass())) {
        ApiLoaderInterceptor loader = (ApiLoaderInterceptor) target;
        loader.before(api);
    }
    boolean enhanceHttpPort = target.getClass().getAnnotation(ApiDescriptor.class).enhanceHttpPort();
    if (enhanceHttpPort) {
        List<Endpoint> endpoints = new ArrayList<>(api.getProxy().getEndpoints());
        List<Integer> bindPorts = SocketUtils.getBindPorts();
        for (int i = 0; i < bindPorts.size(); i++) {
            int port = SocketUtils.getBindPorts().get(i);
            if (i < endpoints.size()) {
                Endpoint edpt = endpoints.get(i);
                URL target = new URL(edpt.getTarget());
                URL newTarget = new URL(target.getProtocol(), target.getHost(), port, target.getFile());
                edpt.setTarget(newTarget.toString());
                edpt.setName(UUID.random().toString());
            } else {
                // Use the first defined endpoint as reference
                HttpEndpoint first = (HttpEndpoint) endpoints.get(0);
                URL target = new URL(first.getTarget());
                URL newTarget = new URL(target.getProtocol(), target.getHost(), port, target.getFile());
                HttpEndpoint edpt = new HttpEndpoint(UUID.random().toString(), newTarget.toString());
                edpt.setHttpClientOptions(first.getHttpClientOptions());
                api.getProxy().getEndpoints().add(edpt);
            }
        }
    }
    if (ApiLoaderInterceptor.class.isAssignableFrom(target.getClass())) {
        ApiLoaderInterceptor loader = (ApiLoaderInterceptor) target;
        loader.after(api);
    }
    return api;
}
Also used : ApiLoaderInterceptor(io.gravitee.gateway.standalone.ApiLoaderInterceptor) ApiDescriptor(io.gravitee.gateway.standalone.junit.annotation.ApiDescriptor) ArrayList(java.util.ArrayList) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) URL(java.net.URL) Endpoint(io.gravitee.definition.model.Endpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Endpoint(io.gravitee.definition.model.Endpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) GraviteeMapper(io.gravitee.definition.jackson.datatype.GraviteeMapper) Api(io.gravitee.gateway.handlers.api.definition.Api)

Aggregations

GraviteeMapper (io.gravitee.definition.jackson.datatype.GraviteeMapper)1 Endpoint (io.gravitee.definition.model.Endpoint)1 HttpEndpoint (io.gravitee.definition.model.endpoint.HttpEndpoint)1 Api (io.gravitee.gateway.handlers.api.definition.Api)1 ApiLoaderInterceptor (io.gravitee.gateway.standalone.ApiLoaderInterceptor)1 ApiDescriptor (io.gravitee.gateway.standalone.junit.annotation.ApiDescriptor)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1