Search in sources :

Example 36 with Request

use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.

the class SoapOperationExtractorTest method getExchange.

private Exchange getExchange(String path) throws IOException {
    Exchange exc = new Exchange(null);
    Request req = new Request();
    req.create("POST", "http://test", "HTTP/", new Header(), getClass().getClassLoader().getResourceAsStream(path));
    exc.setRequest(req);
    return exc;
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Header(com.predic8.membrane.core.http.Header) Request(com.predic8.membrane.core.http.Request)

Example 37 with Request

use of com.predic8.membrane.core.http.Request 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 38 with Request

use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.

the class HTTP2XMLInterceptorTest method parseXML.

@Test
public void parseXML() throws Exception {
    String xml = "<request method='POST' http-version='1.1'><uri value='http://localhost:3011/manager/person?vorname=jim&amp;nachname=panse'><host>localhost</host><port>8080</port><path><component>manager</component><component>person</component></path><query><param name='vorname'>jim</param><param name='nachname'>panse</param></query></uri></request>";
    XMLStreamReader r = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xml));
    // skip DocumentNode
    r.next();
    Request req = new Request();
    req.parse(r);
    assertEquals("POST", req.getMethod());
    assertEquals("1.1", req.getHttpVersion());
    assertURI(req.getUri());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) Request(com.predic8.membrane.core.http.xml.Request) Test(org.junit.Test)

Example 39 with Request

use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.

the class RequestPerformanceVersion1 method parseHeaderPerCharWithBuf.

@Test
public void parseHeaderPerCharWithBuf() throws Exception {
    long time = System.currentTimeMillis();
    for (int i = 0; i <= 100000; i++) {
        parseHeaderWithTokensFromBuffer(new Request(), getStream());
    }
    System.out.println("time per char with buf: " + (System.currentTimeMillis() - time) / 60000.0);
}
Also used : Request(com.predic8.membrane.core.http.Request)

Example 40 with Request

use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.

the class RequestPerformanceVersion4 method parseHeaderFromArray.

@Test
public void parseHeaderFromArray() throws Exception {
    long time = System.currentTimeMillis();
    for (int i = 0; i <= 1000000; i++) {
        parseHeaderFromArray(new Request(), buffer);
    }
    System.out.println("time from array: " + (System.currentTimeMillis() - time) / 1000.0);
}
Also used : Request(com.predic8.membrane.core.http.Request) Test(org.junit.Test)

Aggregations

Request (com.predic8.membrane.core.http.Request)39 Exchange (com.predic8.membrane.core.exchange.Exchange)20 Test (org.junit.Test)12 IOException (java.io.IOException)11 Header (com.predic8.membrane.core.http.Header)8 Response (com.predic8.membrane.core.http.Response)8 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)5 Message (com.predic8.membrane.core.http.Message)5 Outcome (com.predic8.membrane.core.interceptor.Outcome)5 Body (com.predic8.membrane.core.http.Body)3 Request (com.predic8.membrane.core.http.xml.Request)3 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 CacheBuilder (com.google.common.cache.CacheBuilder)2 HeaderField (com.predic8.membrane.core.http.HeaderField)2 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)2 ResolverMap (com.predic8.membrane.core.resolver.ResolverMap)2 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)2 StringReader (java.io.StringReader)2 SocketException (java.net.SocketException)2