SOAP请求中的DataHandler

时间:2018-12-13 15:03:30

标签: java soap axis2

该Web服务要求我设置应为xml附件的DataHandler类型。

DataSource dataSource = new FileDataSource(tempFile.getAbsolutePath()); 
DataHandler dataHandler = new DataHandler(dataSource);
request.setDataHandler(dataHandler);

问题是从Axis2生成的SOAPMessage的值为base64

<dataHandler>big string64 string representing my content</dataHandler>

应该在哪里

<dataHandler><inc:Include href="cid:attachmentid" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></dataHandler>

Content-Type: text/xml; charset=us-ascii; name=Sample.xml
Content-Transfer-Encoding: 7bit
Content-ID: <attachmentid>
Content-Disposition: attachment; name="Sample.xml"; filename="Sample.xml"

... the xml content....

WSDL

  <xsd:element name="dataHandler" type="xsd:base64Binary" maxOccurs="1" minOccurs="1" xmime:expectedContentTypes="application/octet-stream"/>

我该怎么做才能解决此问题?

1 个答案:

答案 0 :(得分:1)

我在xmlobject中有一个值,该值是一个DataHandler,这是将datahandler放入值中作为带有xml的对象的解决方案:

具有rootelement的XmlObject:

Public Sub sortThis()
    Dim r() As Variant
    r = Selection.Value2
    Debug.Print ("Selected Range: " & getRngAddress(Selection))
    Call QuickSort(r)
    Selection.Value2 = r
End Sub

Sub QuickSort(ByRef VA_array(), Optional V_Low1, Optional V_High1)
    Dim V_Low2 As Long, V_High2 As Long
    Dim V_val1 As Variant, V_val2 As Variant
    If IsMissing(V_Low1) Then V_Low1 = LBound(VA_array, 1)
    If IsMissing(V_High1) Then V_High1 = UBound(VA_array, 1)
    Debug.Print ("qs: Low1: " & V_Low1 & " / V_High1: " & V_High1)
    V_Low2 = V_Low1
    V_High2 = V_High1
    V_val1 = VA_array((V_Low1 + V_High1) / 2)
    While (V_Low2 <= V_High2)
        While (VA_array(V_Low2) < V_val1 And V_Low2 < V_High1)
            V_Low2 = V_Low2 + 1
        Wend
        While (VA_array(V_High2) > V_val1 And V_High2 > V_Low1)
            V_High2 = V_High2 - 1
        Wend
        If (V_Low2 <= V_High2) Then
            V_val2 = VA_array(V_Low2)
            VA_array(V_Low2) = VA_array(V_High2)
            VA_array(V_High2) = V_val2
            V_Low2 = V_Low2 + 1
            V_High2 = V_High2 - 1
        End If
    Wend
    If (V_High2 > V_Low1) Then Call QuickSort(VA_array, V_Low1, V_High2)
    If (V_Low2 < V_High1) Then Call QuickSort(VA_array, V_Low2, V_High1)
End Sub


然后您将marshaller设置为转换为字符串:

RootXmlObject xml = new RootXmlObject();

创建自定义数据处理程序:

JAXBContext context = JAXBContext.newInstance(RootXmlObject.class);
Marshaller mar= context.createMarshaller();
mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
mar.marshal(xml, sw);
String xmlString = sw.toString();

“ writeTo”方法的工作方式如下:

DataHandler.setDataContentHandlerFactory(new YourDatahandler());
private class YourDatahandler implements DataContentHandlerFactory {

    @Override
    public DataContentHandler createDataContentHandler(String mimeType) {

        return new XmlDataContentHandler();
    }
}

public static class XmlDataContentHandler implements DataContentHandler {


    @Override
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[] {DataFlavor.stringFlavor};
    }
    @Override
    public Object getTransferData(DataFlavor dataFlavor, DataSource dataSource) throws UnsupportedFlavorException, IOException {
        return new String("Whateverstring");
    }

    @Override
    public Object getContent(DataSource dataSource) throws IOException {
        return new String("Whateverstring");

最后将xmlString插入到Datahandler中:

    @Override
    public void writeTo(Object o, String s, OutputStream outputStream) throws IOException {
        byte[] stringByte = (byte[]) ((String) o).getBytes("UTF-8");
        outputStream.write(stringByte);
    }

这是我的项目的链接:https://github.com/habelo/domibusdemo