SCA

POX/POJSON Example

worldclock.c
Complete example source and XML files for basic POX or JSON service as described in reference manual
worldclock.wsdl
WSDL file
worldclock.composite
Composite file

worldclock.c

#include <stdio.h>

#include "SDO.h"
#include "SCA.h"

static int utc_timezone_offset(char *city);
static int calculate_time_diff(int hour, char *city, char *otherCity);

/**
 * Implementation of worldclock service. 
 */
DATAOBJECT getLocalTimeForCity(DATAOBJECT request) {
		
	SDOFACTORY df = getDataFactory();

	char *requestCity = getCStringByName(request, "city");
	int requestTime = getIntByName(request, "local-time");
	char *otherCity = getCStringByName(request, "city-to-request-time-for");

	int time_diff = calculate_time_diff(requestTime, requestCity, otherCity);

	// error handling: create a SOAP fault
	if (time_diff < -24 || time_diff > 24) {
		int compcode, reason;
		DATAOBJECT faultResponse = doAlloc(df,
			"http://www.w3.org/2001/XMLSchema",
			"anyType");
		setCStringByName(faultResponse,
			"faultstring",
			"unrecognized city name");
		setCStringByName(faultResponse,
			"faultcode",
			"soap:Client");
		SCASetFaultMessage("worldclock",
			"getLocalTimeForCity",
			"worldclock-fault",
			0,
			faultResponse,
			&compcode,
			&reason);
		return 0;
	}

	SDOXML xmlhlp = getXMLHelper(df);
	SDOXMLDOC doc = XMLDocAlloc(xmlhlp, 0, 0, "response");
	DATAOBJECT response = getXMLRootDataObject(doc);

	setCStringByName(response, "city", otherCity);
	setIntByName(response, "hour", time_diff);

	return response;
}

static int calculate_time_diff(int hour, char *city, char *otherCity) {
	return hour + utc_timezone_offset(city) - utc_timezone_offset(otherCity); 
}

static int utc_timezone_offset(char *city) {
	if (!strcmp("Bangalore", city)) return +5;
	if (!strcmp("Berlin", city)) return +1;
	if (!strcmp("San Francisco", city)) return -8;

	// ...

	// return 100 to indicate unknown argument city
	return 100;
}

worldclock.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions
  targetNamespace="http://www.example.com/worldclock.wsdl"
  xmlns="http://www.example.com/worldclock.wsdl"
  xmlns:t="http://www.example.com/worldclock.xsd"
  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
  xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
  xmlns:sca-c="http://docs.oasis-open.org/ns/opencsa/sca-c-cpp/c/200901"
  xsi:schemaLocation="http://www.w3.org/ns/wsdl http://www.w3.org/2007/06/wsdl/wsdl20.xsd">

	<wsdl:types>

		<xs:schema targetNamespace="http://www.example.com/worldclock.xsd"
			          elementFormDefault="qualified">

			<xs:element name="worldclockRequest" type="xs:anyType"/>
			<xs:element name="worldclockResponse" type="xs:anyType"/>
		</xs:schema>

	</wsdl:types>

	<wsdl:message name="worldclockRequest">
  		<wsdl:part name="request" element="t:worldclockRequest"/>
	</wsdl:message>

	<wsdl:message name="worldclockResponse">
  		<wsdl:part name="response" element="t:worldclockResponse"/>
	</wsdl:message>

	<wsdl:message name="worldclockFaultResponse">
  		<wsdl:part name="message" element="xs:string"/>
</wsdl:message>

<wsdl:portType name="worldclock-ops">
	<sca-c:bindings>
		<sca-c:enableWrapperStyle>false</sca-c:enableWrapperStyle>
	</sca-c:bindings>
	<wsdl:operation name="getLocalTimeForCity">
		<wsdl:input message="worldclock-request"/>
		<wsdl:output message="worldclock-response"/>
	</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="http-binding" type="worldclock-ops">

  	<http:binding verb="GET"/>

	<wsdl:operation name="getLocalTimeForCity">
		<http:operation location="getLocalTimeForCity"/>
		<wsdl:input>
			<http:urlEncoded/> <!-- note this is ignored on POST -->
		</wsdl:input>
		<wsdl:output>
			<mime:content type="text/xml"/>
			<mime:content type="application/json"/>
      		</wsdl:output>
	</wsdl:operation>
</wsdl:binding>

<wsdl:service name="worldclock">
	<wsdl:port name="worldclock-ops" binding="http-binding">
		<http:address location="http://localhost:9090/services/worldclock"/>
	</wsdl:port>
</wsdl:service>

</wsdl:definitions>

worldclock.composite

<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
           targetNamespace="http://www.pmsca.com" name="WorldclockComposite">

	<!-- expose worldclock as web service -->
	<service name="worldclock" promote="worldclock-impl">
		<interface.wsdl interface="http://www.pmsca.com/worldclock.wsdl#wsdl.interface(worldclock-ops)"/>
		<binding.ws port="http://www.pmsca.com/worldclock.wsdl#wsdl.endpoint(worldclock/worldclock-ops)"/>
	</service>

	<!-- worldclock implementation -->
	<component name="worldclock-impl">
		<service name="worldclock"/>
		<implementation.c module="worldclock" library="true"/>
	</component>
</composite>