1 /*
   2  * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.bind.attachment;
  27 
  28 import javax.activation.DataHandler;
  29 
  30 /**
  31  * <p>Enables JAXB unmarshalling of a root document containing optimized binary data formats.</p>
  32  *
  33  * <p>This API enables an efficient cooperative processing of optimized
  34  * binary data formats between a JAXB 2.0 implementation and MIME-based package
  35  * processor (MTOM/XOP and WS-I AP 1.0). JAXB unmarshals the body of a package, delegating the
  36  * understanding of the packaging format being used to a MIME-based
  37  * package processor that implements this abstract class.</p>
  38  *
  39  * <p>This abstract class identifies if a package requires XOP processing, {@link #isXOPPackage()}
  40  * and provides retrieval of binary content stored as attachments by content-id.</p>
  41  *
  42  * <h2>Identifying the content-id, cid, to pass to {@code getAttachment*(String cid)}</h2>
  43  * <ul>
  44  * <li>
  45  * For XOP processing, the infoset representation of the cid is described
  46  * in step 2a in
  47  * <a href="http://www.w3.org/TR/2005/REC-xop10-20050125/#interpreting_xop_packages">Section 3.2 Interpreting XOP Packages</a>
  48  * </li>
  49  * <li>
  50  * For WS-I AP 1.0, the cid is identified as an element or attribute of
  51  * type {@code ref:swaRef} specified in
  52  * <a href="http://www.ws-i.org/Profiles/AttachmentsProfile-1.0-2004-08-24.html#Referencing_Attachments_from_the_SOAP_Envelope">
  53  * Section 4.4 Referencing Attachments from the SOAP Envelope</a>
  54  * </li>
  55  * </ul>
  56  *
  57  * @author Marc Hadley
  58  * @author Kohsuke Kawaguchi
  59  * @author Joseph Fialli
  60  *
  61  * @since 1.6, JAXB 2.0
  62  *
  63  * @see javax.xml.bind.Unmarshaller#setAttachmentUnmarshaller(AttachmentUnmarshaller)
  64  *
  65  * @see <a href="http://www.w3.org/TR/2005/REC-xop10-20050125/">XML-binary Optimized Packaging</a>
  66  * @see <a href="http://www.ws-i.org/Profiles/AttachmentsProfile-1.0-2004-08-24.html">WS-I Attachments Profile Version 1.0.</a>
  67  * @see <a href="http://www.w3.org/TR/xml-media-types/">Describing Media Content of Binary Data in XML</a>
  68  */
  69 public abstract class AttachmentUnmarshaller {
  70    /**
  71     * <p>Lookup MIME content by content-id, {@code cid}, and return as a {@link DataHandler}.</p>
  72     *
  73     * <p>The returned {@code DataHandler} instance must be configured
  74     * to meet the following required mapping constaint.
  75     * <table class="striped">
  76     *   <caption>Required Mappings between MIME and Java Types</caption>
  77     *   <thead>
  78     *     <tr>
  79     *       <th scope="col">MIME Type</th>
  80     *       <th scope="col">Java Type</th>
  81     *     </tr>
  82     *     <tr>
  83     *       <th scope="col">{@code DataHandler.getContentType()}</th>
  84     *       <th scope="col">{@code instanceof DataHandler.getContent()}</th>
  85     *     </tr>
  86     *   </thead>
  87     *   <tbody style="text-align:left">
  88     *     <tr>
  89     *       <th scope="row">image/gif</th>
  90     *       <td>java.awt.Image</td>
  91     *     </tr>
  92     *     <tr>
  93     *       <th scope="row">image/jpeg</th>
  94     *       <td>java.awt.Image</td>
  95     *     </tr>
  96     *     <tr>
  97     *       <th scope="row">text/xml  or application/xml</th>
  98     *       <td>javax.xml.transform.Source</td>
  99     *     </tr>
 100     *   </tbody>
 101     *  </table>
 102     * Note that it is allowable to support additional mappings.
 103     *
 104     * @param cid It is expected to be a valid lexical form of the XML Schema
 105     * {@code xs:anyURI} datatype. If {@link #isXOPPackage()}{@code ==true},
 106     * it must be a valid URI per the {@code cid:} URI scheme (see <a href="http://www.ietf.org/rfc/rfc2387.txt">RFC 2387</a>)
 107     *
 108     * @return
 109     *       a {@link DataHandler} that represents the MIME attachment.
 110     *
 111     * @throws IllegalArgumentException if the attachment for the given cid is not found.
 112     */
 113    public abstract DataHandler getAttachmentAsDataHandler(String cid);
 114 
 115     /**
 116      * <p>Retrieve the attachment identified by content-id, {@code cid}, as a {@code byte[]}.
 117      *
 118      * @param cid It is expected to be a valid lexical form of the XML Schema
 119      * {@code xs:anyURI} datatype. If {@link #isXOPPackage()}{@code ==true},
 120      * it must be a valid URI per the {@code cid:} URI scheme (see <a href="http://www.ietf.org/rfc/rfc2387.txt">RFC 2387</a>)
 121      *
 122      * @return byte[] representation of attachment identified by cid.
 123      *
 124     * @throws IllegalArgumentException if the attachment for the given cid is not found.
 125      */
 126     public abstract byte[] getAttachmentAsByteArray(String cid);
 127 
 128     /**
 129      * <p>Read-only property that returns true if JAXB unmarshaller needs to perform XOP processing.</p>
 130      *
 131      * <p>This method returns {@code true} when the constraints specified
 132      * in  <a href="http://www.w3.org/TR/2005/REC-xop10-20050125/#identifying_xop_documents">Identifying XOP Documents</a> are met.
 133      * This value must not change during the unmarshalling process.</p>
 134      *
 135      * @return true when MIME context is a XOP Document.
 136      */
 137     public boolean isXOPPackage() { return false; }
 138 }