src/share/jaxws_classes/com/sun/xml/internal/ws/addressing/EPRSDDocumentFilter.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, 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.addressing;
ohair@286 27
ohair@286 28 import com.sun.xml.internal.ws.api.server.*;
ohair@286 29 import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
ohair@286 30 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
ohair@286 31 import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
ohair@286 32 import com.sun.xml.internal.ws.util.xml.XMLStreamWriterFilter;
ohair@286 33 import com.sun.xml.internal.ws.util.xml.XMLStreamReaderToXMLStreamWriter;
ohair@286 34 import com.sun.xml.internal.ws.server.WSEndpointImpl;
ohair@286 35 import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
ohair@286 36 import com.sun.istack.internal.Nullable;
ohair@286 37 import com.sun.istack.internal.NotNull;
ohair@286 38
ohair@286 39 import javax.xml.stream.XMLStreamWriter;
ohair@286 40 import javax.xml.stream.XMLStreamException;
ohair@286 41 import javax.xml.stream.XMLStreamReader;
ohair@286 42 import javax.xml.namespace.NamespaceContext;
ohair@286 43 import java.io.IOException;
ohair@286 44 import java.util.List;
ohair@286 45 import java.util.Collection;
ohair@286 46 import java.util.Collections;
ohair@286 47
ohair@286 48 /**
ohair@286 49 * This class acts as a filter for the Extension elements in the wsa:EndpointReference in the wsdl.
ohair@286 50 * In addition to filtering the EPR extensions from WSDL, it adds the extensions configured by the JAX-WS runtime
ohair@286 51 * specifc to an endpoint.
ohair@286 52 *
ohair@286 53 * @author Rama Pulavarthi
ohair@286 54 */
ohair@286 55 public class EPRSDDocumentFilter implements SDDocumentFilter {
ohair@286 56 private final WSEndpointImpl<?> endpoint;
ohair@286 57 //initialize lazily
ohair@286 58 List<BoundEndpoint> beList;
ohair@286 59 public EPRSDDocumentFilter(@NotNull WSEndpointImpl<?> endpoint) {
ohair@286 60 this.endpoint = endpoint;
ohair@286 61 }
ohair@286 62
ohair@286 63 private @Nullable WSEndpointImpl<?> getEndpoint(String serviceName, String portName) {
ohair@286 64 if (serviceName == null || portName == null)
ohair@286 65 return null;
ohair@286 66 if (endpoint.getServiceName().getLocalPart().equals(serviceName) && endpoint.getPortName().getLocalPart().equals(portName))
ohair@286 67 return endpoint;
ohair@286 68
ohair@286 69 if(beList == null) {
ohair@286 70 //check if it is run in a Java EE Container and get hold of other endpoints in the application
ohair@286 71 Module module = endpoint.getContainer().getSPI(Module.class);
ohair@286 72 if (module != null) {
ohair@286 73 beList = module.getBoundEndpoints();
ohair@286 74 } else {
ohair@286 75 beList = Collections.<BoundEndpoint>emptyList();
ohair@286 76 }
ohair@286 77 }
ohair@286 78
ohair@286 79 for (BoundEndpoint be : beList) {
ohair@286 80 WSEndpoint wse = be.getEndpoint();
ohair@286 81 if (wse.getServiceName().getLocalPart().equals(serviceName) && wse.getPortName().getLocalPart().equals(portName)) {
ohair@286 82 return (WSEndpointImpl) wse;
ohair@286 83 }
ohair@286 84 }
ohair@286 85
ohair@286 86 return null;
ohair@286 87
ohair@286 88 }
ohair@286 89
ohair@286 90 public XMLStreamWriter filter(SDDocument doc, XMLStreamWriter w) throws XMLStreamException, IOException {
ohair@286 91 if (!doc.isWSDL()) {
ohair@286 92 return w;
ohair@286 93 }
ohair@286 94
ohair@286 95 return new XMLStreamWriterFilter(w) {
ohair@286 96 private boolean eprExtnFilterON = false; //when true, all writer events are filtered out
ohair@286 97
ohair@286 98 private boolean portHasEPR = false;
ohair@286 99 private int eprDepth = -1; // -1 -> outside wsa:epr, 0 -> on wsa:epr start/end , > 0 inside wsa:epr
ohair@286 100
ohair@286 101 private String serviceName = null; //non null when inside wsdl:service scope
ohair@286 102 private boolean onService = false; //flag to get service name when on wsdl:service element start
ohair@286 103 private int serviceDepth = -1; // -1 -> outside wsdl:service, 0 -> on wsdl:service start/end , > 0 inside wsdl:service
ohair@286 104
ohair@286 105 private String portName = null; //non null when inside wsdl:port scope
ohair@286 106 private boolean onPort = false; //flag to get port name when on wsdl:port element start
ohair@286 107 private int portDepth = -1; // -1 -> outside wsdl:port, 0 -> on wsdl:port start/end , > 0 inside wsdl:port
ohair@286 108
ohair@286 109 private String portAddress; // when a complete epr is written, endpoint address is used as epr address
ohair@286 110 private boolean onPortAddress = false; //flag to get endpoint address when on soap:address element start
ohair@286 111
ohair@286 112 private void handleStartElement(String localName, String namespaceURI) throws XMLStreamException {
ohair@286 113 resetOnElementFlags();
ohair@286 114 if (serviceDepth >= 0) {
ohair@286 115 serviceDepth++;
ohair@286 116 }
ohair@286 117 if (portDepth >= 0) {
ohair@286 118 portDepth++;
ohair@286 119 }
ohair@286 120 if (eprDepth >= 0) {
ohair@286 121 eprDepth++;
ohair@286 122 }
ohair@286 123
ohair@286 124 if (namespaceURI.equals(WSDLConstants.QNAME_SERVICE.getNamespaceURI()) && localName.equals(WSDLConstants.QNAME_SERVICE.getLocalPart())) {
ohair@286 125 onService = true;
ohair@286 126 serviceDepth = 0;
ohair@286 127 } else if (namespaceURI.equals(WSDLConstants.QNAME_PORT.getNamespaceURI()) && localName.equals(WSDLConstants.QNAME_PORT.getLocalPart())) {
ohair@286 128 if (serviceDepth >= 1) {
ohair@286 129 onPort = true;
ohair@286 130 portDepth = 0;
ohair@286 131 }
ohair@286 132 } else if (namespaceURI.equals(W3CAddressingConstants.WSA_NAMESPACE_NAME) && localName.equals("EndpointReference")) {
ohair@286 133 if (serviceDepth >= 1 && portDepth >= 1) {
ohair@286 134 portHasEPR = true;
ohair@286 135 eprDepth = 0;
ohair@286 136 }
ohair@286 137 } else if ((namespaceURI.equals(WSDLConstants.NS_SOAP_BINDING_ADDRESS.getNamespaceURI()) || namespaceURI.equals(WSDLConstants.NS_SOAP12_BINDING_ADDRESS.getNamespaceURI()))
ohair@286 138 && localName.equals("address") && portDepth ==1) {
ohair@286 139 onPortAddress = true;
ohair@286 140 }
ohair@286 141 WSEndpoint endpoint = getEndpoint(serviceName,portName);
ohair@286 142 //filter epr for only for the port corresponding to this endpoint
ohair@286 143 //if (service.getLocalPart().equals(serviceName) && port.getLocalPart().equals(portName)) {
ohair@286 144 if ( endpoint != null) {
ohair@286 145 if ((eprDepth == 1) && !namespaceURI.equals(W3CAddressingConstants.WSA_NAMESPACE_NAME)) {
ohair@286 146 //epr extension element
ohair@286 147 eprExtnFilterON = true;
ohair@286 148
ohair@286 149 }
ohair@286 150
ohair@286 151 /*
ohair@286 152 if (eprExtnFilterON) {
ohair@286 153 writeEPRExtensions();
ohair@286 154 }
ohair@286 155 */
ohair@286 156 }
ohair@286 157 }
ohair@286 158
ohair@286 159 private void resetOnElementFlags() {
ohair@286 160 if (onService) {
ohair@286 161 onService = false;
ohair@286 162 }
ohair@286 163 if (onPort) {
ohair@286 164 onPort = false;
ohair@286 165 }
ohair@286 166 if (onPortAddress) {
ohair@286 167 onPortAddress = false;
ohair@286 168 }
ohair@286 169
ohair@286 170 }
ohair@286 171
ohair@286 172
ohair@286 173 private void writeEPRExtensions(Collection<WSEndpointReference.EPRExtension> eprExtns) throws XMLStreamException {
ohair@286 174 if (eprExtns != null) {
ohair@286 175 for (WSEndpointReference.EPRExtension e : eprExtns) {
ohair@286 176 XMLStreamReaderToXMLStreamWriter c = new XMLStreamReaderToXMLStreamWriter();
ohair@286 177 XMLStreamReader r = e.readAsXMLStreamReader();
ohair@286 178 c.bridge(r, writer);
ohair@286 179 XMLStreamReaderFactory.recycle(r);
ohair@286 180 }
ohair@286 181 }
ohair@286 182 }
ohair@286 183
ohair@286 184 @Override
ohair@286 185 public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
ohair@286 186 handleStartElement(localName, namespaceURI);
ohair@286 187 if (!eprExtnFilterON) {
ohair@286 188 super.writeStartElement(prefix, localName, namespaceURI);
ohair@286 189 }
ohair@286 190 }
ohair@286 191
ohair@286 192 @Override
ohair@286 193 public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
ohair@286 194 handleStartElement(localName, namespaceURI);
ohair@286 195 if (!eprExtnFilterON) {
ohair@286 196 super.writeStartElement(namespaceURI, localName);
ohair@286 197 }
ohair@286 198 }
ohair@286 199
ohair@286 200 @Override
ohair@286 201 public void writeStartElement(String localName) throws XMLStreamException {
ohair@286 202 if (!eprExtnFilterON) {
ohair@286 203 super.writeStartElement(localName);
ohair@286 204 }
ohair@286 205 }
ohair@286 206
ohair@286 207 private void handleEndElement() throws XMLStreamException {
ohair@286 208 resetOnElementFlags();
ohair@286 209 //End of wsdl:port, write complete EPR if not present.
ohair@286 210 if (portDepth == 0) {
ohair@286 211
ohair@286 212 if (!portHasEPR && getEndpoint(serviceName,portName) != null) {
ohair@286 213
ohair@286 214 //write the complete EPR with address.
ohair@286 215 writer.writeStartElement(AddressingVersion.W3C.getPrefix(),"EndpointReference", AddressingVersion.W3C.nsUri );
ohair@286 216 writer.writeNamespace(AddressingVersion.W3C.getPrefix(), AddressingVersion.W3C.nsUri);
ohair@286 217 writer.writeStartElement(AddressingVersion.W3C.getPrefix(), AddressingVersion.W3C.eprType.address, AddressingVersion.W3C.nsUri);
ohair@286 218 writer.writeCharacters(portAddress);
ohair@286 219 writer.writeEndElement();
ohair@286 220 writeEPRExtensions(getEndpoint(serviceName, portName).getEndpointReferenceExtensions());
ohair@286 221 writer.writeEndElement();
ohair@286 222
ohair@286 223 }
ohair@286 224 }
ohair@286 225 //End of wsa:EndpointReference, write EPR extension elements
ohair@286 226 if (eprDepth == 0) {
ohair@286 227 if (portHasEPR && getEndpoint(serviceName,portName) != null) {
ohair@286 228 writeEPRExtensions(getEndpoint(serviceName, portName).getEndpointReferenceExtensions());
ohair@286 229 }
ohair@286 230 eprExtnFilterON = false;
ohair@286 231 }
ohair@286 232
ohair@286 233 if(serviceDepth >= 0 ) {
ohair@286 234 serviceDepth--;
ohair@286 235 }
ohair@286 236 if(portDepth >= 0) {
ohair@286 237 portDepth--;
ohair@286 238 }
ohair@286 239 if(eprDepth >=0) {
ohair@286 240 eprDepth--;
ohair@286 241 }
ohair@286 242
ohair@286 243 if (serviceDepth == -1) {
ohair@286 244 serviceName = null;
ohair@286 245 }
ohair@286 246 if (portDepth == -1) {
ohair@286 247 portHasEPR = false;
ohair@286 248 portAddress = null;
ohair@286 249 portName = null;
ohair@286 250 }
ohair@286 251 }
ohair@286 252
ohair@286 253 @Override
ohair@286 254 public void writeEndElement() throws XMLStreamException {
ohair@286 255 handleEndElement();
ohair@286 256 if (!eprExtnFilterON) {
ohair@286 257 super.writeEndElement();
ohair@286 258 }
ohair@286 259 }
ohair@286 260
ohair@286 261 private void handleAttribute(String localName, String value) {
ohair@286 262 if (localName.equals("name")) {
ohair@286 263 if (onService) {
ohair@286 264 serviceName = value;
ohair@286 265 onService = false;
ohair@286 266 } else if (onPort) {
ohair@286 267 portName = value;
ohair@286 268 onPort = false;
ohair@286 269 }
ohair@286 270 }
ohair@286 271 if (localName.equals("location") && onPortAddress) {
ohair@286 272 portAddress = value;
ohair@286 273 }
ohair@286 274
ohair@286 275
ohair@286 276 }
ohair@286 277
ohair@286 278 @Override
ohair@286 279 public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
ohair@286 280 handleAttribute(localName, value);
ohair@286 281 if (!eprExtnFilterON) {
ohair@286 282 super.writeAttribute(prefix, namespaceURI, localName, value);
ohair@286 283 }
ohair@286 284 }
ohair@286 285
ohair@286 286 @Override
ohair@286 287 public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
ohair@286 288 handleAttribute(localName, value);
ohair@286 289 if (!eprExtnFilterON) {
ohair@286 290 super.writeAttribute(namespaceURI, localName, value);
ohair@286 291 }
ohair@286 292 }
ohair@286 293
ohair@286 294 @Override
ohair@286 295 public void writeAttribute(String localName, String value) throws XMLStreamException {
ohair@286 296 handleAttribute(localName, value);
ohair@286 297 if (!eprExtnFilterON) {
ohair@286 298 super.writeAttribute(localName, value);
ohair@286 299 }
ohair@286 300 }
ohair@286 301
ohair@286 302
ohair@286 303 @Override
ohair@286 304 public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
ohair@286 305 if (!eprExtnFilterON) {
ohair@286 306 super.writeEmptyElement(namespaceURI, localName);
ohair@286 307 }
ohair@286 308 }
ohair@286 309
ohair@286 310 @Override
ohair@286 311 public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
ohair@286 312 if (!eprExtnFilterON) {
ohair@286 313 super.writeNamespace(prefix, namespaceURI);
ohair@286 314 }
ohair@286 315 }
ohair@286 316
ohair@286 317 @Override
ohair@286 318 public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
ohair@286 319 if (!eprExtnFilterON) {
ohair@286 320 super.setNamespaceContext(context);
ohair@286 321 }
ohair@286 322 }
ohair@286 323
ohair@286 324 @Override
ohair@286 325 public void setDefaultNamespace(String uri) throws XMLStreamException {
ohair@286 326 if (!eprExtnFilterON) {
ohair@286 327 super.setDefaultNamespace(uri);
ohair@286 328 }
ohair@286 329 }
ohair@286 330
ohair@286 331 @Override
ohair@286 332 public void setPrefix(String prefix, String uri) throws XMLStreamException {
ohair@286 333 if (!eprExtnFilterON) {
ohair@286 334 super.setPrefix(prefix, uri);
ohair@286 335 }
ohair@286 336 }
ohair@286 337
ohair@286 338 @Override
ohair@286 339 public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
ohair@286 340 if (!eprExtnFilterON) {
ohair@286 341 super.writeProcessingInstruction(target, data);
ohair@286 342 }
ohair@286 343 }
ohair@286 344
ohair@286 345 @Override
ohair@286 346 public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
ohair@286 347 if (!eprExtnFilterON) {
ohair@286 348 super.writeEmptyElement(prefix, localName, namespaceURI);
ohair@286 349 }
ohair@286 350 }
ohair@286 351
ohair@286 352 @Override
ohair@286 353 public void writeCData(String data) throws XMLStreamException {
ohair@286 354 if (!eprExtnFilterON) {
ohair@286 355 super.writeCData(data);
ohair@286 356 }
ohair@286 357 }
ohair@286 358
ohair@286 359 @Override
ohair@286 360 public void writeCharacters(String text) throws XMLStreamException {
ohair@286 361 if (!eprExtnFilterON) {
ohair@286 362 super.writeCharacters(text);
ohair@286 363 }
ohair@286 364 }
ohair@286 365
ohair@286 366 @Override
ohair@286 367 public void writeComment(String data) throws XMLStreamException {
ohair@286 368 if (!eprExtnFilterON) {
ohair@286 369 super.writeComment(data);
ohair@286 370 }
ohair@286 371 }
ohair@286 372
ohair@286 373 @Override
ohair@286 374 public void writeDTD(String dtd) throws XMLStreamException {
ohair@286 375 if (!eprExtnFilterON) {
ohair@286 376 super.writeDTD(dtd);
ohair@286 377 }
ohair@286 378 }
ohair@286 379
ohair@286 380 @Override
ohair@286 381 public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
ohair@286 382 if (!eprExtnFilterON) {
ohair@286 383 super.writeDefaultNamespace(namespaceURI);
ohair@286 384 }
ohair@286 385 }
ohair@286 386
ohair@286 387 @Override
ohair@286 388 public void writeEmptyElement(String localName) throws XMLStreamException {
ohair@286 389 if (!eprExtnFilterON) {
ohair@286 390 super.writeEmptyElement(localName);
ohair@286 391 }
ohair@286 392 }
ohair@286 393
ohair@286 394 @Override
ohair@286 395 public void writeEntityRef(String name) throws XMLStreamException {
ohair@286 396 if (!eprExtnFilterON) {
ohair@286 397 super.writeEntityRef(name);
ohair@286 398 }
ohair@286 399 }
ohair@286 400
ohair@286 401 @Override
ohair@286 402 public void writeProcessingInstruction(String target) throws XMLStreamException {
ohair@286 403 if (!eprExtnFilterON) {
ohair@286 404 super.writeProcessingInstruction(target);
ohair@286 405 }
ohair@286 406 }
ohair@286 407
ohair@286 408
ohair@286 409 @Override
ohair@286 410 public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {
ohair@286 411 if (!eprExtnFilterON) {
ohair@286 412 super.writeCharacters(text, start, len);
ohair@286 413 }
ohair@286 414 }
ohair@286 415
ohair@286 416 };
ohair@286 417
ohair@286 418 }
ohair@286 419
ohair@286 420 }

mercurial