Thu, 31 Aug 2017 15:18:52 +0800
merge
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.encoding; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.xml.internal.ws.api.SOAPVersion; |
aoqi@0 | 29 | import com.sun.xml.internal.ws.api.WSFeatureList; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.client.SelectOptimalEncodingFeature; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.fastinfoset.FastInfosetFeature; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.api.message.Message; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.api.message.Packet; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.api.message.ExceptionHasMessage; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.api.pipe.Codec; |
aoqi@0 | 36 | import com.sun.xml.internal.ws.api.pipe.Codecs; |
aoqi@0 | 37 | import com.sun.xml.internal.ws.api.pipe.ContentType; |
aoqi@0 | 38 | import com.sun.xml.internal.ws.api.pipe.StreamSOAPCodec; |
aoqi@0 | 39 | import com.sun.xml.internal.ws.client.ContentNegotiation; |
aoqi@0 | 40 | import com.sun.xml.internal.ws.protocol.soap.MessageCreationException; |
aoqi@0 | 41 | import com.sun.xml.internal.ws.resources.StreamingMessages; |
aoqi@0 | 42 | import com.sun.xml.internal.ws.server.UnsupportedMediaException; |
aoqi@0 | 43 | import static com.sun.xml.internal.ws.binding.WebServiceFeatureList.getSoapVersion; |
aoqi@0 | 44 | |
aoqi@0 | 45 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 46 | import javax.xml.ws.WebServiceFeature; |
aoqi@0 | 47 | import javax.xml.ws.soap.MTOMFeature; |
aoqi@0 | 48 | import java.io.IOException; |
aoqi@0 | 49 | import java.io.InputStream; |
aoqi@0 | 50 | import java.io.OutputStream; |
aoqi@0 | 51 | import java.lang.reflect.Method; |
aoqi@0 | 52 | import java.nio.channels.ReadableByteChannel; |
aoqi@0 | 53 | import java.nio.channels.WritableByteChannel; |
aoqi@0 | 54 | //import java.util.StringTokenizer; |
aoqi@0 | 55 | |
aoqi@0 | 56 | /** |
aoqi@0 | 57 | * SOAP binding {@link Codec} that can handle MTOM, SwA, and SOAP messages |
aoqi@0 | 58 | * encoded using XML or Fast Infoset. |
aoqi@0 | 59 | * |
aoqi@0 | 60 | * <p> |
aoqi@0 | 61 | * This is used when we need to determine the encoding from what we received (for decoding) |
aoqi@0 | 62 | * and from configuration and {@link Message} contents (for encoding) |
aoqi@0 | 63 | * |
aoqi@0 | 64 | * <p> |
aoqi@0 | 65 | * TODO: Split this Codec into two, one that supports FI and one that does not. |
aoqi@0 | 66 | * Then further split the FI Codec into two, one for client and one for |
aoqi@0 | 67 | * server. This will simplify the logic and make it easier to understand/maintain. |
aoqi@0 | 68 | * |
aoqi@0 | 69 | * @author Vivek Pandey |
aoqi@0 | 70 | * @author Kohsuke Kawaguchi |
aoqi@0 | 71 | */ |
aoqi@0 | 72 | public class SOAPBindingCodec extends MimeCodec implements com.sun.xml.internal.ws.api.pipe.SOAPBindingCodec { |
aoqi@0 | 73 | |
aoqi@0 | 74 | public static final String UTF8_ENCODING = "utf-8"; |
aoqi@0 | 75 | public static final String DEFAULT_ENCODING = UTF8_ENCODING; |
aoqi@0 | 76 | |
aoqi@0 | 77 | |
aoqi@0 | 78 | /** |
aoqi@0 | 79 | * True if Fast Infoset functionality has been |
aoqi@0 | 80 | * configured to be disabled, or the Fast Infoset |
aoqi@0 | 81 | * runtime is not available. |
aoqi@0 | 82 | */ |
aoqi@0 | 83 | private boolean isFastInfosetDisabled; |
aoqi@0 | 84 | |
aoqi@0 | 85 | /** |
aoqi@0 | 86 | * True if the Fast Infoset codec should be used for encoding. |
aoqi@0 | 87 | */ |
aoqi@0 | 88 | private boolean useFastInfosetForEncoding; |
aoqi@0 | 89 | |
aoqi@0 | 90 | /** |
aoqi@0 | 91 | * True if the content negotiation property should |
aoqi@0 | 92 | * be ignored by the client. This will be used in |
aoqi@0 | 93 | * the case of Fast Infoset being configured to be |
aoqi@0 | 94 | * disabled or automatically selected. |
aoqi@0 | 95 | */ |
aoqi@0 | 96 | private boolean ignoreContentNegotiationProperty; |
aoqi@0 | 97 | |
aoqi@0 | 98 | // The XML SOAP codec |
aoqi@0 | 99 | private final StreamSOAPCodec xmlSoapCodec; |
aoqi@0 | 100 | |
aoqi@0 | 101 | // The Fast Infoset SOAP codec |
aoqi@0 | 102 | private final Codec fiSoapCodec; |
aoqi@0 | 103 | |
aoqi@0 | 104 | // The XML MTOM codec |
aoqi@0 | 105 | private final MimeCodec xmlMtomCodec; |
aoqi@0 | 106 | |
aoqi@0 | 107 | // The XML SWA codec |
aoqi@0 | 108 | private final MimeCodec xmlSwaCodec; |
aoqi@0 | 109 | |
aoqi@0 | 110 | // The Fast Infoset SWA codec |
aoqi@0 | 111 | private final MimeCodec fiSwaCodec; |
aoqi@0 | 112 | |
aoqi@0 | 113 | /** |
aoqi@0 | 114 | * The XML SOAP MIME type |
aoqi@0 | 115 | */ |
aoqi@0 | 116 | private final String xmlMimeType; |
aoqi@0 | 117 | |
aoqi@0 | 118 | /** |
aoqi@0 | 119 | * The Fast Infoset SOAP MIME type |
aoqi@0 | 120 | */ |
aoqi@0 | 121 | private final String fiMimeType; |
aoqi@0 | 122 | |
aoqi@0 | 123 | /** |
aoqi@0 | 124 | * The Accept header for XML encodings |
aoqi@0 | 125 | */ |
aoqi@0 | 126 | private final String xmlAccept; |
aoqi@0 | 127 | |
aoqi@0 | 128 | /** |
aoqi@0 | 129 | * The Accept header for Fast Infoset and XML encodings |
aoqi@0 | 130 | */ |
aoqi@0 | 131 | private final String connegXmlAccept; |
aoqi@0 | 132 | |
aoqi@0 | 133 | public StreamSOAPCodec getXMLCodec() { |
aoqi@0 | 134 | return xmlSoapCodec; |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | private ContentTypeImpl setAcceptHeader(Packet p, ContentTypeImpl c) { |
aoqi@0 | 138 | String _accept; |
aoqi@0 | 139 | if (!ignoreContentNegotiationProperty && p.contentNegotiation != ContentNegotiation.none) { |
aoqi@0 | 140 | _accept = connegXmlAccept; |
aoqi@0 | 141 | } else { |
aoqi@0 | 142 | _accept = xmlAccept; |
aoqi@0 | 143 | } |
aoqi@0 | 144 | c.setAcceptHeader(_accept); |
aoqi@0 | 145 | return c; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | public SOAPBindingCodec(WSFeatureList features) { |
aoqi@0 | 149 | this(features, Codecs.createSOAPEnvelopeXmlCodec(features)); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | public SOAPBindingCodec(WSFeatureList features, StreamSOAPCodec xmlSoapCodec) { |
aoqi@0 | 153 | super(getSoapVersion(features), features); |
aoqi@0 | 154 | |
aoqi@0 | 155 | this.xmlSoapCodec = xmlSoapCodec; |
aoqi@0 | 156 | xmlMimeType = xmlSoapCodec.getMimeType(); |
aoqi@0 | 157 | |
aoqi@0 | 158 | xmlMtomCodec = new MtomCodec(version, xmlSoapCodec, features); |
aoqi@0 | 159 | |
aoqi@0 | 160 | xmlSwaCodec = new SwACodec(version, features, xmlSoapCodec); |
aoqi@0 | 161 | |
aoqi@0 | 162 | String clientAcceptedContentTypes = xmlSoapCodec.getMimeType() + ", " + |
aoqi@0 | 163 | xmlMtomCodec.getMimeType(); |
aoqi@0 | 164 | |
aoqi@0 | 165 | WebServiceFeature fi = features.get(FastInfosetFeature.class); |
aoqi@0 | 166 | isFastInfosetDisabled = (fi != null && !fi.isEnabled()); |
aoqi@0 | 167 | if (!isFastInfosetDisabled) { |
aoqi@0 | 168 | fiSoapCodec = getFICodec(xmlSoapCodec, version); |
aoqi@0 | 169 | if (fiSoapCodec != null) { |
aoqi@0 | 170 | fiMimeType = fiSoapCodec.getMimeType(); |
aoqi@0 | 171 | fiSwaCodec = new SwACodec(version, features, fiSoapCodec); |
aoqi@0 | 172 | connegXmlAccept = fiMimeType + ", " + clientAcceptedContentTypes; |
aoqi@0 | 173 | |
aoqi@0 | 174 | /** |
aoqi@0 | 175 | * This feature will only be present on the client side. |
aoqi@0 | 176 | * |
aoqi@0 | 177 | * Fast Infoset is enabled on the client if the service |
aoqi@0 | 178 | * explicitly supports Fast Infoset. |
aoqi@0 | 179 | */ |
aoqi@0 | 180 | WebServiceFeature select = features.get(SelectOptimalEncodingFeature.class); |
aoqi@0 | 181 | if (select != null) { // if the client FI feature is set - ignore negotiation property |
aoqi@0 | 182 | ignoreContentNegotiationProperty = true; |
aoqi@0 | 183 | if (select.isEnabled()) { |
aoqi@0 | 184 | // If the client's FI encoding feature is enabled, and server's is not disabled |
aoqi@0 | 185 | if (fi != null) { // if server's FI feature also enabled |
aoqi@0 | 186 | useFastInfosetForEncoding = true; |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | clientAcceptedContentTypes = connegXmlAccept; |
aoqi@0 | 190 | } else { // If client FI feature is disabled |
aoqi@0 | 191 | isFastInfosetDisabled = true; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | } |
aoqi@0 | 194 | } else { |
aoqi@0 | 195 | // Fast Infoset could not be loaded by the runtime |
aoqi@0 | 196 | isFastInfosetDisabled = true; |
aoqi@0 | 197 | fiSwaCodec = null; |
aoqi@0 | 198 | fiMimeType = ""; |
aoqi@0 | 199 | connegXmlAccept = clientAcceptedContentTypes; |
aoqi@0 | 200 | ignoreContentNegotiationProperty = true; |
aoqi@0 | 201 | } |
aoqi@0 | 202 | } else { |
aoqi@0 | 203 | // Fast Infoset is explicitly not supported by the service |
aoqi@0 | 204 | fiSoapCodec = fiSwaCodec = null; |
aoqi@0 | 205 | fiMimeType = ""; |
aoqi@0 | 206 | connegXmlAccept = clientAcceptedContentTypes; |
aoqi@0 | 207 | ignoreContentNegotiationProperty = true; |
aoqi@0 | 208 | } |
aoqi@0 | 209 | |
aoqi@0 | 210 | xmlAccept = clientAcceptedContentTypes; |
aoqi@0 | 211 | |
aoqi@0 | 212 | if(getSoapVersion(features) == null) |
aoqi@0 | 213 | throw new WebServiceException("Expecting a SOAP binding but found "); |
aoqi@0 | 214 | } |
aoqi@0 | 215 | |
aoqi@0 | 216 | public String getMimeType() { |
aoqi@0 | 217 | return null; |
aoqi@0 | 218 | } |
aoqi@0 | 219 | |
aoqi@0 | 220 | public ContentType getStaticContentType(Packet packet) { |
aoqi@0 | 221 | ContentType toAdapt = getEncoder(packet).getStaticContentType(packet); |
aoqi@0 | 222 | return setAcceptHeader(packet, (ContentTypeImpl)toAdapt); |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | public ContentType encode(Packet packet, OutputStream out) throws IOException { |
aoqi@0 | 226 | preEncode(packet); |
aoqi@0 | 227 | ContentType ct = getEncoder(packet).encode(packet, out); |
aoqi@0 | 228 | ct = setAcceptHeader(packet, (ContentTypeImpl)ct); |
aoqi@0 | 229 | postEncode(); |
aoqi@0 | 230 | return ct; |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | public ContentType encode(Packet packet, WritableByteChannel buffer) { |
aoqi@0 | 234 | preEncode(packet); |
aoqi@0 | 235 | ContentType ct = getEncoder(packet).encode(packet, buffer); |
aoqi@0 | 236 | ct = setAcceptHeader(packet, (ContentTypeImpl)ct); |
aoqi@0 | 237 | postEncode(); |
aoqi@0 | 238 | return ct; |
aoqi@0 | 239 | } |
aoqi@0 | 240 | |
aoqi@0 | 241 | /** |
aoqi@0 | 242 | * Should be called before encode(). |
aoqi@0 | 243 | * Set the state so that such state is used by encode process. |
aoqi@0 | 244 | */ |
aoqi@0 | 245 | private void preEncode(Packet p) { |
aoqi@0 | 246 | } |
aoqi@0 | 247 | |
aoqi@0 | 248 | /** |
aoqi@0 | 249 | * Should be called after encode() |
aoqi@0 | 250 | * Reset the encoding state. |
aoqi@0 | 251 | */ |
aoqi@0 | 252 | private void postEncode() { |
aoqi@0 | 253 | } |
aoqi@0 | 254 | |
aoqi@0 | 255 | /** |
aoqi@0 | 256 | * Should be called before decode(). |
aoqi@0 | 257 | * Set the state so that such state is used by decode(). |
aoqi@0 | 258 | */ |
aoqi@0 | 259 | private void preDecode(Packet p) { |
aoqi@0 | 260 | if (p.contentNegotiation == null) |
aoqi@0 | 261 | useFastInfosetForEncoding = false; |
aoqi@0 | 262 | } |
aoqi@0 | 263 | |
aoqi@0 | 264 | /** |
aoqi@0 | 265 | * Should be called after decode(). |
aoqi@0 | 266 | * Set the state so that such state is used by encode(). |
aoqi@0 | 267 | */ |
aoqi@0 | 268 | private void postDecode(Packet p) { |
aoqi@0 | 269 | p.setFastInfosetDisabled(isFastInfosetDisabled); |
aoqi@0 | 270 | if(features.isEnabled(MTOMFeature.class)) p.checkMtomAcceptable(); |
aoqi@0 | 271 | // p.setMtomAcceptable( isMtomAcceptable(p.acceptableMimeTypes) ); |
aoqi@0 | 272 | MTOMFeature mtomFeature = features.get(MTOMFeature.class); |
aoqi@0 | 273 | if (mtomFeature != null) { |
aoqi@0 | 274 | p.setMtomFeature(mtomFeature); |
aoqi@0 | 275 | } |
aoqi@0 | 276 | if (!useFastInfosetForEncoding) { |
aoqi@0 | 277 | useFastInfosetForEncoding = p.getFastInfosetAcceptable(fiMimeType); |
aoqi@0 | 278 | // useFastInfosetForEncoding = isFastInfosetAcceptable(p.acceptableMimeTypes); |
aoqi@0 | 279 | } |
aoqi@0 | 280 | } |
aoqi@0 | 281 | |
aoqi@0 | 282 | public void decode(InputStream in, String contentType, Packet packet) throws IOException { |
aoqi@0 | 283 | if (contentType == null) { |
aoqi@0 | 284 | contentType = xmlMimeType; |
aoqi@0 | 285 | } |
aoqi@0 | 286 | packet.setContentType(new ContentTypeImpl(contentType)); |
aoqi@0 | 287 | preDecode(packet); |
aoqi@0 | 288 | try { |
aoqi@0 | 289 | if(isMultipartRelated(contentType)) |
aoqi@0 | 290 | // parse the multipart portion and then decide whether it's MTOM or SwA |
aoqi@0 | 291 | super.decode(in, contentType, packet); |
aoqi@0 | 292 | else if(isFastInfoset(contentType)) { |
aoqi@0 | 293 | if (!ignoreContentNegotiationProperty && packet.contentNegotiation == ContentNegotiation.none) |
aoqi@0 | 294 | throw noFastInfosetForDecoding(); |
aoqi@0 | 295 | |
aoqi@0 | 296 | useFastInfosetForEncoding = true; |
aoqi@0 | 297 | fiSoapCodec.decode(in, contentType, packet); |
aoqi@0 | 298 | } else |
aoqi@0 | 299 | xmlSoapCodec.decode(in, contentType, packet); |
aoqi@0 | 300 | } catch(RuntimeException we) { |
aoqi@0 | 301 | if (we instanceof ExceptionHasMessage || we instanceof UnsupportedMediaException) { |
aoqi@0 | 302 | throw we; |
aoqi@0 | 303 | } else { |
aoqi@0 | 304 | throw new MessageCreationException(version, we); |
aoqi@0 | 305 | } |
aoqi@0 | 306 | } |
aoqi@0 | 307 | postDecode(packet); |
aoqi@0 | 308 | } |
aoqi@0 | 309 | |
aoqi@0 | 310 | public void decode(ReadableByteChannel in, String contentType, Packet packet) { |
aoqi@0 | 311 | if (contentType == null) { |
aoqi@0 | 312 | throw new UnsupportedMediaException(); |
aoqi@0 | 313 | } |
aoqi@0 | 314 | |
aoqi@0 | 315 | preDecode(packet); |
aoqi@0 | 316 | try { |
aoqi@0 | 317 | if(isMultipartRelated(contentType)) |
aoqi@0 | 318 | super.decode(in, contentType, packet); |
aoqi@0 | 319 | else if(isFastInfoset(contentType)) { |
aoqi@0 | 320 | if (packet.contentNegotiation == ContentNegotiation.none) |
aoqi@0 | 321 | throw noFastInfosetForDecoding(); |
aoqi@0 | 322 | |
aoqi@0 | 323 | useFastInfosetForEncoding = true; |
aoqi@0 | 324 | fiSoapCodec.decode(in, contentType, packet); |
aoqi@0 | 325 | } else |
aoqi@0 | 326 | xmlSoapCodec.decode(in, contentType, packet); |
aoqi@0 | 327 | } catch(RuntimeException we) { |
aoqi@0 | 328 | if (we instanceof ExceptionHasMessage || we instanceof UnsupportedMediaException) { |
aoqi@0 | 329 | throw we; |
aoqi@0 | 330 | } else { |
aoqi@0 | 331 | throw new MessageCreationException(version, we); |
aoqi@0 | 332 | } |
aoqi@0 | 333 | } |
aoqi@0 | 334 | postDecode(packet); |
aoqi@0 | 335 | } |
aoqi@0 | 336 | |
aoqi@0 | 337 | public SOAPBindingCodec copy() { |
aoqi@0 | 338 | return new SOAPBindingCodec(features, (StreamSOAPCodec)xmlSoapCodec.copy()); |
aoqi@0 | 339 | } |
aoqi@0 | 340 | |
aoqi@0 | 341 | @Override |
aoqi@0 | 342 | protected void decode(MimeMultipartParser mpp, Packet packet) throws IOException { |
aoqi@0 | 343 | // is this SwA or XOP? |
aoqi@0 | 344 | final String rootContentType = mpp.getRootPart().getContentType(); |
aoqi@0 | 345 | boolean isMTOM = isApplicationXopXml(rootContentType); |
aoqi@0 | 346 | packet.setMtomRequest(isMTOM); |
aoqi@0 | 347 | if(isMTOM) { |
aoqi@0 | 348 | xmlMtomCodec.decode(mpp,packet); |
aoqi@0 | 349 | } else if (isFastInfoset(rootContentType)) { |
aoqi@0 | 350 | if (packet.contentNegotiation == ContentNegotiation.none) |
aoqi@0 | 351 | throw noFastInfosetForDecoding(); |
aoqi@0 | 352 | |
aoqi@0 | 353 | useFastInfosetForEncoding = true; |
aoqi@0 | 354 | fiSwaCodec.decode(mpp,packet); |
aoqi@0 | 355 | } else if (isXml(rootContentType)) |
aoqi@0 | 356 | xmlSwaCodec.decode(mpp,packet); |
aoqi@0 | 357 | else { |
aoqi@0 | 358 | // TODO localize exception |
aoqi@0 | 359 | throw new IOException(""); |
aoqi@0 | 360 | } |
aoqi@0 | 361 | // checkDuplicateKnownHeaders(packet); |
aoqi@0 | 362 | } |
aoqi@0 | 363 | |
aoqi@0 | 364 | private boolean isMultipartRelated(String contentType) { |
aoqi@0 | 365 | return compareStrings(contentType, MimeCodec.MULTIPART_RELATED_MIME_TYPE); |
aoqi@0 | 366 | } |
aoqi@0 | 367 | |
aoqi@0 | 368 | private boolean isApplicationXopXml(String contentType) { |
aoqi@0 | 369 | return compareStrings(contentType, MtomCodec.XOP_XML_MIME_TYPE); |
aoqi@0 | 370 | } |
aoqi@0 | 371 | |
aoqi@0 | 372 | private boolean isXml(String contentType) { |
aoqi@0 | 373 | return compareStrings(contentType, xmlMimeType); |
aoqi@0 | 374 | } |
aoqi@0 | 375 | |
aoqi@0 | 376 | private boolean isFastInfoset(String contentType) { |
aoqi@0 | 377 | if (isFastInfosetDisabled) return false; |
aoqi@0 | 378 | |
aoqi@0 | 379 | return compareStrings(contentType, fiMimeType); |
aoqi@0 | 380 | } |
aoqi@0 | 381 | |
aoqi@0 | 382 | private boolean compareStrings(String a, String b) { |
aoqi@0 | 383 | return a.length() >= b.length() && |
aoqi@0 | 384 | b.equalsIgnoreCase( |
aoqi@0 | 385 | a.substring(0, |
aoqi@0 | 386 | b.length())); |
aoqi@0 | 387 | } |
aoqi@0 | 388 | |
aoqi@0 | 389 | // private boolean isFastInfosetAcceptable(String accept) { |
aoqi@0 | 390 | // if (accept == null || isFastInfosetDisabled) return false; |
aoqi@0 | 391 | // |
aoqi@0 | 392 | // StringTokenizer st = new StringTokenizer(accept, ","); |
aoqi@0 | 393 | // while (st.hasMoreTokens()) { |
aoqi@0 | 394 | // final String token = st.nextToken().trim(); |
aoqi@0 | 395 | // if (token.equalsIgnoreCase(fiMimeType)) { |
aoqi@0 | 396 | // return true; |
aoqi@0 | 397 | // } |
aoqi@0 | 398 | // } |
aoqi@0 | 399 | // return false; |
aoqi@0 | 400 | // } |
aoqi@0 | 401 | |
aoqi@0 | 402 | /* |
aoqi@0 | 403 | * Just check if the Accept header contains application/xop+xml, |
aoqi@0 | 404 | * no need to worry about q values. |
aoqi@0 | 405 | */ |
aoqi@0 | 406 | // private boolean isMtomAcceptable(String accept) { |
aoqi@0 | 407 | // if (accept == null || isFastInfosetDisabled) return false; |
aoqi@0 | 408 | // StringTokenizer st = new StringTokenizer(accept, ","); |
aoqi@0 | 409 | // while (st.hasMoreTokens()) { |
aoqi@0 | 410 | // final String token = st.nextToken().trim(); |
aoqi@0 | 411 | // if (token.toLowerCase().contains(MtomCodec.XOP_XML_MIME_TYPE)) { |
aoqi@0 | 412 | // return true; |
aoqi@0 | 413 | // } |
aoqi@0 | 414 | // } |
aoqi@0 | 415 | // return false; |
aoqi@0 | 416 | // } |
aoqi@0 | 417 | |
aoqi@0 | 418 | /** |
aoqi@0 | 419 | * Determines the encoding codec. |
aoqi@0 | 420 | */ |
aoqi@0 | 421 | private Codec getEncoder(Packet p) { |
aoqi@0 | 422 | /** |
aoqi@0 | 423 | * The following logic is only for outbound packets |
aoqi@0 | 424 | * to be encoded by a client. |
aoqi@0 | 425 | * For a server the p.contentNegotiation == null. |
aoqi@0 | 426 | */ |
aoqi@0 | 427 | if (!ignoreContentNegotiationProperty) { |
aoqi@0 | 428 | if (p.contentNegotiation == ContentNegotiation.none) { |
aoqi@0 | 429 | // The client may have changed the negotiation property from |
aoqi@0 | 430 | // pessismistic to none between invocations |
aoqi@0 | 431 | useFastInfosetForEncoding = false; |
aoqi@0 | 432 | } else if (p.contentNegotiation == ContentNegotiation.optimistic) { |
aoqi@0 | 433 | // Always encode using Fast Infoset if in optimisitic mode |
aoqi@0 | 434 | useFastInfosetForEncoding = true; |
aoqi@0 | 435 | } |
aoqi@0 | 436 | } |
aoqi@0 | 437 | |
aoqi@0 | 438 | // Override the MTOM binding for now |
aoqi@0 | 439 | // Note: Using FI with MTOM does not make sense |
aoqi@0 | 440 | if (useFastInfosetForEncoding) { |
aoqi@0 | 441 | final Message m = p.getMessage(); |
aoqi@0 | 442 | if(m==null || m.getAttachments().isEmpty() || features.isEnabled(MTOMFeature.class)) |
aoqi@0 | 443 | return fiSoapCodec; |
aoqi@0 | 444 | else |
aoqi@0 | 445 | return fiSwaCodec; |
aoqi@0 | 446 | } |
aoqi@0 | 447 | |
aoqi@0 | 448 | //If the packet does not have a binding, explicitly set the MTOMFeature |
aoqi@0 | 449 | //on the packet so that it has a way to determine whether to use MTOM |
aoqi@0 | 450 | if (p.getBinding() == null) { |
aoqi@0 | 451 | if (features != null) { |
aoqi@0 | 452 | p.setMtomFeature(features.get(MTOMFeature.class)); |
aoqi@0 | 453 | } |
aoqi@0 | 454 | } |
aoqi@0 | 455 | |
aoqi@0 | 456 | if (p.shouldUseMtom()) { |
aoqi@0 | 457 | return xmlMtomCodec; |
aoqi@0 | 458 | } |
aoqi@0 | 459 | |
aoqi@0 | 460 | Message m = p.getMessage(); |
aoqi@0 | 461 | if(m==null || m.getAttachments().isEmpty()) |
aoqi@0 | 462 | return xmlSoapCodec; |
aoqi@0 | 463 | else |
aoqi@0 | 464 | return xmlSwaCodec; |
aoqi@0 | 465 | } |
aoqi@0 | 466 | |
aoqi@0 | 467 | private RuntimeException noFastInfosetForDecoding() { |
aoqi@0 | 468 | return new RuntimeException(StreamingMessages.FASTINFOSET_DECODING_NOT_ACCEPTED()); |
aoqi@0 | 469 | } |
aoqi@0 | 470 | |
aoqi@0 | 471 | /** |
aoqi@0 | 472 | * Obtain an FI SOAP codec instance using reflection. |
aoqi@0 | 473 | */ |
aoqi@0 | 474 | private static Codec getFICodec(StreamSOAPCodec soapCodec, SOAPVersion version) { |
aoqi@0 | 475 | try { |
aoqi@0 | 476 | Class c = Class.forName("com.sun.xml.internal.ws.encoding.fastinfoset.FastInfosetStreamSOAPCodec"); |
aoqi@0 | 477 | Method m = c.getMethod("create", StreamSOAPCodec.class, SOAPVersion.class); |
aoqi@0 | 478 | return (Codec)m.invoke(null, soapCodec, version); |
aoqi@0 | 479 | } catch (Exception e) { |
aoqi@0 | 480 | // TODO Log that FI cannot be loaded |
aoqi@0 | 481 | return null; |
aoqi@0 | 482 | } |
aoqi@0 | 483 | } |
aoqi@0 | 484 | } |