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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

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

mercurial