use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.
the class SynapseConfigUtils method readNonXML.
/**
* Helper method to handle non-XMl resources
*
* @param url The resource url
* @return The content as an OMNode
*/
public static OMNode readNonXML(URL url) {
try {
// Open a new connection
URLConnection newConnection = getURLConnection(url);
if (newConnection == null) {
if (log.isDebugEnabled()) {
log.debug("Cannot create a URLConnection for given URL : " + url);
}
return null;
}
BufferedInputStream newInputStream = new BufferedInputStream(newConnection.getInputStream());
OMFactory omFactory = OMAbstractFactory.getOMFactory();
return omFactory.createOMText(new DataHandler(new SynapseBinaryDataSource(newInputStream, newConnection.getContentType())), true);
} catch (IOException e) {
handleException("Error when getting a stream from resource's content", e);
}
return null;
}
use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.
the class HessianMessageFormatter method writeHessianMessage.
/**
* Writes the Hessian message contained in the message context to the provided output stream.
*
* @param msgCtxt the message context containing the Hessian message
* @param out the provided output stream to which the message shall be written
*
* @throws AxisFault if an error occurs writing to the output stream
*/
private void writeHessianMessage(MessageContext msgCtxt, OutputStream out) throws AxisFault {
OMElement omElement = msgCtxt.getEnvelope().getBody().getFirstElement();
SynapseBinaryDataSource synapseBinaryDataSource = extractSynapseBinaryDataSource(omElement);
if (synapseBinaryDataSource != null) {
InputStream inputStream = null;
try {
inputStream = synapseBinaryDataSource.getInputStream();
IOUtils.copy(inputStream, out);
} catch (IOException e) {
handleException("Couldn't get the bytes from the HessianDataSource", e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignore) {
log.warn("Error closing input stream.", ignore);
}
}
if (out != null) {
try {
out.close();
} catch (IOException ignore) {
log.warn("Error closing output stream.", ignore);
}
}
}
} else {
handleException("Unable to find the Hessian content in the payload");
}
}
use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.
the class HessianMessageBuilder method processDocument.
/**
* Returns an OMElement from a Hessian encoded message
*
* @param inputStream stream containing the Hessian message to be built
* @param contentType content type of the message
* @param messageContext message to which the hessian message has to be attached
* @return OMElement containing Hessian data handler keeping the message
* @throws AxisFault in case of a failure in building the hessian message
*
* @see org.apache.axis2.builder.Builder#processDocument(java.io.InputStream,
* String, org.apache.axis2.context.MessageContext)
*/
public OMElement processDocument(final InputStream inputStream, final String contentType, final MessageContext messageContext) throws AxisFault {
if (log.isDebugEnabled()) {
log.debug("Start building the hessian message in to a HessianDataSource");
}
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace ns = factory.createOMNamespace(HessianConstants.HESSIAN_NAMESPACE_URI, HessianConstants.HESSIAN_NS_PREFIX);
OMElement element = factory.createOMElement(HessianConstants.HESSIAN_ELEMENT_LOCAL_NAME, ns);
try {
Parameter synEnv = messageContext.getConfigurationContext().getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_ENV);
PushbackInputStream pis = detectAndMarkMessageFault(messageContext, inputStream);
DataHandler dataHandler;
if (synEnv != null && synEnv.getValue() != null) {
dataHandler = new DataHandler(new SynapseBinaryDataSource(pis, contentType, (SynapseEnvironment) synEnv.getValue()));
} else {
// add Hessian data inside a data handler
dataHandler = new DataHandler(new SynapseBinaryDataSource(pis, contentType));
}
OMText textData = factory.createOMText(dataHandler, true);
element.addChild(textData);
// indicate that message faults shall be handled as http 200
messageContext.setProperty(NhttpConstants.FAULTS_AS_HTTP_200, NhttpConstants.TRUE);
} catch (IOException e) {
String msg = "Unable to create the HessianDataSource";
log.error(msg, e);
throw new AxisFault(msg, e);
}
if (log.isDebugEnabled()) {
log.debug("Building the hessian message using HessianDataSource is successful");
}
return element;
}
use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.
the class HessianMessageBuilderTest method test.
private MessageContext test(String testMessageName, SynapseEnvironment synEnv) throws IOException {
HessianTestHelper hessianTestHelper = new HessianTestHelper();
MessageContext msgContext = hessianTestHelper.createAxis2MessageContext(synEnv);
OMElement element = hessianTestHelper.buildHessianTestMessage(testMessageName, msgContext);
OMNode hessianNode = element.getFirstOMChild();
OMText hessianTextNode = (OMText) hessianNode;
SynapseBinaryDataSource synapseBinaryDataSource = (SynapseBinaryDataSource) ((DataHandler) hessianTextNode.getDataHandler()).getDataSource();
InputStream inputStream = synapseBinaryDataSource.getInputStream();
byte[] originalByteArray = IOUtils.toByteArray(getClass().getResourceAsStream(testMessageName));
byte[] builderByteArray = IOUtils.toByteArray(inputStream);
assertTrue(Arrays.equals(originalByteArray, builderByteArray));
return msgContext;
}
use of org.apache.synapse.util.SynapseBinaryDataSource in project wso2-synapse by wso2.
the class HessianMessageFormatter method extractSynapseBinaryDataSource.
/**
* Tries to extract the binary data source containing the Hessian message.
*
* @param omElement
*
* @return the binary data source containing the Hessian message or null, if the OMElement
* does not contain a binary datasource.
*/
private SynapseBinaryDataSource extractSynapseBinaryDataSource(OMElement omElement) {
SynapseBinaryDataSource synapseBinaryDataSource = null;
Iterator it = omElement.getChildren();
while (it.hasNext() && synapseBinaryDataSource == null) {
OMNode hessianElement = (OMNode) it.next();
if (hessianElement instanceof OMText) {
OMText tempNode = (OMText) hessianElement;
if (tempNode.getDataHandler() != null && ((DataHandler) tempNode.getDataHandler()).getDataSource() instanceof SynapseBinaryDataSource) {
synapseBinaryDataSource = (SynapseBinaryDataSource) ((DataHandler) tempNode.getDataHandler()).getDataSource();
}
}
}
return synapseBinaryDataSource;
}
Aggregations