Showing posts with label XSD. Show all posts
Showing posts with label XSD. Show all posts

Monday, 10 April 2017

Modifying the Namespace prefix while marshaling the JAXB object

There would be times when we want to see a meaningful name to namespace prefixes given by JAXB marshaling API. By default for any QName which does not have prefix attribute, JAXB will give prefix in this format 'ns<number>' eg: ns0, ns1 etc.

To achieve it, there is a property called MarshallerProperties.NAMESPACE_PREFIX_MAPPER ("eclipselink.namespace-prefix-mapper"); this is for JAXB reference implementation by eclipselink. If it is a different vendor, the property name will vary. For this key, we need to set a value as an instance of org.eclipse.persistence.oxm.NamespacePrefixMapper class.


  jc = JAXBContext.newInstance("com.mypackage.jaxb");
  m = jc.createMarshaller();
  m.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER,
                new NamespacePrefixMapper() {
                   // Map to store the namespace and prefixes
                    Map<String, String> namespacePrefixMap = new HashMap<>();
                        @Override
                        public String getPreferredPrefix(String namespaceuri, String suggestion, boolean requiredPrefix) {      
                            String prefix = namespacePrefixMap.get(namespaceuri);
                            if (null == prefix) {
                               prefix = "myprefix" + suggestion;
                               namespacePrefixMap.put(namespaceuri, prefix);
                             }
                             return prefix;
                        }
               });

Monday, 26 December 2016

Exposing Endpoints in both SOAP1.0 and SOAP1.2 version in Oracle SOA Suite

Sometimes we want to expose SOAP endpoint URL in both SOAP1.0 and SOAP1.2 versions. In this blog, I am trying to explain how we can achieve it when we expose SOAP endpoints using Oracle SOA Suite.

The configuration to mention the version of SOAP is available in composite.xml file. Whoever worked on creating a SOA project using Oracle SOA Suite would know about composite.xml file, still I will take as an opportunity to explain what composite.xml mean and what it contains.

Composite.xml is an auto generated files which is like a master xml file. This file describes the entire composite assembly of services, service components, references, and wires. 

Composite.xml contains following parts

1. Defines composite name, version and namespaces
2. Importing dependent WSDLs
3. Defining list of services being exposed as an endpoints
4. Composite properties if any
5. Component implementation details
6. References detail of services being used by this composite
7. Wiring between service and components

If we observe the below composite.xml file, we could see the soapVersion attribute in <binding.ws> tag under <service> definition part in composite.xml. This is the one which will be referred by WebLogic server while deploying the composite application and appropriate SOAP service and binding information will be added dynamically to the WSDL document.  

<?xml version="1.0" encoding="UTF-8" ?>
<!-- Generated by Oracle SOA Modeler version 12.1.3.0.0 at [11/24/16 10:38 AM]. -->

<composite name="ThrowingFaults"
           revision="2.0"
           label="2016-11-24_10-38-01_605"
           mode="active"
           state="on"
           xmlns="http://xmlns.oracle.com/sca/1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
           xmlns:orawsp="http://schemas.oracle.com/ws/2006/01/policy"
           xmlns:ui="http://xmlns.oracle.com/soa/designer/"
           xmlns:sca-ext="http://xmlns.oracle.com/sca/1.0-ext">
  <import namespace="http://xmlns/ThrowMeFault"
          location="WSDLs/ThrowMeFault.wsdl" importType="wsdl"/>
  <service name="throwmefault_client_ep" ui:wsdlLocation="WSDLs/ThrowMeFault.wsdl">
    <interface.wsdl interface="http://xmlns/ThrowMeFault#wsdl.interface(ThrowMeFault)"/>       <binding.ws port="http://xmlns/ThrowMeFault#wsdl.endpoint(throwmefault_client_ep/ThrowMeFault_pt)" soapVersion="1.1"/>
  </service>
  <service name="throwmefault12_client_ep" ui:wsdlLocation="WSDLs/ThrowMeFault.wsdl">
    <interface.wsdl interface="http://xmlns/ThrowMeFault#wsdl.interface(ThrowMeFault)"/>
    <binding.ws port="http://xmlns/ThrowMeFault#wsdl.endpoint(throwmefault_client/ThrowMeFault_pt)" soapVersion="1.2">     
    </binding.ws>
  </service>
  <property name="productVersion" type="xs:string" many="false">12.1.3.0.0</property>
  <property name="compositeID" type="xs:string" many="false">cfeae6cc-91a3-4ef0-a143-be9a62514b5b</property>
  <component name="ThrowMeFault" version="1.1">
    <implementation.bpel src="BPEL/ThrowMeFault.bpel"/>
    <componentType>
      <service name="throwmefault_client" ui:wsdlLocation="WSDLs/ThrowMeFault.wsdl">
        <interface.wsdl interface="http://xmlns/ThrowMeFault#wsdl.interface(ThrowMeFault)"/>
      </service>           
    </componentType>
    <property name="bpel.config.transaction" type="xs:string" many="false">required</property>
  </component>
  <wire>
    <source.uri>throwmefault_client_ep</source.uri>
    <target.uri>ThrowMeFault/throwmefault_client</target.uri>
  </wire>
  <wire>
    <source.uri>throwmefault12_client_ep</source.uri>
    <target.uri>ThrowMeFault/throwmefault_client</target.uri>
  </wire>
</composite>

If we do not mention the version of SOAP in composite.xml then the default one would be SOAP1.1.

Tuesday, 3 May 2016

elementFormDefault attribute in XSD Schema definition

This attribute (elementFormDefault) is useful when we are defining our own <complexType> in our schema and the main purpose is to decide whether the <element> inside <complexType> should be in targetNamespace or global namespace

Example:
Sample XSD:
<?xml version = '1.0' encoding = 'UTF-8'?>
<xsd:schema xmlns:ns0="http://ticket.com/ns" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ticket.com/ns">  
   <xsd:complexType name="routeTicket">
      <xsd:sequence>
         <xsd:element name="ticket" type="xsd:string" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="routeTicketResponse">
      <xsd:sequence>
         <xsd:element name="return" type="xsd:string" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="routeTicket" type="ns0:routeTicket"/>
   <xsd:element name="routeTicketResponse" type="ns0:routeTicketResponse"/>

</xsd:schema>


when elementFormDefault="unqualified"
when elementFormDefault="qualified"
Valid XML for the given XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:routeTicket xmlns:xs="http://ticket.com/ns">
  <ticket>str1234</ticket>
</xs:routeTicket>

Valid XML for the given XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:routeTicket xmlns:xs="http://ticket.com/ns">
  <xs:ticket>str1234</xs:ticket>
</xs:routeTicket>

XPATH to access ticket

/xs:routeTicket/ticket

XPATH to access ticket

/xs:routeTicket/xs:ticket


Useful online tools