Search in sources :

Example 11 with Message

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

the class XMLContentFilter method removeElementsIfNecessary.

/**
 * @param originalMessage
 * @param xopDecodedMessage
 * @param doc
 * @throws XPathExpressionException
 * @throws TransformerException
 * @throws TransformerConfigurationException
 * @throws TransformerFactoryConfigurationError
 */
private void removeElementsIfNecessary(Message originalMessage, Message xopDecodedMessage, Document doc) throws XPathExpressionException, TransformerException, TransformerConfigurationException, TransformerFactoryConfigurationError {
    NodeList toBeDeleted = (NodeList) createXPathExpression().evaluate(doc, XPathConstants.NODESET);
    if (toBeDeleted.getLength() > 0) {
        // change is necessary
        originalMessage.getHeader().removeFields(Header.CONTENT_ENCODING);
        if (xopDecodedMessage != null) {
            originalMessage.getHeader().removeFields(Header.CONTENT_TYPE);
            if (xopDecodedMessage.getHeader().getContentType() != null)
                originalMessage.getHeader().setContentType(xopDecodedMessage.getHeader().getContentType());
        }
        for (int i = 0; i < toBeDeleted.getLength(); i++) {
            Node n = toBeDeleted.item(i);
            n.getParentNode().removeChild(n);
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        createTransformer().transform(new DOMSource(doc), new StreamResult(baos));
        originalMessage.setBodyContent(baos.toByteArray());
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ContainerNode(com.predic8.membrane.core.interceptor.xmlcontentfilter.SimpleXPathParser.ContainerNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with Message

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

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

the class XOPReconstitutor method split.

@SuppressWarnings("deprecation")
private HashMap<String, Part> split(Message message, String boundary) throws IOException, EndOfStreamException, MalformedStreamException {
    HashMap<String, Part> parts = new HashMap<String, Part>();
    MultipartStream multipartStream = new MultipartStream(MessageUtil.getContentAsStream(message), boundary.getBytes(Constants.UTF_8_CHARSET));
    boolean nextPart = multipartStream.skipPreamble();
    while (nextPart) {
        Header header = new Header(multipartStream.readHeaders());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        multipartStream.readBodyData(baos);
        // see http://www.iana.org/assignments/transfer-encodings/transfer-encodings.xml
        String cte = header.getFirstValue("Content-Transfer-Encoding");
        if (cte != null && !cte.equals("binary") && !cte.equals("8bit") && !cte.equals("7bit"))
            throw new RuntimeException("Content-Transfer-Encoding '" + cte + "' not implemented.");
        Part part = new Part(header, baos.toByteArray());
        String id = part.getContentID();
        if (id != null) {
            parts.put(id, part);
        }
        nextPart = multipartStream.readBoundary();
    }
    return parts;
}
Also used : Header(com.predic8.membrane.core.http.Header) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MultipartStream(org.apache.commons.fileupload.MultipartStream)

Example 14 with Message

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

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

the class ReassembleTest method testXMLContentFilter.

private void testXMLContentFilter(String xpath, int expectedNumberOfRemainingElements) throws IOException, XPathExpressionException {
    XMLContentFilter cf = new XMLContentFilter(xpath);
    Message m = getResponse();
    cf.removeMatchingElements(m);
    Assert.assertEquals("text/xml", m.getHeader().getContentType());
    Assert.assertEquals(expectedNumberOfRemainingElements + 1, StringUtils.countMatches(m.getBody().toString(), "<"));
}
Also used : XMLContentFilter(com.predic8.membrane.core.interceptor.xmlcontentfilter.XMLContentFilter) Message(com.predic8.membrane.core.http.Message)

Aggregations

Message (com.predic8.membrane.core.http.Message)9 Response (com.predic8.membrane.core.http.Response)7 IOException (java.io.IOException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5 JsonFactory (com.fasterxml.jackson.core.JsonFactory)4 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)4 Header (com.predic8.membrane.core.http.Header)4 Request (com.predic8.membrane.core.http.Request)4 Exchange (com.predic8.membrane.core.exchange.Exchange)2 Body (com.predic8.membrane.core.http.Body)2 HeaderField (com.predic8.membrane.core.http.HeaderField)2 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)2 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)2 ResolverMap (com.predic8.membrane.core.resolver.ResolverMap)2 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)2 StringWriter (java.io.StringWriter)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1