Search in sources :

Example 1 with ResolverMap

use of com.predic8.membrane.core.resolver.ResolverMap in project service-proxy by membrane.

the class JSONSchemaValidationTest method validate.

private void validate(String schema, String json, boolean success) throws IOException, Exception {
    final StringBuffer sb = new StringBuffer();
    FailureHandler fh = new FailureHandler() {

        @Override
        public void handleFailure(String message, Exchange exc) {
            sb.append(message);
            sb.append("\n");
        }
    };
    JSONValidator jsonValidator = new JSONValidator(new ResolverMap(), schema, fh);
    Request request = new Request.Builder().body(IOUtils.toByteArray(getClass().getResourceAsStream(json))).build();
    Exchange exchange = new Exchange(null);
    jsonValidator.validateMessage(exchange, request, "request");
    if (success)
        Assert.assertTrue(sb.toString(), sb.length() == 0);
    else
        Assert.assertTrue("No error occurred, but expected one.", sb.length() != 0);
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) FailureHandler(com.predic8.membrane.core.interceptor.schemavalidation.ValidatorInterceptor.FailureHandler) Request(com.predic8.membrane.core.http.Request) ResolverMap(com.predic8.membrane.core.resolver.ResolverMap)

Example 2 with ResolverMap

use of com.predic8.membrane.core.resolver.ResolverMap in project service-proxy by membrane.

the class ValidatorInterceptorTest method createSchemaValidatorInterceptor.

private ValidatorInterceptor createSchemaValidatorInterceptor(String schema) throws Exception {
    ValidatorInterceptor interceptor = new ValidatorInterceptor();
    interceptor.setResourceResolver(new ResolverMap());
    interceptor.setSchema(schema);
    interceptor.init();
    return interceptor;
}
Also used : ResolverMap(com.predic8.membrane.core.resolver.ResolverMap)

Example 3 with ResolverMap

use of com.predic8.membrane.core.resolver.ResolverMap in project service-proxy by membrane.

the class ProxySSLTest method test.

@Test
public void test() throws Exception {
    // Step 1: create the backend
    Router backend = new Router();
    backend.setHotDeploy(false);
    ServiceProxy sp = new ServiceProxy(new ServiceProxyKey(backendPort), null, 0);
    if (backendUsesSSL) {
        SSLParser ssl = new SSLParser();
        ssl.setKeyStore(new KeyStore());
        ssl.getKeyStore().setLocation("classpath:/ssl-rsa.keystore");
        ssl.getKeyStore().setKeyPassword("secret");
        sp.setSslInboundParser(ssl);
    }
    sp.getInterceptors().add(new CountInterceptor());
    backend.getRuleManager().addProxy(sp, RuleManager.RuleDefinitionSource.MANUAL);
    backend.start();
    // Step 2: put a proxy in front of it
    AtomicInteger proxyCounter = new AtomicInteger();
    Router proxyRouter = new Router();
    proxyRouter.setHotDeploy(false);
    ProxyRule proxy = new ProxyRule(new ProxyRuleKey(proxyPort));
    proxy.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            proxyCounter.incrementAndGet();
            return super.handleRequest(exc);
        }
    });
    if (proxyUsesSSL) {
        SSLParser ssl = new SSLParser();
        ssl.setKeyStore(new KeyStore());
        ssl.getKeyStore().setLocation("classpath:/ssl-rsa2.keystore");
        ssl.getKeyStore().setKeyPassword("secret");
        proxy.setSslInboundParser(ssl);
    }
    proxyRouter.getRuleManager().addProxy(proxy, RuleManager.RuleDefinitionSource.MANUAL);
    proxyRouter.start();
    // Step 3: configure the client to access the backend through the proxy
    HttpClientConfiguration httpClientConfiguration = new HttpClientConfiguration();
    ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
    proxyConfiguration.setHost("localhost");
    proxyConfiguration.setPort(proxyPort);
    if (proxyUsesSSL) {
        SSLParser ssl = new SSLParser();
        ssl.setTrustStore(new TrustStore());
        ssl.getTrustStore().setLocation("classpath:/ssl-rsa-pub2.keystore");
        ssl.getTrustStore().setPassword("secret");
        // workarond the fact that the certificate was not issued for 'localhost'
        ssl.setEndpointIdentificationAlgorithm("");
        proxyConfiguration.setSslParser(ssl);
    }
    httpClientConfiguration.setProxy(proxyConfiguration);
    HttpClient hc = new HttpClient(httpClientConfiguration);
    // Step 4: Test client
    Exchange exc = new Request.Builder().get("http" + (backendUsesSSL ? "s" : "") + "://localhost:" + backendPort + "/foo").buildExchange();
    if (backendUsesSSL) {
        SSLParser ssl = new SSLParser();
        ssl.setTrustStore(new TrustStore());
        ssl.getTrustStore().setLocation("classpath:/ssl-rsa-pub.keystore");
        ssl.getTrustStore().setPassword("secret");
        // workarond the fact that the certificate was not issued for 'localhost'
        ssl.setEndpointIdentificationAlgorithm("");
        exc.setProperty(Exchange.SSL_CONTEXT, new StaticSSLContext(ssl, new ResolverMap(), null));
    }
    hc.call(exc);
    Assert.assertEquals(200, exc.getResponse().getStatusCode());
    Assert.assertEquals("Did the request go through the proxy?", 1, proxyCounter.get());
    proxyRouter.shutdown();
    backend.shutdown();
}
Also used : CountInterceptor(com.predic8.membrane.core.interceptor.CountInterceptor) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) StaticSSLContext(com.predic8.membrane.core.transport.ssl.StaticSSLContext) Router(com.predic8.membrane.core.Router) ResolverMap(com.predic8.membrane.core.resolver.ResolverMap) TrustStore(com.predic8.membrane.core.config.security.TrustStore) KeyStore(com.predic8.membrane.core.config.security.KeyStore) SSLParser(com.predic8.membrane.core.config.security.SSLParser) Exchange(com.predic8.membrane.core.exchange.Exchange) ProxyConfiguration(com.predic8.membrane.core.transport.http.client.ProxyConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Outcome(com.predic8.membrane.core.interceptor.Outcome) HttpClient(com.predic8.membrane.core.transport.http.HttpClient) HttpClientConfiguration(com.predic8.membrane.core.transport.http.client.HttpClientConfiguration) Test(org.junit.Test)

Example 4 with ResolverMap

use of com.predic8.membrane.core.resolver.ResolverMap in project service-proxy by membrane.

the class ResolverTestTriggerTest method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) {
    try {
        Class<?> clazz = Class.forName("com.predic8.membrane.core.resolver.ResolverTest");
        clazz.getField("deployment").set(null, "J2EE");
        Object value = router.getResolverMap().getFileSchemaResolver();
        Object resolverMap = clazz.getField("resolverMap").get(null);
        resolverMap.getClass().getMethod("addSchemaResolver", SchemaResolver.class).invoke(resolverMap, value);
        Parameterized p = new Parameterized(clazz);
        JUnitCore c = new JUnitCore();
        Result run = c.run(Request.runner(p));
        StringBuilder sb = new StringBuilder();
        sb.append(MAGIC);
        for (Failure f : run.getFailures()) {
            sb.append(f.toString());
            StringWriter stringWriter = new StringWriter();
            f.getException().printStackTrace(new PrintWriter(stringWriter));
            sb.append(stringWriter.toString());
            sb.append("\n");
            sb.append("\n");
        }
        exc.setResponse(Response.ok().header(Header.CONTENT_TYPE, MimeType.TEXT_PLAIN_UTF8).body(sb.toString()).build());
    } catch (Throwable t) {
        LOG.error(t.getMessage(), t);
    }
    return Outcome.RETURN;
}
Also used : Parameterized(org.junit.runners.Parameterized) JUnitCore(org.junit.runner.JUnitCore) StringWriter(java.io.StringWriter) SchemaResolver(com.predic8.membrane.core.resolver.SchemaResolver) Failure(org.junit.runner.notification.Failure) Result(org.junit.runner.Result) PrintWriter(java.io.PrintWriter)

Example 5 with ResolverMap

use of com.predic8.membrane.core.resolver.ResolverMap in project service-proxy by membrane.

the class SOAPMessageValidatorInterceptorTest method createValidatorInterceptor.

private ValidatorInterceptor createValidatorInterceptor(String wsdl) throws Exception {
    ValidatorInterceptor interceptor = new ValidatorInterceptor();
    interceptor.setWsdl(wsdl);
    interceptor.setResourceResolver(new ResolverMap());
    interceptor.init();
    return interceptor;
}
Also used : ResolverMap(com.predic8.membrane.core.resolver.ResolverMap)

Aggregations

ResolverMap (com.predic8.membrane.core.resolver.ResolverMap)5 Exchange (com.predic8.membrane.core.exchange.Exchange)2 Router (com.predic8.membrane.core.Router)1 KeyStore (com.predic8.membrane.core.config.security.KeyStore)1 SSLParser (com.predic8.membrane.core.config.security.SSLParser)1 TrustStore (com.predic8.membrane.core.config.security.TrustStore)1 Request (com.predic8.membrane.core.http.Request)1 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)1 CountInterceptor (com.predic8.membrane.core.interceptor.CountInterceptor)1 Outcome (com.predic8.membrane.core.interceptor.Outcome)1 FailureHandler (com.predic8.membrane.core.interceptor.schemavalidation.ValidatorInterceptor.FailureHandler)1 HTTPSchemaResolver (com.predic8.membrane.core.resolver.HTTPSchemaResolver)1 SchemaResolver (com.predic8.membrane.core.resolver.SchemaResolver)1 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)1 HttpClientConfiguration (com.predic8.membrane.core.transport.http.client.HttpClientConfiguration)1 ProxyConfiguration (com.predic8.membrane.core.transport.http.client.ProxyConfiguration)1 StaticSSLContext (com.predic8.membrane.core.transport.ssl.StaticSSLContext)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1