gpt4 book ai didi

java - 如何在 SOAPHandler 中发送 xml 而不进行任何转换

转载 作者:行者123 更新时间:2023-12-01 05:13:18 26 4
gpt4 key购买 nike

IBM WebSphere Jax-WS RI 框架。我有一个 chunkHandler。我需要向客户端发送一行 xml 响应。我制作oneline xml。调试显示一切正常 - xml 是单行的。但在发送之前,框架会生成一个“ pretty-print ”输出。它需要签名,并且不需要任何 xml 转换。一般问题:如何在 SOAPHandler 中发送 xml 数据而不进行任何转换。

public class SignHandler implements SOAPHandler<SOAPMessageContext>{


private String makeOneLineXml(String xml) throws IOException{
BufferedReader br = new BufferedReader(new StringReader(xml));
String line=null;
StringBuilder sb = new StringBuilder();

while((line=br.readLine())!= null){
sb.append(line.trim());
}
return sb.toString();

}

public boolean handleMessage(SOAPMessageContext context) {


Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

//for response message only, true for outbound messages, false for inbound
if(isRequest){

try{
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
Document orig = soapEnv.getOwnerDocument();
orig =
// I need send to client one-line-xml
XmlUtils.deserialize(makeOneLineXml(XmlUtils.serializeNode(signedSoap, false, false)));
soapMsg.saveChanges();



//tracking
soapMsg.writeTo(System.out); // In CONSOLE ALL ok. Xml is one line

}catch(SOAPException e){
System.err.println(e);
}catch(IOException e){
System.err.println(e);
}

}

//continue other handler chain
return true;
}

最佳答案

我在 SoapUI 中进行的所有测试。但默认情况下,soapUI 响应是 pretty-print 格式的。一切问题都在于此。

要替换 SoapMessage 中的可选肥皂 xml,您可以使用以下命令:

 private SOAPMessage makeSOAPMessage(String msg)
{
try {

SOAPMessage message = MessageFactory.newInstance().createMessage();

StringReader sReader = new StringReader(msg);

message.getSOAPPart().setContent(new StreamSource(sReader));
message.saveChanges();
return message;
}
catch (Exception e) {
return null;
}
}
.....................

context.setMessage(makeSOAPMessage(yours_xml_string));

此后,您将在客户端收到此 xml_string。它对于 xmldsig、​​jax-wss 安全问题很有用。

关于java - 如何在 SOAPHandler 中发送 xml 而不进行任何转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11719756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com