Search in sources :

Example 26 with Body

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

the class TestServiceInterceptor method handleSOAP11.

private Response handleSOAP11(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.SOAP11_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, true);
}
Also used : MCElement(com.predic8.membrane.annot.MCElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text)

Example 27 with Body

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

the class XOPReconstitutor method getReconstitutedMessage.

/**
 * @return reassembled SOAP message or null if message is not SOAP or not multipart
 */
public Message getReconstitutedMessage(Message message) throws ParseException, MalformedStreamException, IOException, EndOfStreamException, XMLStreamException, FactoryConfigurationError {
    ContentType contentType = message.getHeader().getContentTypeObject();
    if (contentType == null || contentType.getPrimaryType() == null)
        return null;
    if (!contentType.getPrimaryType().equals("multipart") || !contentType.getSubType().equals("related"))
        return null;
    String type = contentType.getParameter("type");
    if (!"application/xop+xml".equals(type))
        return null;
    String start = contentType.getParameter("start");
    if (start == null)
        return null;
    String boundary = contentType.getParameter("boundary");
    if (boundary == null)
        return null;
    HashMap<String, Part> parts = split(message, boundary);
    Part startPart = parts.get(start);
    if (startPart == null)
        return null;
    ContentType innerContentType = new ContentType(startPart.getHeader().getContentType());
    if (!innerContentType.getPrimaryType().equals("application") || !innerContentType.getSubType().equals("xop+xml"))
        return null;
    byte[] body = fillInXOPParts(startPart.getInputStream(), parts);
    Message m = new Message() {

        @Override
        protected void parseStartLine(InputStream in) throws IOException, EndOfStreamException {
            throw new RuntimeException("not implemented.");
        }

        @Override
        public String getStartLine() {
            throw new RuntimeException("not implemented.");
        }

        @Override
        public <T extends Message> T createSnapshot() {
            throw new RuntimeException("not implemented.");
        }
    };
    m.setBodyContent(body);
    String reconstitutedContentType = innerContentType.getParameter("type");
    if (reconstitutedContentType != null)
        m.getHeader().add(Header.CONTENT_TYPE, reconstitutedContentType);
    return m;
}
Also used : ContentType(javax.mail.internet.ContentType) Message(com.predic8.membrane.core.http.Message) InputStream(java.io.InputStream)

Example 28 with Body

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

the class SOAPStackTraceFilterTest method doit.

@Test
public void doit() throws XPathExpressionException, Exception {
    Exchange exc = new Exchange(null);
    exc.setRequest(getRequest());
    new SOAPStackTraceFilterInterceptor().handleRequest(exc);
    String body = exc.getRequest().getBody().toString();
    AssertUtils.assertContainsNot("SECRET", body);
    AssertUtils.assertContains("KEEP1", body);
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Test(org.junit.Test)

Example 29 with Body

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

the class SOAPStackTraceFilterTest method getRequest.

private Request getRequest() throws IOException {
    Request r = new Request();
    r.setBody(new Body(getClass().getResourceAsStream("/xml/soap-stack-trace.xml")));
    return r;
}
Also used : Request(com.predic8.membrane.core.http.Request) Body(com.predic8.membrane.core.http.Body)

Example 30 with Body

use of com.predic8.membrane.core.http.Body 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)

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