#!/usr/bin/env python3 """ Python script to test JAX-WS Hello World Service """ import requests import xml.etree.ElementTree as ET from xml.dom import minidom class HelloWorldClient: """Client for Hello World SOAP Service""" def __init__(self, service_url="http://localhost:8080/jaxws-hello-world/hello"): """ Initialize the Hello World client Args: service_url: The SOAP service endpoint URL """ self.service_url = service_url self.wsdl_url = f"{service_url}?wsdl" self.headers = { 'Content-Type': 'text/xml; charset=utf-8', 'SOAPAction': '' } def create_soap_envelope(self, name): """ Create SOAP envelope for getHelloWorld request Args: name: The name parameter for the hello world service Returns: XML string of SOAP envelope """ soap_envelope = f""" {name} """ return soap_envelope def call_hello_world(self, name): """ Call the Hello World SOAP service Args: name: The name to send to the service Returns: Response message from the service """ try: # Create SOAP envelope soap_request = self.create_soap_envelope(name) # Make the SOAP request response = requests.post( self.service_url, data=soap_request, headers=self.headers, timeout=10 ) # Check if request was successful response.raise_for_status() # Parse the response return self.parse_response(response.text) except requests.exceptions.RequestException as e: return f"Error calling service: {str(e)}" def parse_response(self, response_xml): """ Parse the SOAP response Args: response_xml: The XML response from the service Returns: The return value from the service """ try: # Parse XML root = ET.fromstring(response_xml) # Find the return element # Handle namespaces namespaces = { 'S': 'http://schemas.xmlsoap.org/soap/envelope/', 'ns2': 'http://service.example.com/' } # Find the return value return_elem = root.find('.//ns2:getHelloWorldResponse/return', namespaces) if return_elem is not None: return return_elem.text else: return "Could not find return value in response" except ET.ParseError as e: return f"Error parsing response: {str(e)}" def check_wsdl(self): """ Check if WSDL is accessible Returns: True if WSDL is accessible, False otherwise """ try: response = requests.get(self.wsdl_url, timeout=5) response.raise_for_status() return True, "WSDL is accessible" except requests.exceptions.RequestException as e: return False, f"Cannot access WSDL: {str(e)}" def pretty_print_xml(self, xml_string): """ Pretty print XML string Args: xml_string: XML string to format Returns: Formatted XML string """ try: parsed = minidom.parseString(xml_string) return parsed.toprettyxml(indent=" ") except Exception: return xml_string def main(): """Main function to test the Hello World service""" print("=" * 60) print("JAX-WS Hello World Service Test Client") print("=" * 60) print() # Create client client = HelloWorldClient() # Check WSDL print("1. Checking WSDL availability...") wsdl_ok, wsdl_msg = client.check_wsdl() print(f" {wsdl_msg}") if not wsdl_ok: print(" Service is not available. Make sure the service is running.") return print() # Test with different names test_names = [ "World", "Python Client", "JAX-WS User", "Docker" ] print("2. Testing Hello World Service...") print() for name in test_names: print(f" Calling service with name: '{name}'") result = client.call_hello_world(name) print(f" Response: {result}") print() # Interactive mode print("3. Interactive Mode") print(" " + "-" * 56) try: while True: user_input = input(" Enter a name (or 'quit' to exit): ").strip() if user_input.lower() in ['quit', 'exit', 'q']: break if user_input: result = client.call_hello_world(user_input) print(f" Response: {result}") print() else: print(" Please enter a valid name.") except KeyboardInterrupt: print("\n Interrupted by user") print() print("=" * 60) print("Test completed!") print("=" * 60) if __name__ == "__main__": main()