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());
}
}
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;
}
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;
}
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);
}
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(), "<"));
}
Aggregations