Skip to main content
Omnitracs Knowledge Base

Scanning Sample Web Services Client

Overview

The following Java based EIP Web Services client code is available as an example of how to work with EIP Web Services for the Scanning application.

The intent of this sample is to illustrate a working client from which to base an integration project.

As with all examples this code is for illustration purposes only and not for production usage.

Requirements

Must have the following Java versions and tools to work with this sample:

  • Java 6.0 and above
  • Maven 3

Working Knowledge

  • Spring and Spring Web Services
  • Java and Spring Annotations
  • JUnit
  • Maven and Maven Plugins: jaxws-maven-plugin, maven-jar-plugin, maven-source-plugin, maven-surefire-plugin

Projects


We have split the sample into 2 projects:

 

Project

 

 

Description

 

eip-wsdl-import

Creates the Web Service Java Proxy classes representing the Outgoing and Incoming EIP Web Service artifacts. Must build/install prior to running the eip-client.

eip-client

Contains the infrastructure necessary to communicate with the EIP services. Additionally within the Unit Testing classes there is a Scanning class containing examples of the basic scanning operations.
This project is dependent on the eip-wsdl-import project.

 

eip-wsdl-import usage

Please click here to download the project

Once the project has been downloaded please run the following maven command from within the folder containing the pom.xml

> mvn install

This will generate, package and install the classes required for communicating with the EIP web services API.

Under the target directory there should be 2 jars (eip-wsld-import-0.0.1-SNAPSHOT.jar and eip-wsld-import-0.0.1-SNAPSHOT-sources.jar) containing the classes and the sources derived from the EIP services.
1.png

Under the src directory there should be java files:
2.png
 
 

The pom.xml

<plugin>

                <groupId>org.codehaus.mojo</groupId>

                <artifactId>jaxws-maven-plugin</artifactId>

                <version>1.12</version>

                <executions>

                    <execution>

                        <goals>

                            <goal>wsimport</goal>

                        </goals>

                    </execution>

                </executions>

                <!--TODO: Need to replace wsdlUrl with production versions -->

                <configuration>

                    <wsdlUrls>

                        <wsdlUrl>http://sdqesdevvgw4.devqes.omnitracs.com:8184/eipWebWS/EIPOutMsgSvcs1.0?wsdl</wsdlUrl>

                        <wsdlUrl>http://sdqesdevvgw4.devqes.omnitracs.com:8184/eipWebWS/EIPInMsgSvcs1.0?wsdl</wsdlUrl>

                    </wsdlUrls>

                    <packageName>com.omnitracs.eip.ws.schema</packageName>

                    <sourceDestDir>${basedir}/src/main/java</sourceDestDir>

                    <verbose>true</verbose>

                </configuration>

            </plugin>

            <plugin>

The wsdlUrls in the configuration points to the services.
 

eip-client usage

Please click here to download the project.

The project structure is as follows:
3.png

The src directory contains some basic services that enable to communication to the EIP services.

Below are some of the methods in the Services.java class that represent the main EIP API calls.

/**

     * getOutgoingMessages

     *

     * @param GetMessagesRequest

     * @return GetMessagesResponse

     * @throws Exception

     */

    public GetMessagesResponse getOutgoingMessages(GetMessagesRequest request) throws ServicesException

    {

        log.debug("getOutgoingMessages");

 

        GetMessagesResponse response = (GetMessagesResponse) wsCaller(eipOutMsgTemplate, request);

 

        return response;

    }

 

    /**

     * getOutgoingMessagesCount

     *

     * @param GetMessagesCountRequest

     * @return GetMessagesCountResponse

     * @throws Exception

     */

    public GetMessagesCountResponse getOutgoingMessagesCount( GetMessagesCountRequest request) throws ServicesException

    {

        log.debug("getOutgoingMessagesCount");

 

        GetMessagesCountResponse response = (GetMessagesCountResponse) wsCaller(eipOutMsgTemplate, request);

 

        return response;

    }

 

    /**

     * sendMessages

     *

     * @param SendMessagesRequest

     * @return SendMessagesResponse

     * @throws Exception

     */

    public SendMessagesResponse sendMessages(SendMessagesRequest request) throws ServicesException

    {

        log.debug("sendMessages");

 

        SendMessagesResponse response = (SendMessagesResponse) wsCaller(eipInMsgTemplate, request);

 

        return response;

    }

 

 

    /**

     * getSentMessages

     *

     * @param GetSentMessagesRequest

     * @return GetSentMessagesResponse

     * @throws Exception

     */

    public GetSentMessagesResponse getSentMessages( GetSentMessagesRequest request) throws ServicesException

    {

        log.debug("getSentMessages");

 

        GetSentMessagesResponse response = (GetSentMessagesResponse) wsCaller( eipInMsgTemplate, request);

 

        return response;

    }

 

The test directory contains a unit test class for scanning that encapsulates a few of the basic calls needed to interact with the scanning application.

Below is one of the sample test cases in the ScanningTest.java file. Though these are not truly unit test we have encapsulated the calls within test methods to call attention that these are not intended to be used for production quality.

As you can see the context configurations are wired in via a annotation  func-test-context.xml and web-service-test-context.xml additionally the Services api described above is wired in as a resource. We can then setup the scanning specific calls. In this case we are demonstrating how to look up available count of scanned messages based on their read state.

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:func-test-context.xml", "classpath:web-service-test-context.xml" })

public class ScanningTest implements Constants {

 

 

 

    @Resource

    protected Services eipService;

 

    /**

     * Setter for EIP Messaging Services

     *

     * @param eipOutMsgTemplate

     */

    public void setEipService(Services eipService)

    {

        this.eipService = eipService;

    }

 

    /**

     * testGetScannedMessageCounts

     *

     * Example of how to look up a count for available unread, read and failed scanned messages.

     *

     * @throws Exception

     */

    @Test

    public void testGetScannedMessageCounts() throws Exception

    {

 

        // Create a RoutingType and assign the Scanning Service/IdentityType

        RoutingType routing = helper.createSimpleServiceRouting(SCANNING_PRIMARY_SERVICE_ID);

 

        // Create the GetMessagesCountRequest

        GetMessagesCountRequest countRequest = new GetMessagesCountRequest();

 

        // Assign the RoutingType

        countRequest.setRouting(routing);

 

        GetMessagesCountResponse countResponse = null;

        try

        {

 

            /*------------------------------------------------------*/

 

            // Set for UNREAD message counts

            countRequest.setReadState(OutgoingReadStatesType.UNREAD);

 

            // Call the EIP Outgoing Messages Count Service API

            countResponse = eipService.getOutgoingMessagesCount(countRequest);

 

            // check the response for a count:

            log.info("The number of available UNREAD scanning messages are: " + countResponse.getMessageCount());

 

            /*------------------------------------------------------*/

 

            // Set for READ message counts

            countRequest.setReadState(OutgoingReadStatesType.READ);

 

            // Call the EIP Outgoing Messages Count Service API

            countResponse = eipService.getOutgoingMessagesCount(countRequest);

 

            // check the response for a count:

            log.info("The number of available READ scanning messages are: " + countResponse.getMessageCount());

 

            /*------------------------------------------------------*/

 

            // Set for FAILED message counts

            countRequest.setReadState(OutgoingReadStatesType.FAILED);

 

            // Call the EIP Outgoing Messages Count Service API

            countResponse = eipService.getOutgoingMessagesCount(countRequest);

 

            // check the response for a count:

            log.info("The number of available FAILED scanning messages are: " + countResponse.getMessageCount());

 

 

        }

        catch (ServicesException exp)

        {

            handleServicesException(exp);

        }

 

    }

  

 

The resources directory in the test directory contains the spring based contexts required to wire the EIP services.

The  web-service-test-context.xml configures the web services by first deonoting the marshaller class, then the security interceptor for handling login to the services and finally the messaging templates that bring it all together. The eipOutMsgTemplate and the eipInMsgTemplate are wired into the Services object to do the actual web service calls.

<!-- Web Service client specific bean instantiations -->

  <oxm:jaxb2-marshaller id="wsJaxbMarshaller" contextPath="com.omnitracs.eip.ws.schema" />

 

  <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">

  </bean>

 

  <bean id="xwsSecurityInterceptor" class="org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor">

    <property name="policyConfiguration" value="securityPolicyClient.xml" />

    <property name="callbackHandlers">

      <list />

    </property>

  </bean>

 

  <bean id="eipOutMsgTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">

    <constructor-arg ref="messageFactory" />

    <property name="marshaller" ref="wsJaxbMarshaller"></property>

    <property name="unmarshaller" ref="wsJaxbMarshaller"></property>

    <property name="messageSender">

      <bean class="org.springframework.ws.transport.http.CommonsHttpMessageSender"/>

    </property>

    <property name="defaultUri" value="${eip.outgoing.url}" />

    <property name="interceptors">

      <list>

        <ref bean="xwsSecurityInterceptor" />

      </list>

    </property>

  </bean>

 

  <bean id="eipInMsgTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">

    <constructor-arg ref="messageFactory" />

    <property name="marshaller" ref="wsJaxbMarshaller"></property>

    <property name="unmarshaller" ref="wsJaxbMarshaller"></property>

    <property name="messageSender">

      <bean class="org.springframework.ws.transport.http.CommonsHttpMessageSender"/>

    </property>

    <property name="defaultUri" value="${eip.incoming.url}" />

    <property name="interceptors">

      <list>

        <ref bean="xwsSecurityInterceptor" />

      </list>

    </property>

  </bean>


The above references: eip.outgoing.url and the eip.incoming.url are specified in the dev.properties file.

eip.outgoing.url=http://sdqesintvgw4.devqes.omnitracs.com:8184/eipWebWS/EIPOutMsgSvcs1.0

eip.incoming.url=http://sdqesintvgw4.devqes.omnitracs.com:8184/eipWebWS/EIPInMsgSvcs1.0

The func-test-context.xml  allows us to wire in the dev.properties file and also wire in the Services with the spring based web service messaging templates

<bean id="propertyPlaceHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="locations">

      <list>

        <value>classpath:dev.properties</value>

      </list>

    </property>

  </bean>

 

 

  <bean id="eipService" class="com.omnitracs.eip.ws.client.Services" scope="singleton"

    p:eipOutMsgTemplate-ref="eipOutMsgTemplate"

    p:eipInMsgTemplate-ref="eipInMsgTemplate" />

The securityPolicyClient.xml contains the credentials for the web service calls

<xwss:SecurityConfiguration dumpMessages="true" xmlns:xwss="http://java.sun.com/xml/ns/xwss/config">

    <xwss:Timestamp />

    <xwss:UsernameToken name="USER@COMPANY" password="somepassword" digestPassword="false" useNonce="true" />

</xwss:SecurityConfiguration>


The following code highlights how to pull scan messages from EIP.

/**

 * testGetScannedMessages

 *

 * Example of how to look up scanned messages.

 *

 * @throws Exception

 */

@Test

public void testGetScannedMessages() throws Exception

{

    // decide which type of read state to look

    OutgoingReadStatesType readState = OutgoingReadStatesType.UNREAD;

 

    // Create a RoutingType and assign the Scanning Service/IdentityType

    RoutingType routing = helper.createSimpleServiceRouting(SCANNING_PRIMARY_SERVICE_ID);

 

    // Creat the messages request

    GetMessagesRequest messagesRequest = new GetMessagesRequest();

 

    // Assign the routing info

    messagesRequest.setRouting(routing);

 

    // Set read state

    messagesRequest.setReadState(readState);

 

    // Set max messages (Scanning has a limit of 2 messages per request).

    messagesRequest.setMaxMessages(SCANNING_MAX_OUTGOING_MESSAGE);

 

    // For UnRead Messages management

    // For testing purposes we will not acknowledge the reading of an unread scanned message.

    // NEED TO SET TO TRUE DURING ACTUAL USAGE

    messagesRequest.setAutoAck(false);

 

    try

    {

        GetMessagesResponse countResponse = null;

 

        // internal count so we can report how many scanned processed

        int processedCnt = 0;

 

        // Lookup number of available messages

        // only take 1/2 as we are only able to get 2 scanned messages per request

        int availableMessagesCnt = roundUp( lookupMessageCounts(readState) , 2);

 

        // loop through all the available messages

        while ( (availableMessagesCnt--) > 0)

        {

            // Call the EIP Outgoing Messages Service API

            countResponse = eipService.getOutgoingMessages(messagesRequest);

 

            // retrieve the list of scanned messages

            List<OutgoingMessageType> list = countResponse.getMessage();

 

            if (list != null)

            {

                for (OutgoingMessageType scanMsg : list)

                {

                    String fileName = scanMsg.getPayload().getFilename();

                    log.info("filename: " + fileName);

 

                    // do not need the content type if the file name has the appropriate extension

                    String contentType = scanMsg.getPayload().getContentType();

                    log.info("contentType: " + contentType);

 

                    StringBuilder sb = new StringBuilder();

                    // collect the associated meta data

                    for (NameValueType nameValue : scanMsg.getMetaData())

                    {

                        sb.append(nameValue.getName()).append("=").append(nameValue.getValue()).append("\n");

                    }

                    log.info("MetaData for file:\n" + sb);

 

                    fileWriteBytes(fileName, scanMsg.getPayload().getBinary());

 

                    ++processedCnt;

                }

            }

        }

 

        // check the response for a count:

        log.info("The number of scanning messages processed: " + processedCnt);

 

    }

    catch (ServicesException exp)

    {

        handleServicesException(exp);

    }

 

 

}


Run the following maven command from within the folder containing the pom.xml

> mvn test

This will build and run the test classes against the live EIP services.

  • Was this article helpful?