/
WSProcedureExecution.java
214 lines (194 loc) · 8.24 KB
/
WSProcedureExecution.java
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
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.teiid.translator.ws;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.util.Arrays;
import java.util.List;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service.Mode;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.handler.MessageContext;
import org.teiid.core.types.InputStreamFactory;
import org.teiid.core.types.SQLXMLImpl;
import org.teiid.core.types.XMLType;
import org.teiid.core.types.XMLType.Type;
import org.teiid.language.Argument;
import org.teiid.language.Argument.Direction;
import org.teiid.language.Call;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.translator.DataNotAvailableException;
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.ProcedureExecution;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.WSConnection;
import org.teiid.translator.WSConnection.Util;
import org.teiid.translator.ws.WSExecutionFactory.Binding;
import org.teiid.util.StAXSQLXML;
/**
* A soap call executor - handles all styles doc/literal, rpc/encoded etc.
*/
public class WSProcedureExecution implements ProcedureExecution {
RuntimeMetadata metadata;
ExecutionContext context;
private Call procedure;
private Source returnValue;
private WSConnection conn;
private WSExecutionFactory executionFactory;
/**
* @param env
*/
public WSProcedureExecution(Call procedure, RuntimeMetadata metadata, ExecutionContext context, WSExecutionFactory executionFactory, WSConnection conn) {
this.metadata = metadata;
this.context = context;
this.procedure = procedure;
this.conn = conn;
this.executionFactory = executionFactory;
}
@SuppressWarnings("unchecked")
public void execute() throws TranslatorException {
List<Argument> arguments = this.procedure.getArguments();
String style = (String)arguments.get(0).getArgumentValue().getValue();
String action = (String)arguments.get(1).getArgumentValue().getValue();
XMLType docObject = (XMLType)arguments.get(2).getArgumentValue().getValue();
Source source = null;
try {
Class type = StAXSource.class;
if (executionFactory.getDefaultServiceMode() == Mode.MESSAGE) {
type = DOMSource.class;
}
source = convertToSource(type, docObject);
String endpoint = (String)arguments.get(3).getArgumentValue().getValue();
if (style == null) {
style = executionFactory.getDefaultBinding().getBindingId();
} else {
try {
style = Binding.valueOf(style.toUpperCase()).getBindingId();
} catch (IllegalArgumentException e) {
throw new TranslatorException(WSExecutionFactory.UTIL.getString("invalid_invocation", Arrays.toString(Binding.values()))); //$NON-NLS-1$
}
}
Dispatch dispatch = conn.createDispatch(style, endpoint, type, executionFactory.getDefaultServiceMode());
if (Binding.HTTP.getBindingId().equals(style)) {
if (action == null) {
action = "POST"; //$NON-NLS-1$
}
dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_METHOD, action);
if (source != null && !"POST".equalsIgnoreCase(action)) { //$NON-NLS-1$
if (this.executionFactory.getXMLParamName() == null) {
throw new WebServiceException(WSExecutionFactory.UTIL.getString("http_usage_error")); //$NON-NLS-1$
}
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
StringWriter writer = new StringWriter();
//TODO: prevent this from being too large
t.transform(source, new StreamResult(writer));
String param = Util.httpURLEncode(this.executionFactory.getXMLParamName())+"="+Util.httpURLEncode(writer.toString()); //$NON-NLS-1$
endpoint = WSConnection.Util.appendQueryString(endpoint, param);
} catch (TransformerException e) {
throw new WebServiceException(e);
}
}
} else {
if (action != null) {
dispatch.getRequestContext().put(Dispatch.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
dispatch.getRequestContext().put(Dispatch.SOAPACTION_URI_PROPERTY, action);
}
}
if (source == null) {
// JBoss Native DispatchImpl throws exception when the source is null
source = new StAXSource(XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<none/>"))); //$NON-NLS-1$
}
this.returnValue = (Source) dispatch.invoke(source);
} catch (SQLException e) {
throw new TranslatorException(e);
} catch (WebServiceException e) {
throw new TranslatorException(e);
} catch (XMLStreamException e) {
throw new TranslatorException(e);
} finally {
Util.closeSource(source);
}
}
private Source convertToSource(Class<? extends Source> T, SQLXML xml) throws SQLException {
if (xml == null) {
return null;
}
return xml.getSource(T);
}
@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
return null;
}
@Override
public List<?> getOutputParameterValues() throws TranslatorException {
Object result = returnValue;
if (returnValue != null && (returnValue instanceof StAXSource) && procedure.getArguments().size() > 4
&& procedure.getArguments().get(4).getDirection() == Direction.IN
&& Boolean.TRUE.equals(procedure.getArguments().get(4).getArgumentValue().getValue())) {
SQLXMLImpl sqlXml = new StAXSQLXML((StAXSource)returnValue);
XMLType xml = new XMLType(sqlXml);
xml.setType(Type.DOCUMENT);
result = xml;
} else if (returnValue != null && returnValue instanceof DOMSource){
final DOMSource xmlSource = (DOMSource) returnValue;
SQLXMLImpl sqlXml = new SQLXMLImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Result outputTarget = new StreamResult(outputStream);
try {
TransformerFactory.newInstance().newTransformer()
.transform(xmlSource, outputTarget);
} catch (Exception e) {
throw new IOException(e);
}
return new ByteArrayInputStream(outputStream.toByteArray());
}
});
XMLType xml = new XMLType(sqlXml);
xml.setType(Type.DOCUMENT);
result = xml;
}
return Arrays.asList(result);
}
public void close() {
}
public void cancel() throws TranslatorException {
// no-op
}
}