src/share/jaxws_classes/com/sun/xml/internal/ws/util/pipe/AbstractSchemaValidationTube.java

Fri, 23 Aug 2013 09:57:21 +0100

author
mkos
date
Fri, 23 Aug 2013 09:57:21 +0100
changeset 397
b99d7e355d4b
parent 368
0989ad8c0860
child 408
b0610cd08440
permissions
-rw-r--r--

8022885: Update JAX-WS RI integration to 2.2.9-b14140
8013016: Rebase 8009009 against the latest jdk8/jaxws
Reviewed-by: alanb, chegar

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

mercurial