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.util.pipe; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.WSBinding; |
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.pipe.Tube; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.api.pipe.TubeCloner; |
aoqi@0 | 36 | import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl; |
aoqi@0 | 37 | import com.sun.xml.internal.ws.api.server.DocumentAddressResolver; |
aoqi@0 | 38 | import com.sun.xml.internal.ws.api.server.SDDocument; |
aoqi@0 | 39 | import com.sun.xml.internal.ws.api.server.SDDocumentSource; |
aoqi@0 | 40 | import com.sun.xml.internal.ws.developer.SchemaValidationFeature; |
aoqi@0 | 41 | import com.sun.xml.internal.ws.developer.ValidationErrorHandler; |
aoqi@0 | 42 | import com.sun.xml.internal.ws.server.SDDocumentImpl; |
aoqi@0 | 43 | import com.sun.xml.internal.ws.util.ByteArrayBuffer; |
aoqi@0 | 44 | import com.sun.xml.internal.ws.util.xml.XmlUtil; |
aoqi@0 | 45 | import com.sun.xml.internal.ws.wsdl.SDDocumentResolver; |
aoqi@0 | 46 | import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants; |
aoqi@0 | 47 | import org.w3c.dom.*; |
aoqi@0 | 48 | import org.w3c.dom.ls.LSInput; |
aoqi@0 | 49 | import org.w3c.dom.ls.LSResourceResolver; |
aoqi@0 | 50 | import org.xml.sax.SAXException; |
aoqi@0 | 51 | import org.xml.sax.helpers.NamespaceSupport; |
aoqi@0 | 52 | |
aoqi@0 | 53 | import javax.xml.XMLConstants; |
aoqi@0 | 54 | import javax.xml.namespace.QName; |
aoqi@0 | 55 | import javax.xml.transform.Source; |
aoqi@0 | 56 | import javax.xml.transform.Transformer; |
aoqi@0 | 57 | import javax.xml.transform.TransformerException; |
aoqi@0 | 58 | import javax.xml.transform.dom.DOMResult; |
aoqi@0 | 59 | import javax.xml.transform.dom.DOMSource; |
aoqi@0 | 60 | import javax.xml.transform.stream.StreamSource; |
aoqi@0 | 61 | import javax.xml.validation.SchemaFactory; |
aoqi@0 | 62 | import javax.xml.validation.Validator; |
aoqi@0 | 63 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 64 | import java.io.IOException; |
aoqi@0 | 65 | import java.io.InputStream; |
aoqi@0 | 66 | import java.io.Reader; |
aoqi@0 | 67 | import java.io.StringReader; |
aoqi@0 | 68 | import java.net.MalformedURLException; |
aoqi@0 | 69 | import java.net.URI; |
aoqi@0 | 70 | import java.net.URL; |
aoqi@0 | 71 | import java.util.*; |
aoqi@0 | 72 | import java.util.logging.Level; |
aoqi@0 | 73 | import java.util.logging.Logger; |
aoqi@0 | 74 | |
aoqi@0 | 75 | import static com.sun.xml.internal.ws.util.xml.XmlUtil.allowExternalAccess; |
aoqi@0 | 76 | |
aoqi@0 | 77 | /** |
aoqi@0 | 78 | * {@link Tube} that does the schema validation. |
aoqi@0 | 79 | * |
aoqi@0 | 80 | * @author Jitendra Kotamraju |
aoqi@0 | 81 | */ |
aoqi@0 | 82 | public abstract class AbstractSchemaValidationTube extends AbstractFilterTubeImpl { |
aoqi@0 | 83 | |
aoqi@0 | 84 | private static final Logger LOGGER = Logger.getLogger(AbstractSchemaValidationTube.class.getName()); |
aoqi@0 | 85 | |
aoqi@0 | 86 | protected final WSBinding binding; |
aoqi@0 | 87 | protected final SchemaValidationFeature feature; |
aoqi@0 | 88 | protected final DocumentAddressResolver resolver = new ValidationDocumentAddressResolver(); |
aoqi@0 | 89 | protected final SchemaFactory sf; |
aoqi@0 | 90 | |
aoqi@0 | 91 | public AbstractSchemaValidationTube(WSBinding binding, Tube next) { |
aoqi@0 | 92 | super(next); |
aoqi@0 | 93 | this.binding = binding; |
aoqi@0 | 94 | feature = binding.getFeature(SchemaValidationFeature.class); |
aoqi@0 | 95 | sf = allowExternalAccess(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI), "file", false); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | protected AbstractSchemaValidationTube(AbstractSchemaValidationTube that, TubeCloner cloner) { |
aoqi@0 | 99 | super(that, cloner); |
aoqi@0 | 100 | this.binding = that.binding; |
aoqi@0 | 101 | this.feature = that.feature; |
aoqi@0 | 102 | this.sf = that.sf; |
aoqi@0 | 103 | } |
aoqi@0 | 104 | |
aoqi@0 | 105 | protected abstract Validator getValidator(); |
aoqi@0 | 106 | |
aoqi@0 | 107 | protected abstract boolean isNoValidation(); |
aoqi@0 | 108 | |
aoqi@0 | 109 | private static class ValidationDocumentAddressResolver implements DocumentAddressResolver { |
aoqi@0 | 110 | |
aoqi@0 | 111 | @Nullable |
aoqi@0 | 112 | @Override |
aoqi@0 | 113 | public String getRelativeAddressFor(@NotNull SDDocument current, @NotNull SDDocument referenced) { |
aoqi@0 | 114 | LOGGER.log(Level.FINE, "Current = {0} resolved relative={1}", new Object[]{current.getURL(), referenced.getURL()}); |
aoqi@0 | 115 | return referenced.getURL().toExternalForm(); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | private Document createDOM(SDDocument doc) { |
aoqi@0 | 120 | // Get infoset |
aoqi@0 | 121 | ByteArrayBuffer bab = new ByteArrayBuffer(); |
aoqi@0 | 122 | try { |
aoqi@0 | 123 | doc.writeTo(null, resolver, bab); |
aoqi@0 | 124 | } catch (IOException ioe) { |
aoqi@0 | 125 | throw new WebServiceException(ioe); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | // Convert infoset to DOM |
aoqi@0 | 129 | Transformer trans = XmlUtil.newTransformer(); |
aoqi@0 | 130 | Source source = new StreamSource(bab.newInputStream(), null); //doc.getURL().toExternalForm()); |
aoqi@0 | 131 | DOMResult result = new DOMResult(); |
aoqi@0 | 132 | try { |
aoqi@0 | 133 | trans.transform(source, result); |
aoqi@0 | 134 | } catch(TransformerException te) { |
aoqi@0 | 135 | throw new WebServiceException(te); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | return (Document)result.getNode(); |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | protected class MetadataResolverImpl implements SDDocumentResolver, LSResourceResolver { |
aoqi@0 | 141 | |
aoqi@0 | 142 | // systemID --> SDDocument |
aoqi@0 | 143 | final Map<String, SDDocument> docs = new HashMap<String, SDDocument>(); |
aoqi@0 | 144 | |
aoqi@0 | 145 | // targetnamespace --> SDDocument |
aoqi@0 | 146 | final Map<String, SDDocument> nsMapping = new HashMap<String, SDDocument>(); |
aoqi@0 | 147 | |
aoqi@0 | 148 | public MetadataResolverImpl() { |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | public MetadataResolverImpl(Iterable<SDDocument> it) { |
aoqi@0 | 152 | for(SDDocument doc : it) { |
aoqi@0 | 153 | if (doc.isSchema()) { |
aoqi@0 | 154 | docs.put(doc.getURL().toExternalForm(), doc); |
aoqi@0 | 155 | nsMapping.put(((SDDocument.Schema)doc).getTargetNamespace(), doc); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | } |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | void addSchema(Source schema) { |
aoqi@0 | 161 | assert schema.getSystemId() != null; |
aoqi@0 | 162 | |
aoqi@0 | 163 | String systemId = schema.getSystemId(); |
aoqi@0 | 164 | try { |
aoqi@0 | 165 | XMLStreamBufferResult xsbr = XmlUtil.identityTransform(schema, new XMLStreamBufferResult()); |
aoqi@0 | 166 | SDDocumentSource sds = SDDocumentSource.create(new URL(systemId), xsbr.getXMLStreamBuffer()); |
aoqi@0 | 167 | SDDocument sdoc = SDDocumentImpl.create(sds, new QName(""), new QName("")); |
aoqi@0 | 168 | docs.put(systemId, sdoc); |
aoqi@0 | 169 | nsMapping.put(((SDDocument.Schema)sdoc).getTargetNamespace(), sdoc); |
aoqi@0 | 170 | } catch(Exception ex) { |
aoqi@0 | 171 | LOGGER.log(Level.WARNING, "Exception in adding schemas to resolver", ex); |
aoqi@0 | 172 | } |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | void addSchemas(Collection<? extends Source> schemas) { |
aoqi@0 | 176 | for(Source src : schemas) { |
aoqi@0 | 177 | addSchema(src); |
aoqi@0 | 178 | } |
aoqi@0 | 179 | } |
aoqi@0 | 180 | |
aoqi@0 | 181 | @Override |
aoqi@0 | 182 | public SDDocument resolve(String systemId) { |
aoqi@0 | 183 | SDDocument sdi = docs.get(systemId); |
aoqi@0 | 184 | if (sdi == null) { |
aoqi@0 | 185 | SDDocumentSource sds; |
aoqi@0 | 186 | try { |
aoqi@0 | 187 | sds = SDDocumentSource.create(new URL(systemId)); |
aoqi@0 | 188 | } catch(MalformedURLException e) { |
aoqi@0 | 189 | throw new WebServiceException(e); |
aoqi@0 | 190 | } |
aoqi@0 | 191 | sdi = SDDocumentImpl.create(sds, new QName(""), new QName("")); |
aoqi@0 | 192 | docs.put(systemId, sdi); |
aoqi@0 | 193 | } |
aoqi@0 | 194 | return sdi; |
aoqi@0 | 195 | } |
aoqi@0 | 196 | |
aoqi@0 | 197 | @Override |
aoqi@0 | 198 | public LSInput resolveResource(String type, String namespaceURI, String publicId, final String systemId, final String baseURI) { |
aoqi@0 | 199 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 200 | LOGGER.log(Level.FINE, "type={0} namespaceURI={1} publicId={2} systemId={3} baseURI={4}", new Object[]{type, namespaceURI, publicId, systemId, baseURI}); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | try { |
aoqi@0 | 203 | final SDDocument doc; |
aoqi@0 | 204 | if (systemId == null) { |
aoqi@0 | 205 | doc = nsMapping.get(namespaceURI); |
aoqi@0 | 206 | } else { |
aoqi@0 | 207 | URI rel = (baseURI != null) |
aoqi@0 | 208 | ? new URI(baseURI).resolve(systemId) |
aoqi@0 | 209 | : new URI(systemId); |
aoqi@0 | 210 | doc = docs.get(rel.toString()); |
aoqi@0 | 211 | } |
aoqi@0 | 212 | if (doc != null) { |
aoqi@0 | 213 | return new LSInput() { |
aoqi@0 | 214 | |
aoqi@0 | 215 | @Override |
aoqi@0 | 216 | public Reader getCharacterStream() { |
aoqi@0 | 217 | return null; |
aoqi@0 | 218 | } |
aoqi@0 | 219 | |
aoqi@0 | 220 | @Override |
aoqi@0 | 221 | public void setCharacterStream(Reader characterStream) { |
aoqi@0 | 222 | throw new UnsupportedOperationException(); |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | @Override |
aoqi@0 | 226 | public InputStream getByteStream() { |
aoqi@0 | 227 | ByteArrayBuffer bab = new ByteArrayBuffer(); |
aoqi@0 | 228 | try { |
aoqi@0 | 229 | doc.writeTo(null, resolver, bab); |
aoqi@0 | 230 | } catch (IOException ioe) { |
aoqi@0 | 231 | throw new WebServiceException(ioe); |
aoqi@0 | 232 | } |
aoqi@0 | 233 | return bab.newInputStream(); |
aoqi@0 | 234 | } |
aoqi@0 | 235 | |
aoqi@0 | 236 | @Override |
aoqi@0 | 237 | public void setByteStream(InputStream byteStream) { |
aoqi@0 | 238 | throw new UnsupportedOperationException(); |
aoqi@0 | 239 | } |
aoqi@0 | 240 | |
aoqi@0 | 241 | @Override |
aoqi@0 | 242 | public String getStringData() { |
aoqi@0 | 243 | return null; |
aoqi@0 | 244 | } |
aoqi@0 | 245 | |
aoqi@0 | 246 | @Override |
aoqi@0 | 247 | public void setStringData(String stringData) { |
aoqi@0 | 248 | throw new UnsupportedOperationException(); |
aoqi@0 | 249 | } |
aoqi@0 | 250 | |
aoqi@0 | 251 | @Override |
aoqi@0 | 252 | public String getSystemId() { |
aoqi@0 | 253 | return doc.getURL().toExternalForm(); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | @Override |
aoqi@0 | 257 | public void setSystemId(String systemId) { |
aoqi@0 | 258 | throw new UnsupportedOperationException(); |
aoqi@0 | 259 | } |
aoqi@0 | 260 | |
aoqi@0 | 261 | @Override |
aoqi@0 | 262 | public String getPublicId() { |
aoqi@0 | 263 | return null; |
aoqi@0 | 264 | } |
aoqi@0 | 265 | |
aoqi@0 | 266 | @Override |
aoqi@0 | 267 | public void setPublicId(String publicId) { |
aoqi@0 | 268 | throw new UnsupportedOperationException(); |
aoqi@0 | 269 | } |
aoqi@0 | 270 | |
aoqi@0 | 271 | @Override |
aoqi@0 | 272 | public String getBaseURI() { |
aoqi@0 | 273 | return doc.getURL().toExternalForm(); |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
aoqi@0 | 276 | @Override |
aoqi@0 | 277 | public void setBaseURI(String baseURI) { |
aoqi@0 | 278 | throw new UnsupportedOperationException(); |
aoqi@0 | 279 | } |
aoqi@0 | 280 | |
aoqi@0 | 281 | @Override |
aoqi@0 | 282 | public String getEncoding() { |
aoqi@0 | 283 | return null; |
aoqi@0 | 284 | } |
aoqi@0 | 285 | |
aoqi@0 | 286 | @Override |
aoqi@0 | 287 | public void setEncoding(String encoding) { |
aoqi@0 | 288 | throw new UnsupportedOperationException(); |
aoqi@0 | 289 | } |
aoqi@0 | 290 | |
aoqi@0 | 291 | @Override |
aoqi@0 | 292 | public boolean getCertifiedText() { |
aoqi@0 | 293 | return false; |
aoqi@0 | 294 | } |
aoqi@0 | 295 | |
aoqi@0 | 296 | @Override |
aoqi@0 | 297 | public void setCertifiedText(boolean certifiedText) { |
aoqi@0 | 298 | throw new UnsupportedOperationException(); |
aoqi@0 | 299 | } |
aoqi@0 | 300 | }; |
aoqi@0 | 301 | } |
aoqi@0 | 302 | } catch(Exception e) { |
aoqi@0 | 303 | LOGGER.log(Level.WARNING, "Exception in LSResourceResolver impl", e); |
aoqi@0 | 304 | } |
aoqi@0 | 305 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 306 | LOGGER.log(Level.FINE, "Don''t know about systemId={0} baseURI={1}", new Object[]{systemId, baseURI}); |
aoqi@0 | 307 | } |
aoqi@0 | 308 | return null; |
aoqi@0 | 309 | } |
aoqi@0 | 310 | |
aoqi@0 | 311 | } |
aoqi@0 | 312 | |
aoqi@0 | 313 | private void updateMultiSchemaForTns(String tns, String systemId, Map<String, List<String>> schemas) { |
aoqi@0 | 314 | List<String> docIdList = schemas.get(tns); |
aoqi@0 | 315 | if (docIdList == null) { |
aoqi@0 | 316 | docIdList = new ArrayList<String>(); |
aoqi@0 | 317 | schemas.put(tns, docIdList); |
aoqi@0 | 318 | } |
aoqi@0 | 319 | docIdList.add(systemId); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | |
aoqi@0 | 322 | /* |
aoqi@0 | 323 | * Using the following algorithm described in the xerces discussion thread: |
aoqi@0 | 324 | * |
aoqi@0 | 325 | * "If you're synthesizing schema documents to glue together the ones in |
aoqi@0 | 326 | * the WSDL then you may not even need to use "honour-all-schemaLocations". |
aoqi@0 | 327 | * Create a schema document for each namespace with <xs:include>s |
aoqi@0 | 328 | * (for each schema document in the WSDL with that target namespace) |
aoqi@0 | 329 | * and then combine those together with <xs:import>s for each of those |
aoqi@0 | 330 | * namespaces in a "master" schema document. |
aoqi@0 | 331 | * |
aoqi@0 | 332 | * That should work with any schema processor, not just those which |
aoqi@0 | 333 | * honour multiple imports for the same namespace." |
aoqi@0 | 334 | */ |
aoqi@0 | 335 | protected Source[] getSchemaSources(Iterable<SDDocument> docs, MetadataResolverImpl mdresolver) { |
aoqi@0 | 336 | // All schema fragments in WSDLs are put inlinedSchemas |
aoqi@0 | 337 | // systemID --> DOMSource |
aoqi@0 | 338 | Map<String, DOMSource> inlinedSchemas = new HashMap<String, DOMSource>(); |
aoqi@0 | 339 | |
aoqi@0 | 340 | // Consolidates all the schemas(inlined and external) for a tns |
aoqi@0 | 341 | // tns --> list of systemId |
aoqi@0 | 342 | Map<String, List<String>> multiSchemaForTns = new HashMap<String, List<String>>(); |
aoqi@0 | 343 | |
aoqi@0 | 344 | for(SDDocument sdoc: docs) { |
aoqi@0 | 345 | if (sdoc.isWSDL()) { |
aoqi@0 | 346 | Document dom = createDOM(sdoc); |
aoqi@0 | 347 | // Get xsd:schema node from WSDL's DOM |
aoqi@0 | 348 | addSchemaFragmentSource(dom, sdoc.getURL().toExternalForm(), inlinedSchemas); |
aoqi@0 | 349 | } else if (sdoc.isSchema()) { |
aoqi@0 | 350 | updateMultiSchemaForTns(((SDDocument.Schema)sdoc).getTargetNamespace(), sdoc.getURL().toExternalForm(), multiSchemaForTns); |
aoqi@0 | 351 | } |
aoqi@0 | 352 | } |
aoqi@0 | 353 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 354 | LOGGER.log(Level.FINE, "WSDL inlined schema fragment documents(these are used to create a pseudo schema) = {0}", inlinedSchemas.keySet()); |
aoqi@0 | 355 | } |
aoqi@0 | 356 | for(DOMSource src: inlinedSchemas.values()) { |
aoqi@0 | 357 | String tns = getTargetNamespace(src); |
aoqi@0 | 358 | updateMultiSchemaForTns(tns, src.getSystemId(), multiSchemaForTns); |
aoqi@0 | 359 | } |
aoqi@0 | 360 | |
aoqi@0 | 361 | if (multiSchemaForTns.isEmpty()) { |
aoqi@0 | 362 | return new Source[0]; // WSDL doesn't have any schema fragments |
aoqi@0 | 363 | } else if (multiSchemaForTns.size() == 1 && multiSchemaForTns.values().iterator().next().size() == 1) { |
aoqi@0 | 364 | // It must be a inlined schema, otherwise there would be at least two schemas |
aoqi@0 | 365 | String systemId = multiSchemaForTns.values().iterator().next().get(0); |
aoqi@0 | 366 | return new Source[] {inlinedSchemas.get(systemId)}; |
aoqi@0 | 367 | } |
aoqi@0 | 368 | |
aoqi@0 | 369 | // need to resolve these inlined schema fragments |
aoqi@0 | 370 | mdresolver.addSchemas(inlinedSchemas.values()); |
aoqi@0 | 371 | |
aoqi@0 | 372 | // If there are multiple schema fragments for the same tns, create a |
aoqi@0 | 373 | // pseudo schema for that tns by using <xsd:include> of those. |
aoqi@0 | 374 | // tns --> systemId of a pseudo schema document (consolidated for that tns) |
aoqi@0 | 375 | Map<String, String> oneSchemaForTns = new HashMap<String, String>(); |
aoqi@0 | 376 | int i = 0; |
aoqi@0 | 377 | for(Map.Entry<String, List<String>> e: multiSchemaForTns.entrySet()) { |
aoqi@0 | 378 | String systemId; |
aoqi@0 | 379 | List<String> sameTnsSchemas = e.getValue(); |
aoqi@0 | 380 | if (sameTnsSchemas.size() > 1) { |
aoqi@0 | 381 | // SDDocumentSource should be changed to take String systemId |
aoqi@0 | 382 | // String pseudoSystemId = "urn:x-jax-ws-include-"+i++; |
aoqi@0 | 383 | systemId = "file:x-jax-ws-include-"+i++; |
aoqi@0 | 384 | Source src = createSameTnsPseudoSchema(e.getKey(), sameTnsSchemas, systemId); |
aoqi@0 | 385 | mdresolver.addSchema(src); |
aoqi@0 | 386 | } else { |
aoqi@0 | 387 | systemId = sameTnsSchemas.get(0); |
aoqi@0 | 388 | } |
aoqi@0 | 389 | oneSchemaForTns.put(e.getKey(), systemId); |
aoqi@0 | 390 | } |
aoqi@0 | 391 | |
aoqi@0 | 392 | // create a master pseudo schema with all the different tns |
aoqi@0 | 393 | Source pseudoSchema = createMasterPseudoSchema(oneSchemaForTns); |
aoqi@0 | 394 | return new Source[] { pseudoSchema }; |
aoqi@0 | 395 | } |
aoqi@0 | 396 | |
aoqi@0 | 397 | private @Nullable void addSchemaFragmentSource(Document doc, String systemId, Map<String, DOMSource> map) { |
aoqi@0 | 398 | Element e = doc.getDocumentElement(); |
aoqi@0 | 399 | assert e.getNamespaceURI().equals(WSDLConstants.NS_WSDL); |
aoqi@0 | 400 | assert e.getLocalName().equals("definitions"); |
aoqi@0 | 401 | |
aoqi@0 | 402 | NodeList typesList = e.getElementsByTagNameNS(WSDLConstants.NS_WSDL, "types"); |
aoqi@0 | 403 | for(int i=0; i < typesList.getLength(); i++) { |
aoqi@0 | 404 | NodeList schemaList = ((Element)typesList.item(i)).getElementsByTagNameNS(WSDLConstants.NS_XMLNS, "schema"); |
aoqi@0 | 405 | for(int j=0; j < schemaList.getLength(); j++) { |
aoqi@0 | 406 | Element elem = (Element)schemaList.item(j); |
aoqi@0 | 407 | NamespaceSupport nss = new NamespaceSupport(); |
aoqi@0 | 408 | // Doing this because transformer is not picking up inscope namespaces |
aoqi@0 | 409 | // why doesn't transformer pickup the inscope namespaces ?? |
aoqi@0 | 410 | buildNamespaceSupport(nss, elem); |
aoqi@0 | 411 | patchDOMFragment(nss, elem); |
aoqi@0 | 412 | String docId = systemId+"#schema"+j; |
aoqi@0 | 413 | map.put(docId, new DOMSource(elem, docId)); |
aoqi@0 | 414 | } |
aoqi@0 | 415 | } |
aoqi@0 | 416 | } |
aoqi@0 | 417 | |
aoqi@0 | 418 | |
aoqi@0 | 419 | /* |
aoqi@0 | 420 | * Recursively visit ancestors and build up {@link org.xml.sax.helpers.NamespaceSupport} object. |
aoqi@0 | 421 | */ |
aoqi@0 | 422 | private void buildNamespaceSupport(NamespaceSupport nss, Node node) { |
aoqi@0 | 423 | if (node==null || node.getNodeType()!=Node.ELEMENT_NODE) { |
aoqi@0 | 424 | return; |
aoqi@0 | 425 | } |
aoqi@0 | 426 | |
aoqi@0 | 427 | buildNamespaceSupport( nss, node.getParentNode() ); |
aoqi@0 | 428 | |
aoqi@0 | 429 | nss.pushContext(); |
aoqi@0 | 430 | NamedNodeMap atts = node.getAttributes(); |
aoqi@0 | 431 | for( int i=0; i<atts.getLength(); i++ ) { |
aoqi@0 | 432 | Attr a = (Attr)atts.item(i); |
aoqi@0 | 433 | if( "xmlns".equals(a.getPrefix()) ) { |
aoqi@0 | 434 | nss.declarePrefix( a.getLocalName(), a.getValue() ); |
aoqi@0 | 435 | continue; |
aoqi@0 | 436 | } |
aoqi@0 | 437 | if( "xmlns".equals(a.getName()) ) { |
aoqi@0 | 438 | nss.declarePrefix( "", a.getValue() ); |
aoqi@0 | 439 | //continue; |
aoqi@0 | 440 | } |
aoqi@0 | 441 | } |
aoqi@0 | 442 | } |
aoqi@0 | 443 | |
aoqi@0 | 444 | /** |
aoqi@0 | 445 | * Adds inscope namespaces as attributes to <xsd:schema> fragment nodes. |
aoqi@0 | 446 | * |
aoqi@0 | 447 | * @param nss namespace context info |
aoqi@0 | 448 | * @param elem that is patched with inscope namespaces |
aoqi@0 | 449 | */ |
aoqi@0 | 450 | private @Nullable void patchDOMFragment(NamespaceSupport nss, Element elem) { |
aoqi@0 | 451 | NamedNodeMap atts = elem.getAttributes(); |
aoqi@0 | 452 | for( Enumeration en = nss.getPrefixes(); en.hasMoreElements(); ) { |
aoqi@0 | 453 | String prefix = (String)en.nextElement(); |
aoqi@0 | 454 | |
aoqi@0 | 455 | for( int i=0; i<atts.getLength(); i++ ) { |
aoqi@0 | 456 | Attr a = (Attr)atts.item(i); |
aoqi@0 | 457 | if (!"xmlns".equals(a.getPrefix()) || !a.getLocalName().equals(prefix)) { |
aoqi@0 | 458 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 459 | LOGGER.log(Level.FINE, "Patching with xmlns:{0}={1}", new Object[]{prefix, nss.getURI(prefix)}); |
aoqi@0 | 460 | } |
aoqi@0 | 461 | elem.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:"+prefix, nss.getURI(prefix)); |
aoqi@0 | 462 | } |
aoqi@0 | 463 | } |
aoqi@0 | 464 | } |
aoqi@0 | 465 | } |
aoqi@0 | 466 | |
aoqi@0 | 467 | /* |
aoqi@0 | 468 | * Creates a pseudo schema for the WSDL schema fragments that have the same |
aoqi@0 | 469 | * targetNamespace. |
aoqi@0 | 470 | * |
aoqi@0 | 471 | * <xsd:schema targetNamespace="X"> |
aoqi@0 | 472 | * <xsd:include schemaLocation="Y1"/> |
aoqi@0 | 473 | * <xsd:include schemaLocation="Y2"/> |
aoqi@0 | 474 | * </xsd:schema> |
aoqi@0 | 475 | * |
aoqi@0 | 476 | * @param tns targetNamespace of the the schema documents |
aoqi@0 | 477 | * @param docs collection of systemId for the schema documents that have the |
aoqi@0 | 478 | * same tns, the collection must have more than one document |
aoqi@0 | 479 | * @param psuedoSystemId for the created pseudo schema |
aoqi@0 | 480 | * @return Source of pseudo schema that can be used multiple times |
aoqi@0 | 481 | */ |
aoqi@0 | 482 | private @Nullable Source createSameTnsPseudoSchema(String tns, Collection<String> docs, String pseudoSystemId) { |
aoqi@0 | 483 | assert docs.size() > 1; |
aoqi@0 | 484 | |
aoqi@0 | 485 | final StringBuilder sb = new StringBuilder("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'"); |
aoqi@0 | 486 | if (!tns.equals("")) { |
aoqi@0 | 487 | sb.append(" targetNamespace='").append(tns).append("'"); |
aoqi@0 | 488 | } |
aoqi@0 | 489 | sb.append(">\n"); |
aoqi@0 | 490 | for(String systemId : docs) { |
aoqi@0 | 491 | sb.append("<xsd:include schemaLocation='").append(systemId).append("'/>\n"); |
aoqi@0 | 492 | } |
aoqi@0 | 493 | sb.append("</xsd:schema>\n"); |
aoqi@0 | 494 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 495 | LOGGER.log(Level.FINE, "Pseudo Schema for the same tns={0}is {1}", new Object[]{tns, sb}); |
aoqi@0 | 496 | } |
aoqi@0 | 497 | |
aoqi@0 | 498 | // override getReader() so that the same source can be used multiple times |
aoqi@0 | 499 | return new StreamSource(pseudoSystemId) { |
aoqi@0 | 500 | @Override |
aoqi@0 | 501 | public Reader getReader() { |
aoqi@0 | 502 | return new StringReader(sb.toString()); |
aoqi@0 | 503 | } |
aoqi@0 | 504 | }; |
aoqi@0 | 505 | } |
aoqi@0 | 506 | |
aoqi@0 | 507 | /* |
aoqi@0 | 508 | * Creates a master pseudo schema importing all WSDL schema fragments with |
aoqi@0 | 509 | * different tns+pseudo schema for same tns. |
aoqi@0 | 510 | * <xsd:schema targetNamespace="urn:x-jax-ws-master"> |
aoqi@0 | 511 | * <xsd:import schemaLocation="Y1" namespace="X1"/> |
aoqi@0 | 512 | * <xsd:import schemaLocation="Y2" namespace="X2"/> |
aoqi@0 | 513 | * </xsd:schema> |
aoqi@0 | 514 | * |
aoqi@0 | 515 | * @param pseudo a map(tns-->systemId) of schema documents |
aoqi@0 | 516 | * @return Source of pseudo schema that can be used multiple times |
aoqi@0 | 517 | */ |
aoqi@0 | 518 | private Source createMasterPseudoSchema(Map<String, String> docs) { |
aoqi@0 | 519 | final StringBuilder sb = new StringBuilder("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' targetNamespace='urn:x-jax-ws-master'>\n"); |
aoqi@0 | 520 | for(Map.Entry<String, String> e : docs.entrySet()) { |
aoqi@0 | 521 | String systemId = e.getValue(); |
aoqi@0 | 522 | String ns = e.getKey(); |
aoqi@0 | 523 | sb.append("<xsd:import schemaLocation='").append(systemId).append("'"); |
aoqi@0 | 524 | if (!ns.equals("")) { |
aoqi@0 | 525 | sb.append(" namespace='").append(ns).append("'"); |
aoqi@0 | 526 | } |
aoqi@0 | 527 | sb.append("/>\n"); |
aoqi@0 | 528 | } |
aoqi@0 | 529 | sb.append("</xsd:schema>"); |
aoqi@0 | 530 | if (LOGGER.isLoggable(Level.FINE)) { |
aoqi@0 | 531 | LOGGER.log(Level.FINE, "Master Pseudo Schema = {0}", sb); |
aoqi@0 | 532 | } |
aoqi@0 | 533 | |
aoqi@0 | 534 | // override getReader() so that the same source can be used multiple times |
aoqi@0 | 535 | return new StreamSource("file:x-jax-ws-master-doc") { |
aoqi@0 | 536 | @Override |
aoqi@0 | 537 | public Reader getReader() { |
aoqi@0 | 538 | return new StringReader(sb.toString()); |
aoqi@0 | 539 | } |
aoqi@0 | 540 | }; |
aoqi@0 | 541 | } |
aoqi@0 | 542 | |
aoqi@0 | 543 | protected void doProcess(Packet packet) throws SAXException { |
aoqi@0 | 544 | getValidator().reset(); |
aoqi@0 | 545 | Class<? extends ValidationErrorHandler> handlerClass = feature.getErrorHandler(); |
aoqi@0 | 546 | ValidationErrorHandler handler; |
aoqi@0 | 547 | try { |
aoqi@0 | 548 | handler = handlerClass.newInstance(); |
aoqi@0 | 549 | } catch(Exception e) { |
aoqi@0 | 550 | throw new WebServiceException(e); |
aoqi@0 | 551 | } |
aoqi@0 | 552 | handler.setPacket(packet); |
aoqi@0 | 553 | getValidator().setErrorHandler(handler); |
aoqi@0 | 554 | Message msg = packet.getMessage().copy(); |
aoqi@0 | 555 | Source source = msg.readPayloadAsSource(); |
aoqi@0 | 556 | try { |
aoqi@0 | 557 | // Validator javadoc allows ONLY SAX, and DOM Sources |
aoqi@0 | 558 | // But the impl seems to handle all kinds. |
aoqi@0 | 559 | getValidator().validate(source); |
aoqi@0 | 560 | } catch(IOException e) { |
aoqi@0 | 561 | throw new WebServiceException(e); |
aoqi@0 | 562 | } |
aoqi@0 | 563 | } |
aoqi@0 | 564 | |
aoqi@0 | 565 | private String getTargetNamespace(DOMSource src) { |
aoqi@0 | 566 | Element elem = (Element)src.getNode(); |
aoqi@0 | 567 | return elem.getAttribute("targetNamespace"); |
aoqi@0 | 568 | } |
aoqi@0 | 569 | |
aoqi@0 | 570 | // protected static void printSource(Source src) { |
aoqi@0 | 571 | // try { |
aoqi@0 | 572 | // ByteArrayBuffer bos = new ByteArrayBuffer(); |
aoqi@0 | 573 | // StreamResult sr = new StreamResult(bos ); |
aoqi@0 | 574 | // Transformer trans = TransformerFactory.newInstance().newTransformer(); |
aoqi@0 | 575 | // trans.transform(src, sr); |
aoqi@0 | 576 | // LOGGER.info("**** src ******"+bos.toString()); |
aoqi@0 | 577 | // bos.close(); |
aoqi@0 | 578 | // } catch(Exception e) { |
aoqi@0 | 579 | // e.printStackTrace(); |
aoqi@0 | 580 | // } |
aoqi@0 | 581 | // } |
aoqi@0 | 582 | |
aoqi@0 | 583 | } |