I try to write my own WSO2 3.2 APIM Handler.
My purpose is :
- engage Handler when a dedicated property is set on API
- consume the request and use the binary response to decrypt part of it
- change the content type of the response after decryption
I successfuly write my handler and modify the velocity_template.xml as follow (following the documentation):
#if($apiObj.additionalProperties.get('encrypted') == "true"))
<handler class="org.rudi.wso2.mediation.EncryptedMediaHandler">
<property name="encryptedMimeType" value="$apiObj.additionalProperties.get('encrypted_mime_type')"/>
<property name="mimeType" value="$apiObj.additionalProperties.get('mime_type')"/>
<property name="providerUuid" value="dummy"/>
</handler>
#end
But this does not work and the handler is not engaged.
If I remove the #if condition the handler is engaged but the properties are not interpreted (for exemple the "mimeType" field is explicitly set to "$apiObj.additionalProperties.get('mime_type')" and not to the value of the additionnalProperty set on API.
What is wrong ? How could I use the additionnal properties added on API ?
Next when the Handler is called, I did not find any way to read the response of the endpoint.
I find code to change the response ou the response status code to write fault for exemple
But I did not find a way to read the binary response send by my endpoint to work on it.
Help will be appreciated!
UPDATED
For the second part of the question, I create a method as follow:
private void replaceBody(SOAPBody body) throws IOException {
OMElement element = body.getFirstElement();
if (element.getLocalName().equalsIgnoreCase(BINARY_LOCAL_NAME)) {
OMNode subChild = element.getFirstOMChild();
if (subChild instanceof OMText && ((OMText) subChild).isBinary()) {
OMText textNode = ((OMText) subChild);
DataHandler originalDataHandler = (DataHandler) textNode.getDataHandler();
InputStream modifiedInputStream = modify(originalDataHandler.getInputStream());
DataHandler newDataHandler = new DataHandler(new StreamingOnRequestDataSource(modifiedInputStream ));
OMText newTextNode = body.getOMFactory().createOMText(newDataHandler, true);
textNode.insertSiblingBefore(newTextNode);
textNode.detach();
}
}
}
And I lookup for SOAPBody as follow :
org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext)
.getAxis2MessageContext();
RelayUtils.buildMessage(axis2MC, true);
axis2MC.setProperty(RelayConstants.FORCE_RESPONSE_EARLY_BUILD, Boolean.TRUE);
SOAPBody body = axis2MC.getEnvelope().getBody();
I test the behaviour on small file but I think we need to do more stuff to handle big file ou chunked api.
Any advice?