如何以编程方式(在 TomEE 中)获取 SOAP 请求的内容类型和编码?

How to obtain content-type and encoding of a SOAP request programmatically (in TomEE)?

我有一个在 Java 上编写并部署在 TomEE plus 1.7.1 上的网络服务,并且有一个关于请求编码的问题,我必须处理具有不同编码的请求,更具体地说 ISO-8859-1UTF-8。这就是为什么我需要识别传入请求具有哪种编码。 现在,我正在跟踪传入消息:

ID: 1
Address: http://localhost:8006/services/soaprequest
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml;charset=UTF-8
Headers: {accept-encoding=[gzip,deflate], connection=[Keep-Alive], Content-Length=[12915], content-type=[text/xml;charset=UTF-8], host=[localhost:8006], SOAPAction=["http://tempuri.org/soaprequest/soaprequest"], user-agent=[Apache-HttpClient/4.1.1 (java 1.5)]}
Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://tempuri.org/soaprequest">

...XML message goes here...

</soapenv:Envelope>

从跟踪中可以看出,请求具有 "Encoding" 和 "Content-type" 等标记,根据我可以得出的结论,请求是通过哪种编码到达网络服务的。

我试过 SOAPHandler 来检测它:

public boolean handleMessage(SOAPMessageContext context) {
    if (!(boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
        String[] mimeHeader = context.getMessage().getSOAPPart().getMimeHeader("Content-Type");
        for (int i = 0; i < mimeHeader.length; i++)
            System.out.println("mimeHeader " + (i + 1) + ":" + mimeHeader[i]);
    }
    return true; //indicates to the context to proceed with (normal)message processing
}

输出是:

mimeHeader 1:text/xml

所以这种方式我做不到

问:如何检索 Web 服务请求的内容类型字符集或编码?

怎么样

 Object encProp = context.getMessage().getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
 String encoding = encProp != null ? encProp.toString() : null;

但似乎 属性 主要用于 设置 编码。所以当你收到一条消息时,你需要试试属性是否被填满。

此问题已解决。解决方案在这里找到:

http://middlewaremagic.com/weblogic/?p=351