325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.addressing;
325N/A
325N/Aimport com.sun.xml.internal.ws.api.server.*;
325N/Aimport com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
325N/Aimport com.sun.xml.internal.ws.api.addressing.AddressingVersion;
325N/Aimport com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
325N/Aimport com.sun.xml.internal.ws.util.xml.XMLStreamWriterFilter;
325N/Aimport com.sun.xml.internal.ws.util.xml.XMLStreamReaderToXMLStreamWriter;
325N/Aimport com.sun.xml.internal.ws.server.WSEndpointImpl;
325N/Aimport com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
325N/Aimport com.sun.istack.internal.Nullable;
325N/Aimport com.sun.istack.internal.NotNull;
325N/A
325N/Aimport javax.xml.stream.XMLStreamWriter;
325N/Aimport javax.xml.stream.XMLStreamException;
325N/Aimport javax.xml.stream.XMLStreamReader;
325N/Aimport javax.xml.namespace.NamespaceContext;
325N/Aimport java.io.IOException;
325N/Aimport java.util.List;
325N/Aimport java.util.Collection;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.Collections;
325N/A
325N/A/**
325N/A * This class acts as a filter for the Extension elements in the wsa:EndpointReference in the wsdl.
325N/A * In addition to filtering the EPR extensions from WSDL, it adds the extensions configured by the JAX-WS runtime
325N/A * specifc to an endpoint.
325N/A *
325N/A * @author Rama Pulavarthi
325N/A */
325N/Apublic class EPRSDDocumentFilter implements SDDocumentFilter {
325N/A private final WSEndpointImpl<?> endpoint;
325N/A //initialize lazily
325N/A List<BoundEndpoint> beList;
325N/A public EPRSDDocumentFilter(@NotNull WSEndpointImpl<?> endpoint) {
325N/A this.endpoint = endpoint;
325N/A }
325N/A
325N/A private @Nullable WSEndpointImpl<?> getEndpoint(String serviceName, String portName) {
325N/A if (serviceName == null || portName == null)
325N/A return null;
325N/A if (endpoint.getServiceName().getLocalPart().equals(serviceName) && endpoint.getPortName().getLocalPart().equals(portName))
325N/A return endpoint;
325N/A
325N/A if(beList == null) {
325N/A //check if it is run in a Java EE Container and get hold of other endpoints in the application
325N/A Module module = endpoint.getContainer().getSPI(Module.class);
325N/A if (module != null) {
325N/A beList = module.getBoundEndpoints();
325N/A } else {
325N/A beList = Collections.<BoundEndpoint>emptyList();
325N/A }
325N/A }
325N/A
325N/A for (BoundEndpoint be : beList) {
325N/A WSEndpoint wse = be.getEndpoint();
325N/A if (wse.getServiceName().getLocalPart().equals(serviceName) && wse.getPortName().getLocalPart().equals(portName)) {
325N/A return (WSEndpointImpl) wse;
325N/A }
325N/A }
325N/A
325N/A return null;
325N/A
325N/A }
325N/A
325N/A public XMLStreamWriter filter(SDDocument doc, XMLStreamWriter w) throws XMLStreamException, IOException {
325N/A if (!doc.isWSDL()) {
325N/A return w;
325N/A }
325N/A
325N/A return new XMLStreamWriterFilter(w) {
325N/A private boolean eprExtnFilterON = false; //when true, all writer events are filtered out
325N/A
325N/A private boolean portHasEPR = false;
325N/A private int eprDepth = -1; // -1 -> outside wsa:epr, 0 -> on wsa:epr start/end , > 0 inside wsa:epr
325N/A
325N/A private String serviceName = null; //non null when inside wsdl:service scope
325N/A private boolean onService = false; //flag to get service name when on wsdl:service element start
325N/A private int serviceDepth = -1; // -1 -> outside wsdl:service, 0 -> on wsdl:service start/end , > 0 inside wsdl:service
325N/A
325N/A private String portName = null; //non null when inside wsdl:port scope
325N/A private boolean onPort = false; //flag to get port name when on wsdl:port element start
325N/A private int portDepth = -1; // -1 -> outside wsdl:port, 0 -> on wsdl:port start/end , > 0 inside wsdl:port
325N/A
325N/A private String portAddress; // when a complete epr is written, endpoint address is used as epr address
325N/A private boolean onPortAddress = false; //flag to get endpoint address when on soap:address element start
325N/A
325N/A private void handleStartElement(String localName, String namespaceURI) throws XMLStreamException {
325N/A resetOnElementFlags();
325N/A if (serviceDepth >= 0) {
325N/A serviceDepth++;
325N/A }
325N/A if (portDepth >= 0) {
325N/A portDepth++;
325N/A }
325N/A if (eprDepth >= 0) {
325N/A eprDepth++;
325N/A }
325N/A
325N/A if (namespaceURI.equals(WSDLConstants.QNAME_SERVICE.getNamespaceURI()) && localName.equals(WSDLConstants.QNAME_SERVICE.getLocalPart())) {
325N/A onService = true;
325N/A serviceDepth = 0;
325N/A } else if (namespaceURI.equals(WSDLConstants.QNAME_PORT.getNamespaceURI()) && localName.equals(WSDLConstants.QNAME_PORT.getLocalPart())) {
325N/A if (serviceDepth >= 1) {
325N/A onPort = true;
325N/A portDepth = 0;
325N/A }
325N/A } else if (namespaceURI.equals(W3CAddressingConstants.WSA_NAMESPACE_NAME) && localName.equals("EndpointReference")) {
325N/A if (serviceDepth >= 1 && portDepth >= 1) {
325N/A portHasEPR = true;
325N/A eprDepth = 0;
325N/A }
325N/A } else if ((namespaceURI.equals(WSDLConstants.NS_SOAP_BINDING_ADDRESS.getNamespaceURI()) || namespaceURI.equals(WSDLConstants.NS_SOAP12_BINDING_ADDRESS.getNamespaceURI()))
325N/A && localName.equals("address") && portDepth ==1) {
325N/A onPortAddress = true;
325N/A }
325N/A WSEndpoint endpoint = getEndpoint(serviceName,portName);
325N/A //filter epr for only for the port corresponding to this endpoint
325N/A //if (service.getLocalPart().equals(serviceName) && port.getLocalPart().equals(portName)) {
325N/A if ( endpoint != null) {
325N/A if ((eprDepth == 1) && !namespaceURI.equals(W3CAddressingConstants.WSA_NAMESPACE_NAME)) {
325N/A //epr extension element
325N/A eprExtnFilterON = true;
325N/A
325N/A }
325N/A
325N/A /*
325N/A if (eprExtnFilterON) {
325N/A writeEPRExtensions();
325N/A }
325N/A */
325N/A }
325N/A }
325N/A
325N/A private void resetOnElementFlags() {
325N/A if (onService) {
325N/A onService = false;
325N/A }
325N/A if (onPort) {
325N/A onPort = false;
325N/A }
325N/A if (onPortAddress) {
325N/A onPortAddress = false;
325N/A }
325N/A
325N/A }
325N/A
325N/A
325N/A private void writeEPRExtensions(Collection<WSEndpointReference.EPRExtension> eprExtns) throws XMLStreamException {
325N/A if (eprExtns != null) {
325N/A for (WSEndpointReference.EPRExtension e : eprExtns) {
325N/A XMLStreamReaderToXMLStreamWriter c = new XMLStreamReaderToXMLStreamWriter();
325N/A XMLStreamReader r = e.readAsXMLStreamReader();
325N/A c.bridge(r, writer);
325N/A XMLStreamReaderFactory.recycle(r);
325N/A }
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
325N/A handleStartElement(localName, namespaceURI);
325N/A if (!eprExtnFilterON) {
325N/A super.writeStartElement(prefix, localName, namespaceURI);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
325N/A handleStartElement(localName, namespaceURI);
325N/A if (!eprExtnFilterON) {
325N/A super.writeStartElement(namespaceURI, localName);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeStartElement(String localName) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeStartElement(localName);
325N/A }
325N/A }
325N/A
325N/A private void handleEndElement() throws XMLStreamException {
325N/A resetOnElementFlags();
325N/A //End of wsdl:port, write complete EPR if not present.
325N/A if (portDepth == 0) {
325N/A
325N/A if (!portHasEPR && getEndpoint(serviceName,portName) != null) {
325N/A
325N/A //write the complete EPR with address.
325N/A writer.writeStartElement(AddressingVersion.W3C.getPrefix(),"EndpointReference", AddressingVersion.W3C.nsUri );
325N/A writer.writeNamespace(AddressingVersion.W3C.getPrefix(), AddressingVersion.W3C.nsUri);
325N/A writer.writeStartElement(AddressingVersion.W3C.getPrefix(), AddressingVersion.W3C.eprType.address, AddressingVersion.W3C.nsUri);
325N/A writer.writeCharacters(portAddress);
325N/A writer.writeEndElement();
325N/A writeEPRExtensions(getEndpoint(serviceName, portName).getEndpointReferenceExtensions());
325N/A writer.writeEndElement();
325N/A
325N/A }
325N/A }
325N/A //End of wsa:EndpointReference, write EPR extension elements
325N/A if (eprDepth == 0) {
325N/A if (portHasEPR && getEndpoint(serviceName,portName) != null) {
325N/A writeEPRExtensions(getEndpoint(serviceName, portName).getEndpointReferenceExtensions());
325N/A }
325N/A eprExtnFilterON = false;
325N/A }
325N/A
325N/A if(serviceDepth >= 0 ) {
325N/A serviceDepth--;
325N/A }
325N/A if(portDepth >= 0) {
325N/A portDepth--;
325N/A }
325N/A if(eprDepth >=0) {
325N/A eprDepth--;
325N/A }
325N/A
325N/A if (serviceDepth == -1) {
325N/A serviceName = null;
325N/A }
325N/A if (portDepth == -1) {
325N/A portHasEPR = false;
325N/A portAddress = null;
325N/A portName = null;
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeEndElement() throws XMLStreamException {
325N/A handleEndElement();
325N/A if (!eprExtnFilterON) {
325N/A super.writeEndElement();
325N/A }
325N/A }
325N/A
325N/A private void handleAttribute(String localName, String value) {
325N/A if (localName.equals("name")) {
325N/A if (onService) {
325N/A serviceName = value;
325N/A onService = false;
325N/A } else if (onPort) {
325N/A portName = value;
325N/A onPort = false;
325N/A }
325N/A }
325N/A if (localName.equals("location") && onPortAddress) {
325N/A portAddress = value;
325N/A }
325N/A
325N/A
325N/A }
325N/A
325N/A @Override
325N/A public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
325N/A handleAttribute(localName, value);
325N/A if (!eprExtnFilterON) {
325N/A super.writeAttribute(prefix, namespaceURI, localName, value);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
325N/A handleAttribute(localName, value);
325N/A if (!eprExtnFilterON) {
325N/A super.writeAttribute(namespaceURI, localName, value);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeAttribute(String localName, String value) throws XMLStreamException {
325N/A handleAttribute(localName, value);
325N/A if (!eprExtnFilterON) {
325N/A super.writeAttribute(localName, value);
325N/A }
325N/A }
325N/A
325N/A
325N/A @Override
325N/A public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeEmptyElement(namespaceURI, localName);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeNamespace(prefix, namespaceURI);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.setNamespaceContext(context);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void setDefaultNamespace(String uri) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.setDefaultNamespace(uri);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void setPrefix(String prefix, String uri) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.setPrefix(prefix, uri);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeProcessingInstruction(target, data);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeEmptyElement(prefix, localName, namespaceURI);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeCData(String data) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeCData(data);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeCharacters(String text) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeCharacters(text);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeComment(String data) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeComment(data);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeDTD(String dtd) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeDTD(dtd);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeDefaultNamespace(namespaceURI);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeEmptyElement(String localName) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeEmptyElement(localName);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeEntityRef(String name) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeEntityRef(name);
325N/A }
325N/A }
325N/A
325N/A @Override
325N/A public void writeProcessingInstruction(String target) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeProcessingInstruction(target);
325N/A }
325N/A }
325N/A
325N/A
325N/A @Override
325N/A public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {
325N/A if (!eprExtnFilterON) {
325N/A super.writeCharacters(text, start, len);
325N/A }
325N/A }
325N/A
325N/A };
325N/A
325N/A }
325N/A
325N/A}