Search in sources :

Example 11 with ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.

the class ApiManagementConfiguration method parsePolicies.

private Map<String, Policy> parsePolicies(Map<String, Object> yaml) {
    Map<String, Policy> result = new HashMap<String, Policy>();
    Object policies = yaml.get("policies");
    if (policies == null) {
        log.warn("No policies in policy file");
        return result;
    }
    List<Object> yamlPolicies = (List<Object>) policies;
    for (Object yamlPolicyObj : yamlPolicies) {
        if (yamlPolicyObj == null) {
            continue;
        }
        LinkedHashMap<String, Object> yamlPolicy = (LinkedHashMap<String, Object>) yamlPolicyObj;
        for (Object polObj : yamlPolicy.values()) {
            if (polObj == null) {
                continue;
            }
            LinkedHashMap<String, Object> yamlPolicyDef = (LinkedHashMap<String, Object>) polObj;
            Policy policy = new Policy();
            Object name = yamlPolicyDef.get("id");
            if (name == null) {
                log.warn("Policy object found, but no \"id\" field");
                continue;
            }
            String policyName = (String) name;
            policy.setName(policyName);
            Object serviceProxiesObj = yamlPolicyDef.get("serviceProxy");
            if (serviceProxiesObj == null) {
                log.warn("Policy object found, but no service proxies specified ");
                continue;
            }
            List<String> serviceProxyNames = (List<String>) serviceProxiesObj;
            for (String sp : serviceProxyNames) {
                policy.getServiceProxies().add(sp);
            }
            // Optionals like rateLimit/quota etc. follow
            Object rateLimitObj = yamlPolicyDef.get("rateLimit");
            if (rateLimitObj != null) {
                LinkedHashMap<String, Object> rateLimitData = (LinkedHashMap<String, Object>) rateLimitObj;
                RateLimit rateLimit = new RateLimit();
                int requests = parseInteger(rateLimitData.get("requests"), RateLimit.REQUESTS_DEFAULT);
                int interval = parseInteger(rateLimitData.get("interval"), RateLimit.INTERVAL_DEFAULT);
                rateLimit.setRequests(requests);
                rateLimit.setInterval(interval);
                policy.setRateLimit(rateLimit);
            }
            Object quotaObj = yamlPolicyDef.get("quota");
            if (quotaObj != null) {
                LinkedHashMap<String, Object> quota = (LinkedHashMap<String, Object>) quotaObj;
                Object quotaSizeObj = quota.get("size");
                long quotaNumber = getQuotaNumber(quotaSizeObj);
                int quotaInterval = parseInteger(quota.get("interval"), Quota.INTERVAL_DEFAULT);
                Quota q = new Quota();
                q.setSize(quotaNumber);
                q.setInterval(quotaInterval);
                policy.setQuota(q);
            }
            parseUnauthenticatedField(yamlPolicyDef, policy);
            result.put(policyName, policy);
        }
    }
    return result;
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RateLimit(com.predic8.membrane.core.interceptor.apimanagement.policy.RateLimit) Quota(com.predic8.membrane.core.interceptor.apimanagement.policy.Quota)

Example 12 with ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.

the class EtcdRegistryApiConfig method findAdminConsole.

private EtcdNodeInformation findAdminConsole() {
    Object routerObj = context.getBean(Router.class);
    if (routerObj == null)
        throw new RuntimeException("Router not found, cannot publish admin console");
    Router router = (Router) routerObj;
    for (Rule r : router.getRuleManager().getRules()) {
        if (!(r instanceof AbstractServiceProxy))
            continue;
        for (Interceptor i : r.getInterceptors()) {
            if (i instanceof AdminConsoleInterceptor) {
                String name = r.getName();
                String host = ((ServiceProxy) r).getExternalHostname();
                if (host == null)
                    host = getLocalHostname();
                String port = Integer.toString(((AbstractServiceProxy) r).getPort());
                EtcdNodeInformation node = new EtcdNodeInformation(null, null, host, port, name);
                return node;
            }
        }
    }
    throw new RuntimeException("Admin console not found but is needed. Add a service proxy with an admin console.");
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) AdminConsoleInterceptor(com.predic8.membrane.core.interceptor.administration.AdminConsoleInterceptor) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) EtcdNodeInformation(com.predic8.membrane.core.cloud.etcd.EtcdNodeInformation) Router(com.predic8.membrane.core.Router) Rule(com.predic8.membrane.core.rules.Rule) Interceptor(com.predic8.membrane.core.interceptor.Interceptor) AdminConsoleInterceptor(com.predic8.membrane.core.interceptor.administration.AdminConsoleInterceptor)

Example 13 with ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.

the class HttpTransport method openPort.

/**
 * @param port
 * @throws IOException
 */
@Override
public synchronized void openPort(String ip, int port, SSLProvider sslProvider) throws IOException {
    if (isAnyThreadListeningAt(ip, port)) {
        return;
    }
    if (port == -1)
        throw new RuntimeException("The port-attribute is missing (probably on a <serviceProxy> element).");
    HttpEndpointListener portListenerThread = new HttpEndpointListener(ip, port, this, sslProvider);
    portListenerMapping.put(new IpPort(ip, port), portListenerThread);
    portListenerThread.start();
    for (IPortChangeListener listener : menuListeners) {
        listener.addPort(port);
    }
}
Also used : IPortChangeListener(com.predic8.membrane.core.model.IPortChangeListener)

Example 14 with ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.

the class HttpKeepAliveTest method setUp.

@Before
public void setUp() throws Exception {
    set = new HashSet<Integer>();
    service1 = new HttpRouter();
    sp1 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 2003), "thomas-bayer.com", 80);
    sp1.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            exc.getRequest().readBody();
            exc.setResponse(Response.ok("OK.").build());
            set.add(((HttpServerHandler) exc.getHandler()).getSrcOut().hashCode());
            return Outcome.RETURN;
        }
    });
    service1.getRuleManager().addProxyAndOpenPortIfNew(sp1);
    service1.init();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Before(org.junit.Before)

Example 15 with ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.

the class RewriteInterceptorTest method setUp.

@Before
public void setUp() throws Exception {
    HttpRouter router = new HttpRouter();
    di = new DispatchingInterceptor();
    di.init(router);
    sp = new ServiceProxy(new ServiceProxyKey(80, null), "www.predic8.de", 80);
    sp.init(router);
    exc = new Exchange(null);
    exc.setRequest(MessageUtil.getGetRequest("/buy/banana/3"));
    rewriter = new RewriteInterceptor();
    List<Mapping> mappings = new ArrayList<Mapping>();
    mappings.add(new Mapping("/buy/(.*)/(.*)", "/buy?item=$1&amount=$2", null));
    rewriter.setMappings(mappings);
    rewriter.init(router);
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) ArrayList(java.util.ArrayList) Mapping(com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptor.Mapping) HttpRouter(com.predic8.membrane.core.HttpRouter) DispatchingInterceptor(com.predic8.membrane.core.interceptor.DispatchingInterceptor) Before(org.junit.Before)

Aggregations

ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)47 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)34 HttpRouter (com.predic8.membrane.core.HttpRouter)25 Before (org.junit.Before)19 Rule (com.predic8.membrane.core.rules.Rule)17 Exchange (com.predic8.membrane.core.exchange.Exchange)16 Outcome (com.predic8.membrane.core.interceptor.Outcome)14 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)12 IOException (java.io.IOException)9 Test (org.junit.Test)9 LoadBalancingInterceptor (com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor)5 ArrayList (java.util.ArrayList)4 Router (com.predic8.membrane.core.Router)3 Mapping (com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptor.Mapping)3 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)3 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)3 HttpClientConfiguration (com.predic8.membrane.core.transport.http.client.HttpClientConfiguration)3 URISyntaxException (java.net.URISyntaxException)3 Interceptor (com.predic8.membrane.core.interceptor.Interceptor)2 MockInterceptor (com.predic8.membrane.core.interceptor.MockInterceptor)2