如何通过WSO2 ESB流式加密有效载荷文件

时间:2017-04-05 06:36:51

标签: wso2esb

我必须使用WSO2 ESB实现一个场景,因为在响应客户端时流式加密二进制有效负载(我假设内容类型是Application / Octet-Stream),下面是我的一些细节思想:

  1. 提供业务功能的端点,例如“http://myhost/backend/”;
  2. 通过端点传递消息的代理;
  3. 我尝试编写一个OutSequence来检查Content-type:如果Content-Type匹配from tkinter import * from random import randint root = Tk() mframe = Frame(root).pack() gframe = Frame(root).pack() frame.pack() start = True class mainMenu: def __init__(self): gframe.destroy() #gets rid of previous interface title = Label(mframe, text = "main menu").pack() #mfame stands for menu frame class mainGame: def __init__(self): if start == False: mframe.destroy() #gets rid of previous interface #question self.question_var = StringVar() Label(gframe, textvariable=self.question_var).pack() #gframe stands for game frame #answer self.user_answer_var = StringVar() entry = Entry(gframe, textvariable=self.user_answer_var) entry.pack() submit = Button(gframe, text = "submit", command = self.check_answer).pack() #response output self.response_var = StringVar() self.count = 0 self.score = 0 Label(gframe, textvariable=self.response_var).pack() #starts loop self.ask_question() root.mainloop() def ask_question(self): if self.count == 1: self.endGame() num1 = randint(1, 10) num2 = randint(1, 10) self.question_var.set("What is "+str(num1)+" + " +str(num2)+"?") self.true_answer = num1 + num2 #print(self.true_answer) #testing purposes def check_answer(self): self.count += 1 user_answer = self.user_answer_var.get() #print(user_answer) #testing purposes if int(user_answer) == self.true_answer: text = "Good job" self.score += 1 else: text = "Oh no" self.response_var.set(text) #clear answer for next loop self.user_answer_var.set("") self.ask_question() def endGame(self): print("endGame") mainMenu() mainGame() ,则调用我的自定义类中介来加密文件流Streamingly和响应。
  4. 我不知道如何编写类中介来实现它?我怎么能从消息中获取/读取文件流以及如何将outputStream放回到响应中,而我只能在调解方法中看到Application/Octet-Stream?以下是我当前的调解员,但不起作用。

    mc.getEnvelope().getBody()

    非常感谢有经验的人提供帮助。

1 个答案:

答案 0 :(得分:0)

将我当前的解决方案粘贴给遇到类似问题的任何其他人。 在中介中,我通过OMText.InputStream从响应流中读取文件内容,并使用net.lingala.zip4j包写入zip文件(在内存中),加密原始文件;最后,我将Zip文件内容作为ByteArray写回soap消息的OMElement。

    public boolean mediate(MessageContext mc) {
    System.out.println("========================Mediator log start================================");
    org.apache.axis2.context.MessageContext amc = ((Axis2MessageContext) mc).getAxis2MessageContext();
    try {
        @SuppressWarnings("unchecked")
        Map<String, String> responseHeaders = (Map<String, String>) amc.getProperty("TRANSPORT_HEADERS");
        String rawFileName = "";
        String[] contentDisps = responseHeaders.get("Content-Disposition").split(";");
        for (String item : contentDisps) {
            System.out.println("item::" + item);
            if (item.trim().startsWith(CONTENT_DISPOSITION_FILENAME)) {
                rawFileName = item.substring(item.indexOf("\"") + 1, item.length() - 1);
                break;
            }
        }
        responseHeaders.put(
            "Content-Disposition",
            responseHeaders.get("Content-Disposition").replace(rawFileName,
                rawFileName.substring(0, rawFileName.lastIndexOf(".")) + ".myzip"));
        OMElement binaryPayload =
            amc.getEnvelope().getBody()
                .getFirstChildWithName(new QName("http://ws.apache.org/commons/ns/payload", "binary"));
        OMText binaryNode = (OMText) binaryPayload.getFirstOMChild();
        DataHandler dataHandler = (DataHandler) binaryNode.getDataHandler();
        InputStream is = dataHandler.getInputStream();
        ByteArrayOutputStream responseOutputStream = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = getZipOutputStreamInstance(responseOutputStream, rawFileName);
        // write to zipOutputStream
        byte data[] = new byte[BUFFER_SIZE];
        int count;
        while ((count = is.read(data, 0, BUFFER_SIZE)) != -1) {
            zipOutputStream.write(data, 0, count);
            zipOutputStream.flush();
        }
        zipOutputStream.closeEntry();
        zipOutputStream.finish();
        InputStream in = new ByteArrayInputStream(responseOutputStream.toByteArray());
        DataHandler zipDataHandler = new DataHandler(new StreamingOnRequestDataSource(in));
        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMText zipData = factory.createOMText(zipDataHandler, true);
        zipData.setBinary(true);
        binaryPayload.getFirstOMChild().detach();
        binaryPayload.addChild(zipData);
        amc.setProperty("TRANSPORT_HEADERS", responseHeaders);
        System.out.println("========================Mediator end==================================");
    } catch (Exception ex) {
        System.out.println("exception found here:");
        ex.printStackTrace();
    }
    return true;
}