发送附带http:outbound-endpoint的附件的json msg Mule 3.5

时间:2016-08-22 18:35:04

标签: java rest jersey mule attachment

编辑更新问题:

我想通过mule http:outbound endpoint使用Mule 3.5发送附件和一些json

更新的问题:

我正在尝试使用mule transormers并在相同的出站端点请求中,可以这样做吗?

<set-attachment attachmentName="attach-Attachment1" contentType="text/plain" doc:name="Attach text file" value="#[bin1]"/> <set-payload value="{'Attachment1' : 'file.txt'}"/> <http:outbound-endpoint exchange-pattern="request-response" host="${myhost}" port="${myPort}" method="POST" mimeType="multipart/form-data" doc:name="HTTP" path="myPath" >

原始问题: 我可以这样做吗?

  1. 在Java Transformer中,构建http实体:

        // build the JSON entry
    String json = "{ \"values\" : { ";
    json += "\"Submitter\" : \"Allen\", ";
    json += "\"Short Description\" : \"testing 123\", ";
    json += "\"Attachment1\" : <Base64 encoded file data String here> ;
    json += " } }";
    
    // build the multipart entity
    HttpEntity entity = MultipartEntityBuilder.create()
            .addTextBody("entry", json, ContentType.APPLICATION_JSON)
            .addBinaryBody("attach-Attachment1", new File(filename))
            .build();
    
  2. 将此HttpEntity对象从Java转换器返回到消息有效负载中的mule流。
  3. 将流中生成的mule消息传递给http:outbound-endpoint
  4. 我正在尝试构建一个mule流,它将向休息服务发送附件,该服务要求请求包含Multipart表单数据。我想将实体传递给出站端点,以便请求是通过mule流进行的,而不是在java变换器内。

    我有可以在这里发布的代码作为其他人发现这篇文章的可能解决方案,我不得不回顾旧代码,因为我的雇主决定改变我们的堆栈,我们不再使用Mule了。所以,把它作为免责声明,我不记得我的骡子流中的这段代码是否曾经过测试。 `

                <set-variable variableName="attaches" value="#[new java.util.HashMap()]" doc:name="Variable"/>
                <!-- note, message object inside for loop is not same message as in flow, after for loop, original message is not changed, so 
                     we can't add attachments to actual mule flow message   inside for loop, we need to put the attachments into a hashmap, then 
                     outside the loop add the attachments to the mule Message object  -->
                <foreach collection="#[message.payload.binaryEncodedAttachmentData.entrySet()]">
                    <!-- Payload is now a Map.Entry object --> 
                    <logger message="key is #[payload.key]" level="INFO" doc:name="LoggerPayloadError" />
                    <logger message="value is #[payload.value]" level="INFO" doc:name="LoggerPayloadError" />
                    <set-attachment attachmentName="#[payload.key]" contentType="text/plain" doc:name="Attach text file" value="#[payload.value]"/>
                    <expression-component doc:name="Expression">
                        <![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
                                                    for(Object key:keys){
                                                     flowVars.attaches.put(key, message.outboundAttachments.get(key));
                                                    }]]>
                    </expression-component>
               </foreach> 
               <expression-component doc:name="Expression">
                   <![CDATA[message.outboundAttachments.putAll(flowVars.attaches);]]>
               </expression-component>
    
               <logger message="done adding attachments" level="INFO" doc:name="Done Adding Attach" />
    
              <!-- done adding attachments --> 
          </when>`
    

    ** note文档表明添加附件后有效负载应为null,因此此代码遵循上面添加附件的代码,调试位可能对其他开发人员发现此问题有帮助,另请注意上面的免责声明代码未经测试!

                `<set-payload value="#[null]" /> 
                <!--  below code for debugging only -->
                     <set-variable variableName="dh1" value="#[message.outboundAttachments['attach-Attachment1']]"></set-variable> 
                     <set-variable variableName="dh2" value="#[message.outboundAttachments['entry']]"></set-variable><expression-component>
                     <![CDATA[
                    ByteArrayOutputStream baos = new ByteArrayOutputStream(4);
                    DataHandler dh1 = flowVars.dh1;
            dh1.writeTo(baos);
            System.out.println("<<attach-Attachment1>>");
            System.out.println(baos.toString());
                    ByteArrayOutputStream baos2 = new ByteArrayOutputStream(4);          
                    DataHandler dh2 = flowVars.dh2;
            dh2.writeTo(baos2);
            System.out.println("<<entry>>");
            System.out.println(baos2.toString());        
                    ]]></expression-component>
    
                <!--  end code for debugging only -->`
    

0 个答案:

没有答案
相关问题