Spring integeration DefaultSoapHeaderMapper - getStandardRequestHeaderNames - 覆盖

时间:2016-05-13 01:40:24

标签: spring-integration

在以前的si版本中(si 2.11是特定的和spring 3.1.1)可以覆盖getStandardRequestHeaderNames以在si消息头中包含Additional Application特定对象。我们的应用程序依赖于此功能(可能是错误的),以覆盖此方法并提供下游传输的自定义POJO,包括许多分离器,聚合器等。应用程序使用ws入站网关并使用header-mapper属性指定自定义soap标头映射器。

有关为何无法覆盖getStandardRequestHeaderNames的原因的任何线索?

需要一些建议我如何将其迁移到当前的spring版本。 要求是从soapHeader中提取元素并将它们映射到SI消息头作为POJO并将其发送到下游。 所有帮助表示赞赏。

代码段:适用于旧版本的spring

<int-ws:inbound-gateway id="webservice-inbound-gateway"
        request-channel="input-request-channel"
        reply-channel="output-response-channel"       
        header-mapper="CustomSoapHeaderMapper"   
        marshaller="marshaller"       
        unmarshaller="marshaller" />



@Component("CustomSoapHeaderMapper")
public class CustomSoapHeaderMapper extends DefaultSoapHeaderMapper {

    private static final Logger logger = Logger.getLogger("CustomSoapHeaderMapper");

    public static final String HEADER_SEARCH_METADATA = SearchMetadata.HEADER_ATTRIBUTE_NAME;
    public static final String HEADER_SERVICE_AUDIT = "XXXXXXXX";
    // Use simulation if security token is set to this value
    public static final String SECURITY_TOKEN_SIMULATION = "XXXX";

    private static final List<String> CUSTOM_HEADER_NAMES = new ArrayList<String>();
    static {
        CUSTOM_HEADER_NAMES.add(WebServiceHeaders.SOAP_ACTION);
        CUSTOM_HEADER_NAMES.add(HEADER_SEARCH_METADATA);
    }

    private int version =SearchMetadata.VERSION_CURRENT;

    public void setVersion(int version) {
        this.version = version;
    }

    @Override
    protected List<String> getStandardRequestHeaderNames() {
        return CUSTOM_HEADER_NAMES;
    }

    @Override
    protected Map<String, Object> extractUserDefinedHeaders(SoapMessage source) {
        // logger.log(Level.INFO,"extractUserDefinedHeaders");
        // call base class to extract header
        Map<String, Object> map = super.extractUserDefinedHeaders(source);
        Document doc = source.getDocument();
        SearchMetadata searchMetadata = new SearchMetadata();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            source.writeTo(baos);
            baos.flush();
            searchMetadata.setRequestXML(baos.toString());
            baos.close();
        } catch (IOException e1) {

        }
        //logger.log(Level.WARNING, "Incoming Message " + baos.toString());

        SOAPMessage soapMessage = ((SaajSoapMessage) source).getSaajMessage();
        // generate TransactionID with UUID value
        String transactionID = UUID.randomUUID().toString();
        // logger.log(Level.WARNING, "TransactionID=" + transactionID);
        Date now = new Date();

        searchMetadata.setTransactionID(transactionID);
        searchMetadata.setRequestType(SearchMetadata.REQUEST_TYPE_SYNCHRONOUS);
        searchMetadata.setRequestTime(now);// initialize the request time
        searchMetadata.setReceivedTime(now);// mark time system receives request
        searchMetadata.setVersion(version);

        Map<String, Object> finalHeaders = new HashMap<String, Object>();
        finalHeaders.put(HEADER_SEARCH_METADATA, searchMetadata);

        if (!CollectionUtils.isEmpty(map)) {
            // copy from other map
            finalHeaders.putAll(map);

            // check if ServiceAudit is available
            SoapHeaderElement serviceAuditElement = null;
            for (String key : map.keySet()) {
                // logger.log(Level.WARNING, "SoapHeader.{0}", key);
                if (StringUtils.contains(key, HEADER_SERVICE_AUDIT)) {
                    serviceAuditElement = (SoapHeaderElement) map.get(key);
                    break; 
                }
            }


        }
        return finalHeaders;
    }

        // GK Key Thing here for performance improvement is avoiding marshalling
        public gov.dhs.ice.ess.schema.ServiceAudit ExtractAuditHeader(Document doc) {
        ....
        }
        return serviceAudit;
    }
}

1 个答案:

答案 0 :(得分:0)

请分享一些代码你想怎么看?

也许您可以实现自己的SoapHeaderMapper并将其注入WS Inbound Gateway?

您仍然可以重复使用逻辑并复制/粘贴DefaultSoapHeaderMapper中的标准行为。

<强>更新

用于演示如何手动添加用户定义标头的测试用例:

@Test
public void testCustomSoapHeaderMapper() {
    DefaultSoapHeaderMapper mapper = new DefaultSoapHeaderMapper() {

        @Override
        protected Map<String, Object> extractUserDefinedHeaders(SoapMessage source) {
            Map<String, Object> headers = super.extractUserDefinedHeaders(source);
            headers.put("foo", "bar");
            return headers;
        }
    };

    mapper.setRequestHeaderNames("*");

    SoapMessage soapMessage = mock(SoapMessage.class);

    Map<String, Object> headers = mapper.toHeadersFromRequest(soapMessage);
    assertTrue(headers.containsKey("foo"));
    assertEquals("bar", headers.get("foo"));
}