1.1.41. fejezet, SOAP kliens

Eclipse-ben új Web Service Client generálásával létrehozzuk az osztályokat a wsdl-ből. Ehhez kell egy Dynamic Web projekt vagy egy Java EE Web Project (JBossTools vagy CodeReady Studio használatakor). New -> Web Service Client futtatásakor a Web service runtime-nál az Apache CXF 2.x legyen kiválasztva, amit be is konfiguráltunk a Window > Preferences > Web Services > CXF 2.x Preferences menüpont alatt.

A JDK11 használatakor a pom.xml-be a függőségekhez hozzá kell adni ezeket:

 	<dependency>
 		<groupId>javax.xml.rpc</groupId>
 		<artifactId>javax.xml.rpc-api</artifactId>
 		<version>1.1.2</version>
 	</dependency>
 
	<dependency>
	    <groupId>javax.xml.bind</groupId>
	    <artifactId>jaxb-api</artifactId>
	    <version>2.3.1</version>
	</dependency>
 
	<dependency>
	    <groupId>javax.xml.ws</groupId>
	    <artifactId>jaxws-api</artifactId>
	    <version>2.3.1</version>
	</dependency>
 
	<dependency>
	    <groupId>javax.jws</groupId>
	    <artifactId>javax.jws-api</artifactId>
	    <version>1.1</version>
	</dependency>
<!-- runtime -->
	<dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>rt</artifactId>
            <version>2.3.1</version>
	</dependency>
 
	<dependency>
	    <groupId>com.sun.org.apache.xml.internal</groupId>
	    <artifactId>resolver</artifactId>
	    <version>20050927</version>
	</dependency>

A module-info.java-ba fel kell venni a következő modulokat:

module FileLocatorCLI {
	opens hu.infokristaly.soapserver; //soap kliens csomagja
	requires java.xml;
	requires java.xml.bind;
	requires java.xml.ws;
	requires java.logging;
	requires javax.jws;
}

A szolgáltatást hívó kliens kódja pedig:

package hu.infokristaly.soapserver;
 
/**
 * Please modify this class to meet your needs
 * This class is not complete
 */
 
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;;
 
/**
 * This class was generated by Apache CXF 3.4.2
 * 2021-03-08T16:25:12.348+01:00
 * Generated source version: 3.4.2
 *
 */
public final class FileLocatorSoapServiceClient {
 
    private static final QName SERVICE_NAME = new QName("http://soapserver.infokristaly.hu/", "FileLocatorService");
 
    private FileLocatorSoapServiceClient() {
    }
 
    public static void main(String args[]) throws java.lang.Exception {
        URL wsdlURL = FileLocatorService.WSDL_LOCATION;
 
		if (args.length != 3) {
			System.out.println("Usage: java -jar FileLocatorCLI.jar <start-path> <location> <wsdl-url>");
			System.out.println("Usage: java -jar FileLocatorCLI.jar -find <filename> <wsdl-url>");
			System.exit(1);
		}
		System.out.println("***********************");
		System.out.println("Create Web Service Client...");
		wsdlURL= new URL(args[2]);
		FileLocatorService service1 = new FileLocatorService(wsdlURL, SERVICE_NAME);
 
		System.out.println("Create Web Service...");
		final FileLocatorSoapService port1 = service1.getFileLocatorSoapServicePort();
		System.out.println("Call Web Service Operation...");
 
		Map<String, Object> req_ctx = ((BindingProvider)port1).getRequestContext();
 
		Map<String, List<String>> headers = new HashMap<String, List<String>>();
		headers.put("Username", Collections.singletonList("mkyong"));
		headers.put("Password", Collections.singletonList("password"));
		req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
 
		if (args[0].equals("-find")) {
			String result = port1.find("%"+args[1]+"%");
			System.out.println(result);
			System.exit(0);
		}
		String pathString = args[0];
		final Integer locationId = port1.createPhisicalLocation(args[1]);
 
		final LinkedList<Integer> directoryId = new LinkedList<Integer>();
		Files.walkFileTree(Paths.get(pathString), new FileVisitor<Path>() {
 
			@Override
			public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
				directoryId.addLast(port1.createFolder(dir.toString()));
				System.out.println("preVisitDirectory[" + directoryId.getLast() + "]: " + dir);
				return FileVisitResult.CONTINUE;
			}
 
			@Override
			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
				System.out.println("visitFile[dirId:" + directoryId.getLast() + "]: " + file);
				port1.createFileLocation(file.getFileName().toString(), directoryId.getLast(), locationId);
				return FileVisitResult.CONTINUE;
			}
 
			@Override
			public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
				System.out.println("visitFileFailed: " + file);
				return FileVisitResult.CONTINUE;
			}
 
			@Override
			public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
				Integer lastDirId = directoryId.pollLast();
				System.out.println("postVisitDirectory [" + lastDirId + "]: " + dir);
				return FileVisitResult.CONTINUE;
			}
		});
 
    }
 
}