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;
}
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.");
}
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);
}
}
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();
}
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);
}
Aggregations