通過創建一個自定義Class中介來解決這個問題,該中介採用數據並將其傳輸到文件中。課堂調解員完成作業後,課程將繼續進行。
這裏的類中介代碼:
public class FileSaveMediator extends AbstractMediator {
boolean traceOn = false;
boolean traceOrDebugOn = false;
public boolean mediate(MessageContext context) {
traceOn = isTraceOn(context);
traceOrDebugOn = isTraceOrDebugOn(traceOn);
traceOrDebug(traceOn, "Start : FileSaveMediator");
// Get property "fileuri" from synapse context
String fileuri = (String)context.getProperty("fileuri");
SOAPBody body = context.getEnvelope().getBody();
OMText binaryNode = (OMText) (body.getFirstElement()).getFirstOMChild();
DataHandler actualDH;
actualDH = (DataHandler) binaryNode.getDataHandler();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fileuri);
actualDH.writeTo(fos);
} catch (Exception e)
{
e.printStackTrace();
}
// Clear Body after saving file by setting dummy tags.
try {
body.setFirstChild(AXIOMUtil.stringToOM("<p></p>"));
} catch (XMLStreamException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// Finally make sure that fileoutstream is closed
if (fos!=null)
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
traceOrDebug(traceOn, "End : FileSaveMediator");
return true;
}