浏览 4674 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (11)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-22
客户有一个需求,发布了一个Web Services,其中一个Function,输入的请求参数是Soap协议的XML文件,通过请求后.会输出一个XML格式的文件. 涉及的技术有Freemaker. 基本的思路是:通过一个连接请求,发送soap方式的XML请求文档,然后通过读取流的方式,返回结果.
private String serviceAddress = "http://localhost:8080/axis/services/Corticon"; private URL webServiceUrl; @PostConstruct private void init() { try { webServiceUrl = new URL(serviceAddress); } catch (MalformedURLException e) { e.printStackTrace(); } } public String processRequest(DialogDecisionInput input) { try { cfg.setClassForTemplateLoading(this.getClass(), "/"); cfg.setObjectWrapper(new DefaultObjectWrapper()); cfg.setNumberFormat("################"); // Get and config the connection. HttpURLConnection conn; conn = (HttpURLConnection) webServiceUrl.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); conn.setRequestProperty("SOAPAction", "Corticon"); Map root = new HashMap(); root.put("model", input); Template template = cfg.getTemplate("soapRequest.ftl"); OutputStream os = conn.getOutputStream(); Writer out = new BufferedWriter(new OutputStreamWriter(os, "utf-8")); template.process(root, out); out.flush(); out.close(); // Get the soap response. InputStream is = conn.getInputStream(); String rc = IOUtils.toString(is); return rc; } catch (TemplateException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); corticonlogger.error("[E00004]" + E00004 + "\n" + sw); } catch (IOException e1) { StringWriter sw = new StringWriter(); e1.printStackTrace(new PrintWriter(sw)); corticonlogger.error("[E00003]" + E00003 + "\n" + sw); } return null; } 呵呵,因为Freemarker的process默认是要写到一个文件当中.增加了IO的操作,比较慢,所以我的另外一篇文章也写了: Freemarker输出字符串而不是文件或者参考如下的代码:
private String serviceAddress = "http://localhost:8080/axis/services/Corticon"; private URL webServiceUrl; @PostConstruct private void init() { try { webServiceUrl = new URL(serviceAddress); } catch (MalformedURLException e) { e.printStackTrace(); } } public DialogDecisionService() { // Get and config the connection. cfg.setClassForTemplateLoading(this.getClass(), "/"); cfg.setObjectWrapper(new DefaultObjectWrapper()); cfg.setNumberFormat("################"); cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250)); } public String processRequest(DialogDecisionInput input) { try { HttpURLConnection conn; conn = (HttpURLConnection) webServiceUrl.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); conn.setRequestProperty("SOAPAction", "Corticon"); Map root = new HashMap(); root.put("model", input); Template template = cfg.getTemplate("soapRequest.ftl"); /** * StringWriter instead of Writer * Class StringWriter contains a StringBuffer which can be rendered toString * modify by heweiya */ OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "utf-8")); template.setEncoding("UTF-8"); cfg.setDefaultEncoding("UTF-8"); template.process(root, writer); writer.flush(); writer.close(); // Get the soap response. InputStream is = conn.getInputStream(); String rc = IOUtils.toString(is); return rc; } catch (TemplateException e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); corticonlogger.error("[E00004]" + E00004 + "\n" + sw); } catch (IOException e1) { StringWriter sw = new StringWriter(); e1.printStackTrace(new PrintWriter(sw)); corticonlogger.error("[E00003]" + E00003 + "\n" + sw); } corticonlogger.error("[E00004]" + E00004 + "\n"); return null; } 发送的XML文件格式如下: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <CorticonRequest xmlns="urn:Corticon" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" decisionServiceName="diabetes"> <WorkDocuments> <D_diabetes id="D_diabetes_id_1"> <currentPage>p_medInfo</currentPage> <dialogId>3</dialogId> <dialogInstanceId>771</dialogInstanceId> <displayTitle>Pediatric Diabetes Screening Tool</displayTitle> <function>screen</function> <lastQuestionAnswered>q_sex</lastQuestionAnswered> <locale /> <noOfChangedFlags>0</noOfChangedFlags> <productLine>Loan</productLine> <refId>D-00012</refId> <region>region 1</region> <sessionId xsi:nil="1" /> <version>1</version> <p_medInfo id="P_medInfo_id_1"> <displayTitle>Diabetes Screening - Info</displayTitle> <helpText></helpText> <pageId>7</pageId> <refId>D-00012</refId> <version>1</version> <q_sex id="Q_sex_id_1"> <displayFlag>true</displayFlag> <helpText></helpText> <questionId>41</questionId> <questionText>Sex</questionText> <refId>D-00012</refId> <responseType>Custom</responseType> <uiControl>Radio Button</uiControl> <sequence>1.2</sequence> <version>1</version> <pageQuestionId>44</pageQuestionId> <response id="R_sex_id_13" Type="R_sex"> <responseId>74</responseId> <respEnabled xsi:nil="1" /> <respLabel>Female</respLabel> <respSelected>false</respSelected> <respSeq>3</respSeq> <respValue>F</respValue> </response> <response id="R_sex_id_22" Type="R_sex"> <responseId>73</responseId> <respEnabled xsi:nil="1" /> <respLabel>Male</respLabel> <respSelected>true</respSelected> <respSeq>2</respSeq> <respValue>M</respValue> </response> </q_sex> <q_medIntro id="Q_medIntro_id_7"> <displayFlag>true</displayFlag> <helpText></helpText> <questionId>39</questionId> <questionText>We are asking a few extra questions in order to assess special risks in our new members. I would like to get some additional information about your child. This will just take a couple of minutes.</questionText> <refId>D-00012</refId> <responseType>No response</responseType> <uiControl xsi:nil="1" /> <version>1</version> <sequence>1</sequence> <pageQuestionId>40</pageQuestionId> <response id="R_medIntro_id_101" Type="R_medIntro"> <respEnabled xsi:nil="1" /> <respLabel xsi:nil="1" /> <respSelected xsi:nil="1" /><respValue></respValue> </response> </q_medIntro> <q_age id="Q_age_id_8"> <displayFlag>true</displayFlag> <helpText></helpText> <questionId>40</questionId> <questionText>Current Age</questionText> <refId>D-00012</refId> <responseType>Custom</responseType> <uiControl>Text Box</uiControl> <version>1</version> <sequence>1.1</sequence> <pageQuestionId>45</pageQuestionId> <response id="R_age_id_102" Type="R_age"> <respEnabled xsi:nil="1" /> <respLabel xsi:nil="1" /> <respSelected xsi:nil="1" /><respValue>0</respValue> </response> </q_age> <q_weight id="Q_weight_id_9"> <displayFlag>true</displayFlag> <helpText></helpText> <questionId>42</questionId> <questionText>Weight</questionText> <refId>D-00012</refId> <responseType>Custom</responseType> <uiControl>Text Box</uiControl> <version>1</version> <sequence>1.3</sequence> <pageQuestionId>43</pageQuestionId> <response id="R_weight_id_103" Type="R_weight"> <respEnabled xsi:nil="1" /> <respLabel xsi:nil="1" /> <respSelected xsi:nil="1" /><respValue>0</respValue> </response> </q_weight> <q_heightFeet id="Q_heightFeet_id_10"> <displayFlag>true</displayFlag> <helpText></helpText> <questionId>43</questionId> <questionText>Height - Feet</questionText> <refId>D-00012</refId> <responseType>Custom</responseType> <uiControl>Text Box</uiControl> <version>1</version> <sequence>1.4</sequence> <pageQuestionId>42</pageQuestionId> <response id="R_heightFeet_id_104" Type="R_heightFeet"> <respEnabled xsi:nil="1" /> <respLabel xsi:nil="1" /> <respSelected xsi:nil="1" /><respValue>0</respValue> </response> </q_heightFeet> <displayFlag>true</displayFlag> <helpText></helpText> <questionId>44</questionId> <questionText>Height - Inches</questionText> <refId>D-00012</refId> <responseType>Custom</responseType> <uiControl>Text Box</uiControl> <version>1</version> <sequence>1.5</sequence> <pageQuestionId>41</pageQuestionId> <response id="R_heightInches_id_105" Type="R_heightInches"> <respEnabled xsi:nil="1" /> <respLabel xsi:nil="1" /> <respSelected xsi:nil="1" /><respValue>0</respValue> </response> </q_heightInches> </p_medInfo> <displayTitle></displayTitle> <helpText></helpText> <pageId>9</pageId> <refId>D-00012</refId> <version>1</version> <q_findingsIntro id="Q_findingsIntro_id_9"> <displayFlag xsi:nil="1" /> <helpText></helpText> <questionId>50</questionId> <questionText>Recap:</questionText> <refId>D-00012</refId> <responseType>No response</responseType> <uiControl xsi:nil="1" /> <version>1</version> <sequence>1</sequence> <pageQuestionId>60</pageQuestionId> <response id="R_findingsIntro_id_103" Type="R_findingsIntro"> <respEnabled xsi:nil="1" /> <respLabel xsi:nil="1" /> <respSelected xsi:nil="1" /> <respValue></respValue> </response> </q_findingsIntro> </p_findings> </D_diabetes> </WorkDocuments> </CorticonRequest> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |