Dokumentation der SOAP Schnittstelle

Im folgenden Abschnitt wird ausführlich erklärt, wie man die bereit gestellten Remote-Funktionen nutzt.

Programmiersprache Java mit Eclipse Mars for Java EE Developers

Bei SOAP werden die Daten im XML-Format übertragen. Davon bekommt man aber normalerweise nichts mit, da die Daten in POJO's ausgeliefert werden. Um sich das Arbeiten mit (P)lain-(O)ld-(J)ava-(O)bjects zu erleichern, erstellen Sie sich nun einen einfachen Converter.


  1. Erstellen Sie eine neue Klasse Converter.java im Client Package ohne Main-Methode

    Listing Converter.java

    /**
     * Converter for otela Enterprise Systems
     *
     * The MIT License (MIT)
     *
     * @copyright Copyright(C) 2016 otela Enterprise Systems
     * @author    Markus Bröker<markus.broeker@otela.net>
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */
    package de.otela.soap.client;
    
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class Converter {
    
    	/**
    	 * Erzeugt aus einem Remote-Objekt eine Map, die einfach zu durchlaufen ist
    	 *
    	 * @param object
    	 * @return
    	 */
    	public static Map<String, String> remoteObject2Map(Object object) {
    
    		Map<String, String> map = new HashMap<>();
    		for (Field field : object.getClass().getDeclaredFields()) {
    			field.setAccessible(true);
    			String name = field.getName();
    
    			try {
    				Object value = field.get(object);
    
    				if (value instanceof String) {
    					map.put(name, String.valueOf(value));
    				}
    
    			} catch (Exception e) {
    				System.err.println("Konvertierungsfehler");
    			}
    		}
    
    		return map;
    	}
    
    	/**
    	 * Simple Dump-Methode für die Antwort des Webservice
    	 *
    	 * @param object
    	 */
    	public static void printObject(Object object) {
    		Map<String, String> map = remoteObject2Map(object);
    
    		for (Entry<String, String> entry : map.entrySet()) {
    			if (!entry.getValue().equals("")) {
    				System.out.printf("%40s => %s\n", entry.getKey(), entry.getValue());
    			}
    		}
    	}
    
    	/**
    	 * Mappt die otela Webservice Felder auf benutzerdefinierte Felder
    	 *
    	 * @param object
    	 * @param otelaKeys
    	 * @param myKeys
    	 *
    	 * @return List<String[]>
    	 */
    	public static List<String[]> transFormKeysToListArray(Object object, String[] otelaKeys, String[] myKeys) {
    
    		if (otelaKeys.length != myKeys.length) {
    			throw new IllegalArgumentException("Key-Sizes differ");
    		}
    
    		Map<String, String> original = remoteObject2Map(object);
    
    		List<String[]> list = new ArrayList<>();
    		for (int i = 0; i < otelaKeys.length; i++) {
    			if (original.containsKey(otelaKeys[i])) {
    				String value = original.get(otelaKeys[i]);
    
    				list.add(new String[] { myKeys[i], value });
    			}
    		}
    
    		return list;
    	}
    
    }
    

  2. Der Vorteil von SOAP ist die Einfachheit der Datenverabeitung über Unternehmensgrenzen hinweg.
    Sollten Sie beispielsweise an allen Hotels in der Nähe von Antalya interessiert sein, reicht folgendes Code-Fragment...

    Code Fragment

    
    
            CustomerServicePortProxy proxy = new CustomerServicePortProxy();
            CustomerServicePort port = proxy.getCustomerServicePort();
    
            try {
                CustomerServiceBindingStub binding = (CustomerServiceBindingStub) port;
    
                ApiKey key = new ApiKey();
    
                binding.setUsername(key.getServiceLogin());
                binding.setPassword(key.getServicePassword());
    
                Credentials credentials = new Credentials(key.getLogin(), key.getPassword());
    
                // Suche alle Hotels mit (Lat/Lon) => (36, 30) im Umkreis von 100 km.
                RLocation[] locations = proxy.findHotelsByLocation(credentials, 36, 30, 100);
    
                // Die neu erstellte Converter Klasse stellt eine simple Dump-Methode für Pojo's bereit
                for (RLocation r : locations) {
                    Converter.printObject(r);
                }
    
            } catch (Exception e) {
                System.out.println("Ausnahme: " + e.getLocalizedMessage());
            }
    
    

Programmiersprache PHP mit Netbeans

Bei SOAP werden die Daten im XML-Format übertragen. Davon bekommt man aber normalerweise nichts mit, da die Daten im Datentyp stdClass ausgeliefert werden. Mit einem einfachen foreach lassen sich in PHP sowohl assoziative Arrays als auch stdClass'es durchlaufen.

In diesem Kapitel wird das nun erläutert.


Code Fragment



    // Proxy initialisieren
    $proxy = new SoapClient(WSDL_URL, $options);

    // Suche alle Hotels mit (Lat/Lon) => (36, 30) im Umkreis von 100 km.
    $locations = $proxy->findHotelsByLocation($credentials, 36, 30, 100);

    foreach ($locations as $location) {
        foreach ($location as $key => $value) {
            printf("%30s => %s\n", $key, $value);
        }
    }

Programmiersprache C# mit Visual Studio

Bei SOAP werden die Daten im XML-Format übertragen. Davon bekommt man aber normalerweise nichts mit, da die Daten als Objekte/Entitäten ausgeliefert werden. Mit üblichen Gettern und Settern können Sie nun reihenweise auf die abgefragten Daten zugreifen.

In diesem Kapitel wird das nun erläutert.


Code Fragment


    // ApiKey instanzieren
    ApiKey key = new ApiKey();

    // otelaClient instanzieren
    otelaClient client = new otelaClient(key.GetServiceLogin(), key.GetServicePassword());

    try {

        Credentials credentials = new Credentials();
        credentials.login = key.GetLogin();
        credentials.password = key.GetPassword();

        // Abfrage aller Hotels in Antalya im Umkreis von 100km
        RLocation[] locations = client.findHotelsByLocation(credentials, 36, 32, 100);

        // Ausgabe im CSV-Format
        foreach (RLocation location in locations) {
            System.Console.WriteLine(location.hotelname + ";" + location.street + ";" + location.city_name + ";" + location.phone);
        }

    } catch (FaultException fe) {
        System.Console.WriteLine("Service Error" + fe.Code);
    }

    // Warte auf Tastendruck
    System.Console.ReadKey();