src/share/jaxws_classes/com/sun/xml/internal/ws/api/pipe/Codec.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.api.pipe;
ohair@286 27
ohair@286 28 import com.sun.xml.internal.ws.api.BindingID;
ohair@286 29 import com.sun.xml.internal.ws.api.WSBinding;
ohair@286 30 import com.sun.xml.internal.ws.api.message.Message;
ohair@286 31 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 32 import com.sun.xml.internal.ws.api.server.EndpointAwareCodec;
ohair@286 33
ohair@286 34 import javax.xml.stream.XMLStreamWriter;
ohair@286 35 import java.io.IOException;
ohair@286 36 import java.io.InputStream;
ohair@286 37 import java.io.OutputStream;
ohair@286 38 import java.nio.ByteBuffer;
ohair@286 39 import java.nio.channels.ReadableByteChannel;
ohair@286 40 import java.nio.channels.WritableByteChannel;
ohair@286 41
ohair@286 42 /**
ohair@286 43 * Encodes a {@link Message} (its XML infoset and attachments) to a sequence of bytes.
ohair@286 44 *
ohair@286 45 * <p>
ohair@286 46 * This interface provides pluggability for different ways of encoding XML infoset,
ohair@286 47 * such as plain XML (plus MIME attachments), XOP, and FastInfoset.
ohair@286 48 *
ohair@286 49 * <p>
ohair@286 50 * Transport usually needs a MIME content type of the encoding, so the {@link Codec}
ohair@286 51 * interface is designed to return this information. However, for some encoding
ohair@286 52 * (such as XOP), the encoding may actually change based on the actual content of
ohair@286 53 * {@link Message}, therefore the codec returns the content type as a result of encoding.
ohair@286 54 *
ohair@286 55 * <p>
ohair@286 56 * {@link Codec} does not produce transport-specific information, such as HTTP headers.
ohair@286 57 *
ohair@286 58 * <p>
alanb@368 59 * {@link Codec} implementations should be thread-safe; a codec instance could be used
alanb@368 60 * concurrently in multiple threads. If a codec have to generate or use a per-request
alanb@368 61 * state, the codec implementation must store the state in the Packet instead of using an
alanb@368 62 * instance variable of the codec implementation.
ohair@286 63 *
ohair@286 64 * <p>
ohair@286 65 * {@link BindingID} determines the {@link Codec}. See {@link BindingID#createEncoder(WSBinding)}.
ohair@286 66 *
ohair@286 67 * @author Kohsuke Kawaguchi
alanb@368 68 * @author shih-chang.chen@oracle.com
alanb@368 69 *
ohair@286 70 * @see EndpointAwareCodec
ohair@286 71 */
ohair@286 72 public interface Codec {
ohair@286 73
ohair@286 74 /**
ohair@286 75 * Get the MIME type associated with this Codec.
ohair@286 76 * <p>
ohair@286 77 * If available the MIME type will represent the media that the codec
ohair@286 78 * encodes and decodes.
ohair@286 79 *
ohair@286 80 * The MIME type returned will be the most general representation independent
ohair@286 81 * of an instance of this MIME type utilized as a MIME content-type.
ohair@286 82 *
ohair@286 83 * @return
ohair@286 84 * null if the MIME type can't be determined by the <code>Codec</code>
ohair@286 85 * implementation. Otherwise the MIME type is returned.
ohair@286 86 */
ohair@286 87 public String getMimeType();
ohair@286 88
ohair@286 89 /**
ohair@286 90 * If the MIME content-type of the encoding is known statically
ohair@286 91 * then this method returns it.
ohair@286 92 *
ohair@286 93 * <p>
ohair@286 94 * Transports often need to write the content type before it writes
ohair@286 95 * the message body, and since the encode method returns the content type
ohair@286 96 * after the body is written, it requires a buffering.
ohair@286 97 *
ohair@286 98 * For those {@link Codec}s that always use a constant content type,
ohair@286 99 * This method allows a transport to streamline the write operation.
ohair@286 100 *
ohair@286 101 * @return
ohair@286 102 * null if the content-type can't be determined in short of
ohair@286 103 * encodin the packet. Otherwise content type for this {@link Packet},
ohair@286 104 * such as "application/xml".
ohair@286 105 */
ohair@286 106 ContentType getStaticContentType(Packet packet);
ohair@286 107
ohair@286 108 /**
ohair@286 109 * Encodes an XML infoset portion of the {@link Message}
ohair@286 110 * (from &lt;soap:Envelope> to &lt;/soap:Envelope>).
ohair@286 111 *
ohair@286 112 * <p>
ohair@286 113 * Internally, this method is most likely invoke {@link Message#writeTo(XMLStreamWriter)}
ohair@286 114 * to turn the message into infoset.
ohair@286 115 *
ohair@286 116 * @param packet
ohair@286 117 * @param out
ohair@286 118 * Must not be null. The caller is responsible for closing the stream,
ohair@286 119 * not the callee.
ohair@286 120 *
ohair@286 121 * @return
ohair@286 122 * The MIME content type of the encoded message (such as "application/xml").
ohair@286 123 * This information is often ncessary by transport.
ohair@286 124 *
ohair@286 125 * @throws IOException
ohair@286 126 * if a {@link OutputStream} throws {@link IOException}.
ohair@286 127 */
ohair@286 128 ContentType encode( Packet packet, OutputStream out ) throws IOException;
ohair@286 129
ohair@286 130 /**
ohair@286 131 * The version of {@link #encode(Packet,OutputStream)}
ohair@286 132 * that writes to NIO {@link ByteBuffer}.
ohair@286 133 *
ohair@286 134 * <p>
ohair@286 135 * TODO: for the convenience of implementation, write
ohair@286 136 * an adapter that wraps {@link WritableByteChannel} to {@link OutputStream}.
ohair@286 137 */
ohair@286 138 ContentType encode( Packet packet, WritableByteChannel buffer );
ohair@286 139
ohair@286 140 /*
ohair@286 141 * The following methods need to be documented and implemented.
ohair@286 142 *
ohair@286 143 * Such methods will be used by a client side
ohair@286 144 * transport pipe that implements the ClientEdgePipe.
ohair@286 145 *
ohair@286 146 String encode( InputStreamMessage message, OutputStream out ) throws IOException;
ohair@286 147 String encode( InputStreamMessage message, WritableByteChannel buffer );
ohair@286 148 */
ohair@286 149
ohair@286 150 /**
ohair@286 151 * Creates a copy of this {@link Codec}.
ohair@286 152 *
ohair@286 153 * <p>
ohair@286 154 * Since {@link Codec} instance is not re-entrant, the caller
ohair@286 155 * who needs to encode two {@link Message}s simultaneously will
ohair@286 156 * want to have two {@link Codec} instances. That's what this
ohair@286 157 * method produces.
ohair@286 158 *
ohair@286 159 * <h3>Implentation Note</h3>
ohair@286 160 * <p>
ohair@286 161 * Note that this method might be invoked by one thread while
ohair@286 162 * another thread is executing one of the {@link #encode} methods.
ohair@286 163 * <!-- or otherwise you'd always have to maintain one idle copy -->
ohair@286 164 * <!-- just so that you can make copies from -->
ohair@286 165 * This should be OK because you'll be only copying things that
ohair@286 166 * are thread-safe, and creating new ones for thread-unsafe resources,
ohair@286 167 * but please let us know if this contract is difficult.
ohair@286 168 *
ohair@286 169 * @return
ohair@286 170 * always non-null valid {@link Codec} that performs
ohair@286 171 * the encoding work in the same way --- that is, if you
ohair@286 172 * copy an FI codec, you'll get another FI codec.
ohair@286 173 *
ohair@286 174 * <p>
ohair@286 175 * Once copied, two {@link Codec}s may be invoked from
ohair@286 176 * two threads concurrently; therefore, they must not share
ohair@286 177 * any state that requires isolation (such as temporary buffer.)
ohair@286 178 *
ohair@286 179 * <p>
ohair@286 180 * If the {@link Codec} implementation is already
ohair@286 181 * re-entrant and multi-thread safe to begin with,
ohair@286 182 * then this method may simply return <tt>this</tt>.
ohair@286 183 */
ohair@286 184 Codec copy();
ohair@286 185
ohair@286 186 /**
ohair@286 187 * Reads bytes from {@link InputStream} and constructs a {@link Message}.
ohair@286 188 *
ohair@286 189 * <p>
ohair@286 190 * The design encourages lazy decoding of a {@link Message}, where
ohair@286 191 * a {@link Message} is returned even before the whole message is parsed,
ohair@286 192 * and additional parsing is done as the {@link Message} body is read along.
ohair@286 193 * A {@link Codec} is most likely have its own implementation of {@link Message}
ohair@286 194 * for this purpose.
ohair@286 195 *
ohair@286 196 * @param in
ohair@286 197 * the data to be read into a {@link Message}. The transport would have
ohair@286 198 * read any transport-specific header before it passes an {@link InputStream},
ohair@286 199 * and {@link InputStream} is expected to be read until EOS. Never null.
ohair@286 200 *
ohair@286 201 * <p>
ohair@286 202 * Some transports, such as SMTP, may 'encode' data into another format
ohair@286 203 * (such as uuencode, base64, etc.) It is the caller's responsibility to
ohair@286 204 * 'decode' these transport-level encoding before it passes data into
ohair@286 205 * {@link Codec}.
ohair@286 206 *
ohair@286 207 * @param contentType
ohair@286 208 * The MIME content type (like "application/xml") of this byte stream.
ohair@286 209 * Thie text includes all the sub-headers of the content-type header. Therefore,
ohair@286 210 * in more complex case, this could be something like
ohair@286 211 * <tt>multipart/related; boundary="--=_outer_boundary"; type="multipart/alternative"</tt>.
ohair@286 212 * This parameter must not be null.
ohair@286 213 *
ohair@286 214 * @param response
ohair@286 215 * The parsed {@link Message} will be set to this {@link Packet}.
ohair@286 216 * {@link Codec} may add additional properties to this {@link Packet}.
ohair@286 217 * On a successful method completion, a {@link Packet} must contain a
ohair@286 218 * {@link Message}.
ohair@286 219 *
ohair@286 220 * @throws IOException
ohair@286 221 * if {@link InputStream} throws an exception.
ohair@286 222 */
ohair@286 223 void decode( InputStream in, String contentType, Packet response ) throws IOException;
ohair@286 224
ohair@286 225 /**
ohair@286 226 *
ohair@286 227 * @see #decode(InputStream, String, Packet)
ohair@286 228 */
ohair@286 229 void decode( ReadableByteChannel in, String contentType, Packet response );
ohair@286 230
ohair@286 231 /*
ohair@286 232 * The following methods need to be documented and implemented.
ohair@286 233 *
ohair@286 234 * Such methods will be used by a server side
ohair@286 235 * transport pipe that can support the invocation of methods on a
ohair@286 236 * ServerEdgePipe.
ohair@286 237 *
ohair@286 238 XMLStreamReaderMessage decode( InputStream in, String contentType ) throws IOException;
ohair@286 239 XMLStreamReaderMessage decode( ReadableByteChannel in, String contentType );
ohair@286 240 */
ohair@286 241 }

mercurial