1 /* 2 * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.] 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.opensaml.ws.soap.client; 18 19 import java.net.URI; 20 import java.util.HashMap; 21 import java.util.Map; 22 23 import org.opensaml.ws.message.MessageContext; 24 import org.opensaml.ws.message.decoder.MessageDecodingException; 25 import org.opensaml.ws.security.SecurityPolicyException; 26 import org.opensaml.ws.soap.soap11.Envelope; 27 import org.opensaml.ws.transport.Transport; 28 import org.opensaml.ws.transport.TransportException; 29 30 /** 31 * A client for sending and receiving SOAP messages. 32 * 33 * When a client sends a message it will create a {@link Transport} instance, based on the endpoint's scheme, marshall 34 * and bind the message to the transport, receive, decode, and umarshall the response, evaluate the message security 35 * policy, and finally return the response. After this process is complete the response message and transport will be 36 * added to the message context. 37 */ 38 public class SOAPClient { 39 40 /** Registered transport factories. */ 41 private HashMap<String, ClientTransportFactory> transportFactories; 42 43 /** 44 * Constructor. 45 */ 46 public SOAPClient() { 47 } 48 49 /** 50 * Gets the transports registered with this client. 51 * 52 * @return mutable list of transports registered with this client 53 */ 54 public Map<String, ClientTransportFactory> getRegisteredTransports() { 55 return transportFactories; 56 } 57 58 /** 59 * Sends a SOAP message to the given endpoint. 60 * 61 * @param endpointURI endpoint to send the SOAP message to 62 * @param messageContext context of the message to send 63 * 64 * @throws TransportException thrown if there is a problem creating or using the {@link Transport} 65 * @throws MessageDecodingException thrown if there is a problem decoding the response 66 * @throws SecurityPolicyException thrown if there is a problem evaluating the decoder's security policy 67 */ 68 public void send(URI endpointURI, MessageContext messageContext) throws TransportException, 69 MessageDecodingException, SecurityPolicyException { 70 71 if(!(messageContext.getOutboundMessage() instanceof Envelope)){ 72 throw new TransportException("Outbound message must be a SOAP Envelope"); 73 } 74 75 String transportScheme = endpointURI.getScheme(); 76 ClientTransportFactory transFactory = transportFactories.get(transportScheme); 77 78 if (transFactory == null) { 79 throw new TransportException("No transport registered for URI scheme: " + transportScheme); 80 } 81 82 ClientTransport transport = transFactory.createTransport(); 83 transport.send(endpointURI, messageContext); 84 } 85 }