use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.
the class SwaggerRewriterInterceptor method handleResponse.
@Override
public Outcome handleResponse(Exchange exc) throws Exception {
// replacement in swagger.json
if (exc.getRequest().getUri().endsWith(swaggerJson) && exc.getResponseContentType().equalsIgnoreCase(MediaType.APPLICATION_JSON_VALUE)) {
Swagger swagBody = new SwaggerParser().parse(exc.getResponse().getBodyAsStringDecoded());
swagBody.setHost(exc2originalHostPort(exc));
exc.getResponse().setBodyContent(Json.pretty(swagBody).getBytes(exc.getResponse().getCharset()));
}
// replacement in json and javascript (specifically UI)
if (rewriteUI && (exc.getRequest().getUri().matches("/.*.js(on)?") || exc.getResponse().getHeader().getContentType() != null && exc.getResponse().getHeader().getContentType().equals(MediaType.TEXT_HTML_VALUE))) {
String from = "(http(s)?://)" + Pattern.quote(((ServiceProxy) exc.getRule()).getTarget().getHost()) + "(/.*\\.js(on)?)";
String to = "$1" + exc2originalHostPort(exc) + "$3";
byte[] body = exc.getResponse().getBodyAsStringDecoded().replaceAll(from, to).getBytes(exc.getResponse().getCharset());
exc.getResponse().setBodyContent(body);
}
return super.handleResponse(exc);
}
use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.
the class AMRateLimitInterceptorTest method testHandleRequestRateLimit5SecondConcurrency.
@Test
public void testHandleRequestRateLimit5SecondConcurrency() throws Exception {
final Exchange exc = new Exchange(null);
exc.setResponse(Response.ResponseBuilder.newInstance().build());
exc.setProperty(Exchange.API_KEY, "junit");
exc.setRule(new ServiceProxy());
exc.getRule().setName("junit API");
final AMRateLimiter rli = new AMRateLimiter();
ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"), "src\\test\\resources\\apimanagement\\api.yaml");
rli.setAmc(amc);
ArrayList<Thread> threads = new ArrayList<Thread>();
final AtomicInteger continues = new AtomicInteger();
final AtomicInteger returns = new AtomicInteger();
for (int i = 0; i < 1000; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Outcome out = rli.handleRequest(exc);
if (out == Outcome.CONTINUE) {
continues.incrementAndGet();
} else if (out == Outcome.RETURN) {
returns.incrementAndGet();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
threads.add(t);
t.start();
}
for (Thread t : threads) {
t.join();
}
assertEquals(5, continues.get());
assertEquals(995, returns.get());
Thread.sleep(2000);
assertEquals(Outcome.CONTINUE, rli.handleRequest(exc));
}
use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.
the class BasicAuthenticationInterceptorIntegrationTest method setup.
@Before
public void setup() throws Exception {
Rule rule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3001), "thomas-bayer.com", 80);
router.getRuleManager().addProxyAndOpenPortIfNew(rule);
BasicAuthenticationInterceptor interceptor = new BasicAuthenticationInterceptor();
List<User> users = new ArrayList<User>();
users.add(new User("admin", "admin"));
interceptor.setUsers(users);
router.addUserFeatureInterceptor(interceptor);
router.init();
}
use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.
the class ClusterBalancerTest method setUp.
@Override
@Before
public void setUp() throws Exception {
extracor = new XMLElementSessionIdExtractor();
extracor.setLocalName("session");
extracor.setNamespace("http://predic8.com/session/");
r = new HttpRouter();
lb = new LoadBalancingInterceptor();
lb.setSessionIdExtractor(extracor);
lb.setName("Default");
ServiceProxy sp = new ServiceProxy(new ServiceProxyKey(3011), "predic8.com", 80);
sp.getInterceptors().add(lb);
r.getRuleManager().addProxyAndOpenPortIfNew(sp);
r.init();
BalancerUtil.up(r, "Default", "Default", "localhost", 2000);
BalancerUtil.up(r, "Default", "Default", "localhost", 3000);
}
use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.
the class ClusterNotificationInterceptorTest method setUp.
@Override
@Before
public void setUp() throws Exception {
Rule rule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3002), "thomas-bayer.com", 80);
router = new HttpRouter();
router.getRuleManager().addProxyAndOpenPortIfNew(rule);
interceptor = new ClusterNotificationInterceptor();
router.addUserFeatureInterceptor(interceptor);
lbi = new LoadBalancingInterceptor();
lbi.setName("Default");
Rule rule2 = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3003), "thomas-bayer.com", 80);
router.getRuleManager().addProxyAndOpenPortIfNew(rule2);
rule2.getInterceptors().add(lbi);
router.init();
}
Aggregations