Search in sources :

Example 21 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class ProviderHelper method init.

@SuppressWarnings("unchecked")
public void init(ClassLoader parentClassLoader, ServletContext sc, boolean mtomEnabled) {
    this.parentClassLoader = parentClassLoader;
    this.mtomEnabled = mtomEnabled;
    InputStream xrServiceStream = initXRServiceStream(parentClassLoader, sc);
    DBWSModelProject xrServiceModelProject = new DBWSModelProject();
    XMLContext xmlContext = new XMLContext(xrServiceModelProject);
    XMLUnmarshaller unmarshaller = xmlContext.createUnmarshaller();
    XRServiceModel xrServiceModel;
    try {
        xrServiceModel = (XRServiceModel) unmarshaller.unmarshal(xrServiceStream);
    } catch (XMLMarshalException e) {
        // something went wrong parsing the eclipselink-dbws.xml - can't recover from that
        throw new WebServiceException(DBWSException.couldNotParseDBWSFile());
    } finally {
        try {
            xrServiceStream.close();
        } catch (IOException e) {
        // ignore
        }
    }
    xrSchemaStream = initXRSchemaStream(parentClassLoader, sc);
    try {
        // inherit xrService processing from XRServiceFactory
        buildService(xrServiceModel);
    } catch (Exception e) {
        // something went wrong building the service
        throw new WebServiceException(e);
    }
    // the xrService built by 'buildService' above is overridden to produce an
    // instance of DBWSAdapter (a sub-class of XRService)
    DBWSAdapter dbwsAdapter = (DBWSAdapter) xrService;
    // get inline schema from WSDL - has additional types for the operations
    try (InputStream wsdlInputStream = initWSDLInputStream(parentClassLoader, sc)) {
        StringWriter sw = new StringWriter();
        StreamSource wsdlStreamSource = new StreamSource(wsdlInputStream);
        Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(MATCH_SCHEMA)));
        StreamResult streamResult = new StreamResult(sw);
        t.transform(wsdlStreamSource, streamResult);
        SchemaModelProject schemaProject = new SchemaModelProject();
        XMLContext xmlContext2 = new XMLContext(schemaProject);
        unmarshaller = xmlContext2.createUnmarshaller();
        Schema extendedSchema = (Schema) unmarshaller.unmarshal(new StringReader(sw.toString()));
        dbwsAdapter.setExtendedSchema(extendedSchema);
    } catch (IOException | TransformerException e) {
        // that's Ok, WSDL may not contain inline schema
        xmlContext.getSession().getSessionLog().log(SessionLog.FINE, SessionLog.DBWS, "dbws_no_wsdl_inline_schema", e.getLocalizedMessage());
    }
    // an Invocation needs a mapping for its parameters - use XMLAnyCollectionMapping +
    // custom AttributeAccessor
    // NB - this code is NOt in it own initNNN method, cannot be overridden
    String tns = dbwsAdapter.getExtendedSchema().getTargetNamespace();
    Project oxProject = dbwsAdapter.getOXSession().getProject();
    XMLDescriptor invocationDescriptor = new XMLDescriptor();
    invocationDescriptor.setJavaClass(Invocation.class);
    NamespaceResolver nr = new NamespaceResolver();
    invocationDescriptor.setNamespaceResolver(nr);
    nr.put(SERVICE_NAMESPACE_PREFIX, tns);
    nr.setDefaultNamespaceURI(tns);
    XMLAnyCollectionMapping parametersMapping = new XMLAnyCollectionMapping();
    parametersMapping.setAttributeName("parameters");
    parametersMapping.setAttributeAccessor(new AttributeAccessor() {

        Project oxProject;

        DBWSAdapter dbwsAdapter;

        @Override
        public Object getAttributeValueFromObject(Object object) {
            return ((Invocation) object).getParameters();
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) {
            Invocation invocation = (Invocation) object;
            Vector<Object> values = (Vector<Object>) value;
            for (Iterator<Object> i = values.iterator(); i.hasNext(); ) {
                /* scan through values:
                   *  if XML conforms to something mapped, it an object; else it is a DOM Element
                   *  (probably a scalar). Walk through operations for the types, converting
                   *   as required. The 'key' is the local name of the element - for mapped objects,
                   *   have to get the element name from the schema context for the object
                   */
                Object o = i.next();
                if (o instanceof Element) {
                    Element e = (Element) o;
                    String key = e.getLocalName();
                    if ("theInstance".equals(key)) {
                        NodeList nl = e.getChildNodes();
                        for (int j = 0; j < nl.getLength(); j++) {
                            Node n = nl.item(j);
                            if (n.getNodeType() == Node.ELEMENT_NODE) {
                                try {
                                    Object theInstance = dbwsAdapter.getXMLContext().createUnmarshaller().unmarshal(n);
                                    if (theInstance instanceof XMLRoot) {
                                        theInstance = ((XMLRoot) theInstance).getObject();
                                    }
                                    invocation.setParameter(key, theInstance);
                                    break;
                                } catch (XMLMarshalException xmlMarshallException) {
                                    throw new WebServiceException(xmlMarshallException);
                                }
                            }
                        }
                    } else {
                        ClassDescriptor desc = null;
                        for (ClassDescriptor xdesc : oxProject.getOrderedDescriptors()) {
                            XMLSchemaReference schemaReference = xdesc instanceof XMLDescriptor ? ((XMLDescriptor) xdesc).getSchemaReference() : null;
                            if (schemaReference != null && schemaReference.getSchemaContext().equalsIgnoreCase(key)) {
                                desc = xdesc;
                                break;
                            }
                        }
                        if (desc != null) {
                            try {
                                Object theObject = dbwsAdapter.getXMLContext().createUnmarshaller().unmarshal(e, desc.getJavaClass());
                                if (theObject instanceof XMLRoot) {
                                    theObject = ((XMLRoot) theObject).getObject();
                                }
                                invocation.setParameter(key, theObject);
                            } catch (XMLMarshalException xmlMarshallException) {
                                throw new WebServiceException(xmlMarshallException);
                            }
                        } else {
                            String serviceName = e.getParentNode().getLocalName();
                            boolean found = false;
                            for (Operation op : dbwsAdapter.getOperationsList()) {
                                if (op.getName().equals(serviceName)) {
                                    for (Parameter p : op.getParameters()) {
                                        if (p.getName().equals(key)) {
                                            desc = dbwsAdapter.getDescriptorsByQName().get(p.getType());
                                            if (desc != null) {
                                                found = true;
                                            }
                                            break;
                                        }
                                    }
                                }
                                if (found) {
                                    break;
                                }
                            }
                            if (found) {
                                Object theObject = dbwsAdapter.getXMLContext().createUnmarshaller().unmarshal(e, desc.getJavaClass());
                                if (theObject instanceof XMLRoot) {
                                    theObject = ((XMLRoot) theObject).getObject();
                                }
                                invocation.setParameter(key, theObject);
                            } else {
                                // cant use e.getTextContent() - some DOM impls dont support it :-(
                                // String val = e.getTextContent();
                                StringBuilder sb = new StringBuilder();
                                NodeList childNodes = e.getChildNodes();
                                for (int idx = 0; idx < childNodes.getLength(); idx++) {
                                    if (childNodes.item(idx).getNodeType() == Node.TEXT_NODE) {
                                        sb.append(childNodes.item(idx).getNodeValue());
                                    }
                                }
                                invocation.setParameter(key, sb.toString());
                            }
                        }
                    }
                } else {
                    XMLDescriptor descriptor = (XMLDescriptor) oxProject.getDescriptor(o.getClass());
                    String key = descriptor.getDefaultRootElement();
                    int idx = key.indexOf(':');
                    if (idx != -1) {
                        key = key.substring(idx + 1);
                    }
                    invocation.setParameter(key, o);
                }
            }
        }

        public AttributeAccessor setProjectAndAdapter(Project oxProject, DBWSAdapter dbwsAdapter) {
            this.oxProject = oxProject;
            this.dbwsAdapter = dbwsAdapter;
            return this;
        }
    }.setProjectAndAdapter(oxProject, dbwsAdapter));
    parametersMapping.setKeepAsElementPolicy(KEEP_UNKNOWN_AS_ELEMENT);
    invocationDescriptor.addMapping(parametersMapping);
    oxProject.addDescriptor(invocationDescriptor);
    ((DatabaseSessionImpl) dbwsAdapter.getOXSession()).initializeDescriptorIfSessionAlive(invocationDescriptor);
    dbwsAdapter.getXMLContext().storeXMLDescriptorByQName(invocationDescriptor);
    // create SOAP message response handler of appropriate version
    responseWriter = new SOAPResponseWriter(dbwsAdapter);
    responseWriter.initialize();
}
Also used : Transformer(javax.xml.transform.Transformer) Invocation(org.eclipse.persistence.internal.xr.Invocation) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) SOAPElement(jakarta.xml.soap.SOAPElement) Element(org.w3c.dom.Element) SOAPBodyElement(jakarta.xml.soap.SOAPBodyElement) Node(org.w3c.dom.Node) Operation(org.eclipse.persistence.internal.xr.Operation) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) Vector(java.util.Vector) TransformerException(javax.xml.transform.TransformerException) WebServiceException(jakarta.xml.ws.WebServiceException) StreamResult(javax.xml.transform.stream.StreamResult) XMLContext(org.eclipse.persistence.oxm.XMLContext) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) InputStream(java.io.InputStream) XMLAnyCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) SOAPException(jakarta.xml.soap.SOAPException) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) SOAPFaultException(jakarta.xml.ws.soap.SOAPFaultException) WebServiceException(jakarta.xml.ws.WebServiceException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) DBWSException(org.eclipse.persistence.exceptions.DBWSException) Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) XMLSchemaReference(org.eclipse.persistence.oxm.schema.XMLSchemaReference) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) XRServiceModel(org.eclipse.persistence.internal.xr.XRServiceModel) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) Parameter(org.eclipse.persistence.internal.xr.Parameter) ValueObject(org.eclipse.persistence.internal.xr.ValueObject) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor)

Example 22 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class GenerateSingleSchemaTestCases method testElementFormUnqualifiedNoSchemaContext.

/**
 * Test global element generation.  Element form default is unqualified.
 * Element refs will be generated for local elements - the global
 * elements they refer to will have anonymous complex types generated.
 */
public void testElementFormUnqualifiedNoSchemaContext() throws Exception {
    Schema generatedSchema = null;
    Document tDoc = null;
    Document cDoc = null;
    try {
        boolean setSchemaContext = false;
        boolean setDefaultRootElement = true;
        SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
        props.addProperty(MYEMPTYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, false);
        Project prj = new TestProject(setSchemaContext, setDefaultRootElement);
        loginProject(prj);
        List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
        Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
        generatedSchema = generatedSchemas.get(MYEMPTYNS);
        assertNotNull("No schema was generated for namespace [" + MYEMPTYNS + "]", generatedSchema);
        // debugging
        // writeSchema(generatedSchema);
        tDoc = getDocument(generatedSchema);
        cDoc = getDocument(ELEMENT_FORM_UNQUALIFIED_NO_CTX_RESOURCE);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail();
    }
    assertNotNull("Schema to Document conversion failed", tDoc);
    assertNotNull("A problem occurred loading the control schema", cDoc);
    assertTrue("Schema comparsion failed", comparer.isSchemaEqual(cDoc, tDoc));
}
Also used : Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) SchemaModelGeneratorProperties(org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Document(org.w3c.dom.Document)

Example 23 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class GenerateSingleSchemaTestCases method testElementFormUnqualified.

/**
 * Test global complex type and global element generation.  Element form default
 * is unqualified.  Element refs will be generated for local elements whose
 * type is that of a global complex type.
 */
public void testElementFormUnqualified() throws Exception {
    Schema generatedSchema = null;
    Document tDoc = null;
    Document cDoc = null;
    try {
        boolean setSchemaContext = true;
        boolean setDefaultRootElement = true;
        Project prj = new TestProject(setSchemaContext, setDefaultRootElement);
        loginProject(prj);
        List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
        SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
        props.addProperty(MYEMPTYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, false);
        Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
        generatedSchema = generatedSchemas.get(MYEMPTYNS);
        assertNotNull("No schema was generated for namespace [" + MYEMPTYNS + "]", generatedSchema);
        // debugging
        // writeSchema(generatedSchema);
        tDoc = getDocument(generatedSchema);
        cDoc = getDocument(ELEMENT_FORM_UNQUALIFIED_RESOURCE);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail();
    }
    assertNotNull("Schema to Document conversion failed", tDoc);
    assertNotNull("A problem occurred loading the control schema", cDoc);
    assertTrue("Schema comparsion failed", comparer.isSchemaEqual(cDoc, tDoc));
}
Also used : Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) SchemaModelGeneratorProperties(org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties) Document(org.w3c.dom.Document)

Example 24 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class GenerateSingleSchemaTestCases method testElementFormQualified.

/**
 * Test global complex type and global element generation.  Element form default
 * is qualified.
 */
public void testElementFormQualified() throws Exception {
    Schema generatedSchema = null;
    Document tDoc = null;
    Document cDoc = null;
    try {
        boolean setSchemaContext = true;
        boolean setDefaultRootElement = true;
        SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
        props.addProperty(MYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, true);
        Project prj = new TestProject(setSchemaContext, setDefaultRootElement, MYNS);
        loginProject(prj);
        List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
        Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
        generatedSchema = generatedSchemas.get(MYNS);
        // debugging
        // writeSchema(generatedSchema);
        assertNotNull("No schema was generated for namespace [" + MYNS + "]", generatedSchema);
        tDoc = getDocument(generatedSchema);
        cDoc = getDocument(ELEMENT_FORM_QUALIFIED_RESOURCE);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail();
    }
    assertNotNull("Schema to Document conversion failed", tDoc);
    assertNotNull("A problem occurred loading the control schema", cDoc);
    assertTrue("Schema comparsion failed", comparer.isSchemaEqual(cDoc, tDoc));
}
Also used : Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) SchemaModelGeneratorProperties(org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Document(org.w3c.dom.Document)

Example 25 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class GenerateSingleSchemaTestCases method testElementFormQualifiedNoCtxNoDRE.

/**
 * No schema should be generated since there is no default root element
 * or schema context set on any descriptors
 */
public void testElementFormQualifiedNoCtxNoDRE() throws Exception {
    Schema generatedSchema = null;
    try {
        boolean setSchemaContext = false;
        boolean setDefaultRootElement = false;
        SchemaModelGeneratorProperties props = new SchemaModelGeneratorProperties();
        props.addProperty(MYNS, SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY, false);
        Project prj = new TestProject(setSchemaContext, setDefaultRootElement, MYNS);
        loginProject(prj);
        List<Descriptor> descriptorsToProcess = setupDescriptorList(prj);
        Map<String, Schema> generatedSchemas = sg.generateSchemas(descriptorsToProcess, props);
        generatedSchema = generatedSchemas.get(MYNS);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail();
    }
    assertNull("A schema should not have been generated", generatedSchema);
}
Also used : Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) SchemaModelGeneratorProperties(org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor)

Aggregations

Schema (org.eclipse.persistence.internal.oxm.schema.model.Schema)44 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)18 SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)17 Project (org.eclipse.persistence.sessions.Project)17 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)16 Element (org.eclipse.persistence.internal.oxm.schema.model.Element)15 ComplexType (org.eclipse.persistence.internal.oxm.schema.model.ComplexType)14 SchemaModelGeneratorProperties (org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties)13 XmlVirtualAccessMethodsSchema (org.eclipse.persistence.jaxb.xmlmodel.XmlVirtualAccessMethodsSchema)12 XMLContext (org.eclipse.persistence.oxm.XMLContext)12 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)10 Document (org.w3c.dom.Document)10 QName (javax.xml.namespace.QName)7 StreamSource (javax.xml.transform.stream.StreamSource)7 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)7 Sequence (org.eclipse.persistence.internal.oxm.schema.model.Sequence)6 TypeDefParticle (org.eclipse.persistence.internal.oxm.schema.model.TypeDefParticle)6 IOException (java.io.IOException)5 StringWriter (java.io.StringWriter)5 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)5