欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mule的Servlet组件将request body放到message的payload中 mulepayloadrequest body 

程序员文章站 2022-06-17 13:27:21
...

mule源代码

/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */
package org.mule.transport.servlet;

import org.mule.DefaultMuleMessage;
import org.mule.api.MuleContext;
import org.mule.api.MuleMessage;
import org.mule.transport.AbstractMuleMessageFactory;
import org.mule.transport.http.HttpConstants;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.EnumerationUtils;

public class ServletMuleMessageFactory extends AbstractMuleMessageFactory
{
    ……

    protected Object extractPayloadFromPostRequest(HttpServletRequest request) throws Exception
    {
        /*
         * Servlet Spec v2.5:
         *
         * SRV.3.1.1
         * When Parameters Are Available
         *
         * The following are the conditions that must be met before post form data will
         * be populated to the parameter set:
         *
         * 1. The request is an HTTP or HTTPS request.
         * 2. The HTTP method is POST.
         * 3. The content type is application/x-www-form-urlencoded.
         * 4. The servlet has made an initial call of any of the getParameter family of meth-
         *    ods on the request object.
         *
         * If the conditions are not met and the post form data is not included in the
         * parameter set, the post data must still be available to the servlet via the request
         * object's input stream. If the conditions are met, post form data will no longer be
         * available for reading directly from the request object's input stream.
         *
         * -----------------------------------------------------------------------------------
         *
         * In plain english:if you call getInputStream on a POSTed request before you call one of
         * the getParameter* methods, you'll lose the parameters. So we touch the parameters first
         * and only then we return the input stream that will be the payload of the message.
         */
        request.getParameterNames();

        return request.getInputStream();
    }
    ……
}

 

 “request.getParameterNames();”这一句代码已经对InpuStream进行了读取,当执行“return request.getInputStream();”时,已经无法从InpuStream中获取到request body了,所以要把 “request.getParameterNames();”这一句代码删除。

重写该factory,然后让Servlet组件调用connector,该connector使用新的factory,如下:

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

 

Ref:

http://download.oracle.com/otn-pub/jcp/servlet-2.5-mr5-oth-JSpec/servlet-2.5-mr5-spec.pdf?AuthParam=1480926368_0fcec9d71c58792a07c45e3f8f129d65#search=%27servlet+2.5+spec%27

SRV.3.1.1