2

base64 埋め込み画像データから XOP 画像データに切り替えたデータ ソースがあります。Java/JAXB を使用してデータをアンマーシャリングしていますが、その方法を説明している適切なソースが見つかりません。すべての参考文献は、重労働の一部を処理するように見えるSOAPメッセージでこれを行うことを説明しているようです。

私の場合、データは事実上、JAXB によって作成されたオブジェクトに非整列化する必要がある文字列として入ってきます。新しいメッセージは

Mime-Version: 1.0
Content-Type: multipart/related; start-info="text/xml"; type="application/xop+xml"; 
    start="<-963165769043289641.1400077877224@xxxxxyyyyyy.ca>"; 
    boundary="----=_Part_0_-338193320.1400077877317"

XML のように見えない (プロローグの前にデータを許可しない) ため、データを XML としてアンマーシャリングしないことは明らかです。

これは JAXB で実行できますか? スキーマからのオブジェクトの作成は正常に機能しているようで、適切なオブジェクトのように見えるものを作成します (Include 要素を作成します)。AttachmentUnmarshaller を手動で作成しようとしましたが、役に立ちませんでした...受信データはまだ XML として認識されません。基本的なステップが欠けているように感じますが、良い参考文献や例が見つかりません。

どんな助けでも大歓迎です。

4

1 に答える 1

2

以下は、役立つ可能性のあるアプローチです。

メッセージ

以下は、大まかに処理しようとしているものです。

 MIME-Version: 1.0
 Content-Type: Multipart/Related;boundary=MIME_boundary;
 ...
 --MIME_boundary
 Content-Type: application/xop+xml; 
 ...

 <soap:Envelope ...
  <soap:Body>...
    <foo>
      <photo xmlmime:contentType='image/png'>
        <xop:Include xmlns:xop='http://www.w3.org/2004/08/xop/include' 
           href='cid:http://example.org/me.jpeg'/></m:photo>
 ...

 --MIME_boundary
 Content-Type: image/png
 Content-Transfer-Encoding: binary
 Content-ID: <http://example.org/me.png>

 // binary octets for png

デモコード

デモ

以下のコードは、次のことを行うためにメッセージを処理したことを前提としています。

  1. XML フラグメントを抽出する
  2. 添付ファイルを抽出し、cid でキーを設定できます。
import java.io.File;
import javax.xml.bind.*;

public class Demo {

    private static String base64 = "/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5/ooooA//2Q==";

    public static void main(String[] args) throws Exception {
        // Create the JAXBContext & Unmarshaller
        JAXBContext jc = JAXBContext.newInstance(Foo.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();

        // Create a custom AttachementUnmarshaller.  Populate the Map
        // AttachmentUnmarshaller with the attachments keyed on the cid.
        MyAttachmentUnmarshaller attachmentUnmarshaller = new MyAttachmentUnmarshaller();
        attachmentUnmarshaller.getAttachments().put("cid:http://example.org/me.jpeg", DatatypeConverter.parseBase64Binary(base64));

        // Set the AttachmentUnmarshaller on the Unmarshaller
        unmarshaller.setAttachmentUnmarshaller(attachmentUnmarshaller);

        // Unmarshal the XML piece
        File xml = new File("src/forum24407360/input.xml");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);
    }

}

MyAttachmentUnmarshaller

以下はそのAttachmentUnmarshaller外観です。AnAttachmentUnmarshallerは cid で渡され、対応するバイナリ データを返す役割を果たします。JAX-WS 環境では、これは自動的に処理されますが、手動で行うことを妨げるものは何もありません。詳細については 、Java で生のバイナリ データを XML と共に格納する最も標準的な方法は何ですか? .

import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.xml.bind.attachment.AttachmentUnmarshaller;

public class MyAttachmentUnmarshaller extends AttachmentUnmarshaller {

    private Map<String, byte[]> attachments = new HashMap<String, byte[]>();

    public Map<String, byte[]> getAttachments() {
        return attachments;
    }

    @Override
    public DataHandler getAttachmentAsDataHandler(String cid) {
        byte[] bytes = attachments.get(cid);
        return new DataHandler(new ByteArrayDataSource(bytes));
    }

    @Override
    public byte[] getAttachmentAsByteArray(String cid) {
        return attachments.get(cid);
    }

    @Override
    public boolean isXOPPackage() {
        return true;
    }

    private static class ByteArrayDataSource implements DataSource {

        private byte[] bytes;

        public ByteArrayDataSource(byte[] bytes) {
            this.bytes = bytes;
        }

        public String getContentType() {
            return  "application/octet-stream";
        }

        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }

        public String getName() {
            return null;
        }

        public OutputStream getOutputStream() throws IOException {
            return null;
        }

    }

}

XML ピース

<foo>
    <image>
        <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:http://example.org/me.jpeg"/>
    </image>
</foo>
于 2014-06-25T19:32:09.450 に答える