我正在使用 Apache xmlrpc 使用 Java 构建 xmlrpc 客户端,但无法弄清楚如何记录输入/输出 xml(接收和发送的原始数据)。我该怎么做呢?
谢谢
我正在使用 Apache xmlrpc 使用 Java 构建 xmlrpc 客户端,但无法弄清楚如何记录输入/输出 xml(接收和发送的原始数据)。我该怎么做呢?
谢谢
我的解决方法是使用自定义传输,如下所示。也许有更优雅的方式来做到这一点。
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcStreamTransport;
import org.apache.xmlrpc.client.XmlRpcSunHttpTransport;
import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
import org.xml.sax.SAXException;
/**
* This is a custom XML-RPC transport which logs the outgoing and incoming
* XML-RPC messages.
*/
public class MessageLoggingTransport extends XmlRpcSunHttpTransport
{
private static final Logger log = Logger.getLogger(MessageLoggingTransport.class.getName());
/**
* Default constructor
*
* @see XmlRpcSunHttpTransport#XmlRpcSunHttpTransport(XmlRpcClient)
* @param pClient
*/
public CookieHandlingTransport(final XmlRpcClient pClient)
{
super(pClient);
}
/**
* Dumps outgoing XML-RPC requests to the log
*/
@Override
protected void writeRequest(final XmlRpcStreamTransport.ReqWriter pWriter) throws IOException, XmlRpcException, SAXException
{
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
pWriter.write(baos);
log.info(baos.toString());
super.writeRequest(pWriter);
}
/**
* Dumps incoming XML-RPC responses to the log
*/
@Override
protected Object readResponse(XmlRpcStreamRequestConfig pConfig, InputStream pStream) throws XmlRpcException
{
final StringBuffer sb = new StringBuffer();
try
{
final BufferedReader reader = new BufferedReader(new InputStreamReader(pStream));
String line = reader.readLine();
while(line != null)
{
sb.append(line);
line = reader.readLine();
}
}
catch(final IOException e)
{
log.log(Level.SEVERE, "While reading server response", e);
}
log.info(sb.toString());
final ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes());
return super.readResponse(pConfig, bais);
}
}
然后在创建 XML-RPC 客户端的代码中:
final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(url));
final XmlRpcTransportFactory transportFactory = new XmlRpcTransportFactory()
{
public XmlRpcTransport getTransport()
{
return new MessageLoggingTransport(client);
}
};
client = new XmlRpcClient();
client.setTransportFactory(transportFactory);
client.setConfig(config);
尝试为org.apache.xmlrpc.client使用记录器;我敢打赌它会在那个包的某个地方吐出 xml。尝试这样的事情:
<appender name="LOG_FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="File" value="C:\xmlrpc_client.log"/>
<param name="datePattern" value="'.'yyyy-ww" />
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %t %d{DATE} %M - %m%n"/>
</layout>
</appender>
<logger name="org.apache.xmlrpc.client">
<level value="DEBUG"/>
</logger>
<root>
<level value="INFO" />
<appender-ref ref="LOG_FILE"/>
</root>