Repository navigation
Expand file tree
/
Copy pathcallSoapService.m
More file actions
196 lines (171 loc) · 6.31 KB
/
Copy pathcallSoapService.m
File metadata and controls
196 lines (171 loc) · 6.31 KB
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
function resp = callSoapService(endpoint,soapAction,message)
%callSoapService Send a SOAP message off to an endpoint.
% callSoapService(ENDPOINT,SOAPACTION,MESSAGE) sends the MESSAGE, a Java DOM,
% to the SOAPACTION service at the ENDPOINT.
%
% Example:
%
% message = createSoapMessage( ...
% 'urn:xmethods-delayed-quotes', ...
% 'getQuote', ...
% {'GOOG'}, ...
% {'symbol'}, ...
% {'{http://www.w3.org/2001/XMLSchema}string'}, ...
% 'rpc');
% response = callSoapService( ...
% 'http://64.124.140.30:9090/soap', ...
% 'urn:xmethods-delayed-quotes#getQuote', ...
% message);
% price = parseSoapResponse(response)
%
% See also createClassFromWsdl, createSoapMessage, parseSoapResponse.
% Matthew J. Simoneau, June 2003
% $Revision: 1.1.6.6 $ $Date: 2005/06/21 19:34:09 $
% Copyright 1984-2005 The MathWorks, Inc.
% Use inline Java to send the message.
import java.io.*;
import java.net.*;
import com.mathworks.mlwidgets.io.InterruptibleStreamCopier;
% Convert the dom to a byte stream.
toSend = serializeDOM(message);
toSend = java.lang.String(toSend);
b = toSend.getBytes('UTF8');
% Create the connection where we're going to send the file.
url = URL(endpoint);
httpConn = url.openConnection;
% Set the appropriate HTTP parameters.
httpConn.setRequestProperty('Content-Type','text/xml; charset=utf-8;');
httpConn.setRequestProperty('SOAPAction',soapAction);
httpConn.setRequestMethod('POST');
%% Set Session Cookies
global cookiesGTAS
%httpConn.setRequestProperty('User-Agent',['Matlab ' version]);
%httpConn.setRequestProperty('Expect','100-continue');
for kk=length(cookiesGTAS):-1:1
if strcmp(cookiesGTAS{kk}.url,endpoint)
httpConn.setRequestProperty('Cookie',[cookiesGTAS{kk}.name '=' cookiesGTAS{kk}.value]);
break;
end
end
%%
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
try
% Everything's set up; send the XML that was read in to b.
outputStream = httpConn.getOutputStream;
outputStream.write(b);
outputStream.close;
catch
exception = regexp(lasterr, ...
'Java exception occurred: \n(.*?)\s*\n','tokens','once');
if ~isempty(exception)
exception = exception{1};
if strcmp(exception, ...
'java.net.ConnectException: Connection refused: connect')
error('Connection refused.');
end
if strcmp(exception, ...
'ice.net.URLNotFoundException: Document not found on server')
error('The requested URL was not found on this server.')
end
host = regexp(exception,'java.net.UnknownHostException: (.*)','tokens','once');
if ~isempty(host)
error('Unknown host: %s',host{1})
end
error(exception)
else
rethrow(lasterror)
end
end
try
% Read the response.
inputStream = httpConn.getInputStream;
%% Get Session Cookies
if (length(cookiesGTAS)>10) %Allow a maximum of 10 cookies
cookiesGTAS={};
end
cookie_str=httpConn.getHeaderField('Set-Cookie');
if cookie_str~=[]
%Initialize cookie
cookie.url=''; % TODO: separate into domain and path
% TODO: cookie.expiration_timestamp=0;
cookie.name='';
cookie.value='';
%Process response Set-Cookie header
%%cookie_str = cookie_str.substring(0, cookie_str.indexOf(';'))
cookie.url=endpoint;
cookie.name = char(cookie_str.substring(0, cookie_str.indexOf('=')));
cookie.value = char(cookie_str.substring(cookie_str.indexOf('=') + 1, cookie_str.length()));
idx=length(cookiesGTAS)+1;
for kk=1:length(cookiesGTAS)
if(strcmp(cookiesGTAS{kk}.url,cookie.url) && strcmp(cookiesGTAS{kk}.name,cookie.name))
idx=kk;
end
end
%Save cookie
cookiesGTAS{idx}=cookie;
end
%%
byteArrayOutputStream = java.io.ByteArrayOutputStream;
% This StreamCopier is unsupported and may change at any time.
isc = InterruptibleStreamCopier.getInterruptibleStreamCopier;
isc.copyStream(inputStream,byteArrayOutputStream);
inputStream.close;
byteArrayOutputStream.close;
% TODO: Handle attachments.
% inputStream = httpConn.getInputStream
% mimeHeaders = javax.xml.soap.MimeHeaders;
% mimeHeaders.addHeader('Content-Type',httpConn.getHeaderField('Content-Type'));
% messageFactory = javax.xml.soap.MessageFactory.newInstance;
% message = messageFactory.createMessage(mimeHeaders,inputStream);
catch
% Try to chop off the Java stack trace.
message = lasterr;
t = regexp(message,'java.io.IOException: (.*?)\n','tokens','once');
if ~isempty(t)
message = t{1};
end
% Try to get more info from a SOAP Fault.
try
isr = InputStreamReader(httpConn.getErrorStream,'UTF-8');
in = BufferedReader(isr);
stringBuffer = java.lang.StringBuffer;
while true
inputLine = in.readLine;
if isempty(inputLine)
break
end
stringBuffer.append(inputLine);
end
in.close;
resp = stringBuffer.toString;
try
d = org.apache.xerces.parsers.DOMParser;
d.parse(org.xml.sax.InputSource(java.io.StringReader(resp)));
doc = d.getDocument;
fault = char(doc.getElementsByTagName('faultstring').item(0).getTextContent);
catch
% Received some text, but couldn't extract the faultstring.
% Use whatever text we've received.
fault = char(resp);
end
message = ['SOAP Fault: ' fault];
catch
% Couldn't get the fault. Stick with what we pulled from lasterr.
end
error(message)
end
% Leave the response as a java.lang.String so we don't squash Unicode.
resp = byteArrayOutputStream.toString('UTF-8');
%===============================================================================
function s = serializeDOM(x)
% Serialization through tranform.
domSource = javax.xml.transform.dom.DOMSource(x);
tf = javax.xml.transform.TransformerFactory.newInstance;
serializer = tf.newTransformer;
serializer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,'utf-8');
serializer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT,'yes');
stringWriter = java.io.StringWriter;
streamResult = javax.xml.transform.stream.StreamResult(stringWriter);
serializer.transform(domSource, streamResult);
s = char(stringWriter.toString);