use of com.sun.xml.ws.util.xml.XMLStreamWriterFilter in project metro-jax-ws by eclipse-ee4j.
the class EPRSDDocumentFilter method filter.
public XMLStreamWriter filter(SDDocument doc, XMLStreamWriter w) throws XMLStreamException, IOException {
if (!doc.isWSDL()) {
return w;
}
return new XMLStreamWriterFilter(w) {
// when true, all writer events are filtered out
private boolean eprExtnFilterON = false;
private boolean portHasEPR = false;
// -1 -> outside wsa:epr, 0 -> on wsa:epr start/end , > 0 inside wsa:epr
private int eprDepth = -1;
// non null when inside wsdl:service scope
private String serviceName = null;
// flag to get service name when on wsdl:service element start
private boolean onService = false;
// -1 -> outside wsdl:service, 0 -> on wsdl:service start/end , > 0 inside wsdl:service
private int serviceDepth = -1;
// non null when inside wsdl:port scope
private String portName = null;
// flag to get port name when on wsdl:port element start
private boolean onPort = false;
// -1 -> outside wsdl:port, 0 -> on wsdl:port start/end , > 0 inside wsdl:port
private int portDepth = -1;
// when a complete epr is written, endpoint address is used as epr address
private String portAddress;
// flag to get endpoint address when on soap:address element start
private boolean onPortAddress = false;
private void handleStartElement(String localName, String namespaceURI) throws XMLStreamException {
resetOnElementFlags();
if (serviceDepth >= 0) {
serviceDepth++;
}
if (portDepth >= 0) {
portDepth++;
}
if (eprDepth >= 0) {
eprDepth++;
}
if (namespaceURI.equals(WSDLConstants.QNAME_SERVICE.getNamespaceURI()) && localName.equals(WSDLConstants.QNAME_SERVICE.getLocalPart())) {
onService = true;
serviceDepth = 0;
} else if (namespaceURI.equals(WSDLConstants.QNAME_PORT.getNamespaceURI()) && localName.equals(WSDLConstants.QNAME_PORT.getLocalPart())) {
if (serviceDepth >= 1) {
onPort = true;
portDepth = 0;
}
} else if (namespaceURI.equals(W3CAddressingConstants.WSA_NAMESPACE_NAME) && localName.equals("EndpointReference")) {
if (serviceDepth >= 1 && portDepth >= 1) {
portHasEPR = true;
eprDepth = 0;
}
} else if ((namespaceURI.equals(WSDLConstants.NS_SOAP_BINDING_ADDRESS.getNamespaceURI()) || namespaceURI.equals(WSDLConstants.NS_SOAP12_BINDING_ADDRESS.getNamespaceURI())) && localName.equals("address") && portDepth == 1) {
onPortAddress = true;
}
WSEndpoint endpoint = getEndpoint(serviceName, portName);
// if (service.getLocalPart().equals(serviceName) && port.getLocalPart().equals(portName)) {
if (endpoint != null) {
if ((eprDepth == 1) && !namespaceURI.equals(W3CAddressingConstants.WSA_NAMESPACE_NAME)) {
// epr extension element
eprExtnFilterON = true;
}
/*
if (eprExtnFilterON) {
writeEPRExtensions();
}
*/
}
}
private void resetOnElementFlags() {
if (onService) {
onService = false;
}
if (onPort) {
onPort = false;
}
if (onPortAddress) {
onPortAddress = false;
}
}
private void writeEPRExtensions(Collection<WSEndpointReference.EPRExtension> eprExtns) throws XMLStreamException {
if (eprExtns != null) {
for (WSEndpointReference.EPRExtension e : eprExtns) {
XMLStreamReaderToXMLStreamWriter c = new XMLStreamReaderToXMLStreamWriter();
XMLStreamReader r = e.readAsXMLStreamReader();
c.bridge(r, writer);
XMLStreamReaderFactory.recycle(r);
}
}
}
@Override
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
handleStartElement(localName, namespaceURI);
if (!eprExtnFilterON) {
super.writeStartElement(prefix, localName, namespaceURI);
}
}
@Override
public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
handleStartElement(localName, namespaceURI);
if (!eprExtnFilterON) {
super.writeStartElement(namespaceURI, localName);
}
}
@Override
public void writeStartElement(String localName) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeStartElement(localName);
}
}
private void handleEndElement() throws XMLStreamException {
resetOnElementFlags();
// End of wsdl:port, write complete EPR if not present.
if (portDepth == 0) {
if (!portHasEPR && getEndpoint(serviceName, portName) != null) {
// write the complete EPR with address.
writer.writeStartElement(AddressingVersion.W3C.getPrefix(), "EndpointReference", AddressingVersion.W3C.nsUri);
writer.writeNamespace(AddressingVersion.W3C.getPrefix(), AddressingVersion.W3C.nsUri);
writer.writeStartElement(AddressingVersion.W3C.getPrefix(), AddressingVersion.W3C.eprType.address, AddressingVersion.W3C.nsUri);
writer.writeCharacters(portAddress);
writer.writeEndElement();
writeEPRExtensions(getEndpoint(serviceName, portName).getEndpointReferenceExtensions());
writer.writeEndElement();
}
}
// End of wsa:EndpointReference, write EPR extension elements
if (eprDepth == 0) {
if (portHasEPR && getEndpoint(serviceName, portName) != null) {
writeEPRExtensions(getEndpoint(serviceName, portName).getEndpointReferenceExtensions());
}
eprExtnFilterON = false;
}
if (serviceDepth >= 0) {
serviceDepth--;
}
if (portDepth >= 0) {
portDepth--;
}
if (eprDepth >= 0) {
eprDepth--;
}
if (serviceDepth == -1) {
serviceName = null;
}
if (portDepth == -1) {
portHasEPR = false;
portAddress = null;
portName = null;
}
}
@Override
public void writeEndElement() throws XMLStreamException {
handleEndElement();
if (!eprExtnFilterON) {
super.writeEndElement();
}
}
private void handleAttribute(String localName, String value) {
if (localName.equals("name")) {
if (onService) {
serviceName = value;
onService = false;
} else if (onPort) {
portName = value;
onPort = false;
}
}
if (localName.equals("location") && onPortAddress) {
portAddress = value;
}
}
@Override
public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
handleAttribute(localName, value);
if (!eprExtnFilterON) {
super.writeAttribute(prefix, namespaceURI, localName, value);
}
}
@Override
public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
handleAttribute(localName, value);
if (!eprExtnFilterON) {
super.writeAttribute(namespaceURI, localName, value);
}
}
@Override
public void writeAttribute(String localName, String value) throws XMLStreamException {
handleAttribute(localName, value);
if (!eprExtnFilterON) {
super.writeAttribute(localName, value);
}
}
@Override
public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeEmptyElement(namespaceURI, localName);
}
}
@Override
public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeNamespace(prefix, namespaceURI);
}
}
@Override
public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
if (!eprExtnFilterON) {
super.setNamespaceContext(context);
}
}
@Override
public void setDefaultNamespace(String uri) throws XMLStreamException {
if (!eprExtnFilterON) {
super.setDefaultNamespace(uri);
}
}
@Override
public void setPrefix(String prefix, String uri) throws XMLStreamException {
if (!eprExtnFilterON) {
super.setPrefix(prefix, uri);
}
}
@Override
public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeProcessingInstruction(target, data);
}
}
@Override
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeEmptyElement(prefix, localName, namespaceURI);
}
}
@Override
public void writeCData(String data) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeCData(data);
}
}
@Override
public void writeCharacters(String text) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeCharacters(text);
}
}
@Override
public void writeComment(String data) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeComment(data);
}
}
@Override
public void writeDTD(String dtd) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeDTD(dtd);
}
}
@Override
public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeDefaultNamespace(namespaceURI);
}
}
@Override
public void writeEmptyElement(String localName) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeEmptyElement(localName);
}
}
@Override
public void writeEntityRef(String name) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeEntityRef(name);
}
}
@Override
public void writeProcessingInstruction(String target) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeProcessingInstruction(target);
}
}
@Override
public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {
if (!eprExtnFilterON) {
super.writeCharacters(text, start, len);
}
}
};
}
Aggregations