b2e7ef3e54a1870b05876f053fa2399cd9797a0c
[shibboleth/cpp-xmltooling.git] / xmltooling / soap / SOAPTransport.h
1 /*
2  *  Copyright 2001-2007 Internet2
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 /**
18  * @file xmltooling/soap/SOAPTransport.h
19  *
20  * Encapsulates a transport layer protocol for sending/receiving messages.
21  */
22
23 #ifndef __xmltooling_soaptrans_h__
24 #define __xmltooling_soaptrans_h__
25
26 #include <xmltooling/base.h>
27 #include <iostream>
28
29 namespace xmltooling {
30
31     class XMLTOOL_API Credential;
32     class XMLTOOL_API CredentialCriteria;
33     class XMLTOOL_API CredentialResolver;
34     class XMLTOOL_API X509TrustEngine;
35
36     /**
37      * Encapsulates a transport layer protocol for sending/receiving messages.
38      *
39      * Most of the methods are const, meaning they don't affect the transport
40      * layer until the data is sent.
41      */
42     class XMLTOOL_API SOAPTransport
43     {
44         MAKE_NONCOPYABLE(SOAPTransport);
45     protected:
46         SOAPTransport() {}
47     public:
48         virtual ~SOAPTransport() {}
49
50         /**
51          * A simple structure to capture SOAP addressing information.
52          */
53         struct XMLTOOL_API Address {
54             /**
55              * Constructor.
56              *
57              * @param from      name of sender
58              * @param to        name of recipient
59              * @param endpoint  endpoint URL
60              */
61             Address(const char* from, const char* to, const char* endpoint) : m_from(from), m_to(to), m_endpoint(endpoint) {
62             }
63
64             /** Name of sender. */
65             const char* m_from;
66
67             /** Name of recipient. */
68             const char* m_to;
69
70             /** Endpoint URL. */
71             const char* m_endpoint;
72         };
73
74         /**
75          * Indicates whether transport provides confidentiality.
76          *
77          * @return  true iff transport layer provides confidentiality
78          */
79         virtual bool isConfidential() const=0;
80
81         /**
82          * Sets the connection timeout.
83          *
84          * @param timeout  time to wait for connection to server in seconds, or -1 for no timeout
85          * @return  true iff the transport supports connection timeouts
86          */
87         virtual bool setConnectTimeout(long timeout)=0;
88
89         /**
90          * Sets the request timeout.
91          *
92          * @param timeout  time to wait for a response in seconds, or -1 for no timeout
93          * @return  true iff the transport supports request/response timeouts
94          */
95         virtual bool setTimeout(long timeout)=0;
96
97         /**
98          * Common types of transport authentication that may be supported.
99          */
100         enum transport_auth_t {
101             transport_auth_none = 0,
102             transport_auth_basic = 1,
103             transport_auth_digest = 2,
104             transport_auth_ntlm = 3,
105             transport_auth_gss = 4
106         };
107
108         /**
109          * Sets a particular form of transport authentication and credentials.
110          *
111          * @param authType  type of transport authentication to use
112          * @param username  username for transport authentication
113          * @param password  simple password/credential for transport authentication
114          * @return  true iff the transport supports the indicated form of authentication
115          */
116         virtual bool setAuth(transport_auth_t authType, const char* username=NULL, const char* password=NULL)=0;
117
118         /**
119          * Determines whether TLS/SSL connections include a check of the server's certificate
120          * against the expected hostname or address. Defaults to true, and has no effect for
121          * insecure protocols.
122          *
123          * @param verify    true iff the hostname should be verified against the server's certificate
124          * @return  true iff the transport supports hostname verification
125          */
126         virtual bool setVerifyHost(bool verify)=0;
127
128 #ifndef XMLTOOLING_NO_XMLSEC
129         /**
130          * Supplies transport credentials.
131          *
132          * <p>The lifetime of the credential must be longer than the lifetime of this object.
133          *
134          * @param credential  a Credential instance, or NULL
135          * @return true iff the transport supports the use of the Credential
136          */
137         virtual bool setCredential(const Credential* credential=NULL)=0;
138
139         /**
140          * Provides an X509TrustEngine to the transport to authenticate the transport peer.
141          * The lifetime of the engine must be longer than the lifetime of this object.
142          *
143          * @param trustEngine   an X509TrustEngine instance, or NULL
144          * @param credResolver  a CredentialResolver to supply the peer's trusted credentials, or NULL
145          * @param criteria      optional criteria for selecting peer credentials
146          * @param mandatory     flag controls whether message is sent at all if the
147          *                      transport isn't authenticated using the TrustEngine
148          * @return true iff the transport supports the use of a TrustEngine
149          */
150         virtual bool setTrustEngine(
151             const X509TrustEngine* trustEngine=NULL,
152             const CredentialResolver* credResolver=NULL,
153             CredentialCriteria* criteria=NULL,
154             bool mandatory=true
155             )=0;
156 #endif
157
158         /**
159          * Sets an implementation-specific transport provider option.
160          *
161          * <p>Requires knowledge of the underlying SOAPTransport implementation.
162          * Without the proper knowledge and inputs, crashes may result.
163          *
164          * @param provider  name of the SOAPTransport class the caller believes is in use
165          * @param option    implementation-specific string containing the option to set
166          * @param value     implementation- and option-specific string to use
167          * @return  true iff the transport supports the option and value supplied
168          */
169         virtual bool setProviderOption(const char* provider, const char* option, const char* value) {
170             return false;
171         }
172
173         /**
174          * Sends a stream of data over the transport. The function may return without
175          * having received any data, depending on the nature of the transport.
176          *
177          * <p>If the stream is empty, a request may be issued with no body if the transport
178          * supports that feature.
179          *
180          * @param in    input stream to send
181          */
182         virtual void send(std::istream& in)=0;
183
184         /**
185          * Sends an optional stream of data over the transport. The function may return without
186          * having received any data, depending on the nature of the transport.
187          *
188          * <p>If the parameter is omitted, a request may be issued with no body if the transport
189          * supports that feature.
190          *
191          * @param in    input stream to send
192          */
193         virtual void send(std::istream* in=NULL);
194
195         /**
196          * Returns reference to response stream.  The resulting stream must be
197          * checked directly to determine whether data is available.
198          *
199          * @return  reference to a stream containing the response, if any
200          */
201         virtual std::istream& receive()=0;
202
203         /**
204          * Returns result of authenticating transport peer.
205          *
206          * @return true iff TrustEngine or other mechanism successfully authenticated the peer
207          */
208         virtual bool isAuthenticated() const=0;
209
210         /**
211          * Returns the MIME type of the response, if any.
212          *
213          * @return  MIME type of response, or an empty string
214          */
215         virtual std::string getContentType() const=0;
216     };
217
218 #ifndef XMLTOOLING_NO_XMLSEC
219     /**
220      * Registers SOAPTransport classes into the runtime.
221      */
222     void XMLTOOL_API registerSOAPTransports();
223
224     /**
225      * Notifies transport infrastructure to initialize.
226      */
227     void XMLTOOL_API initSOAPTransports();
228
229     /**
230      * Notifies transport infrastructure to shutdown.
231      */
232     void XMLTOOL_API termSOAPTransports();
233 #endif
234
235 };
236
237 #endif /* __xmltooling_soaptrans_h__ */