ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.policy.jaxws; ohair@286: mkos@408: import com.sun.xml.internal.ws.api.model.wsdl.WSDLObject; mkos@408: import com.sun.xml.internal.ws.api.model.wsdl.editable.*; ohair@286: import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension; ohair@286: import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtensionContext; ohair@286: import com.sun.xml.internal.ws.api.policy.PolicyResolver; ohair@286: import com.sun.xml.internal.ws.resources.PolicyMessages; ohair@286: import com.sun.xml.internal.ws.policy.jaxws.SafePolicyReader.PolicyRecord; ohair@286: import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger; ohair@286: import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; ohair@286: import com.sun.xml.internal.ws.policy.sourcemodel.PolicySourceModel; ohair@286: import com.sun.xml.internal.ws.policy.sourcemodel.PolicySourceModelContext; ohair@286: import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.NamespaceVersion; ohair@286: import com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken; ohair@286: import com.sun.xml.internal.ws.policy.PolicyException; ohair@286: import com.sun.xml.internal.ws.policy.PolicyMap; alanb@368: import com.sun.xml.internal.ws.util.xml.XmlUtil; ohair@286: ohair@286: import java.io.IOException; ohair@286: import java.io.InputStream; ohair@286: import java.net.URI; ohair@286: import java.net.URISyntaxException; ohair@286: import java.net.URL; ohair@286: import java.util.List; ohair@286: import java.util.HashSet; ohair@286: import java.util.ArrayList; ohair@286: import java.util.Collection; ohair@286: import java.util.HashMap; ohair@286: import java.util.LinkedList; ohair@286: import java.util.Map; mkos@408: ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.stream.XMLStreamException; ohair@286: import javax.xml.stream.XMLStreamReader; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: ohair@286: /** ohair@286: * This class parses the Policy Attachments in the WSDL and creates a PolicyMap thaty captures the policies configured on ohair@286: * different PolicySubjects in the wsdl. ohair@286: * ohair@286: * After, it is finished it sets the PolicyMap on the WSDLModel. ohair@286: * ohair@286: * @author Jakub Podlesak (jakub.podlesak at sun.com) ohair@286: * @author Fabian Ritzmann ohair@286: * @author Rama Pulavarthi ohair@286: */ ohair@286: final public class PolicyWSDLParserExtension extends WSDLParserExtension { ohair@286: ohair@286: enum HandlerType { ohair@286: PolicyUri, AnonymousPolicyId ohair@286: } ohair@286: ohair@286: final static class PolicyRecordHandler { ohair@286: String handler; ohair@286: HandlerType type; ohair@286: ohair@286: PolicyRecordHandler(HandlerType type, String handler) { ohair@286: this.type = type; ohair@286: this.handler = handler; ohair@286: } ohair@286: ohair@286: HandlerType getType() { ohair@286: return type; ohair@286: } ohair@286: ohair@286: String getHandler() { ohair@286: return handler; ohair@286: } ohair@286: } ohair@286: ohair@286: private static final PolicyLogger LOGGER = PolicyLogger.getLogger(PolicyWSDLParserExtension.class); ohair@286: ohair@286: //anonymous policy id prefix ohair@286: private static final StringBuffer AnonymnousPolicyIdPrefix = new StringBuffer("#__anonymousPolicy__ID"); ohair@286: ohair@286: // anonymous policies count ohair@286: private int anonymousPoliciesCount; ohair@286: ohair@286: private final SafePolicyReader policyReader = new SafePolicyReader(); ohair@286: ohair@286: // policy queue -- needed for evaluating the right order policy of policy models expansion ohair@286: private PolicyRecord expandQueueHead = null; ohair@286: ohair@286: // storage for policy models with an id passed by ohair@286: private Map policyRecordsPassedBy = null; ohair@286: // storage for anonymous policies defined within given WSDL ohair@286: private Map anonymousPolicyModels = null; ohair@286: ohair@286: // container for URIs of policies referenced ohair@286: private List unresolvedUris = null; ohair@286: ohair@286: // structures for policies really needed to build a map ohair@286: private final LinkedList urisNeeded = new LinkedList(); ohair@286: private final Map modelsNeeded = new HashMap(); ohair@286: ohair@286: // lookup tables for Policy attachments found ohair@286: private Map> handlers4ServiceMap = null; ohair@286: private Map> handlers4PortMap = null; ohair@286: private Map> handlers4PortTypeMap = null; ohair@286: private Map> handlers4BindingMap = null; ohair@286: private Map> handlers4BoundOperationMap = null; ohair@286: private Map> handlers4OperationMap = null; ohair@286: private Map> handlers4MessageMap = null; ohair@286: private Map> handlers4InputMap = null; ohair@286: private Map> handlers4OutputMap = null; ohair@286: private Map> handlers4FaultMap = null; ohair@286: private Map> handlers4BindingInputOpMap = null; ohair@286: private Map> handlers4BindingOutputOpMap = null; ohair@286: private Map> handlers4BindingFaultOpMap = null; ohair@286: ohair@286: private PolicyMapBuilder policyBuilder = new PolicyMapBuilder(); ohair@286: ohair@286: private boolean isPolicyProcessed(final String policyUri) { ohair@286: return modelsNeeded.containsKey(policyUri); ohair@286: } ohair@286: ohair@286: private void addNewPolicyNeeded(final String policyUri, final PolicySourceModel policyModel) { ohair@286: if (!modelsNeeded.containsKey(policyUri)) { ohair@286: modelsNeeded.put(policyUri, policyModel); ohair@286: urisNeeded.addFirst(policyUri); ohair@286: } ohair@286: } ohair@286: ohair@286: private Map getPolicyModels() { ohair@286: return modelsNeeded; ohair@286: } ohair@286: ohair@286: private Map getPolicyRecordsPassedBy() { ohair@286: if (null==policyRecordsPassedBy) { ohair@286: policyRecordsPassedBy = new HashMap(); ohair@286: } ohair@286: return policyRecordsPassedBy; ohair@286: } ohair@286: ohair@286: private Map getAnonymousPolicyModels() { ohair@286: if (null==anonymousPolicyModels) { ohair@286: anonymousPolicyModels = new HashMap(); ohair@286: } ohair@286: return anonymousPolicyModels; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4ServiceMap() { ohair@286: if (null==handlers4ServiceMap) { ohair@286: handlers4ServiceMap = new HashMap>(); ohair@286: } ohair@286: return handlers4ServiceMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4PortMap() { ohair@286: if (null==handlers4PortMap) { ohair@286: handlers4PortMap = new HashMap>(); ohair@286: } ohair@286: return handlers4PortMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4PortTypeMap() { ohair@286: if (null==handlers4PortTypeMap) { ohair@286: handlers4PortTypeMap = new HashMap>(); ohair@286: } ohair@286: return handlers4PortTypeMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4BindingMap() { ohair@286: if (null==handlers4BindingMap) { ohair@286: handlers4BindingMap = new HashMap>(); ohair@286: } ohair@286: return handlers4BindingMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4OperationMap() { ohair@286: if (null==handlers4OperationMap) { ohair@286: handlers4OperationMap = new HashMap>(); ohair@286: } ohair@286: return handlers4OperationMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4BoundOperationMap() { ohair@286: if (null==handlers4BoundOperationMap) { ohair@286: handlers4BoundOperationMap = new HashMap>(); ohair@286: } ohair@286: return handlers4BoundOperationMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4MessageMap() { ohair@286: if (null==handlers4MessageMap) { ohair@286: handlers4MessageMap = new HashMap>(); ohair@286: } ohair@286: return handlers4MessageMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4InputMap() { ohair@286: if (null==handlers4InputMap) { ohair@286: handlers4InputMap = new HashMap>(); ohair@286: } ohair@286: return handlers4InputMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4OutputMap() { ohair@286: if (null==handlers4OutputMap) { ohair@286: handlers4OutputMap = new HashMap>(); ohair@286: } ohair@286: return handlers4OutputMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4FaultMap() { ohair@286: if (null==handlers4FaultMap) { ohair@286: handlers4FaultMap = new HashMap>(); ohair@286: } ohair@286: return handlers4FaultMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4BindingInputOpMap() { ohair@286: if (null==handlers4BindingInputOpMap) { ohair@286: handlers4BindingInputOpMap = new HashMap>(); ohair@286: } ohair@286: return handlers4BindingInputOpMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4BindingOutputOpMap() { ohair@286: if (null==handlers4BindingOutputOpMap) { ohair@286: handlers4BindingOutputOpMap = new HashMap>(); ohair@286: } ohair@286: return handlers4BindingOutputOpMap; ohair@286: } ohair@286: ohair@286: private Map> getHandlers4BindingFaultOpMap() { ohair@286: if (null==handlers4BindingFaultOpMap) { ohair@286: handlers4BindingFaultOpMap = new HashMap>(); ohair@286: } ohair@286: return handlers4BindingFaultOpMap; ohair@286: } ohair@286: ohair@286: private List getUnresolvedUris(final boolean emptyListNeeded) { ohair@286: if ((null == unresolvedUris) || emptyListNeeded) { ohair@286: unresolvedUris = new LinkedList(); ohair@286: } ohair@286: return unresolvedUris; ohair@286: } ohair@286: ohair@286: ohair@286: ohair@286: private void policyRecToExpandQueue(final PolicyRecord policyRec) { ohair@286: if (null==expandQueueHead) { ohair@286: expandQueueHead = policyRec; ohair@286: } else { ohair@286: expandQueueHead = expandQueueHead.insert(policyRec); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a new instance of PolicyWSDLParserExtension ohair@286: */ ohair@286: public PolicyWSDLParserExtension() { ohair@286: ohair@286: } ohair@286: ohair@286: ohair@286: private PolicyRecordHandler readSinglePolicy(final PolicyRecord policyRec, final boolean inner) { ohair@286: PolicyRecordHandler handler = null; ohair@286: String policyId = policyRec.policyModel.getPolicyId(); ohair@286: if (policyId == null) { ohair@286: policyId = policyRec.policyModel.getPolicyName(); ohair@286: } ohair@286: if (policyId != null) { // policy id defined, keep the policy ohair@286: handler = new PolicyRecordHandler(HandlerType.PolicyUri, policyRec.getUri()); ohair@286: getPolicyRecordsPassedBy().put(policyRec.getUri(), policyRec); ohair@286: policyRecToExpandQueue(policyRec); ohair@286: } else if (inner) { // no id given to the policy --> keep as an annonymous policy model ohair@286: final String anonymousId = AnonymnousPolicyIdPrefix.append(anonymousPoliciesCount++).toString(); ohair@286: handler = new PolicyRecordHandler(HandlerType.AnonymousPolicyId,anonymousId); ohair@286: getAnonymousPolicyModels().put(anonymousId, policyRec.policyModel); ohair@286: if (null != policyRec.unresolvedURIs) { ohair@286: getUnresolvedUris(false).addAll(policyRec.unresolvedURIs); ohair@286: } ohair@286: } ohair@286: return handler; ohair@286: } ohair@286: ohair@286: ohair@286: private void addHandlerToMap( ohair@286: final Map> map, final WSDLObject key, final PolicyRecordHandler handler) { ohair@286: if (map.containsKey(key)) { ohair@286: map.get(key).add(handler); ohair@286: } else { ohair@286: final Collection newSet = new LinkedList(); ohair@286: newSet.add(handler); ohair@286: map.put(key,newSet); ohair@286: } ohair@286: } ohair@286: ohair@286: private String getBaseUrl(final String policyUri) { ohair@286: if (null == policyUri) { ohair@286: return null; ohair@286: } ohair@286: // TODO: encoded urls (escaped characters) might be a problem ? ohair@286: final int fragmentIdx = policyUri.indexOf('#'); ohair@286: return (fragmentIdx == -1) ? policyUri : policyUri.substring(0, fragmentIdx); ohair@286: } ohair@286: ohair@286: // adding current url even to locally referenced policies ohair@286: // in order to distinguish imported policies ohair@286: private void processReferenceUri( ohair@286: final String policyUri, ohair@286: final WSDLObject element, ohair@286: final XMLStreamReader reader, ohair@286: final Map> map) { ohair@286: ohair@286: if (null == policyUri || policyUri.length() == 0) { ohair@286: return; ohair@286: } ohair@286: if ('#' != policyUri.charAt(0)) { // external uri (already) ohair@286: getUnresolvedUris(false).add(policyUri); ohair@286: } ohair@286: ohair@286: addHandlerToMap(map, element, ohair@286: new PolicyRecordHandler( ohair@286: HandlerType.PolicyUri, ohair@286: SafePolicyReader.relativeToAbsoluteUrl(policyUri, reader.getLocation().getSystemId()))); ohair@286: } ohair@286: ohair@286: private boolean processSubelement( ohair@286: final WSDLObject element, final XMLStreamReader reader, final Map> map) { ohair@286: if (NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.PolicyReference) { // "PolicyReference" element interests us ohair@286: processReferenceUri(policyReader.readPolicyReferenceElement(reader), element, reader, map); ohair@286: return true; ohair@286: } else if (NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.Policy) { // policy could be defined here ohair@286: final PolicyRecordHandler handler = ohair@286: readSinglePolicy( ohair@286: policyReader.readPolicyElement( ohair@286: reader, ohair@286: (null == reader.getLocation().getSystemId()) ? // baseUrl ohair@286: "" : reader.getLocation().getSystemId()), ohair@286: true); ohair@286: if (null != handler) { // only policies with an Id can work for us ohair@286: addHandlerToMap(map, element, handler); ohair@286: } // endif null != handler ohair@286: return true; // element consumed ohair@286: }//end if Policy element found ohair@286: return false; ohair@286: } ohair@286: ohair@286: private void processAttributes(final WSDLObject element, final XMLStreamReader reader, final Map> map) { ohair@286: final String[] uriArray = getPolicyURIsFromAttr(reader); ohair@286: if (null != uriArray) { ohair@286: for (String policyUri : uriArray) { ohair@286: processReferenceUri(policyUri, element, reader, map); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean portElements(final EditableWSDLPort port, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(port, reader, getHandlers4PortMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void portAttributes(final EditableWSDLPort port, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(port, reader, getHandlers4PortMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean serviceElements(final EditableWSDLService service, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(service, reader, getHandlers4ServiceMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void serviceAttributes(final EditableWSDLService service, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(service, reader, getHandlers4ServiceMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: ohair@286: @Override ohair@286: public boolean definitionsElements(final XMLStreamReader reader){ ohair@286: LOGGER.entering(); ohair@286: if (NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.Policy) { // Only "Policy" element interests me ohair@286: readSinglePolicy( ohair@286: policyReader.readPolicyElement( ohair@286: reader, ohair@286: (null == reader.getLocation().getSystemId()) ? // baseUrl ohair@286: "" : reader.getLocation().getSystemId()), ohair@286: false); ohair@286: LOGGER.exiting(); ohair@286: return true; ohair@286: } ohair@286: LOGGER.exiting(); ohair@286: return false; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean bindingElements(final EditableWSDLBoundPortType binding, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(binding, reader, getHandlers4BindingMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void bindingAttributes(final EditableWSDLBoundPortType binding, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(binding, reader, getHandlers4BindingMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean portTypeElements(final EditableWSDLPortType portType, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(portType, reader, getHandlers4PortTypeMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void portTypeAttributes(final EditableWSDLPortType portType, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(portType, reader, getHandlers4PortTypeMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean portTypeOperationElements(final EditableWSDLOperation operation, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(operation, reader, getHandlers4OperationMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void portTypeOperationAttributes(final EditableWSDLOperation operation, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(operation, reader, getHandlers4OperationMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean bindingOperationElements(final EditableWSDLBoundOperation boundOperation, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(boundOperation, reader, getHandlers4BoundOperationMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void bindingOperationAttributes(final EditableWSDLBoundOperation boundOperation, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(boundOperation, reader, getHandlers4BoundOperationMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean messageElements(final EditableWSDLMessage msg, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(msg, reader, getHandlers4MessageMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void messageAttributes(final EditableWSDLMessage msg, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(msg, reader, getHandlers4MessageMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean portTypeOperationInputElements(final EditableWSDLInput input, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(input, reader, getHandlers4InputMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void portTypeOperationInputAttributes(final EditableWSDLInput input, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(input, reader, getHandlers4InputMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: ohair@286: @Override mkos@408: public boolean portTypeOperationOutputElements(final EditableWSDLOutput output, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(output, reader, getHandlers4OutputMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void portTypeOperationOutputAttributes(final EditableWSDLOutput output, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(output, reader, getHandlers4OutputMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: ohair@286: @Override mkos@408: public boolean portTypeOperationFaultElements(final EditableWSDLFault fault, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(fault, reader, getHandlers4FaultMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void portTypeOperationFaultAttributes(final EditableWSDLFault fault, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(fault, reader, getHandlers4FaultMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean bindingOperationInputElements(final EditableWSDLBoundOperation operation, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(operation, reader, getHandlers4BindingInputOpMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void bindingOperationInputAttributes(final EditableWSDLBoundOperation operation, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(operation, reader, getHandlers4BindingInputOpMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: ohair@286: @Override mkos@408: public boolean bindingOperationOutputElements(final EditableWSDLBoundOperation operation, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(operation, reader, getHandlers4BindingOutputOpMap()); ohair@286: LOGGER.exiting(); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void bindingOperationOutputAttributes(final EditableWSDLBoundOperation operation, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(operation, reader, getHandlers4BindingOutputOpMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: @Override mkos@408: public boolean bindingOperationFaultElements(final EditableWSDLBoundFault fault, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: final boolean result = processSubelement(fault, reader, getHandlers4BindingFaultOpMap()); ohair@286: LOGGER.exiting(result); ohair@286: return result; ohair@286: } ohair@286: ohair@286: @Override mkos@408: public void bindingOperationFaultAttributes(final EditableWSDLBoundFault fault, final XMLStreamReader reader) { ohair@286: LOGGER.entering(); ohair@286: processAttributes(fault, reader, getHandlers4BindingFaultOpMap()); ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: ohair@286: private PolicyMapBuilder getPolicyMapBuilder() { ohair@286: if (null == policyBuilder) { ohair@286: policyBuilder = new PolicyMapBuilder(); ohair@286: } ohair@286: return policyBuilder; ohair@286: } ohair@286: ohair@286: private Collection getPolicyURIs( ohair@286: final Collection handlers, final PolicySourceModelContext modelContext) throws PolicyException{ ohair@286: final Collection result = new ArrayList(handlers.size()); ohair@286: String policyUri; ohair@286: for (PolicyRecordHandler handler : handlers) { ohair@286: policyUri = handler.handler; ohair@286: if (HandlerType.AnonymousPolicyId == handler.type) { ohair@286: final PolicySourceModel policyModel = getAnonymousPolicyModels().get(policyUri); ohair@286: policyModel.expand(modelContext); ohair@286: while (getPolicyModels().containsKey(policyUri)) { ohair@286: policyUri = AnonymnousPolicyIdPrefix.append(anonymousPoliciesCount++).toString(); ohair@286: } ohair@286: getPolicyModels().put(policyUri,policyModel); ohair@286: } ohair@286: result.add(policyUri); ohair@286: } ohair@286: return result; ohair@286: } ohair@286: ohair@286: private boolean readExternalFile(final String fileUrl) { ohair@286: InputStream ios = null; ohair@286: XMLStreamReader reader = null; ohair@286: try { ohair@286: final URL xmlURL = new URL(fileUrl); ohair@286: ios = xmlURL.openStream(); alanb@368: reader = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(ios); ohair@286: while (reader.hasNext()) { ohair@286: if (reader.isStartElement() && NamespaceVersion.resolveAsToken(reader.getName()) == XmlToken.Policy) { ohair@286: readSinglePolicy(policyReader.readPolicyElement(reader, fileUrl), false); ohair@286: } ohair@286: reader.next(); ohair@286: } ohair@286: return true; ohair@286: } catch (IOException ioe) { ohair@286: return false; ohair@286: } catch (XMLStreamException xmlse) { ohair@286: return false; ohair@286: } finally { ohair@286: PolicyUtils.IO.closeResource(reader); ohair@286: PolicyUtils.IO.closeResource(ios); ohair@286: } ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void finished(final WSDLParserExtensionContext context) { ohair@286: LOGGER.entering(context); ohair@286: // need to make sure proper beginning order of internal policies within unresolvedUris list ohair@286: if (null != expandQueueHead) { // any policies found ohair@286: final List externalUris = getUnresolvedUris(false); // protect list of possible external policies ohair@286: getUnresolvedUris(true); // cleaning up the list only ohair@286: final LinkedList baseUnresolvedUris = new LinkedList(); ohair@286: for (PolicyRecord currentRec = expandQueueHead ; null != currentRec ; currentRec = currentRec.next) { ohair@286: baseUnresolvedUris.addFirst(currentRec.getUri()); ohair@286: } ohair@286: getUnresolvedUris(false).addAll(baseUnresolvedUris); ohair@286: expandQueueHead = null; // cut the queue off ohair@286: getUnresolvedUris(false).addAll(externalUris); ohair@286: } ohair@286: ohair@286: while (!getUnresolvedUris(false).isEmpty()) { ohair@286: final List urisToBeSolvedList = getUnresolvedUris(false); ohair@286: getUnresolvedUris(true); // just cleaning up the list ohair@286: for (String currentUri : urisToBeSolvedList) { ohair@286: if (!isPolicyProcessed(currentUri)) { ohair@286: final PolicyRecord prefetchedRecord = getPolicyRecordsPassedBy().get(currentUri); ohair@286: if (null == prefetchedRecord) { ohair@286: if (policyReader.getUrlsRead().contains(getBaseUrl(currentUri))) { // --> unresolvable policy ohair@286: LOGGER.logSevereException(new PolicyException(PolicyMessages.WSP_1014_CAN_NOT_FIND_POLICY(currentUri))); ohair@286: } else { ohair@286: if (readExternalFile(getBaseUrl(currentUri))) { ohair@286: getUnresolvedUris(false).add(currentUri); ohair@286: } ohair@286: } ohair@286: } else { // policy has not been yet passed by ohair@286: if (null != prefetchedRecord.unresolvedURIs) { ohair@286: getUnresolvedUris(false).addAll(prefetchedRecord.unresolvedURIs); ohair@286: } // end-if null != prefetchedRecord.unresolvedURIs ohair@286: addNewPolicyNeeded(currentUri, prefetchedRecord.policyModel); ohair@286: } ohair@286: } // end-if policy already processed ohair@286: } // end-foreach unresolved uris ohair@286: } ohair@286: final PolicySourceModelContext modelContext = PolicySourceModelContext.createContext(); ohair@286: for (String policyUri : urisNeeded) { ohair@286: final PolicySourceModel sourceModel = modelsNeeded.get(policyUri); ohair@286: try { ohair@286: sourceModel.expand(modelContext); ohair@286: modelContext.addModel(new URI(policyUri), sourceModel); ohair@286: } catch (URISyntaxException e) { ohair@286: LOGGER.logSevereException(e); ohair@286: } catch (PolicyException e) { ohair@286: LOGGER.logSevereException(e); ohair@286: } ohair@286: } ohair@286: ohair@286: // Start-preparation of policy map builder ohair@286: // iterating over all services and binding all the policies read before ohair@286: try { ohair@286: // messageSet holds the handlers for all wsdl:message elements. There ohair@286: // may otherwise be multiple entries for policies that are contained ohair@286: // by fault messages. ohair@286: HashSet messageSet = new HashSet(); mkos@408: for (EditableWSDLService service : context.getWSDLModel().getServices().values()) { ohair@286: if (getHandlers4ServiceMap().containsKey(service)) { ohair@286: getPolicyMapBuilder().registerHandler(new BuilderHandlerServiceScope( ohair@286: getPolicyURIs(getHandlers4ServiceMap().get(service),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,service ohair@286: ,service.getName())); ohair@286: } ohair@286: // end service scope ohair@286: mkos@408: for (EditableWSDLPort port : service.getPorts()) { ohair@286: if (getHandlers4PortMap().containsKey(port)) { ohair@286: getPolicyMapBuilder().registerHandler( ohair@286: new BuilderHandlerEndpointScope( ohair@286: getPolicyURIs(getHandlers4PortMap().get(port),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,port ohair@286: ,port.getOwner().getName() ohair@286: ,port.getName())); ohair@286: } ohair@286: if ( // port.getBinding may not be null, but in case ... ohair@286: null != port.getBinding()) { ohair@286: if ( // handler for binding ohair@286: getHandlers4BindingMap().containsKey(port.getBinding())) { ohair@286: getPolicyMapBuilder() ohair@286: .registerHandler( ohair@286: new BuilderHandlerEndpointScope( ohair@286: getPolicyURIs(getHandlers4BindingMap().get(port.getBinding()),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,port.getBinding() ohair@286: ,service.getName() ohair@286: ,port.getName())); ohair@286: } // endif handler for binding ohair@286: if ( // handler for port type ohair@286: getHandlers4PortTypeMap().containsKey(port.getBinding().getPortType())) { ohair@286: getPolicyMapBuilder() ohair@286: .registerHandler( ohair@286: new BuilderHandlerEndpointScope( ohair@286: getPolicyURIs(getHandlers4PortTypeMap().get(port.getBinding().getPortType()),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,port.getBinding().getPortType() ohair@286: ,service.getName() ohair@286: ,port.getName())); ohair@286: } // endif handler for port type ohair@286: // end endpoint scope ohair@286: mkos@408: for (EditableWSDLBoundOperation boundOperation : port.getBinding().getBindingOperations()) { ohair@286: mkos@408: final EditableWSDLOperation operation = boundOperation.getOperation(); ohair@286: final QName operationName = new QName(boundOperation.getBoundPortType().getName().getNamespaceURI(), boundOperation.getName().getLocalPart()); ohair@286: // We store the message and portType/operation under the same namespace as the binding/operation so that we can match them up later ohair@286: if ( // handler for operation scope -- by boundOperation ohair@286: getHandlers4BoundOperationMap().containsKey(boundOperation)) { ohair@286: getPolicyMapBuilder() ohair@286: .registerHandler( ohair@286: new BuilderHandlerOperationScope( ohair@286: getPolicyURIs(getHandlers4BoundOperationMap().get(boundOperation),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,boundOperation ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName)); ohair@286: } // endif handler for binding:operation scope ohair@286: if ( // handler for operation scope -- by operation map ohair@286: getHandlers4OperationMap().containsKey(operation)) { ohair@286: getPolicyMapBuilder() ohair@286: .registerHandler( ohair@286: new BuilderHandlerOperationScope( ohair@286: getPolicyURIs(getHandlers4OperationMap().get(operation),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,operation ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName)); ohair@286: } // endif for portType:operation scope ohair@286: // end operation scope ohair@286: mkos@408: final EditableWSDLInput input = operation.getInput(); ohair@286: if (null!=input) { mkos@408: EditableWSDLMessage inputMsg = input.getMessage(); ohair@286: if (inputMsg != null && getHandlers4MessageMap().containsKey(inputMsg)) { ohair@286: messageSet.add(new BuilderHandlerMessageScope( ohair@286: getPolicyURIs( ohair@286: getHandlers4MessageMap().get(inputMsg), modelContext) ohair@286: ,getPolicyModels() ohair@286: ,inputMsg ohair@286: ,BuilderHandlerMessageScope.Scope.InputMessageScope ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName ohair@286: ,null) ohair@286: ); ohair@286: } ohair@286: } ohair@286: if ( // binding op input msg ohair@286: getHandlers4BindingInputOpMap().containsKey(boundOperation)) { ohair@286: getPolicyMapBuilder() ohair@286: .registerHandler( ohair@286: new BuilderHandlerMessageScope( ohair@286: getPolicyURIs(getHandlers4BindingInputOpMap().get(boundOperation),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,boundOperation ohair@286: ,BuilderHandlerMessageScope.Scope.InputMessageScope ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName ohair@286: ,null)); ohair@286: } // endif binding op input msg ohair@286: if ( null != input // portType op input msg ohair@286: && getHandlers4InputMap().containsKey(input)) { ohair@286: getPolicyMapBuilder() ohair@286: .registerHandler( ohair@286: new BuilderHandlerMessageScope( ohair@286: getPolicyURIs(getHandlers4InputMap().get(input),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,input ohair@286: ,BuilderHandlerMessageScope.Scope.InputMessageScope ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName ohair@286: ,null)); ohair@286: } // endif portType op input msg ohair@286: // end input message scope ohair@286: mkos@408: final EditableWSDLOutput output = operation.getOutput(); ohair@286: if (null!=output) { mkos@408: EditableWSDLMessage outputMsg = output.getMessage(); ohair@286: if (outputMsg != null && getHandlers4MessageMap().containsKey(outputMsg)) { ohair@286: messageSet.add(new BuilderHandlerMessageScope( ohair@286: getPolicyURIs( ohair@286: getHandlers4MessageMap().get(outputMsg),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,outputMsg ohair@286: ,BuilderHandlerMessageScope.Scope.OutputMessageScope ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName ohair@286: ,null) ohair@286: ); ohair@286: } ohair@286: } ohair@286: if ( // binding op output msg ohair@286: getHandlers4BindingOutputOpMap().containsKey(boundOperation)) { ohair@286: getPolicyMapBuilder() ohair@286: .registerHandler( ohair@286: new BuilderHandlerMessageScope( ohair@286: getPolicyURIs(getHandlers4BindingOutputOpMap().get(boundOperation),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,boundOperation ohair@286: ,BuilderHandlerMessageScope.Scope.OutputMessageScope ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName ohair@286: ,null)); ohair@286: } // endif binding op output msg ohair@286: if ( null != output // portType op output msg ohair@286: && getHandlers4OutputMap().containsKey(output)) { ohair@286: getPolicyMapBuilder() ohair@286: .registerHandler( ohair@286: new BuilderHandlerMessageScope( ohair@286: getPolicyURIs(getHandlers4OutputMap().get(output),modelContext) ohair@286: ,getPolicyModels() ohair@286: ,output ohair@286: ,BuilderHandlerMessageScope.Scope.OutputMessageScope ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName ohair@286: ,null)); ohair@286: } // endif portType op output msg ohair@286: // end output message scope ohair@286: mkos@408: for (EditableWSDLBoundFault boundFault : boundOperation.getFaults()) { mkos@408: final EditableWSDLFault fault = boundFault.getFault(); alanb@368: alanb@368: // this shouldn't happen ususally, alanb@368: // but since this scenario tested in lagacy tests, dont' fail here alanb@368: if (fault == null) { alanb@368: LOGGER.warning(PolicyMessages.WSP_1021_FAULT_NOT_BOUND(boundFault.getName())); alanb@368: continue; alanb@368: } alanb@368: mkos@408: final EditableWSDLMessage faultMessage = fault.getMessage(); ohair@286: final QName faultName = new QName(boundOperation.getBoundPortType().getName().getNamespaceURI(), boundFault.getName()); ohair@286: // We store the message and portType/fault under the same namespace as the binding/fault so that we can match them up later ohair@286: if (faultMessage != null && getHandlers4MessageMap().containsKey(faultMessage)) { ohair@286: messageSet.add( ohair@286: new BuilderHandlerMessageScope( ohair@286: getPolicyURIs(getHandlers4MessageMap().get(faultMessage), modelContext) ohair@286: ,getPolicyModels() ohair@286: ,new WSDLBoundFaultContainer(boundFault, boundOperation) ohair@286: ,BuilderHandlerMessageScope.Scope.FaultMessageScope ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName ohair@286: ,faultName) ohair@286: ); ohair@286: } ohair@286: if (getHandlers4FaultMap().containsKey(fault)) { ohair@286: messageSet.add( ohair@286: new BuilderHandlerMessageScope( ohair@286: getPolicyURIs(getHandlers4FaultMap().get(fault), modelContext) ohair@286: ,getPolicyModels() ohair@286: ,new WSDLBoundFaultContainer(boundFault, boundOperation) ohair@286: ,BuilderHandlerMessageScope.Scope.FaultMessageScope ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName ohair@286: ,faultName) ohair@286: ); ohair@286: } ohair@286: if (getHandlers4BindingFaultOpMap().containsKey(boundFault)) { ohair@286: messageSet.add( ohair@286: new BuilderHandlerMessageScope( ohair@286: getPolicyURIs(getHandlers4BindingFaultOpMap().get(boundFault), modelContext) ohair@286: ,getPolicyModels() ohair@286: ,new WSDLBoundFaultContainer(boundFault, boundOperation) ohair@286: ,BuilderHandlerMessageScope.Scope.FaultMessageScope ohair@286: ,service.getName() ohair@286: ,port.getName() ohair@286: ,operationName ohair@286: ,faultName) ohair@286: ); ohair@286: } ohair@286: } // end foreach binding operation fault msg ohair@286: // end fault message scope ohair@286: ohair@286: } // end foreach boundOperation in port ohair@286: } // endif port.getBinding() != null ohair@286: } // end foreach port in service ohair@286: } // end foreach service in wsdl ohair@286: // Add handlers for wsdl:message elements ohair@286: for (BuilderHandlerMessageScope scopeHandler : messageSet) { ohair@286: getPolicyMapBuilder().registerHandler(scopeHandler); ohair@286: } ohair@286: } catch(PolicyException e) { ohair@286: LOGGER.logSevereException(e); ohair@286: } ohair@286: // End-preparation of policy map builder ohair@286: ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: ohair@286: // time to read possible config file and do alternative selection (on client side) ohair@286: @Override ohair@286: public void postFinished(final WSDLParserExtensionContext context) { ohair@286: // finally register the PolicyMap on the WSDLModel mkos@408: EditableWSDLModel wsdlModel = context.getWSDLModel(); ohair@286: PolicyMap effectiveMap; ohair@286: try { ohair@286: if(context.isClientSide()) ohair@286: effectiveMap = context.getPolicyResolver().resolve(new PolicyResolver.ClientContext(policyBuilder.getPolicyMap(),context.getContainer())); ohair@286: else ohair@286: effectiveMap = context.getPolicyResolver().resolve(new PolicyResolver.ServerContext(policyBuilder.getPolicyMap(), context.getContainer(),null)); mkos@408: wsdlModel.setPolicyMap(effectiveMap); ohair@286: } catch (PolicyException e) { ohair@286: LOGGER.logSevereException(e); ohair@286: throw LOGGER.logSevereException(new WebServiceException(PolicyMessages.WSP_1007_POLICY_EXCEPTION_WHILE_FINISHING_PARSING_WSDL(), e)); ohair@286: } ohair@286: try { ohair@286: PolicyUtil.configureModel(wsdlModel,effectiveMap); ohair@286: } catch (PolicyException e) { ohair@286: LOGGER.logSevereException(e); ohair@286: throw LOGGER.logSevereException(new WebServiceException(PolicyMessages.WSP_1012_FAILED_CONFIGURE_WSDL_MODEL(), e)); ohair@286: } ohair@286: LOGGER.exiting(); ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Reads policy reference URIs from PolicyURIs attribute and returns them ohair@286: * as a String array returns null if there is no such attribute. This method ohair@286: * will attempt to check for the attribute in every supported policy namespace. ohair@286: * Resulting array of URIs is concatenation of URIs defined in all found ohair@286: * PolicyURIs attribute version. ohair@286: */ ohair@286: private String[] getPolicyURIsFromAttr(final XMLStreamReader reader) { ohair@286: final StringBuilder policyUriBuffer = new StringBuilder(); ohair@286: for (NamespaceVersion version : NamespaceVersion.values()) { ohair@286: final String value = reader.getAttributeValue(version.toString(), XmlToken.PolicyUris.toString()); ohair@286: if (value != null) { ohair@286: policyUriBuffer.append(value).append(" "); ohair@286: } ohair@286: } ohair@286: return (policyUriBuffer.length() > 0) ? policyUriBuffer.toString().split("[\\n ]+") : null; ohair@286: } ohair@286: ohair@286: }