mule传附件attachmentITeye - 威尼斯人

mule传附件attachmentITeye

2019-01-11 12:06:12 | 作者: 向松 | 标签: 附件,需求,呈现 | 浏览: 284

import org.mule.transport.http.HttpMuleMessageFactory; import org.mule.transport.http.HttpRequest; import org.mule.transport.http.multipart.MultiPartInputStream; import org.mule.transport.http.multipart.Part; import org.mule.transport.http.multipart.PartDataSource; import com.rakuten.api.cabinet.constants.CabinetFlowConstants; * Corretion Alexis ANASTASSIADES ___ alexis - at - ezanas.com * @author ldns_zhuling public class HttpMultipartMuleMessageFactory extends HttpMuleMessageFactory { /** parts */ private Collection Part parts; * @param context context public HttpMultipartMuleMessageFactory(MuleContext context) { super(context); @Override protected Object extractPayloadFromHttpRequest(HttpRequest httpRequest) throws IOException Object body = null; String contentType = httpRequest.getContentType(); if (contentType.toLowerCase().contains("multipart/form-data")) { // modify multipart/form-data to lower case // 或许呈现大小写的contentType,需求悉数转为小写的 contentType = contentType.replace(contentType.substring(0, CabinetFlowConstants.CONTENT_TYPE_MULTIPART_PART.length()), CabinetFlowConstants.CONTENT_TYPE_MULTIPART_PART); MultiPartInputStream in = new MultiPartInputStream(httpRequest.getBody(), contentType, null); // We need to store this so that the headers for the part can be read parts = in.getParts(); for (Part part : parts) { //将名为xml的附件内容放到message.payload中,这个依据项目传进来的附件名进行修正 if ("xml".equals(part.getName())) { body = part.getInputStream(); break; } else { body = super.extractPayloadFromHttpRequest(httpRequest); return body; @Override protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception { if (parts != null) { try { for (Part part : parts) { //将除了名为payload的其他一切的附件放到header中 //主张在附件使用完后,将附件删去,不然回来会呈现过错 if (!"payload".equals(part.getName())) { message.setInboundProperty(part.getName(), new DataHandler(new PartDataSource(part))); } finally { // Attachments are the last thing to get processed parts.clear(); parts = null; @Override protected void convertMultiPartHeaders(Map String, Object headers) { }

 Servlet Endpoint

需求重写ServletMuleMessageFactory.java

package com.rakuten.api.cabinet.factory;
import java.util.Collection;
import javax.activation.DataHandler;
import javax.servlet.http.HttpServletRequest;
import org.mule.DefaultMuleMessage;
import org.mule.api.MuleContext;
import org.mule.transport.http.multipart.MultiPartInputStream;
import org.mule.transport.http.multipart.Part;
import org.mule.transport.http.multipart.PartDataSource;
import org.mule.transport.servlet.ServletMuleMessageFactory;
import com.rakuten.api.cabinet.constants.CabinetFlowConstants;
 * Corretion Alexis ANASTASSIADES ___ alexis - at - ezanas.com
 * @author ldns_zhuling
public class ServletMultipartMuleMessageFactory extends ServletMuleMessageFactory {
 /** parts */
 private Collection Part parts;
 * @param context context
 public ServletMultipartMuleMessageFactory(MuleContext context) {
 super(context);
 @Override
 protected Object extractPayloadFromPostRequest(HttpServletRequest request) throws Exception
 Object body = null;
 String contentType = request.getContentType();
 if (contentType.toLowerCase().contains(CabinetFlowConstants.CONTENT_TYPE_MULTIPART_PART)) {
 // modify multipart/form-data to lower case
 contentType = contentType.replace(contentType.substring(0,
 CabinetFlowConstants.CONTENT_TYPE_MULTIPART_PART.length()),
 CabinetFlowConstants.CONTENT_TYPE_MULTIPART_PART);
 MultiPartInputStream in = new MultiPartInputStream(request.getInputStream(),
 contentType, null);
 // We need to store this so that the headers for the part can be read
 parts = in.getParts();
 for (Part part : parts) {
 if ("xml".equals(part.getName())) {
 body = part.getInputStream();
 break;
 } else {
 //原代码中在获取body之前加了这句“request.getParameterNames();”
 //这句有必要删掉,不然获取不到body,由于InputStream不能重复读取。
 body = request.getInputStream();
 return body;
 @Override
 protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception {
 if (parts != null) {
 try {
 for (Part part : parts) {
 if (!"payload".equals(part.getName())) {
 message.setInboundProperty(part.getName(), new DataHandler(new PartDataSource(part)));
 } finally {
 // Attachments are the last thing to get processed
 parts.clear();
 parts = null;
}

 

然后需求在connector中调用该factory

 http:connector name="defaultconnector" cookieSpec="netscape"
 doc:name="HTTP\HTTPS" receiveBacklog="0" receiveBufferSize="0"
 sendBufferSize="0" socketSoLinger="0" validateConnections="true" 
 dispatcher-threading-profile
 maxBufferSize="0" maxThreadsActive="${maximum.concurrency}"
 maxThreadsIdle="${maximum.concurrency}" poolExhaustedAction="WAIT"
 threadWaitTimeout="${inner.response.timeout}" / 
 service-overrides sessionHandler="org.mule.session.NullSessionHandler" messageFactory="com.rakuten.api.cabinet.factory.HttpMultipartMuleMessageFactory"/ 
 /http:connector 

 

 servlet:connector name="Servlet" doc:name="Servlet" 
 service-overrides messageFactory="com.rakuten.api.coupon.factory.ServletMuleMessageFactory"/ 
 /servlet:connector 

 

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表威尼斯人立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章