SCA

REST Example

worldclock.c
Example service implementation code and XML artifacts; this contains the same service implementation C source code as previously; note in this simplified example both the GET and the POST HTTP verb are mapped to the same single C function (in a realistic example, it is expected that there are different implementation functions for the mapped HTTP verbs)
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);

	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("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=""/>
		<wsdl:input>
			<http:urlEncoded/>
		</wsdl:input>
		<wsdl:output>
			<mime:content type="text/xml"/>
			<mime:content type="application/json"/>
		</wsdl:output>
	</wsdl:operation>
</wsdl:binding>

<wsdl:portType name="worldclock-post-op">
	<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-post-binding" type="worldclock-post-op">

	<http:binding verb="POST"/>

	<wsdl:operation name="getLocalTimeForCity">
		<http:operation location=""/>
		<wsdl:input>
			<http:urlEncoded/>
		</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:port name="worldclock-post-op" binding="http-post-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">

	<!-- exposed worldclock via wsdl/SOAP -->
	<service name="worldclock" promote="worldclock-impl">
		<interface.wsdl interface="http://www.pmsca.com/worldclock.wsdl#wsdl.interface(worldclock-ops)"/>
		<binding.ws wsdlElement="http://www.pmsca.com/worldclock.wsdl#wsdl.port(worldclock/worldclock-ops)"/>
		<binding.ws wsdlElement="http://www.pmsca.com/worldclock.wsdl#wsdl.port(worldclock/worldclock-post-op)"/>
	</service>

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