Open Interfaces CCT SDK_img_0

CCT Open Interfaces SDK

Tutorial 2 - Web Service Subscriptions

1. Introduction. 2

2. SOA CCT Web Service Subscriptions Tutorial 2

Description of Tutorial 2

Notes about this tutorial 3

Step 1: Updating the local hosts file. 4

Step 2: Create Project 5

Step 3: Create Web Service Proxy classes using CXF’s wsdl2java tool 7

Step 4: Create Notification Consumer Service Stubs. 8

Step 5: Login to CCT Server. 11

Step 6: Subscribe via the User Service. 13

Step 7: Subscribe via the Notification Producer Service. 16

Step 8: Unsubscribe and Logout 21

Step 9: Putting it All Together. 23

3. Running the Sample Application. 25

 

 

1. Introduction

This tutorial will demonstrate how to subscribe for events via the User Service and the Notification Producer Service. It will outline the main differences between the two services:

·          User Service – Forced to register for all events.

·          Notification Producer Service – Can select which events to register for.

 

 

 

 

2. SOA CCT Web Service Subscriptions Tutorial

Description of Tutorial

This tutorial describes how to subscribe for SOA CCT Web Service Notifications via two different means.

The first utilises the UserService to subscribe for notifications in the simplest manor. This notification will subscribe for all event types. The second utilises the NotificationProducer to subscribe for specific notifications. In the case of this tutorial it will just subscribe for Agent Property LOGIN_STATUS notifications.

The main procedures in this tutorial are:

Step 1: Updating the local hosts file.

Step 2: Create Project.

Step 3: Create Web Service Proxy classes using CXF’s wsdl2java tool.

Step 4: Create Notification Consumer service stubs using wsdl2java.

Step 5: Login to CCT Server.

Step 6: Subscribe via the User Service.

Step 7: Subscribe via the Notification Producer Service.

Step 8: Unsubscribe & Logout.

Step 9: Putting it all together.

This tutorial utilises the SessionService and the NotificationProducer service to demonstrate this functionality.

Please download the source code here.

Notes about this tutorial

The SOA Web Services must be installed and running on an available server.

The tutorial uses eclipse as the IDE, Java 8 JRE & and CXF 2.2.5 to publish services.

Common programming practices such as delegates, events, threading, and exceptions are left to the individual programmer and are not covered in this tutorial.

Exception handling is not shown in the code samples for brevity.

You must determine exactly where or how you want to implement the example code shown in these tutorials in your own application.

To instigate events being received by the consumers, CCT Ref client can be used to login and logout agents as well as going ready/not ready.

 

 

Step 1: Updating the local hosts file

Open up the hosts file and map the host name of your server to an IP address.

 

 

Step 2: Create Project

To create an Eclipse Java Project:

1. Open Eclipse IDE.

2. Select File > New > Java Project.

3. Select Next.

4. Enter Project Name & Location as desired.

5. Select Next, Next & Finished.

6. Add a new src folder : Right click on the project tab, Select New->Src Folder. Folder named ‘autogen.src’ in this tutorial and is where generated code will be stored.

 

 

Step 3: Create Web Service Proxy classes using CXF’s wsdl2java tool

1.      Open a command window.

2.      Cd to the project’s autogen.src directory.

3.      Run wsdl2java command supplying the service urls.

e.g. wsdl2java http://<SERVER_IP_ADDRESS>:<SOA_OPEN_PORT>/SOAOICCT/services/SessionService?wsdl

4.       Refresh project in eclipse to import generated files.

The Open Interface Splash is available on the SOA Server: http://<SERVER_IP_ADDRESS>:9090/OI_Splash.jsp

 

 

 

Step 4: Create Notification Consumer Service Stubs

1.      Open a command window

2.      Cd to the project’s autogen.src directory

3.      Run wsdl2java –client command supplying the url to the notification consumer wsdl

wsdl2java -client http://<SERVER_IP_ADDRESS>:9090/NotificationConsumer.wsdl

4.       Refresh project in eclipse to import generated files

The Notification Consumer wsdl is available from the SOA Server splash screen, available on the SOA Server: http://<SERVER_IP_ADDRESS>:9090/NotificationConsumer.html

 

 

 

 

 

Step 5: Login to CCT Server

[Refer to UserSubscribe.java in the source of the sample project]

1. Create a private attribute for the service classes required as shown below.

Code sample: Declare service attributes

private UserService userService = null;

 

 

2. Instantiate the service classes as shown below.

Code sample: Instantiating the Service Proxies

public UserService getUserService(String serverDetails) throws Exception {

     if (userService == null) {

          String sslocation =

                  "http://" + serverDetails + "/SOAOICCT/services/UserService?wsdl";

          URL url = new URL(sslocation);

          userService = new SOAOICCTUserService(url).getUserService();

     }

     return userService;

}

 

 

3. Set the User Credentials in a Login Request as shown below.

Code sample: Create Request to Login to CCT Server

LogInToCCTServerRequest req = new LogInToCCTServerRequest();

AuthenticationLevel auth = new AuthenticationLevel();

auth.setDomain(domain);

auth.setPassword(password);

auth.setUsername(user);

req.setAuthenticationLevel(auth);

 

 

4. Invoke the Login Request & Retrieve Session Token.

Code sample: Invoking SessionServiceloginToCCTServer request

try {

     LogInToCCTServerResponse token = userService.logInToCCTServer(req);

     if (token != null) {

          return token.getSsoToken();

     }

} catch (LogInToCCTServerException e) {

     //Handle Exception

}

           

NOTE: Maintain the ssoToken returned from login as it identifies the user’s session and a valid SSO token is required for all other service requests.

 

 

 

 

Step 6: Subscribe via the User Service

[Refer to Consumer1Impl.java in the source of the sample project]

1. Add a class to implement the Notification Consumer Service interface as follows.

Code sample: Service Implementer

@javax.jws.WebService(name = "NotificationConsumer",

serviceName = "SOAOICCT_NotificationConsumer",

portName = "NotificationConsumer",

targetNamespace = "http://www.nortel.com/soa/oi/cct/NotificationConsumer",

endpointInterface = "com.nortel.soa.oi.cct.notificationconsumer.NotificationConsumer")

 

public class Consumer1Impl implements NotificationConsumer

{

     @Override

     public void notify(List<NotificationMessageHolderType> notificationMessage) {

          // TODO Handle Notification Here

     }

}

 

 

2. Publish Consumer Endpoint where notifications will be received.

Code sample: Publish the Notification Consumer

public String publishNotificationConsumer() {

     String notificationConsumerUri =

     "http://" + getIpAddress() + ":" + consumerPort + "/SOAOICCT/services/NotificationConsumer";

     Consumer1Impl notificationConsumerImpl = new Consumer1Impl();

     Endpoint.publish(notificationConsumerUri, notificationConsumerImpl);

     return notificationConsumerUri;

}

 

 

3. Subscribe using User Service.

Code sample: Subscribe providing the consumerEndpoint from the above figure

public String simpleSubscribe(String consumerEndpoint, SsoToken ssoToken) {

     try {

          String subscription = userService.subscribe(consumerEndpoint, ssoToken);

          return subscription;

     } catch (SubscribeFailedFault e) {

          //Handle Exception here

     } catch (SessionNotCreatedException e) {

          //Handle Exception here

     }

     return null;

}

 

 

 

Step 7: Subscribe via the Notification Producer Service

[Refer to NotificationProducerSubscribe.java in the source of the sample project]

1. Publish the Notification Consumer.

Code sample: In the example the Notification Producer uses the Consmer2Impl.java

public String publishNotificationConsumer() {

String notificationConsumerUri =  "http://" + getIpAddress() + ":" + consumerPort + "/SOAOICCT/services/NotificationConsumer";

Consumer2Impl notificationConsumerImpl = new Consumer2Impl();

Endpoint.publish(notificationConsumerUri, notificationConsumerImpl);

return notificationConsumerUri;

}

 

NOTE: You can verify the NotificationConsumer is up by viewing the wsdl in a web browser, replacing <LOCAL_IP_ADDRESS:LOCAL_OPEN_PORT> as appropriate e.g. http://<LOCAL_IP_ADDRESS>:<LOCAL_OPEN_PORT>/SOAOICCT/services/NotificationConsumer?wsdl

 

 2. Subscribe using the Notification Producer Service.

Code sample: In this example the client registers for AGENT_PROPERTY – LOGIN STATUS notifications.

   AgentPropertyListenerType agentPropertyListenerType = new AgentPropertyListenerType();

   List<AgentProperty> agentPropertyListenerEvents = new ArrayList<AgentProperty>();

   agentPropertyListenerEvents.add(AgentProperty.LOGIN_STATUS);

   agentPropertyListenerType.getAgentPropertyEvent().addAll(agentPropertyListenerEvents);

   <Agent> agentListenerEntities = new ArrayList<Agent>();

   agentPropertyListenerType.getAgentEntity().addAll(agentListenerEntities);

   otificationTopic.setAgentPropertyListener(agentPropertyListenerType);

 

NOTE: This web service allows the user to register for specific events that they are interested in, as opposed to getting all events to handle.

 

3. Initialise the Notification Producer Service Proxy.

Code sample: Instantiating the Service Proxies

public NotificationProducer getNotificationProducerService(String serverDetails) throws Exception {

     if (npService == null) {

          try {

                String sslocation = "http://" + serverDetails + "/SOAOICCT/services/NotificationProducer?wsdl";

                URL url = new URL(sslocation);

                npService = new SOAOICCTNotificationProducer(url).getNotificationProducer();

          } catch (Exception e) {

          }

     }

     return npService;

}

 

4. Using the notification producer service to subscribe for notifications. The subscription method takes a number of parameters.

a) Define Endpoint Reference Type - This details the notification consumer service endpoint where notifications will be sent to.

Code sample: Define a EndpointReferenceType

//Setup the endpoint reference type object

EndpointReferenceType consumerReference = new EndpointReferenceType();

AttributedURI uri = new AttributedURI();

value.setValue(notificationConsumerUri);

//QName(targetNameSpace, serviceName)

QName name = new QName("http://www.nortel.com/soa/oi/cct/NotificationConsumer", "SOAOICCT_NotificationConsumer");

consumerReference.setAddress(uri);

consumerReference.setServiceName(new ServiceNameType()); consumerReference.getServiceName().setPortName("NotificationConsumer");

consumerReference.getServiceName().setValue(name);

 

 

b) Define Subscription Policy - These attributes are set to true if session related notifications are required.

Code sample: Define a Topic Expression

SubscriptionPolicyType subscriptionPolicy = new SubscriptionPolicyType();

subscriptionPolicy.setSessionTerminationImminentNotificationsEnabled(true);

subscriptionPolicy.setSessionTerminationNotificationsEnabled(true); subscriptionPolicy.setSubscriptionTerminationImminentNotificationsEnabled(true);

subscriptionPolicy.setSubscriptionTerminationNotificationsEnabled(true);

 

 

c) Define Topic Expression - This details the types of notifications that will be subscribed for. In the example below AgentProperty Events of type LOGIN_STATUS are registered for.

Code sample: Define a Topic Expression

//Detail the types of notifications that you are going to subscribe for

TopicExpressionType topicExp = new TopicExpressionType();

topicExp.setDialect("Simple");

NotificationTopicType notificationTopic = new NotificationTopicType();

 

AgentPropertyListenerType agentPropertyListenerType = new AgentPropertyListenerType();

List<AgentProperty> agentPropertyListenerEvents = new ArrayList<AgentProperty>();

agentPropertyListenerEvents.add(AgentProperty.LOGIN_STATUS); agentPropertyListenerType.getAgentPropertyEvent().addAll(agentPropertyListenerEvents);

List<Agent> agentListenerEntities = new ArrayList<Agent>();

agentPropertyListenerType.getAgentEntity().addAll(agentListenerEntities);

notificationTopic.setAgentPropertyListener(agentPropertyListenerType);

 

QName _TopicExpressionTypeNotificationTopic_QNAME = new QName("http://www.nortel.com/soa/oi/cct/BaseNotification", "NotificationTopic");

JAXBElement<NotificationTopicType> notificationTopicType = new JAXBElement<NotificationTopicType>(_TopicExpressionTypeNotificationTopic_QNAME, NotificationTopicType.class, TopicExpressionType.class, notificationTopic);

 

topicExp.getContent().add(notificationTopicType);

 

 

5. Call the Notification Producer subscribe method.

Code sample: Notification Consumer Subscription

try {

     EndpointReferenceType subscriptionEndpoint = notificationProducerService.subscribe(

                                                                              consumerReference,

                                                                              topicExp,

                                                                              useNotify, //null

                                                                              precondition, //null

                                                                              selector, //null

                                                                              subscriptionPolicy,

                                                                              initialTerminationTime, //null

                                                                              ssoToken);

}

catch (Exception e ) {

     e.printStackTrace();

     lblInfo.setText("Error : " + e.getMessage());

}

 

NOTE: Maintain the subscriptionEndpoint value returned from the method subscribe as it will be required to unsubscribe.

 

 

 

 

Step 8: Unsubscribe and Logout

[Refer to NotificationProducerSubscribe.java and UserSubscribe.java  in the source of the sample project]

1.Unsubscribe the User Service subscription.

NOTE: The consumer is returned string returned when you subscribe. Please see some of the code samples in step 5.

Code sample: UserSubscribe.java Unsubscribe

public void unsubscribe(String consumer, SsoToken ssoToken) {

    try {

               userService.unsubscribe(consumer, ssoToken);

    } catch (Exception e) {

              e.printStackTrace();

    }

}

 

 

2. Unsubscribe the Notification Producer Service subscription.

Code sample: NotificationProducer.java Unsubscribe

public void unsubscribe(EndpointReferenceType subRef, SsoToken ssoToken) {

     try {

            npService.unsubscribe(subRef, ssoToken);

     } catch (UnableToDestroySubscriptionFault e) {

            e.printStackTrace();

     } catch (ResourceUnknownFault e) {

          e.printStackTrace();

    }

}

 

 

3. Logout using the User Service.

Code sample: UserSubscribe.java Logout

public void logout(SsoToken ssoToken) {

            LogOffFromCCTServerRequest req = new LogOffFromCCTServerRequest();

            req.setSsoToken(ssoToken);

            try {

                        userService.logOffFromCCTServer(req);

                        System.out.println("Logged Off Session: " + ssoToken.getToken());

            } catch (Exception e){

                        e.printStackTrace();

            }

}

 

 

 

 

Step 9: Putting it All Together

[Refer to TestNotifications.java in the source of the sample project]

a)      A simple test program is included that will.

b)      Login to the cct server.

c)      Subscribe the two different consumer endpoints.

d)      Wait for 30 seconds.

e)      At this point you can use CCT Ref Client to login / logout the agent, set ready not ready etc so events will be pushed to the consumers.

f)       Unsubscribe.

g)      Logout.

Code sample: Test Program

public static void main(String[] args) throws Exception {

     String userName = "Agent1000";

     String password = "Password1";

     String domain = "localhost";

     String cctServiceHostAndPort = "cs1ktest:9180";

     String consumerPort = "9099";

     String consumerPort2 = "9078";

 

     //User Input

     if (args!= null && args.length == 6) {

          userName = args[0];

          password = args[1];

          domain = args[2];

          cctServiceHostAndPort = args[3];

          consumerPort = args[4];

          consumerPort2 = args[5];

     }

 

     //Simple subscribe for all events via user service

     UserSubscribe us = new UserSubscribe(consumerPort, cctServiceHostAndPort);

     String consumerEndpoint = us.publishNotificationConsumer();

     SsoToken ssoToken = us.loginToCctServer(userName, password, domain);

     String subscription1 = us.simpleSubscribe(consumerEndpoint, ssoToken);

 

     //Subscribe for Agent Property Notifications via notification producer

     NotificationProducerSubscribe nps = new NotificationProducerSubscribe(consumerPort2, cctServiceHostAndPort);

     String consumerEndpoint2 = nps.publishNotificationConsumer();

     EndpointReferenceType subscription2 = nps.subscribe(consumerEndpoint2, ssoToken);

 

     //Sleep for 30 seconds so we can inject events

     Thread.sleep(30000);

 

     //Unsubsribe

     us.unsubscribe(subscription1, ssoToken);

     nps.unsubscribe(subscription2, ssoToken);

 

     //Sleep to ensure notifications done

     Thread.sleep(1000);

 

     //Logout

     us.logout(ssoToken);

}

 

 

 

3. Running the Sample Application

1.      Download the zip file.

2.      Open Eclipse.

3.      Import an existing project using File – Import option.

4.      Once Project is imported & compiled, Right click on project & select Run As – Java Application.

5.      Once Running – validate the Notification Consumer endpoint is running by using a webrowser.

untitled

6. When the output shows the users have subscribed, use the CCT Ref Client to inject some events.

 

NOTE: Output following Agent Ready to NotReady to Ready & Logout.

Notice – Consumer 2 only received one event for the Agent logout.