2019-03-21 1419 星期四
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="userinfo"
xmlns:s="http://www.midea.com/esb/ESBGetUserInfo/v1" serviceClass="com.midea.esb.esbgetuserinfo.v1.ESBGetUserInfo"
serviceName="s:ESBGetUserInfo" endpointName="s:ESBGetUserInfoSOAP"
address="http://esbdev.midea.com:7801/">
<jaxws:properties>
<!-- 修改默认的SOAP编码 -->
<entry key="org.apache.cxf.message.Message.ENCODING" value="UTF-8" />
</jaxws:properties>
<jaxws:outInterceptors>
<!-- 添加cxf日志出拦截器 -->
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<!-- 添加自定义cxf拦截器 -->
<bean class="interceptor.AddUserOutInterceptor" />
</jaxws:outInterceptors>
<jaxws:inInterceptors>
<!-- 添加cxf日志入拦截器 -->
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<!-- 添加自定义cxf拦截器 -->
<bean class="interceptor.AddUserInInterceptor" />
</jaxws:inInterceptors>
</jaxws:client>
<!-- ESB中注册接口实现类 -->
<bean id="ESBGetUserInfoImpl" class="com.midea.esb.esbgetuserinfo.v1.ESBGetUserInfoImpl">
</bean>
<!--服务发布,服务端配置,由于http是从F5入口,所以只需要一个bean即可 ;address地址是需要提供给ESB(可访问的该接口全路径),可自行定义-->
<jaxws:endpoint id="ESBGetUserInfo" implementor="#ESBGetUserInfoImpl" address="/ESBGetUserInfo">
<jaxws:properties>
<entry key="org.apache.cxf.message.Message.ENCODING" value="UTF-8" />
</jaxws:properties>
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature" />
</jaxws:features>
<jaxws:inInterceptors>
<bean class="com.srcb.esb.interceptor.MediatorInterceptor"></bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
</beans>
in拦截器
package interceptor;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import servlet.GetMessageServlet;
public class AddUserInInterceptor extends AbstractPhaseInterceptor<Message> {
public AddUserInInterceptor() {
super(Phase.PRE_STREAM);
}
public void handleMessage(Message message) throws Fault {
InputStream is = message.getContent(InputStream.class);
InputStream ism = null;
try {
String str = IOUtils.toString(is);
GetMessageServlet.instr=str;
ism = new ByteArrayInputStream(str.getBytes("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
message.setContent(InputStream.class, ism);
}
}
out拦截器
package interceptor;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import servlet.GetMessageServlet;
public class AddUserOutInterceptor extends AbstractPhaseInterceptor<Message>{
public AddUserOutInterceptor() {
super(Phase.PRE_STREAM);
}
public void handleMessage(Message message) throws Fault {
try {
OutputStream os = message.getContent(OutputStream.class);
CachedStream cs = new CachedStream();
message.setContent(OutputStream.class, cs);
message.getInterceptorChain().doIntercept(message);
try {
cs.flush();
CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
BufferedOutputStream bufferOutput = new BufferedOutputStream(os);
CachedOutputStream.copyStream(csnew.getInputStream(), bufferOutput, 1024);
InputStream in = csnew.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//读取缓存
byte[] buffer = new byte[2048];
int length = 0;
while((length = in.read(buffer)) != -1) {
bos.write(buffer, 0, length);//写入输出流
}
in.close();//读取完毕,关闭输入流
// 根据输出流创建字符串对象
GetMessageServlet.outstr= new String(bos.toByteArray(), "UTF-8");
cs.close();
bufferOutput.close();
os.flush();
message.setContent(OutputStream.class, os);
} catch (IOException ioe) {
ioe.printStackTrace();
}
/* OutputStream os = message.getContent(OutputStream.class);
CachedStream cs = new CachedStream();
message.setContent(OutputStream.class, cs);
message.getInterceptorChain().doIntercept(message);
CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
InputStream in = csnew.getInputStream();
String xml = IOUtils.toString(in);
os.write(xml.getBytes("UTF-8"));
os.flush();
System.out.println("replaceBegin"+xml);
message.setContent(OutputStream.class, os);*/
} catch (Exception e) {
e.printStackTrace();
}
}
private class CachedStream extends CachedOutputStream {
public CachedStream() {
super();
}
protected void doFlush() throws IOException {
currentStream.flush();
}
protected void doClose() throws IOException {
}
protected void onWrite() throws IOException {
}
}
}
GetMessageServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.XmlFormatter;
import com.alibaba.fastjson.JSON;
public class GetMessageServlet extends HttpServlet {
public static String outstr="";
public static String instr="";
public GetMessageServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json; charset=utf-8");
response.setHeader("cache-control", "no-cache");
InOutMessge inout = new InOutMessge();
if(!instr.equals("")){
inout.setInstr(XmlFormatter.format(instr));
}
if(!outstr.equals("")){
inout.setOutstr(XmlFormatter.format(outstr));
}
String headOutStr = JSON.toJSONString(inout);
PrintWriter out = response.getWriter();
out.print(headOutStr);
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
CleanMessageServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CleanMessageServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public CleanMessageServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
GetMessageServlet.outstr="";
GetMessageServlet.instr="";
response.setContentType("application/json; charset=utf-8");
response.setHeader("cache-control", "no-cache");
PrintWriter out = response.getWriter();
out.print("{\"msg\":\"成功\",\"success\":\"true\"}");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
InOutMessge.java
package servlet;
public class InOutMessge {
private String instr;
private String outstr;
public String getInstr() {
return instr;
}
public void setInstr(String instr) {
this.instr = instr;
}
public String getOutstr() {
return outstr;
}
public void setOutstr(String outstr) {
this.outstr = outstr;
}
}
SendMessageServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.alibaba.fastjson.JSON;
import com.midea.esb.RequestHeader;
import com.midea.esb.esbgetuserinfo.v1.ESBGetUserInfo;
public class SendMessageServlet extends HttpServlet {
private SimpleDateFormat format = new SimpleDateFormat("yyyy-mmmmmm-dddddd");
public SendMessageServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
GetMessageServlet.instr="";
GetMessageServlet.outstr="";
response.setContentType("application/json; charset=utf-8");
response.setHeader("cache-control", "no-cache");
ServletContext servletContext = this.getServletContext();
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
ESBGetUserInfo esbGetUserInfo = (ESBGetUserInfo) context.getBean("userinfo");
String username = request.getParameter("username");
//请求头
RequestHeader requestHeader=new RequestHeader();
requestHeader.setRequestId("SOA");
requestHeader.setSerialNo("SOA"+format.format(new Date()));
requestHeader.setVersion("1.0");
//响应头信息
javax.xml.ws.Holder<com.midea.esb.ReplyInformation> headOut=new javax.xml.ws.Holder<com.midea.esb.ReplyInformation>();
esbGetUserInfo.getUserInfo(username, requestHeader, headOut);
//打印响应的头
String headOutStr = JSON.toJSONString(headOut);
PrintWriter out = response.getWriter();
out.print("{\"msg\":\"成功\",\"success\":\"true\"}");
out.flush();
out.close();
}
public void init() throws ServletException {
}
}