Accessing PTV xServer internet via WSDL generated classes
-
While xServer internet uses HTTP basic authentication with <user>/<password>, it is highly recommended to work with the token assigned to your subscription using the combination “xtok”/<token>. Using the token is a safer way than using your far more sensible account data.
-
-
Prerequisites
For this sample you will need the following:
- A JDK 6 (or newer) installation
- A Maven2 or Maven3 installation
-
Create a Maven project
First of all, create a new directory and set up a Maven project file named
"pom.xml"
with this content:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ptvag.xserver.xroute</groupId>
<artifactId>xRouteJAXWSClient</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>xRouteJAXWSClient</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>wsimport</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="${basedir}/target/classes" />
<mkdir dir="${basedir}/target/generated-sources" />
<exec executable="wsimport" failonerror="true" dir="${basedir}">
<arg value="-quiet" />
<arg value="-d" />
<arg value="${basedir}/target/classes" />
<arg value="-s" />
<arg value="${basedir}/target/generated-sources" />
<arg value="https://xroute-eu-n-test.cloud.ptvgroup.com/xroute/ws/XRoute?WSDL" />
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
-
Run Maven in the new directory with:
mvn clean install
Maven will now generate client classes for xRoute based on its WSDL. You can find the client classes in the directory "/target/generated-sources".
-
Set up a simple test case
The
"pom.xml"
has one dependency on JUnit which we can use to build a simple test.Create a new Java source file in "src/test/java" named "JAXWSTest.java". Make sure you set up the folder structure like this because Maven will look for test classes in this directory.
The test class creates a simple request and calculates a route.
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import junit.framework.TestCase;
import org.junit.Test;
import com.ptvag.jabba.service.baseservices.ArrayOfCallerContextProperty;
import com.ptvag.jabba.service.baseservices.CallerContext;
import com.ptvag.jabba.service.baseservices.CallerContextProperty;
import com.ptvag.xserver.common.ArrayOfPoint;
import com.ptvag.xserver.common.PlainPoint;
import com.ptvag.xserver.common.Point;
import com.ptvag.xserver.xroute.ArrayOfWaypointDesc;
import com.ptvag.xserver.xroute.LinkType;
import com.ptvag.xserver.xroute.ResultListOptions;
import com.ptvag.xserver.xroute.Route;
import com.ptvag.xserver.xroute.WaypointDesc;
import com.ptvag.xserver.xroute.jwsdp.XRouteWS;
public class JAXWSTest extends TestCase {
private static final String XROUTE_WSDL_LOCATION = "https://xroute-eu-n-test.cloud.ptvgroup.com/xroute/ws/XRoute?WSDL";
public JAXWSTest(String name) {super(name);}
@Test
public void testSimpleCall() throws Exception {
WaypointDesc[] waypoints = new WaypointDesc[2];
waypoints[0] = new WaypointDesc();
waypoints[0].setLinkType(LinkType.NEXT_SEGMENT);
PlainPoint pp = new PlainPoint();
pp.setX(685903);
pp.setY(6372958);
Point point = new Point();
point = new Point();
QName _plainPoint_QNAME = new QName("http://common.xserver.ptvag.com","point");
point.setPoint(new JAXBElement(_plainPoint_QNAME, PlainPoint.class, pp));
ArrayOfPoint arrayOfPoint = new ArrayOfPoint();
arrayOfPoint.getPoint().add(point);
waypoints[0].setWrappedCoords(arrayOfPoint);
waypoints[1] = new WaypointDesc();
waypoints[1].setLinkType(LinkType.NEXT_SEGMENT);
PlainPoint pp1 = new PlainPoint();
pp1.setX(681013);
pp1.setY(6371680);
Point point1 = new Point();
point1 = new Point();
point1.setPoint(new JAXBElement(_plainPoint_QNAME, PlainPoint.class, pp1));
ArrayOfPoint arrayOfPoint1 = new ArrayOfPoint();
arrayOfPoint1.getPoint().add(point1);
waypoints[1].setWrappedCoords(arrayOfPoint1);
ResultListOptions details = new ResultListOptions();
details.setBinaryPathDesc(false);
details.setPolygon(true);
CallerContext cc = new CallerContext();
CallerContextProperty prop = new CallerContextProperty();
prop.setKey("CoordFormat");
prop.setValue("PTV_MERCATOR");
CallerContextProperty prop1 = new CallerContextProperty();
prop1.setKey("Profile");
prop1.setValue("carfast");
ArrayOfCallerContextProperty arrayOfCallerContextProperty = new ArrayOfCallerContextProperty();
arrayOfCallerContextProperty.getCallerContextProperty().add(prop);
arrayOfCallerContextProperty.getCallerContextProperty().add(prop1);
cc.setWrappedProperties(arrayOfCallerContextProperty);
ArrayOfWaypointDesc arrayOfWayPoints = new ArrayOfWaypointDesc();
arrayOfWayPoints.getWaypointDesc().add(waypoints[0]);
arrayOfWayPoints.getWaypointDesc().add(waypoints[1]);
//Set your credentials using the Authenticator
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"xtok",
"<INSERT-YOUR-TOKEN-HERE>".toCharArray()
);
}
});
URL url = new URL(XROUTE_WSDL_LOCATION);
QName qname = new QName("http://jwsdp.xroute.xserver.ptvag.com", "XRouteWSService");
QName xRouteWSPort = new QName("http://jwsdp.xroute.xserver.ptvag.com", "XRouteWSPort");
Service service = Service.create(url, qname);
XRouteWS xroute = service.getPort(xRouteWSPort, XRouteWS.class);
Route route = xroute.calculateRoute(arrayOfWayPoints, null, null, details, cc);
int distance = route.getInfo().getValue().getDistance();
assertEquals(4129, distance);
}
}
-
To run the test, run "maven test" in the project directory using the command prompt:
mvn test