Search in sources :

Example 16 with Body

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

the class HttpUtil method createResponse.

public static Response createResponse(int code, String msg, byte[] body, String contentType, String... headers) {
    Response res = new Response();
    res.setStatusCode(code);
    res.setStatusMessage(msg);
    res.setHeader(createHeaders(contentType, headers));
    if (body != null)
        res.setBodyContent(body);
    return res;
}
Also used : Response(com.predic8.membrane.core.http.Response)

Example 17 with Body

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

the class EchoInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    Outcome outcome = exc.echo();
    exc.getResponse().getHeader().removeFields(Header.CONTENT_LENGTH);
    String body = exc.getRequest().getUri() + "\n" + new String(exc.getRequest().getBody().getContent(), Constants.UTF_8_CHARSET);
    exc.getResponse().setBodyContent(body.getBytes(Constants.UTF_8_CHARSET));
    return outcome;
}
Also used : Outcome(com.predic8.membrane.core.interceptor.Outcome)

Example 18 with Body

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

the class WsaEndpointRewriterInterceptorTest method testRewriterInterceptor.

@Test
public void testRewriterInterceptor() throws Exception {
    exc.setRequest(MessageUtil.getPostRequest("http://localhost:9000/SoapContext/SoapPort?wsdl"));
    InputStream input = WsaEndpointRewriterTest.class.getResourceAsStream("/interceptor/ws_addressing/body.xml");
    exc.getRequest().setBody(new Body(input));
    assertEquals(Outcome.CONTINUE, rewriter.handleRequest(exc));
    assertEquals(exc.getProperty("messageId"), "urn:uuid:62a0de08-055a-4da7-aefa-730af9dbc6b6");
}
Also used : InputStream(java.io.InputStream) Body(com.predic8.membrane.core.http.Body) Test(org.junit.Test)

Example 19 with Body

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

the class AdminRESTInterceptor method getBeautifiedBody.

@Mapping("/admin/web/exchanges/(-?\\d+)/(response|request)/body")
public Response getBeautifiedBody(QueryParameter params, String relativeRootPath) throws Exception {
    AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    if (msg == null || msg.isBodyEmpty()) {
        return Response.noContent().build();
    }
    return Response.ok().contentType(MimeType.TEXT_HTML_UTF8).body(TextUtil.formatXML(new InputStreamReader(msg.getBodyAsStreamDecoded(), msg.getCharset()), true)).build();
}
Also used : Message(com.predic8.membrane.core.http.Message) InputStreamReader(java.io.InputStreamReader) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange)

Example 20 with Body

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

the class AdminRESTInterceptor method getRequestBody.

@Mapping("/admin/rest/exchanges/(-?\\d+)/(response|request)/body")
public Response getRequestBody(QueryParameter params, String relativeRootPath) throws Exception {
    AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    String ct = params.getGroup(2).equals("response") ? exc.getResponseContentType() : exc.getRequestContentType();
    if (msg == null || msg.isBodyEmpty()) {
        return Response.noContent().build();
    }
    ResponseBuilder rb = Response.ok().contentType(ct).body(msg.getBodyAsStream(), false);
    String contentEncoding = msg.getHeader().getContentEncoding();
    if (contentEncoding != null)
        rb.header(Header.CONTENT_ENCODING, contentEncoding);
    return rb.build();
}
Also used : Message(com.predic8.membrane.core.http.Message) ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange)

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