CXF打印SOAP报⽂,记录WebService⽇志
CXF是Apache发布的⼀个⽤于快速构建Services的框架,⽤于构建WebService客户端及服务端。
使⽤cxf构建的java程序,在java层⾯看到的是很多层的java类对象,但是在实际的接⼝调⽤和数据传输过程中则使⽤的soap 报⽂(⽹络传输过程中不可能真的把java对象传过去)。
第⼀种⽅法使⽤cxf⾃带直接在控制台打印输⼊输出报⽂,适合使⽤main()⽅法测试时使⽤。
第⼆种⽅法⾃⼰实现进⾏报⽂输出,报⽂可⽤⽇志⼯具进⾏存储,或者写⼊数据库,适合web项⽬使⽤。
第⼀种⽅法代码如下:
package com.action;
import f.endpoint.Client;
import f.frontend.ClientProxy;
import f.transport.http.HTTPConduit;
import f.figuration.HTTPClientPolicy;
public class CoreAction {
public static void test(BaseDto b, QueryDto b1) {
// 设置URL
String url = "localhost:8080/core/services/query";
// 实例化service
QueryPolicy service = Service(QueryPolicy.class,  url);
// 超时
Client proxy = Client(service);
HTTPConduit conduit = (HTTPConduit) Conduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(6000); // 6000毫秒
policy.setReceiveTimeout(6000); // 6000毫秒
conduit.setClient(policy);
// 调⽤
response response = service.query(b, b1);
}
public static void main(String[] args) {
BaseDto b = new BaseDto();
QueryDto b1 = new QueryDto();
test(b, b1);
}
}
两⾏代码:
package com.util;
import java.util.HashMap;
import java.util.Map;
import f.interceptor.LoggingInInterceptor;
import f.interceptor.LoggingOutInterceptor;
import f.jaxws.JaxWsProxyFactoryBean;
public class CxfUtil {
private static Map<String, Object> serviceMap = new HashMap<String, Object>();
public static <T> T getService(Class<? extends T> type, String serviceUrl) {
Object client = (serviceUrl);
if (client == null) {
synchronized (serviceMap) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(type);
factory.setAddress(serviceUrl);
// 输⼊报⽂
// 输出报⽂
client = ate();
serviceMap.put(serviceUrl, client);
}
}
if (client == null) {
throw new IllegalArgumentException(String.format("The url:%s is not a service url", serviceUrl));
}
if (!type.Class())) {
throw new IllegalArgumentException(String.format("The service's type should be %s", Class()));  }
return (T) client;
}
}
第⼆种⽅法代码如下:
1.实现输⼊报⽂InInterceptor
package com.util;
import java.io.IOException;
import java.io.InputStream;
import f.helpers.IOUtils;
import f.interceptor.Fault;
import f.interceptor.LoggingMessage;
import f.io.CachedOutputStream;
import f.message.Message;
import f.phase.AbstractPhaseInterceptor;
import f.phase.Phase;
public class InInterceptor extends AbstractPhaseInterceptor<Message> {
private int limit = 102400;
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public InInterceptor() {
// 在调⽤⽅法之前拦截SOAP消息
super(Phase.RECEIVE);
}
public void handleMessage(Message message) throws Fault {
logging(message);
}
private void logging(Message message) throws Fault {
if (ainsKey(LoggingMessage.ID_KEY)) {
return;
}
String id = (String) Exchange().get(LoggingMessage.ID_KEY);
if (id == null) {
id = Id();
}
message.put(LoggingMessage.ID_KEY, id);
LoggingMessage buffer = new LoggingMessage("Inbound Message\n----------------------------", id);  String encoding = (String) (Message.ENCODING);
if (encoding != null) {
}
String ct = (String) ("Content-Type");
if (ct != null) {
}
Object headers = (Message.PROTOCOL_HEADERS);
if (headers != null) {
}
String uri = (String) (Message.REQUEST_URI);
if (uri != null) {
}
InputStream is = (InputStream) Content(InputStream.class);
if (is != null) {
CachedOutputStream bos = new CachedOutputStream();
try {
bos.flush();
is.close();
message.setContent(InputStream.class, InputStream());
if (TempFile() != null) {
}
if (bos.size() > this.limit) {
}
bos.Payload(), this.limit);
bos.close();
} catch (IOException e) {
throw new Fault(e);
}
}
// 打印⽇志,保存⽇志保存这⾥就可以,可写库或log4j记录⽇志
System.out.String());
}
/
/ 出现错误输出错误信息和栈信息
public void handleFault(Message message) {
Exception exeption = Content(Exception.class);
System.out.Message());
}
}
2.实现输出报⽂OutInterceptor
package com.util;
import java.io.OutputStream;
import f.interceptor.Fault;
import f.interceptor.LoggingMessage;
import f.io.CacheAndWriteOutputStream;
import f.io.CachedOutputStream;
import f.io.CachedOutputStreamCallback;
import f.message.Message;
import f.phase.AbstractPhaseInterceptor;
import f.phase.Phase;
public class OutInterceptor extends AbstractPhaseInterceptor<Message> {
private int limit = 102400;
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public OutInterceptor() {
// 在调⽤⽅法之前拦截SOAP消息
super(Phase.PRE_STREAM);
}
public void handleMessage(Message message) throws Fault {
OutputStream os = (OutputStream) Content(OutputStream.class);  if (os == null) {
return;
}
CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(os);  message.setContent(OutputStream.class, newOut);
}
// 出现错误输出错误信息和栈信息
public void handleFault(Message message) {
Exception exeption = Content(Exception.class);
System.out.Message());
System.out.Message());
}
class LoggingCallback implements CachedOutputStreamCallback {
private final Message message;
private final OutputStream origStream;
public LoggingCallback(Message msg, OutputStream os) {
}
public void onFlush(CachedOutputStream cos) {
}
public void onClose(CachedOutputStream cos) {
String id = (String) Exchange().get(LoggingMessage.ID_KEY);
if (id == null) {
id = Id();
}
LoggingMessage buffer = new LoggingMessage("Outbound Message\n---------------------------", id);  String encoding = (String) (Message.ENCODING);
if (encoding != null) {
}
String address = (String) (Message.ENDPOINT_ADDRESS);
if (address != null) {
}
String ct = (String) ("Content-Type");
if (ct != null) {
}
Object headers = (Message.PROTOCOL_HEADERS);
if (headers != null) {
Header().append(headers);
}
if (TempFile() == null) {
if (cos.size() > OutInterceptor.this.limit)
if (cos.size() > OutInterceptor.this.limit)
try {
cos.Payload(), OutInterceptor.this.limit);
} catch (Exception ex) {
}
// 打印⽇志,保存⽇志保存这⾥就可以,可写库或log4j记录⽇志
System.out.String());
try {
cos.lockOutputStream();
} catch (Exception ex) {
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。