工作中,我需要和另一个公司(A公司)共同开发一个模块,我写一个servlet接口,A公司携带xml格式的报文来访问。我采用流的形式读取,在处理后以流的形式写入,在返回(相应)给A公司。
demo:
1 package com.sinosoft.undwrt.bl.facade; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.BufferedReader; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.io.StringBufferInputStream; 10 import java.sql.ResultSet; 11 import java.sql.SQLException; 12 import java.util.ArrayList; 13 import java.util.HashMap; 14 import java.util.List; 15 import java.util.Map; 16 17 import javax.servlet.ServletException; 18 import javax.servlet.http.HttpServlet; 19 import javax.servlet.http.HttpServletRequest; 20 import javax.servlet.http.HttpServletResponse; 21 import javax.xml.parsers.FactoryConfigurationError; 22 import javax.xml.parsers.ParserConfigurationException; 23 24 import org.dom4j.Document; 25 import org.dom4j.DocumentException; 26 import org.dom4j.Element; 27 import org.dom4j.io.SAXReader; 28 import org.xml.sax.SAXException; 29 30 import com.sinosoft.sysframework.reference.DBManager; 31 import com.sinosoft.utiall.schema.PrpDriskSchema; 32 import com.sinosoft.utility.error.UserException; 33 34 public class CheckIfPassFacade extends HttpServlet { 35 36 /** 37 * Destruction of the servlet. <br> 38 */ 39 public void destroy() { 40 super.destroy(); // Just puts "destroy" string in log 41 // Put your code here 42 } 43 44 /** 45 * The doGet method of the servlet. <br> 46 * 47 * This method is called when a form has its tag value method equals to get. 48 * 49 * @param request the request send by the client to the server 50 * @param response the response send by the server to the client 51 * @throws ServletException if an error occurred 52 * @throws IOException if an error occurred 53 */ 54 public void doGet(HttpServletRequest request, HttpServletResponse response) 55 throws ServletException, IOException { 56 57 doPost(request, response); 58 } 59 60 /** 61 * @param request the request send by the client to the server 62 * @param response the response send by the server to the client 63 * @throws ServletException if an error occurred 64 * @throws IOException if an error occurred 65 * @author yaowengang 66 * @time 2016-1-26 67 * @desc 共享平台访问此servlet,以流的形式返回passFlag(是否审核通过true or false),passTime(审核通过时间),undwrter(审核通过人) 68 * @desc underwriteflag 1审核通过,9提交到核赔/核保(在双核或共享平台),6注销, 69 2下发修改,0暂存(在核心) ; 70 3自动核保(自核单子审核通过后);4无需核保(自核单子审核通过前的标识) 71 */ 72 public void doPost(HttpServletRequest request, HttpServletResponse response) 73 throws ServletException, IOException { 74 String[] arrs= analyzeXML(request); 75 //获取指定元素的内容 76 String taskId= arrs[0];//businessNo 77 String businessType= arrs[1]; 78 79 boolean isT= businessType.equals("T")? true: false; 80 DBManager dbManager = new DBManager(); 81 List list= new ArrayList<String>(); 82 ResultSet resultSet= null; 83 String underwriteFlag= null; 84 String passTime= null; 85 Boolean passFlag= null ;//true:审核通过,false:未审核通过 86 String undwrter =null;//核保人 87 BufferedOutputStream ou= null; 88 try { 89 if (taskId!=null&&!"".equals(taskId)) { 90 // boolean isT= taskId.startsWith("T") ? true :false ;//需要改,让平台作为参数传给我 91 dbManager.open("ddccDataSource"); 92 if (isT) {//投保单 93 String sqlT= "select underwriteflag,passtime,underwritecode from prptmain where proposalno=?"; 94 dbManager.prepareStatement(sqlT); 95 dbManager.setString(1, taskId); 96 resultSet = dbManager.executePreparedQuery(); 97 while (resultSet.next()) { 98 underwriteFlag= resultSet.getString("underwriteflag"); 99 passTime= resultSet.getString("passtime"); 100 undwrter= resultSet.getString("underwritecode"); 101 passFlag= underwriteFlag.equals("1"); 102 if (!passFlag) { 103 passTime=null; 104 undwrter=null; 105 } 106 break; 107 } 108 /*if (passFlag) { 109 resultSet= null; 110 String sql2= "select passtime,underwritecode from prptmain where proposalno=?"; 111 dbManager.prepareStatement(sql2); 112 dbManager.setString(1, taskId); 113 resultSet = dbManager.executePreparedQuery(); 114 while (resultSet.next()) { 115 passTime= resultSet.getString("passtime"); 116 undwrter= resultSet.getString("underwritecode"); 117 break; 118 } 119 if (passTime==null) { 120 passTime=""; 121 } 122 }*/ 123 124 }else {//批单 125 String sqlP= "select underwriteflag,passtime,underwritecode from prpphead where endorseno=?"; 126 dbManager.prepareStatement(sqlP); 127 dbManager.setString(1, taskId); 128 resultSet = dbManager.executePreparedQuery(); 129 while (resultSet.next()) { 130 underwriteFlag= resultSet.getString("underwriteflag"); 131 passTime= resultSet.getString("passtime"); 132 undwrter= resultSet.getString("underwritecode"); 133 passFlag= underwriteFlag.equals("1"); 134 if (!passFlag) { 135 passTime=null; 136 undwrter=null; 137 } 138 /*passTime= resultSet.getString("passtime"); 139 undwrter= resultSet.getString("underwritecode");*/ 140 /*if (passTime==null) { 141 passTime=""; 142 }*/ 143 break; 144 } 145 /*if (passFlag) { 146 resultSet= null; 147 String sql2= "select passtime,underwritecode from prpphead where endorseno=?"; 148 dbManager.prepareStatement(sql2); 149 dbManager.setString(1, taskId); 150 resultSet = dbManager.executePreparedQuery(); 151 while (resultSet.next()) { 152 passTime= resultSet.getString("passtime"); 153 undwrter= resultSet.getString("underwritecode"); 154 break; 155 } 156 if (passTime==null) { 157 passTime=""; 158 } 159 }*/ 160 } 161 162 163 }else { 164 throw new Exception("解析报文失败!请检查报文是否正确。"); 165 } 166 167 list.add(""+passFlag); 168 list.add(passTime); 169 list.add(undwrter); 170 String resultXML= assembleXML(list); 171 System.out.println("给平台返回的xml :"+resultXML); 172 ou = new BufferedOutputStream(response.getOutputStream()); 173 ou.write(resultXML.getBytes()); 174 ou.flush(); 175 ou.close(); 176 }catch (IOException e1) { 177 e1.printStackTrace(); 178 }catch (SQLException e2) { 179 e2.printStackTrace(); 180 } catch (Exception e) { 181 e.printStackTrace(); 182 }finally{ 183 if (dbManager!=null) { 184 try { 185 dbManager.close(); 186 } catch (Exception e) { 187 e.printStackTrace(); 188 } 189 } 190 } 191 } 192 193 /** 194 * Initialization of the servlet. <br> 195 * 196 * @throws ServletException if an error occurs 197 */ 198 public void init() throws ServletException { 199 // Put your code here 200 } 201 202 /** 203 * @desc 将查询所得数据构造成xml格式,便于传送 204 * @param 205 * @throws UserException, ParserConfigurationException, FactoryConfigurationError, SAXException,Exception 206 */ 207 public String assembleXML(List<String> list) throws UserException, ParserConfigurationException, FactoryConfigurationError, SAXException,Exception { 208 StringBuilder strBuilder = new StringBuilder(); 209 strBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 210 strBuilder.append("<ROOT>"); 211 strBuilder.append("<HEAD>"); 212 strBuilder.append("<INFO>"); 213 strBuilder.append("<PASSFLAG>"+ list.get(0)+ "</PASSFLAG>"); 214 strBuilder.append("<PASSTIME>"+ list.get(1)+ "</PASSTIME>"); 215 strBuilder.append("<UNDWRTER>"+ list.get(2)+ "</UNDWRTER>"); 216 strBuilder.append("</INFO>"); 217 strBuilder.append("</HEAD>"); 218 strBuilder.append("</ROOT>"); 219 return strBuilder.toString(); 220 } 221 222 /** 223 * @desc dom4j框架解析来自request的报文 224 * @author yaowengang 225 * @throws IOException 226 * @date 20160224 227 * */ 228 public String[] analyzeXML(HttpServletRequest request) throws IOException {// 229 230 String[] datas= new String[2]; 231 InputStream XMLInputStream= request.getInputStream(); 232 BufferedReader XMLBuffReader= new BufferedReader(new InputStreamReader(XMLInputStream)); 233 StringBuilder requestXMLSB= new StringBuilder(""); 234 InputStream in= null; 235 try { 236 String tempXML; 237 while (true) { 238 tempXML= XMLBuffReader.readLine(); 239 if (tempXML==null) { 240 break; 241 } 242 requestXMLSB.append(tempXML); 243 } 244 String requestXML= requestXMLSB.toString(); 245 System.out.println("平台发的报文 :"+requestXML); 246 //解析收到的报文--采用dom4j框架 247 in= new StringBufferInputStream(requestXML); 248 SAXReader saxReader= new SAXReader(); 249 Document document= null; 250 document = saxReader.read(in); 251 //获取根元素 252 Element root= document.getRootElement(); 253 Element headElement= root.element("HEAD"); 254 Element infoElement= headElement.element("INFO"); 255 Element businessNoElement= infoElement.element("BUSINESSNO"); 256 Element businessTypeElement= infoElement.element("BUSINESSTYPE"); 257 //获取指定元素的内容 258 String businessNo= businessNoElement.getText(); 259 String businessType= businessTypeElement.getText(); 260 datas[0]= businessNo; 261 datas[1]= businessType; 262 263 }catch (DocumentException e1) { 264 e1.printStackTrace(); 265 } catch (Exception e) { 266 e.printStackTrace(); 267 }finally{ 268 if (XMLBuffReader!=null) { 269 XMLBuffReader.close(); 270 } 271 if (in!=null) { 272 in.close(); 273 } 274 } 275 return datas; 276 } 277 278 }