-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoapResponse.java
More file actions
179 lines (143 loc) · 6.66 KB
/
SoapResponse.java
File metadata and controls
179 lines (143 loc) · 6.66 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
package javaxt.webservices;
import org.w3c.dom.*;
import javaxt.xml.DOM;
//******************************************************************************
//** SoapResponse Class
//******************************************************************************
/**
* Used to encapsulate an XML/SOAP message returned from a WebService.
*
******************************************************************************/
public class SoapResponse {
private java.net.URL url;
private java.util.Map<String, java.util.List<String>> headers;
private String body;
private String message;
//**************************************************************************
//** Constructor
//**************************************************************************
/** Instantiates this class using an HTTP Response object. Note that the
* entire response is parsed and stored as a class variable. This can be
* problematic when dealing with very large SOAP messages.
*/
protected SoapResponse(javaxt.http.Response response, String resultsNode) throws SoapException {
int status = response.getStatus();
if (status==200 || status==202 || status==203){
body = response.getText();
if (body!=null){
url = response.getURL();
headers = response.getHeaders();
//Parse Response
Document xml = DOM.createDocument(body);
if (xml!=null){
try{
NodeList Response = xml.getElementsByTagName(resultsNode);
if (Response!=null){
//Special Case: Probably Missing Namespace in Soap.resultsNode
if (Response.getLength()==0) {
resultsNode = getResultsNode(body, resultsNode);
Response = xml.getElementsByTagName(resultsNode);
}
//Get the content of the results node
message = DOM.getNodeValue(Response.item(0));
//Hack to deal with unexpected outputs from DOM.getNodeValue().
//If the response node has children, the method will return an
//xml fragment which will include the resultsNode as the outer node.
//We obvously don't want that so we need to strip out the outer node.
if (DOM.hasChildren(Response.item(0))){
message = message.substring(message.indexOf(">")+1);
message = message.substring(0, message.lastIndexOf("</"));
}
}
else{
throw new SoapException(
"Failed to parse SOAP Response. " +
"Could not find the " + resultsNode + " node, " +
"possibly due to a service exception.", body);
}
}
catch(Exception e){
throw new SoapException("Failed to parse SOAP Response. " + e.getLocalizedMessage(), body);
}
}
else{
throw new SoapException("Invalid SOAP Response. Response does not appear to be xml.", body);
}
}
else{
throw new SoapException("Invalid SOAP Response.", body);
}
}
else{
throw new SoapException(response.getMessage() + " (" + status + ")", response.getText());
}
}
private String getResultsNode(String ServiceResponse, String resultsNode){
resultsNode = ServiceResponse.substring(0,
ServiceResponse.toLowerCase().indexOf(resultsNode.toLowerCase()) + resultsNode.length());
resultsNode = resultsNode.substring(resultsNode.lastIndexOf("<")+1);
return resultsNode;
}
//**************************************************************************
//** getHeaders
//**************************************************************************
/** Returns key/value map representing all the HTTP headers returned from
* the server.
*/
public java.util.Map<String, java.util.List<String>> getHeaders(){
return headers;
}
//**************************************************************************
//** getURL
//**************************************************************************
/** Returns the url used to connect to the server. Note that this URL may
* differ from the one used to instantiate the Request object.
*/
public java.net.URL getURL(){
return url;
}
//**************************************************************************
//** getRawResponse
//**************************************************************************
/** Returns the body of the HTTP response returned from the server. The body
* contains the raw XML/SOAP document.
*/
public String getBody(){
return body;
}
//**************************************************************************
//** toXML
//**************************************************************************
/** Converts the raw response found in the body of the SOAP message into an
* xml document.
*/
public Document toXML(){
return DOM.createDocument(message);
}
//**************************************************************************
//** toByteArray
//**************************************************************************
/** Converts the raw response found in the body of the SOAP message into a
* byte array. Assumes that the response is Base64 encoded.
*/
public byte[] toByteArray(){
return javaxt.utils.Base64.decode(message.trim());
}
//**************************************************************************
//** toImage
//**************************************************************************
/** Converts the raw response found in the body of the SOAP message into an
* image. Typically images are encoded in Base64.
*/
public javaxt.io.Image toImage(){
return new javaxt.io.Image(toByteArray());
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns the raw response found in the body of the SOAP message.
*/
public String toString(){
return message;
}
}