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