2016-05-17 91 views
0

我想用一個簡單的HttpServlet來管理另一個服務器的肥皂請求。 該請求只有一個byte []類型的參數(它是一個簡單的字符串)。與Java HttpServlet的解組肥皂信封

相關代碼:

@Override 
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
    try { 
      InputStream is = req.getInputStream(); 
      byte[] body = IOUtils.toByteArray(is); 
      String stringRequest = new String(body); 
      log.info("Request -> "+stringRequest); 
     }catch(Exception){log.error(e);} 

我收到請求,如果我打印它似乎在這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <fixedResearch soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
      <MYPARAMETER xsi:type="xsd:hexBinary"> 
       *****bytearray****** 
      </MYPARAMETER> 
     </fixedResearch> 
    </soapenv:Body> 
</soapenv:Envelope> 

我需要得到MYPARAMETER標籤內的值(這是一個字節[])。 有一個聰明的方法,也許使用Axis1的一些utils類(我不能使用Axis2)來處理傳入的請求?

回答

2

我不確定您允許使用什麼,但「手動」方式是使用XPath。下面的代碼我跑了一次,它似乎工作。它沒有利用名稱空間,而是一個開始。你需要優化它 - 這只是一個例子,但我已經有99%的代碼已經完成。

package tld.domainname.stuff; 


import java.io.IOException; 
import java.io.StringReader; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.xpath.XPath; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathExpressionException; 
import javax.xml.xpath.XPathFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 


public class XPathTest { 
    public static void main(String[] argv) { 
     String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
       "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + 
       " <soapenv:Body>\n" + 
       "  <fixedResearch soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" + 
       "   <MYPARAMETER xsi:type=\"xsd:hexBinary\">\n" + 
       "    *****bytearray******\n" + 
       "   </MYPARAMETER>\n" + 
       "  </fixedResearch>\n" + 
       " </soapenv:Body>\n" + 
       "</soapenv:Envelope>"; 

     String value = null; 

     XPath xpath = XPathFactory.newInstance().newXPath(); 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = null; 
     Document doc = null; 
     try { 
      builder = factory.newDocumentBuilder(); 
     } catch (ParserConfigurationException e) { 
      e.printStackTrace(); 
     } 
     InputSource is = new InputSource(new StringReader(xmlString)); 
     try { 
      doc = builder.parse(is); 
     } catch (SAXException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     if (doc == null) { 
      System.out.println("can't parse doc"); 
      return; 
     } 

     Node parentNode = doc.getDocumentElement(); 

     String path = "Body/fixedResearch/MYPARAMETER"; 

     NodeList nodeList; 
     try { 
      nodeList = (NodeList) xpath.evaluate(path, parentNode, XPathConstants.NODESET); 
     } catch (XPathExpressionException xpe) { 
      throw new IllegalArgumentException("Cannot evaluate xpath with path \"" + path + "\"", xpe); 
     } 

     if ((nodeList == null) || (nodeList.getLength() == 0)) { 
      System.out.println("found nothing"); 
      return; 
     } 

     if (nodeList.getLength() > 1) 
      System.out.println("found " + nodeList.getLength() + " nodes in the path \"" + path + "\" - using only the first"); 

     Node nextNode = nodeList.item(0); 

     if (nextNode == null) { 
      System.out.println("found nothing"); 
      return; 
     } 

     if (nextNode.hasChildNodes()) { 
      Node child = nextNode.getFirstChild(); 
      value = child.getNodeValue(); 
     } 

     System.out.println("found value of \"" + value + "\""); 
    } 
} 
+0

幾乎完美的一部分,MYPARAMETER的內容是hexBinary。所以要打印正確的值,你應該做新的String(hexStringToByteArray(value))。我省略了hexStringToByteArray的實現,因爲我在評論中沒有足夠的字符。 – drenda