Tweak Airline SAAJ client.

This commit is contained in:
Greg L. Turnquist
2022-11-29 13:04:06 -06:00
parent 7c9fe22db7
commit 64bccb5f76
5 changed files with 75 additions and 239 deletions

View File

@@ -10,48 +10,29 @@
<relativePath>../../../</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>org.springframework.ws</groupId>
<artifactId>airline-client-saaj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Web Services Samples - Airline - Client - SAAJ</name>
<description>Demo project for Spring Web Services</description>
<properties>
<java.version>1.8</java.version>
<log4j2.version>2.15.0</log4j2.version>
<xws.version>3.0</xws.version>
<saaj-impl.version>2.0.1</saaj-impl.version>
</properties>
<dependencies>
<dependency>
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>${saaj-impl.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.wss</groupId>
<artifactId>xws-security</artifactId>
<version>${xws.version}</version>
<exclusions>
<exclusion>
<groupId>javax.xml.crypto</groupId>
<artifactId>xmldsig</artifactId>
</exclusion>
<exclusion>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- <dependency>-->
<!-- <groupId>javax.activation</groupId>-->
<!-- <artifactId>activation</artifactId>-->
<!-- <version>1.1</version>-->
<!-- </dependency>-->
</dependencies>
</dependencies>
<repositories>
<repository>

View File

@@ -16,20 +16,13 @@
package org.springframework.ws.samples.airline.client.saaj;
import jakarta.xml.soap.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
@@ -44,74 +37,73 @@ import javax.xml.transform.stream.StreamResult;
*/
public class GetFlights {
public static final String NAMESPACE_URI = "http://www.springframework.org/spring-ws/samples/airline/schemas/messages";
public static final String NAMESPACE_URI = "http://www.springframework.org/spring-ws/samples/airline/schemas/messages";
public static final String PREFIX = "airline";
public static final String PREFIX = "airline";
private SOAPConnectionFactory connectionFactory;
private SOAPConnectionFactory connectionFactory;
private MessageFactory messageFactory;
private MessageFactory messageFactory;
private URL url;
private URL url;
private TransformerFactory transfomerFactory;
private TransformerFactory transfomerFactory;
public GetFlights(String url) throws SOAPException, MalformedURLException {
connectionFactory = SOAPConnectionFactory.newInstance();
messageFactory = MessageFactory.newInstance();
transfomerFactory = TransformerFactory.newInstance();
this.url = new URL(url);
}
public GetFlights(String url) throws SOAPException, MalformedURLException {
connectionFactory = SOAPConnectionFactory.newInstance();
messageFactory = MessageFactory.newInstance();
transfomerFactory = TransformerFactory.newInstance();
this.url = new URL(url);
}
private SOAPMessage createGetFlightsRequest() throws SOAPException {
SOAPMessage message = messageFactory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name getFlightsRequestName = envelope.createName("GetFlightsRequest", PREFIX, NAMESPACE_URI);
SOAPBodyElement getFlightsRequestElement = message.getSOAPBody().addBodyElement(getFlightsRequestName);
Name fromName = envelope.createName("from", PREFIX, NAMESPACE_URI);
SOAPElement fromElement = getFlightsRequestElement.addChildElement(fromName);
fromElement.setValue("AMS");
Name toName = envelope.createName("to", PREFIX, NAMESPACE_URI);
SOAPElement toElement = getFlightsRequestElement.addChildElement(toName);
toElement.setValue("VCE");
Name departureDateName = envelope.createName("departureDate", PREFIX, NAMESPACE_URI);
SOAPElement departureDateElement = getFlightsRequestElement.addChildElement(departureDateName);
departureDateElement.setValue("2006-01-31");
return message;
}
private SOAPMessage createGetFlightsRequest() throws SOAPException {
SOAPMessage message = messageFactory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name getFlightsRequestName = envelope.createName("GetFlightsRequest", PREFIX, NAMESPACE_URI);
SOAPBodyElement getFlightsRequestElement = message.getSOAPBody().addBodyElement(getFlightsRequestName);
Name fromName = envelope.createName("from", PREFIX, NAMESPACE_URI);
SOAPElement fromElement = getFlightsRequestElement.addChildElement(fromName);
fromElement.setValue("AMS");
Name toName = envelope.createName("to", PREFIX, NAMESPACE_URI);
SOAPElement toElement = getFlightsRequestElement.addChildElement(toName);
toElement.setValue("VCE");
Name departureDateName = envelope.createName("departureDate", PREFIX, NAMESPACE_URI);
SOAPElement departureDateElement = getFlightsRequestElement.addChildElement(departureDateName);
departureDateElement.setValue("2006-01-31");
return message;
}
public void getFlights() throws SOAPException, IOException, TransformerException {
SOAPMessage request = createGetFlightsRequest();
SOAPConnection connection = connectionFactory.createConnection();
SOAPMessage response = connection.call(request, url);
if (!response.getSOAPBody().hasFault()) {
writeGetFlightsResponse(response);
}
else {
SOAPFault fault = response.getSOAPBody().getFault();
System.err.println("Received SOAP Fault");
System.err.println("SOAP Fault Code: " + fault.getFaultCode());
System.err.println("SOAP Fault String: " + fault.getFaultString());
}
}
public void getFlights() throws SOAPException, IOException, TransformerException {
SOAPMessage request = createGetFlightsRequest();
SOAPConnection connection = connectionFactory.createConnection();
SOAPMessage response = connection.call(request, url);
if (!response.getSOAPBody().hasFault()) {
writeGetFlightsResponse(response);
} else {
SOAPFault fault = response.getSOAPBody().getFault();
System.err.println("Received SOAP Fault");
System.err.println("SOAP Fault Code: " + fault.getFaultCode());
System.err.println("SOAP Fault String: " + fault.getFaultString());
}
}
private void writeGetFlightsResponse(SOAPMessage message) throws SOAPException, TransformerException {
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name getFlightsResponseName = envelope.createName("GetFlightsResponse", PREFIX, NAMESPACE_URI);
SOAPBodyElement getFlightsResponseElement =
(SOAPBodyElement) message.getSOAPBody().getChildElements(getFlightsResponseName).next();
Name flightName = envelope.createName("flight", PREFIX, NAMESPACE_URI);
Iterator iterator = getFlightsResponseElement.getChildElements(flightName);
Transformer transformer = transfomerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
int count = 1;
while (iterator.hasNext()) {
System.out.println("Flight " + count);
System.out.println("--------");
SOAPElement flightElement = (SOAPElement) iterator.next();
DOMSource source = new DOMSource(flightElement);
transformer.transform(source, new StreamResult(System.out));
}
}
private void writeGetFlightsResponse(SOAPMessage message) throws SOAPException, TransformerException {
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name getFlightsResponseName = envelope.createName("GetFlightsResponse", PREFIX, NAMESPACE_URI);
SOAPBodyElement getFlightsResponseElement = (SOAPBodyElement) message.getSOAPBody()
.getChildElements(getFlightsResponseName).next();
Name flightName = envelope.createName("flight", PREFIX, NAMESPACE_URI);
Iterator iterator = getFlightsResponseElement.getChildElements(flightName);
Transformer transformer = transfomerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
int count = 1;
while (iterator.hasNext()) {
System.out.println("Flight " + count);
System.out.println("--------");
SOAPElement flightElement = (SOAPElement) iterator.next();
DOMSource source = new DOMSource(flightElement);
transformer.transform(source, new StreamResult(System.out));
}
}
}

View File

@@ -1,128 +0,0 @@
/*
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.client.saaj;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPMessage;
import com.sun.xml.wss.ProcessingContext;
import com.sun.xml.wss.XWSSProcessor;
import com.sun.xml.wss.XWSSProcessorFactory;
import com.sun.xml.wss.XWSSecurityException;
import com.sun.xml.wss.impl.callback.PasswordCallback;
import com.sun.xml.wss.impl.callback.UsernameCallback;
/**
* Simple client that calls the WS-Security <code>GetFrequentFlyerMileage</code> operation using SAAJ and XWSS.
*
* @author Arjen Poutsma
*/
public class GetFrequentFlyerMileage {
public static final String NAMESPACE_URI = "http://www.springframework.org/spring-ws/samples/airline/schemas/messages";
public static final String PREFIX = "airline";
private SOAPConnectionFactory connectionFactory;
private MessageFactory messageFactory;
private URL url;
private XWSSProcessorFactory processorFactory;
public GetFrequentFlyerMileage(String url) throws SOAPException, MalformedURLException, XWSSecurityException {
connectionFactory = SOAPConnectionFactory.newInstance();
messageFactory = MessageFactory.newInstance();
processorFactory = XWSSProcessorFactory.newInstance();
this.url = new URL(url);
}
private SOAPMessage createGetMileageRequest() throws SOAPException {
SOAPMessage message = messageFactory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name getFlightsRequestName = envelope.createName("GetFrequentFlyerMileageRequest", GetFrequentFlyerMileage.PREFIX,
GetFrequentFlyerMileage.NAMESPACE_URI);
message.getSOAPBody().addBodyElement(getFlightsRequestName);
return message;
}
private SOAPMessage secureMessage(SOAPMessage message, final String username, final String password)
throws IOException, XWSSecurityException {
CallbackHandler callbackHandler = new CallbackHandler() {
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof UsernameCallback) {
UsernameCallback callback = (UsernameCallback) callbacks[i];
callback.setUsername(username);
}
else if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback callback = (PasswordCallback) callbacks[i];
callback.setPassword(password);
}
else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
};
InputStream policyStream = null;
XWSSProcessor processor = null;
try {
policyStream = getClass().getResourceAsStream("securityPolicy.xml");
processor = processorFactory.createProcessorForSecurityConfiguration(policyStream, callbackHandler);
}
finally {
if (policyStream != null) {
policyStream.close();
}
}
ProcessingContext context = processor.createProcessingContext(message);
return processor.secureOutboundMessage(context);
}
public void getMileage(String username, String password) throws SOAPException, IOException, XWSSecurityException {
SOAPMessage request = createGetMileageRequest();
request = secureMessage(request, username, password);
SOAPConnection connection = connectionFactory.createConnection();
SOAPMessage response = connection.call(request, url);
if (!response.getSOAPBody().hasFault()) {
SOAPBodyElement mileage = (SOAPBodyElement) response.getSOAPBody().getChildElements().next();
System.out.println("'" + username + "' has " + mileage.getValue() + " frequent flyer miles");
}
else {
SOAPFault fault = response.getSOAPBody().getFault();
System.err.println("Received SOAP Fault");
System.err.println("SOAP Fault Code: " + fault.getFaultCode());
System.err.println("SOAP Fault String: " + fault.getFaultString());
}
}
}

View File

@@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory;
*/
public class SaajMain {
private static final Logger logger = LoggerFactory.getLogger(SaajMain.class);
private static final Logger logger = LoggerFactory.getLogger(SaajMain.class);
public static void main(String[] args) throws Exception {
@@ -33,15 +33,9 @@ public class SaajMain {
url = args[0];
}
logger.info("Connecting to " + url + " for flight details...");
logger.info("Connecting to " + url + " for flight details...");
GetFlights getFlights = new GetFlights(url);
GetFlights getFlights = new GetFlights(url);
getFlights.getFlights();
String username = "john";
String password = "changeme";
GetFrequentFlyerMileage getMileage = new GetFrequentFlyerMileage(url);
getMileage.getMileage(username, password);
}
}

View File

@@ -1,3 +0,0 @@
<xwss:SecurityConfiguration dumpMessages="false" xmlns:xwss="http://java.sun.com/xml/ns/xwss/config">
<xwss:UsernameToken digestPassword="true" useNonce="true"/>
</xwss:SecurityConfiguration>