This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Endpoint Look-up Sample
This sample explains how to use the Governance API of WSO2 Governance Registry to create a simple webservice which is capable of looking up service endpoints. WSO2 Governance Registry contains an Apache Axis2 based runtime as a part of the WSO2 Carbon Framework. You can deploy web services on this runtime, which can be used as internal endpoints to receive notifications generated on the WSO2 Governance Registry. We will be reusing the code of the Handler Sample in this example. This sample requires Apache Maven. See Installation Prerequisites for more information on downloading this. This example also explains how to use soapUI, which is a very useful tool for testing Web services.
The following are the list of JAR dependencies for this sample:
- axiom_1.2.11-wso2v3.jar
- axis2_1.6.1-wso2v7.jar
- commons-codec_1.4.0-wso2v1.jar
- commons-httpclient_3.1.0-wso2v2.jar
- httpcore_4.1.0-wso2v1.jar
- neethi_2.0.4-wso2v4.jar
- org.wso2.carbon.authenticator.stub_4.0.0.jar
- org.wso2.carbon.base_4.0.0.jar
- org.wso2.carbon.core.common_4.0.0.jar
- org.wso2.carbon.governance.api_4.0.5.jar
- org.wso2.carbon.logging_4.0.0.jar
- org.wso2.carbon.registry.api_4.0.0.jar
- org.wso2.carbon.registry.core_4.0.5.jar
- org.wso2.carbon.registry.ws.client_4.0.2.jar
- org.wso2.carbon.registry.ws.stub_4.0.0.jar
- org.wso2.carbon.user.api_4.0.3.jar
- org.wso2.carbon.user.core_4.0.5.jar
- org.wso2.securevault_1.0.0-wso2v2.jar
- woden_1.0.0.M8-wso2v1.jar
- wsdl4j_1.6.2-wso2v4.jar
- wss4j_1.5.11-wso2v5.jar
- XmlSchema_1.4.7-wso2v2.jar
Instructions
- Navigate to
GREG_HOME/
samples/handler/src
to find the source code of the Handler Sample. Add the following dependency to your POM file:
Project Object Model (POM) is an XML representation of a Maven project held in a file named pom.xml. You should find POM file by the name pom.xml inside
GREG_HOME/
samples/handler/src
.<dependency> <groupId>org.wso2.carbon</groupId> <artifactId>org.wso2.carbon.governance.api</artifactId> <version>4.0.1</version> </dependency>
Add the following plugin to your POM file:
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> </plugin>
Comment-out the following in your POM file:
<!--Fragment-Host>org.wso2.carbon.registry.core</Fragment-Host-->
The Fragment-Host Bundle Manifest Header declares this sample bundle to be a Fragment of the Registry Kernel. This would mean that the sample handler bundle will become an extension to the Registry Kernel. However, bundles containing
services.xml
files would not be deployed until the Apache Axis2 runtime has been initialized. Due to OSGi bundle start-up order, it is required that the Registry Kernel starts up before the Apache Axis2 runtime.Since the Fragment now also being an extension to the Registry Kernel, the combination of this bundle and the Registry Kernel (plus any other fragments) should start up before the Apache Axis2 runtime. But, due to the bundle not getting deployed until the Apache Axis2 runtime has been initialized, it will create a deadlock situation. Due to this reason, we have to get rid of the Fragment-Host header.
Add the following content to a file named
services.xml
inGREG_HOME/samples/handler/src/resources/META-INF
:<serviceGroup> <service name="EndpointLookupService" scope="transportsession"> <transports> <transport>http</transport> </transports> <parameter name="ServiceClass" locked="false"> org.wso2.carbon.registry.samples.services.EndpointLookupService </parameter> </service> </serviceGroup>
Add a new Java Class named
EndpointLookupServiceComponent.java
into theGREG_HOME/samples/handler/src/src/main/java/org/wso2/carbon/registry/samples/services/
directory with the following configurations:package org.wso2.carbon.registry.samples.services; import org.wso2.carbon.registry.core.service.RegistryService; /** * @scr.component name="org.wso2.carbon.registry.samples.endpoint.lookup" immediate="true" * @scr.reference name="registry.service" interface="org.wso2.carbon.registry.core.service.RegistryService" * cardinality="1..1" policy="dynamic" bind="setRegistryService" unbind="unsetRegistryService" */ public class EndpointLookupServiceComponent { private static RegistryService registryService; protected void setRegistryService(RegistryService registryService) { EndpointLookupServiceComponent.registryService = registryService; } protected void unsetRegistryService(RegistryService registryService) { EndpointLookupServiceComponent.registryService = null; } public static RegistryService getRegistryService() { return registryService; } }
See also Declarative Services and Apache Maven SCR Plugin.
Add another new Java Class named
EndpointLookupService.java
into theGREG_HOME/samples/handler/src/src/main/java/org/wso2/carbon/registry/samples/services/
directory. This has the following configurations:package org.wso2.carbon.registry.samples.services; import org.wso2.carbon.governance.api.exception.GovernanceException; import org.wso2.carbon.governance.api.services.ServiceFilter; import org.wso2.carbon.governance.api.services.ServiceManager; import org.wso2.carbon.governance.api.services.dataobjects.Service; import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.exceptions.RegistryException; import java.util.LinkedList; import java.util.List; public class EndpointLookupService { private Registry governanceRegistry; public EndpointLookupService() { try { governanceRegistry = EndpointLookupServiceComponent.getRegistryService() .getGovernanceSystemRegistry(); } catch (RegistryException e) { e.printStackTrace(); } } public String[] getEndpoints(final String serviceName, final String serviceNamespace, final String version, final String environment) throws GovernanceException { ServiceManager manager = new ServiceManager(governanceRegistry); Service[] services = manager.findServices(new ServiceFilter() { public boolean matches(Service service) throws GovernanceException { return serviceName.equals(service.getQName().getLocalPart()) && (serviceNamespace == null || serviceNamespace.length() == 0 || serviceNamespace.equals(service.getQName().getNamespaceURI())) && (version == null || version.length() == 0 || version.equals(service.getAttribute("overview_version"))); } }); if (services != null && services.length > 0) { List<String> endpoints = new LinkedList<String>(); for (Service service : services) { String[] entries = service.getAttributes("endpoints_entry"); if (entries != null && entries.length > 0) { for (String entry : entries) { int idx = entry.indexOf(":"); if (entry.substring(0, idx).equals(environment)) { endpoints.add(entry.substring(idx + 1)); } } } } return endpoints.toArray(new String[endpoints.size()]); } return new String[0]; } }
Compile the source code by running the following command inside
GREG_HOME/
samples/handler/src
:mvn clean install
The command
mvn clean install
will trigger an Apache Maven Build in your command line. This requires you having installed Apache Maven. See Installation Prerequisites for more information on downloading this.A successful run of Apache Maven will generate a report similar to the following:
- Copy the
GREG_HOME/
samples/handler/src/target/
org.wso2.carbon.registry.samples.handler-4.5.0.jar
intoGREG_HOME/repository/components/dropins
. - Start the server and observe the command prompt. See Running the Product on Windows or Running the Product on Linux.
Running the sample
After the sample has been successfully deployed and the server has started, open a browser Window and type the following URL:
http://localhost:9763/services/EndpointLookupService/getEndpoints?serviceName=SimpleStockQuoteService&environment=Dev
In this step, we only have passed in the parameters serviceName and environment. This sample service also supports serviceNamespace and version as optional parameters.
This should produce an output similar to the following:
<ns:getEndpointsResponse xmlns:ns="http://services.samples.registry.carbon.wso2.org" xmlns:ax2549="http://exception.api.governance.carbon.wso2.org/xsd" xmlns:ax2551="http://api.registry.carbon.wso2.org/xsd" xmlns:ax2550="http://exceptions.core.registry.carbon.wso2.org/xsd"> <ns:return>http://localhost:8280/SimpleStockQuoteService</ns:return> <ns:return>https://localhost:8243/SimpleStockQuoteService</ns:return> </ns:getEndpointsResponse>
Web services hosted on an Apache Axis2 runtime can be invoked both via REST as well as SOAP. In the steps above, we have made a REST invocation using a Web Browser. You can invoke the same service using SOAP. To see the WSDL for this service, open the following URL on a web browser.
http://localhost:9763/services/EndpointLookupService?wsdl
Follow the steps below to learn how to invoke this sample through SOAP using soapUI.
- Create a new project using soapUI.
Simply copy the WSDL URL in the New soapUI Project dialog box. Use the following URL:
http://localhost:9763/services/EndpointLookupService?wsdl
- Press the "OK" button to create the project. Make sure to keep the
Create Requests
option selected. - Expand the getEndpoints operations and right-click the auto-generated request to display the request editor.
Fill in the following payload as the request:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ser="http://services.samples.registry.carbon.wso2.org"> <soap:Header/> <soap:Body> <ser:getEndpoints> <ser:serviceName>SimpleStockQuoteService</ser:serviceName> <ser:environment>Dev</ser:environment> </ser:getEndpoints> </soap:Body> </soap:Envelope>
- Click the submit button to make the request to WSO2 Governance Registry.
- Obtain the response from the server.