diff options
author | nash <nash@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-22 14:13:35 +0000 |
---|---|---|
committer | nash <nash@13f79535-47bb-0310-9956-ffa450edef68> | 2008-06-22 14:13:35 +0000 |
commit | 517609638e03d901d797c993bf8cacbf99d5b37a (patch) | |
tree | d2d01cde7bc2403e828b3534a21d4e20e0d52ea3 /java/sca/modules/binding-ws-axis2/src/main | |
parent | c6e6aabeccfad8d564cffb296ab4d5482d493d76 (diff) |
Fix TUSCANY-2418
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@670360 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/binding-ws-axis2/src/main')
2 files changed, 172 insertions, 106 deletions
diff --git a/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/Axis2ServiceProvider.java b/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/Axis2ServiceProvider.java index df7f339c62..1bfd20e937 100644 --- a/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/Axis2ServiceProvider.java +++ b/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/Axis2ServiceProvider.java @@ -27,6 +27,7 @@ import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -38,6 +39,8 @@ import java.util.logging.Logger; import javax.wsdl.Definition; import javax.wsdl.Import; import javax.wsdl.Port; +import javax.wsdl.Types; +import javax.wsdl.extensions.UnknownExtensibilityElement; import javax.wsdl.extensions.soap.SOAPAddress; import javax.wsdl.extensions.soap12.SOAP12Address; import javax.xml.namespace.QName; @@ -85,6 +88,7 @@ import org.apache.tuscany.sca.host.http.ServletHost; import org.apache.tuscany.sca.interfacedef.Interface; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.java.JavaInterface; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.invocation.MessageFactory; import org.apache.tuscany.sca.policy.PolicySet; @@ -98,12 +102,22 @@ import org.apache.tuscany.sca.runtime.ReferenceParameters; import org.apache.tuscany.sca.runtime.RuntimeComponent; import org.apache.tuscany.sca.runtime.RuntimeComponentService; import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.xsd.XSDefinition; +import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaExternal; import org.apache.ws.security.WSSecurityEngineResult; import org.apache.ws.security.handler.WSHandlerConstants; import org.apache.ws.security.handler.WSHandlerResult; import org.osoa.sca.ServiceRuntimeException; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + public class Axis2ServiceProvider { + public static final String IMPORT_TAG = "import"; + public static final String INCLUDE_TAG = "include"; private static final Logger logger = Logger.getLogger(Axis2ServiceProvider.class.getName()); @@ -129,6 +143,21 @@ public class Axis2ServiceProvider { private static final String DEFAULT_QUEUE_CONNECTION_FACTORY = "TuscanyQueueConnectionFactory"; + //Schema element names + public static final String ELEM_SCHEMA = "schema"; + + //Schema URI + public static final String NS_URI_XSD_1999 = "http://www.w3.org/1999/XMLSchema"; + public static final String NS_URI_XSD_2000 = "http://www.w3.org/2000/10/XMLSchema"; + public static final String NS_URI_XSD_2001 = "http://www.w3.org/2001/XMLSchema"; + + //Schema QNames + public static final QName Q_ELEM_XSD_1999 = new QName(NS_URI_XSD_1999, ELEM_SCHEMA); + public static final QName Q_ELEM_XSD_2000 = new QName(NS_URI_XSD_2000, ELEM_SCHEMA); + public static final QName Q_ELEM_XSD_2001 = new QName(NS_URI_XSD_2001, ELEM_SCHEMA); + public static final List<QName> XSD_QNAME_LIST = + Arrays.asList(new QName[] {Q_ELEM_XSD_1999, Q_ELEM_XSD_2000, Q_ELEM_XSD_2001}); + public Axis2ServiceProvider(RuntimeComponent component, AbstractContract contract, WebServiceBinding wsBinding, @@ -290,8 +319,7 @@ public class Axis2ServiceProvider { URI uriPath = new URI(endpointURL); String stringURIPath = uriPath.getPath(); - //[nash] Need a leading slash for WSDL imports to work with ?wsdl - /* + /* [nash] Need a leading slash for WSDL imports to work with ?wsdl // remove any "/" from the start of the path if (stringURIPath.startsWith("/")) { stringURIPath = stringURIPath.substring(1, stringURIPath.length()); @@ -425,6 +453,9 @@ public class Axis2ServiceProvider { } } + // Add schema information to the AxisService (needed for "?xsd=" support) + addSchemas(wsBinding.getWSDLDefinition(), axisService); + // Use the existing WSDL Parameter wsdlParam = new Parameter(WSDLConstants.WSDL_4_J_DEFINITION, null); wsdlParam.setValue(definition); @@ -432,6 +463,12 @@ public class Axis2ServiceProvider { Parameter userWSDL = new Parameter("useOriginalwsdl", "true"); axisService.addParameter(userWSDL); + // Modify schema imports and includes to add "servicename?xsd=" prefix. + // Axis2 does this for schema extensibility elements, but Tuscany has + // overriden the WSDl4J deserializer to create UnknownExtensibilityElement + // elements in place of these. + modifySchemaImportsAndIncludes(definition, name); + // Axis2 1.3 has a bug with returning incorrect values for the port // addresses. To work around this, compute the values here. Parameter modifyAddr = new Parameter("modifyUserWSDLPortAddress", "false"); @@ -447,6 +484,74 @@ public class Axis2ServiceProvider { return axisService; } + private void addSchemas(WSDLDefinition wsdlDef, AxisService axisService) { + for (XSDefinition xsDef : wsdlDef.getXmlSchemas()) { + axisService.addSchema(xsDef.getSchema()); + updateSchemaRefs(xsDef.getSchema(), axisService.getName()); + } + for (WSDLDefinition impDef : wsdlDef.getImportedDefinitions()) { + addSchemas(impDef, axisService); + } + } + + private void updateSchemaRefs(XmlSchema parentSchema, String name) { + for (Iterator iter = parentSchema.getIncludes().getIterator(); iter.hasNext();) { + Object obj = iter.next(); + if (obj instanceof XmlSchemaExternal) { + XmlSchemaExternal extSchema = (XmlSchemaExternal)obj; + String location = extSchema.getSchemaLocation(); + if (location.indexOf(":/") < 0 & location.indexOf("?xsd=") < 0) { + extSchema.setSchemaLocation(name + "?xsd=" + location); + } + updateSchemaRefs(extSchema.getSchema(), name); + } + } + } + + private void modifySchemaImportsAndIncludes(Definition definition, String name){ + // adjust the schema locations in types section + Types types = definition.getTypes(); + if (types != null) { + for (Iterator iter = types.getExtensibilityElements().iterator(); iter.hasNext();) { + Object ext = iter.next(); + if (ext instanceof UnknownExtensibilityElement && + XSD_QNAME_LIST.contains(((UnknownExtensibilityElement)ext).getElementType())) { + changeLocations(((UnknownExtensibilityElement)ext).getElement(), name); + } + } + } + for (Iterator iter = definition.getImports().values().iterator(); iter.hasNext();) { + Vector values = (Vector)iter.next(); + for (Iterator valuesIter = values.iterator(); valuesIter.hasNext();) { + Import wsdlImport = (Import)valuesIter.next(); + modifySchemaImportsAndIncludes(wsdlImport.getDefinition(), name); + } + } + } + + private void changeLocations(Element element, String name) { + NodeList nodeList = element.getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + String tagName = nodeList.item(i).getLocalName(); + if (IMPORT_TAG.equals(tagName) || INCLUDE_TAG.equals(tagName)) { + processImport(nodeList.item(i), name); + } + } + } + + private void processImport(Node importNode, String name) { + NamedNodeMap nodeMap = importNode.getAttributes(); + for (int i = 0; i < nodeMap.getLength(); i++) { + Node attribute = nodeMap.item(i); + if (attribute.getNodeName().equals("schemaLocation")) { + String location = attribute.getNodeValue(); + if (location.indexOf(":/") < 0 & location.indexOf("?xsd=") < 0) { + attribute.setNodeValue(name + "?xsd=" + location); + } + } + } + } + private String getPortAddress(Port port) { Object ext = port.getExtensibilityElements().get(0); if (ext instanceof SOAPAddress) { @@ -606,8 +711,8 @@ public class Axis2ServiceProvider { } private void createPolicyHandlers() throws IllegalAccessException, - InstantiationException, - ClassNotFoundException { + InstantiationException, + ClassNotFoundException { if (wsBinding instanceof PolicySetAttachPoint) { PolicySetAttachPoint policiedBinding = (PolicySetAttachPoint)wsBinding; PolicyHandler policyHandler = null; diff --git a/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/TuscanyListingAgent.java b/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/TuscanyListingAgent.java index 8e2e40c753..2342e7f6ef 100644 --- a/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/TuscanyListingAgent.java +++ b/java/sca/modules/binding-ws-axis2/src/main/java/org/apache/tuscany/sca/binding/ws/axis2/TuscanyListingAgent.java @@ -58,6 +58,7 @@ import org.apache.axis2.wsdl.WSDLConstants; import org.apache.neethi.Policy; import org.apache.neethi.PolicyRegistry; import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaExternal; /** * A Tuscany specific Axis2 ListingAgent as the Axis2 one does not work @@ -86,6 +87,68 @@ public class TuscanyListingAgent extends ListingAgent { return serviceName; } + /** + * Override ?xsd processing so that WSDL documents with XSD imports + * and includes work correctly. When we move to Axis2 1.4, we may + * be able to use SchemaSupplier to do this in a cleaner way. + */ + @Override + public void processListService(HttpServletRequest req, + HttpServletResponse res) + throws IOException, ServletException { + + String url = req.getRequestURL().toString(); + String query = req.getQueryString(); + int xsd = query.indexOf("xsd"); + if (xsd >= 0) { + String serviceName = extractServiceName(url); + HashMap services = configContext.getAxisConfiguration().getServices(); + if ((services != null) && !services.isEmpty()) { + Object serviceObj = services.get(serviceName); + if (serviceObj != null) { + String xsds = req.getParameter("xsd"); + if (xsds != null && !"".equals(xsds)) { + // a schema name (perhaps with path) is present + AxisService axisService = (AxisService)serviceObj; + ArrayList schemas = axisService.getSchema(); + for (Object rootSchema : axisService.getSchema()) { + XmlSchema schema = getSchema(((XmlSchema)rootSchema), xsds); + if (schema != null) { + // found the schema + res.setContentType("text/xml"); + OutputStream out = res.getOutputStream(); + schema.write(new OutputStreamWriter(out, "UTF8")); + out.flush(); + out.close(); + return; + } + } + } + } + } + } + // in all other cases, delegate to the Axis2 code + super.processListService(req, res); + } + + private XmlSchema getSchema(XmlSchema parentSchema, String name) { + for (Iterator iter = parentSchema.getIncludes().getIterator(); iter.hasNext();) { + Object obj = iter.next(); + if (obj instanceof XmlSchemaExternal) { + XmlSchemaExternal extSchema = (XmlSchemaExternal)obj; + if (extSchema.getSchemaLocation().endsWith(name)) { + return extSchema.getSchema(); + } else { + XmlSchema schema = getSchema(extSchema.getSchema(), name); + if (schema != null) { + return schema; + } + } + } + } + return null; + } + private String findAxisServiceName(String path) { HashMap services = configContext.getAxisConfiguration().getServices(); if (services == null) { @@ -130,106 +193,4 @@ public class TuscanyListingAgent extends ListingAgent { } } - private String extractHostAndPort(String filePart, boolean isHttp) { - int ipindex = filePart.indexOf("//"); - String ip = null; - if (ipindex >= 0) { - ip = filePart.substring(ipindex + 2, filePart.length()); - int seperatorIndex = ip.indexOf(":"); - int slashIndex = ip.indexOf("/"); - String port; - if (seperatorIndex >= 0) { - port = ip.substring(seperatorIndex + 1, slashIndex); - ip = ip.substring(0, seperatorIndex); - } else { - ip = ip.substring(0, slashIndex); - port = "80"; - } - if (isHttp) { - configContext.setProperty(RUNNING_PORT, port); - } - } - return ip; - } - - private Policy findPolicy(String id, AxisDescription des) { - - List policyElements = des.getPolicyInclude().getPolicyElements(); - PolicyRegistry registry = des.getPolicyInclude().getPolicyRegistry(); - - Object policyComponent; - - Policy policy = registry.lookup(id); - - if (policy != null) { - return policy; - } - - for (Iterator iterator = policyElements.iterator(); iterator.hasNext();) { - policyComponent = iterator.next(); - - if (policyComponent instanceof Policy) { - // policy found for the id - - if (id.equals(((Policy) policyComponent).getId())) { - return (Policy) policyComponent; - } - } - } - - AxisDescription child; - - for (Iterator iterator = des.getChildren(); iterator.hasNext();) { - child = (AxisDescription) iterator.next(); - policy = findPolicy(id, child); - - if (policy != null) { - return policy; - } - } - - return null; - } - - /** - * Hack to get ?wsdl working with soap 1.2 - * Fixed in Axis2 1.3 - */ - private void patchSOAP12Port(AxisService as) throws AxisFault { - Parameter wsld4jdefinition = as.getParameter(WSDLConstants.WSDL_4_J_DEFINITION); - Definition definition = (Definition) wsld4jdefinition.getValue(); - setPortAddress(definition, null, as); - } - - /** - * This is a copy of the AxisService setPortAddress patched to work with SOAP 1.2 Addresses - * Fixed in Axis2 1.3 - */ - private void setPortAddress(Definition definition, String requestIP, AxisService axisService) throws AxisFault { - Iterator serviceItr = definition.getServices().values().iterator(); - while (serviceItr.hasNext()) { - Service serviceElement = (Service) serviceItr.next(); - Iterator portItr = serviceElement.getPorts().values().iterator(); - while (portItr.hasNext()) { - Port port = (Port) portItr.next(); - List list = port.getExtensibilityElements(); - for (int i = 0; i < list.size(); i++) { - Object extensibilityEle = list.get(i); - String locationURI = null; - if (requestIP == null) { - locationURI = axisService.getEPRs()[0]; - } else { -// can't do this as the method's not visible, but Tuscany doesn't use this path anyway -// locationURI = axisService.getEPRs(requestIP)[0]); - } - if (extensibilityEle instanceof SOAPAddress) { - ((SOAPAddress) extensibilityEle).setLocationURI(locationURI); - } else if (extensibilityEle instanceof SOAP12Address) { - ((SOAP12Address) extensibilityEle).setLocationURI(locationURI); - } - } - } - } - } - } |