summaryrefslogtreecommitdiff
path: root/ksoap2-base/src/main/java/org/ksoap2/transport/Transport.java
blob: d92e8d81ca268d9d244f7adbb829ca2a45366497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
 * Copyright (c) 2006, James Seigel, Calgary, AB., Canada
 * Copyright (c) 2003,2004, Stefan Haustein, Oberhausen, Rhld., Germany
 *
 * 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 org.ksoap2.transport;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.io.*;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;

import libcore.util.XmlObjectFactory;

import org.ksoap2.*;
import org.xmlpull.v1.*;

/**
 * Abstract class which holds common methods and members that are used by the
 * transport layers. This class encapsulates the serialization and
 * deserialization of the soap messages, leaving the basic communication
 * routines to the subclasses.
 */
abstract public class Transport {

    /**
     * Added to enable web service interactions on the emulator to be debugged
     * with Fiddler2 (Windows) but provides utility for other proxy
     * requirements.
     */
    protected Proxy proxy;
    protected String url;
    protected int timeout = ServiceConnection.DEFAULT_TIMEOUT;
    /** Set to true if debugging */
    public boolean debug = true;
    /** String dump of request for debugging. */
    public String requestDump;
    /** String dump of response for debugging */
    public String responseDump;
    private String xmlVersionTag = "";

    protected static final String CONTENT_TYPE_XML_CHARSET_UTF_8 = "text/xml;charset=utf-8";
    protected static final String CONTENT_TYPE_SOAP_XML_CHARSET_UTF_8 =
            "application/soap+xml;charset=utf-8";
    protected static final String USER_AGENT = "ksoap2-android/2.6.0+";

    private int bufferLength = ServiceConnection.DEFAULT_BUFFER_SIZE;

    private HashMap prefixes = new HashMap();

    public HashMap getPrefixes() {
        return prefixes;
    }

    public Transport() {
    }

    public Transport(String url) {
        this(null, url);
    }

    public Transport(String url, int timeout) {
        this.url = url;
        this.timeout = timeout;
    }

    public Transport(String url, int timeout, int bufferLength) {
        this.url = url;
        this.timeout = timeout;
        this.bufferLength = bufferLength;
    }

    /**
     * Construct the transport object
     *
     * @param proxy
     *            Specifies the proxy server to use for accessing the web
     *            service or <code>null</code> if a direct connection is
     *            available
     * @param url
     *            Specifies the web service url
     *
     */
    public Transport(Proxy proxy, String url) {
        this.proxy = proxy;
        this.url = url;
    }

    public Transport(Proxy proxy, String url, int timeout) {
        this.proxy = proxy;
        this.url = url;
        this.timeout = timeout;
    }

    public Transport(Proxy proxy, String url, int timeout, int bufferLength) {
        this.proxy = proxy;
        this.url = url;
        this.timeout = timeout;
        this.bufferLength = bufferLength;
    }

    /**
     * Sets up the parsing to hand over to the envelope to deserialize.
     */
    protected void parseResponse(SoapEnvelope envelope, InputStream is)
            throws XmlPullParserException, IOException {
        // Android-changed: Use XmlObjectFactory instead of a specific implementation.
        // XmlPullParser xp = new KXmlParser();
        XmlPullParser xp = XmlObjectFactory.newXmlPullParser();
        xp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        xp.setInput(is, null);
        envelope.parse(xp);
        /*
         * Fix memory leak when running on android in strict mode. Issue 133
         */
        is.close();
    }

    /**
     * Serializes the request.
     */
    protected byte[] createRequestData(SoapEnvelope envelope, String encoding)
            throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferLength);
        byte result[] = null;
        bos.write(xmlVersionTag.getBytes());
        // Android-changed: Use XmlObjectFactory instead of a specific implementation.
        // XmlSerializer xw = new KXmlSerializer();
        XmlSerializer xw = XmlObjectFactory.newXmlSerializer();

        final Iterator keysIter = prefixes.keySet().iterator();
        xw.setOutput(bos, encoding);
        while (keysIter.hasNext()) {
            String key = (String) keysIter.next();
            xw.setPrefix(key, (String) prefixes.get(key));
        }
        envelope.write(xw);
        xw.flush();
        bos.write('\r');
        bos.write('\n');
        bos.flush();
        result = bos.toByteArray();
        xw = null;
        bos = null;
        return result;
    }

    /**
     * Serializes the request.
     */
    protected byte[] createRequestData(SoapEnvelope envelope)
            throws IOException {
        return createRequestData(envelope, null);
    }

    /**
     * Set the target url.
     *
     * @param url
     *            the target url.
     */
    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        return url;
    }


    /**
     * Sets the version tag for the outgoing soap call. Example <?xml
     * version=\"1.0\" encoding=\"UTF-8\"?>
     *
     * @param tag
     *            the xml string to set at the top of the soap message.
     */
    public void setXmlVersionTag(String tag) {
        xmlVersionTag = tag;
    }

    /**
     * Attempts to reset the connection.
     */
    public void reset() {
    }

    /**
     * Perform a soap call with a given namespace and the given envelope
     * providing any extra headers that the user requires such as cookies.
     * Headers that are returned by the web service will be returned to the
     * caller in the form of a <code>List</code> of <code>HeaderProperty</code>
     * instances.
     *
     * @param soapAction
     *            the namespace with which to perform the call in.
     * @param envelope
     *            the envelope the contains the information for the call.
     * @param headers
     *            <code>List</code> of <code>HeaderProperty</code> headers to
     *            send with the SOAP request.
     *
     * @return Headers returned by the web service as a <code>List</code> of
     *         <code>HeaderProperty</code> instances.
     */
    abstract public List call(String soapAction, SoapEnvelope envelope,
            List headers) throws IOException, XmlPullParserException;

    /**
     * Perform a soap call with a given namespace and the given envelope
     * providing any extra headers that the user requires such as cookies.
     * Headers that are returned by the web service will be returned to the
     * caller in the form of a <code>List</code> of <code>HeaderProperty</code>
     * instances.
     *
     * @param soapAction
     *            the namespace with which to perform the call in.
     * @param envelope
     *            the envelope the contains the information for the call.
     * @param headers
     *            <code>List</code> of <code>HeaderProperty</code> headers to
     *            send with the SOAP request.
     * @param outputFile
     *            a file to stream the response into rather than parsing it,
     *            streaming happens when file is not null
     *
     * @return Headers returned by the web service as a <code>List</code> of
     *         <code>HeaderProperty</code> instances.
     */
    abstract public List call(String soapAction, SoapEnvelope envelope,
            List headers, File outputFile) throws IOException,
            XmlPullParserException;

    /**
     * Perform a soap call with a given namespace and the given envelope.
     *
     * @param soapAction
     *            the namespace with which to perform the call in.
     * @param envelope
     *            the envelope the contains the information for the call.
     */
    public void call(String soapAction, SoapEnvelope envelope)
            throws IOException, XmlPullParserException {
        call(soapAction, envelope, null);
    }

    /**
     * Return the name of the host that is specified as the web service target
     *
     * @return Host name
     */
    public String getHost() throws MalformedURLException {

        return new URL(url).getHost();
    }

    /**
     * Return the port number of the host that is specified as the web service
     * target
     *
     * @return Port number
     */
    public int getPort() throws MalformedURLException {

        return new URL(url).getPort();
    }

    /**
     * Return the path to the web service target
     *
     * @return The URL's path
     */
    public String getPath() throws MalformedURLException {

        return new URL(url).getPath();
    }

    abstract public ServiceConnection getServiceConnection() throws IOException;
}