Search in sources :

Example 36 with Body

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

the class XMLContentFilterTest method getMessage.

private Message getMessage() {
    Message m = new Request();
    m.setBody(new Body(DOC.getBytes()));
    return m;
}
Also used : Message(com.predic8.membrane.core.http.Message) Request(com.predic8.membrane.core.http.Request) Body(com.predic8.membrane.core.http.Body)

Example 37 with Body

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

the class XSLTInterceptorTest method testXSLTParameter.

@Test
public void testXSLTParameter() throws Exception {
    exc = new Exchange(null);
    exc.setResponse(Response.ok().body(getClass().getResourceAsStream("/customer.xml"), true).build());
    exc.setProperty("XSLT_COMPANY", "predic8");
    XSLTInterceptor i = new XSLTInterceptor();
    i.setXslt("classpath:/customer2personAddCompany.xsl");
    i.init(new HttpRouter());
    i.handleResponse(exc);
    // printBodyContent();
    assertXPath("/person/name/first", "Rick");
    assertXPath("/person/name/last", "Cort\u00e9s Ribotta");
    assertXPath("/person/address/street", "Calle P\u00fablica \"B\" 5240 Casa 121");
    assertXPath("/person/address/city", "Omaha");
    assertXPath("/person/company", "predic8");
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) HttpRouter(com.predic8.membrane.core.HttpRouter) Test(org.junit.Test)

Example 38 with Body

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

the class IllegalCharactersInURLTest method doit.

@Test
public void doit() throws Exception {
    URIFactory uriFactory = new URIFactory(true);
    Response res = new HttpClient().call(new Request.Builder().method("GET").url(uriFactory, "http://localhost:3027/foo{}").body("").buildExchange()).getResponse();
    Assert.assertEquals(200, res.getStatusCode());
}
Also used : Response(com.predic8.membrane.core.http.Response) HttpResponse(org.apache.http.HttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URIFactory(com.predic8.membrane.core.util.URIFactory) Test(org.junit.Test)

Example 39 with Body

use of com.predic8.membrane.core.http.Body 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);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Swagger(io.swagger.models.Swagger)

Example 40 with Body

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

the class TestServiceInterceptor method handleSOAP12.

private Response handleSOAP12(Element envelope) {
    Element body = null;
    NodeList children = envelope.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Text) {
            String text = ((Text) children.item(i)).getNodeValue();
            for (int j = 0; j < text.length(); j++) if (!Character.isWhitespace(text.charAt(j)))
                throw new AssertionError("Found non-whitespace text.");
            continue;
        }
        if (!(children.item(i) instanceof Element))
            throw new AssertionError("Non-element child of <Envelope> found: " + children.item(i).getNodeName() + ".");
        Element item = (Element) children.item(i);
        if (!item.getNamespaceURI().equals(Constants.SOAP12_NS))
            throw new AssertionError("Non-SOAP child element of <Envelope> found.");
        if (item.getLocalName().equals("Body"))
            body = item;
    }
    if (body == null)
        throw new AssertionError("No SOAP <Body> found.");
    children = body.getChildNodes();
    Element operation = null;
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Text) {
            String text = ((Text) children.item(i)).getNodeValue();
            for (int j = 0; j < text.length(); j++) if (!Character.isWhitespace(text.charAt(j)))
                throw new AssertionError("Found non-whitespace text.");
            continue;
        }
        if (!(children.item(i) instanceof Element))
            throw new AssertionError("Non-element child of <Body> found: " + children.item(i).getNodeName() + ".");
        operation = (Element) children.item(i);
    }
    if (operation == null)
        throw new AssertionError("No SOAP <Body> found.");
    return handleOperation(operation, false);
}
Also used : MCElement(com.predic8.membrane.annot.MCElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text)

Aggregations

Exchange (com.predic8.membrane.core.exchange.Exchange)30 IOException (java.io.IOException)17 Response (com.predic8.membrane.core.http.Response)15 Request (com.predic8.membrane.core.http.Request)12 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)10 Test (org.junit.Test)10 CacheBuilder (com.google.common.cache.CacheBuilder)8 MCElement (com.predic8.membrane.annot.MCElement)6 Body (com.predic8.membrane.core.http.Body)6 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)6 UnknownHostException (java.net.UnknownHostException)6 Message (com.predic8.membrane.core.http.Message)5 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)5 InputStream (java.io.InputStream)5 JsonFactory (com.fasterxml.jackson.core.JsonFactory)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 HttpRouter (com.predic8.membrane.core.HttpRouter)4 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)4 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)4 Header (com.predic8.membrane.core.http.Header)4