2016-05-17 3 views
0

シンプルなHttpServletを使用して別のサーバーからSOAPリクエストを管理しようとしています。 リクエストにはbyte []タイプのパラメータが1つしかありません(単純な文字列です)。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

関連する問題