1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/MTOMDecorator.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,131 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.runtime.unmarshaller; 1.30 + 1.31 +import javax.activation.DataHandler; 1.32 +import javax.xml.bind.attachment.AttachmentUnmarshaller; 1.33 +import javax.xml.namespace.NamespaceContext; 1.34 + 1.35 +import com.sun.xml.internal.bind.v2.WellKnownNamespace; 1.36 + 1.37 +import org.xml.sax.SAXException; 1.38 + 1.39 +/** 1.40 + * Decorator of {@link XmlVisitor} that performs XOP processing. 1.41 + * Used to support MTOM. 1.42 + * 1.43 + * @author Kohsuke Kawaguchi 1.44 + */ 1.45 +final class MTOMDecorator implements XmlVisitor { 1.46 + 1.47 + private final XmlVisitor next; 1.48 + 1.49 + private final AttachmentUnmarshaller au; 1.50 + 1.51 + private UnmarshallerImpl parent; 1.52 + 1.53 + private final Base64Data base64data = new Base64Data(); 1.54 + 1.55 + /** 1.56 + * True if we are between the start and the end of xop:Include 1.57 + */ 1.58 + private boolean inXopInclude; 1.59 + 1.60 + /** 1.61 + * UGLY HACK: we need to ignore the whitespace that follows 1.62 + * the attached base64 image. 1.63 + * 1.64 + * This happens twice; once before </xop:Include>, another 1.65 + * after </xop:Include>. The spec guarantees that 1.66 + * no valid pcdata can follow </xop:Include>. 1.67 + */ 1.68 + private boolean followXop; 1.69 + 1.70 + public MTOMDecorator(UnmarshallerImpl parent,XmlVisitor next, AttachmentUnmarshaller au) { 1.71 + this.parent = parent; 1.72 + this.next = next; 1.73 + this.au = au; 1.74 + } 1.75 + 1.76 + public void startDocument(LocatorEx loc, NamespaceContext nsContext) throws SAXException { 1.77 + next.startDocument(loc,nsContext); 1.78 + } 1.79 + 1.80 + public void endDocument() throws SAXException { 1.81 + next.endDocument(); 1.82 + } 1.83 + 1.84 + public void startElement(TagName tagName) throws SAXException { 1.85 + if(tagName.local.equals("Include") && tagName.uri.equals(WellKnownNamespace.XOP)) { 1.86 + // found xop:Include 1.87 + String href = tagName.atts.getValue("href"); 1.88 + DataHandler attachment = au.getAttachmentAsDataHandler(href); 1.89 + if(attachment==null) { 1.90 + // report an error and ignore 1.91 + parent.getEventHandler().handleEvent(null); 1.92 + // TODO 1.93 + } 1.94 + base64data.set(attachment); 1.95 + next.text(base64data); 1.96 + inXopInclude = true; 1.97 + followXop = true; 1.98 + } else 1.99 + next.startElement(tagName); 1.100 + } 1.101 + 1.102 + public void endElement(TagName tagName) throws SAXException { 1.103 + if(inXopInclude) { 1.104 + // consume </xop:Include> by ourselves. 1.105 + inXopInclude = false; 1.106 + followXop = true; 1.107 + return; 1.108 + } 1.109 + next.endElement(tagName); 1.110 + } 1.111 + 1.112 + public void startPrefixMapping(String prefix, String nsUri) throws SAXException { 1.113 + next.startPrefixMapping(prefix,nsUri); 1.114 + } 1.115 + 1.116 + public void endPrefixMapping(String prefix) throws SAXException { 1.117 + next.endPrefixMapping(prefix); 1.118 + } 1.119 + 1.120 + public void text( CharSequence pcdata ) throws SAXException { 1.121 + if(!followXop) 1.122 + next.text(pcdata); 1.123 + else 1.124 + followXop = false; 1.125 + } 1.126 + 1.127 + public UnmarshallingContext getContext() { 1.128 + return next.getContext(); 1.129 + } 1.130 + 1.131 + public TextPredictor getPredictor() { 1.132 + return next.getPredictor(); 1.133 + } 1.134 +}