1 |
280 |
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.apache.xmlrpc.server; |
17 |
|
|
18 |
|
import java.io.ByteArrayOutputStream; |
19 |
|
import java.io.IOException; |
20 |
|
import java.io.InputStream; |
21 |
|
import java.io.OutputStream; |
22 |
|
import java.util.List; |
23 |
|
import java.util.zip.GZIPInputStream; |
24 |
|
import java.util.zip.GZIPOutputStream; |
25 |
|
|
26 |
|
import javax.xml.parsers.ParserConfigurationException; |
27 |
|
import javax.xml.parsers.SAXParserFactory; |
28 |
|
|
29 |
|
import org.apache.commons.logging.Log; |
30 |
|
import org.apache.commons.logging.LogFactory; |
31 |
|
import org.apache.xmlrpc.XmlRpcException; |
32 |
|
import org.apache.xmlrpc.XmlRpcRequest; |
33 |
|
import org.apache.xmlrpc.XmlRpcRequestConfig; |
34 |
|
import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig; |
35 |
|
import org.apache.xmlrpc.parser.XmlRpcRequestParser; |
36 |
|
import org.apache.xmlrpc.serializer.DefaultXMLWriterFactory; |
37 |
|
import org.apache.xmlrpc.serializer.XmlRpcWriter; |
38 |
|
import org.apache.xmlrpc.serializer.XmlWriterFactory; |
39 |
|
import org.xml.sax.ContentHandler; |
40 |
|
import org.xml.sax.InputSource; |
41 |
|
import org.xml.sax.SAXException; |
42 |
|
import org.xml.sax.XMLReader; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
441 |
public abstract class XmlRpcStreamServer extends XmlRpcServer { |
50 |
1 |
private static final Log log = LogFactory.getLog(XmlRpcStreamServer.class); |
51 |
|
private static final SAXParserFactory spf; |
52 |
220 |
private XmlWriterFactory writerFactory = new DefaultXMLWriterFactory(); |
53 |
|
static { |
54 |
1 |
spf = SAXParserFactory.newInstance(); |
55 |
1 |
spf.setNamespaceAware(true); |
56 |
1 |
spf.setValidating(false); |
57 |
|
} |
58 |
|
|
59 |
|
protected XmlRpcRequest getRequest(final XmlRpcStreamRequestConfig pConfig, |
60 |
|
InputStream pStream) throws XmlRpcException { |
61 |
280 |
final XmlRpcRequestParser parser = new XmlRpcRequestParser(pConfig, getTypeFactory()); |
62 |
|
final XMLReader xr; |
63 |
|
try { |
64 |
280 |
xr = spf.newSAXParser().getXMLReader(); |
65 |
0 |
} catch (ParserConfigurationException e) { |
66 |
0 |
throw new XmlRpcException("Unable to create XML parser: " + e.getMessage(), e); |
67 |
0 |
} catch (SAXException e) { |
68 |
0 |
throw new XmlRpcException("Unable to create XML parser: " + e.getMessage(), e); |
69 |
|
} |
70 |
280 |
xr.setContentHandler(parser); |
71 |
|
try { |
72 |
280 |
xr.parse(new InputSource(pStream)); |
73 |
0 |
} catch (SAXException e) { |
74 |
0 |
Exception ex = e.getException(); |
75 |
0 |
if (ex != null && ex instanceof XmlRpcException) { |
76 |
0 |
throw (XmlRpcException) ex; |
77 |
|
} |
78 |
0 |
throw new XmlRpcException("Failed to parse XML-RPC request: " + e.getMessage(), e); |
79 |
0 |
} catch (IOException e) { |
80 |
0 |
throw new XmlRpcException("Failed to read XML-RPC request: " + e.getMessage(), e); |
81 |
|
} |
82 |
280 |
final List params = parser.getParams(); |
83 |
280 |
return new XmlRpcRequest(){ |
84 |
35 |
public XmlRpcRequestConfig getConfig() { return pConfig; } |
85 |
280 |
public String getMethodName() { return parser.getMethodName(); } |
86 |
280 |
public int getParameterCount() { return params.size(); } |
87 |
280 |
public Object getParameter(int pIndex) { return params.get(pIndex); } |
88 |
|
}; |
89 |
|
} |
90 |
|
|
91 |
|
protected XmlRpcWriter getXmlRpcWriter(XmlRpcStreamRequestConfig pConfig, |
92 |
|
OutputStream pStream) |
93 |
|
throws XmlRpcException { |
94 |
280 |
ContentHandler w = getXMLWriterFactory().getXmlWriter(pConfig, pStream); |
95 |
280 |
return new XmlRpcWriter(pConfig, w, getTypeFactory()); |
96 |
|
} |
97 |
|
|
98 |
|
protected void writeResponse(XmlRpcStreamRequestConfig pConfig, OutputStream pStream, |
99 |
|
Object pResult) throws XmlRpcException { |
100 |
|
try { |
101 |
280 |
getXmlRpcWriter(pConfig, pStream).write(pConfig, pResult); |
102 |
0 |
} catch (SAXException e) { |
103 |
0 |
throw new XmlRpcException("Failed to write XML-RPC response: " + e.getMessage(), e); |
104 |
|
} |
105 |
280 |
} |
106 |
|
|
107 |
|
protected void writeError(XmlRpcStreamRequestConfig pConfig, OutputStream pStream, |
108 |
|
Throwable pError) |
109 |
|
throws XmlRpcException { |
110 |
|
final int code; |
111 |
|
final String message; |
112 |
0 |
if (pError instanceof XmlRpcException) { |
113 |
0 |
XmlRpcException ex = (XmlRpcException) pError; |
114 |
0 |
code = ex.code; |
115 |
|
} else { |
116 |
0 |
code = 0; |
117 |
|
} |
118 |
0 |
message = pError.getMessage(); |
119 |
|
try { |
120 |
0 |
getXmlRpcWriter(pConfig, pStream).write(pConfig, code, message); |
121 |
0 |
} catch (SAXException e) { |
122 |
0 |
throw new XmlRpcException("Failed to write XML-RPC response: " + e.getMessage(), e); |
123 |
|
} |
124 |
0 |
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
public void setXMLWriterFactory(XmlWriterFactory pFactory) { |
130 |
0 |
writerFactory = pFactory; |
131 |
0 |
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
public XmlWriterFactory getXMLWriterFactory() { |
137 |
280 |
return writerFactory; |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
protected abstract InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, |
143 |
|
Object pConnection) throws IOException; |
144 |
|
|
145 |
|
protected InputStream getInputStream(XmlRpcStreamRequestConfig pConfig, |
146 |
|
Object pConnection) throws IOException { |
147 |
280 |
InputStream istream = newInputStream(pConfig, pConnection); |
148 |
280 |
if (pConfig.isEnabledForExtensions() && pConfig.isGzipCompressing()) { |
149 |
0 |
istream = new GZIPInputStream(istream); |
150 |
|
} |
151 |
280 |
return istream; |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
protected abstract OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig, |
157 |
|
Object pConnection) throws IOException; |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
protected OutputStream getOutputStream(XmlRpcStreamRequestConfig pConfig, |
163 |
|
OutputStream pStream) throws IOException { |
164 |
280 |
if (pConfig.isEnabledForExtensions() && pConfig.isGzipRequesting()) { |
165 |
0 |
return new GZIPOutputStream(pStream); |
166 |
|
} else { |
167 |
280 |
return pStream; |
168 |
|
} |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
protected OutputStream getOutputStream(XmlRpcStreamRequestConfig pConfig, |
175 |
|
Object pConnection, |
176 |
|
int pSize) throws IOException { |
177 |
70 |
return newOutputStream(pConfig, pConnection); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
protected boolean isContentLengthRequired(XmlRpcStreamRequestConfig pConfig) { |
183 |
210 |
return false; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
protected abstract void closeConnection(Object pConnection) throws IOException; |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
public void execute(XmlRpcStreamRequestConfig pConfig, |
199 |
|
Object pConnection) |
200 |
|
throws IOException, XmlRpcException { |
201 |
280 |
log.debug("execute: ->"); |
202 |
|
try { |
203 |
|
Object result; |
204 |
|
Throwable error; |
205 |
280 |
InputStream istream = null; |
206 |
|
try { |
207 |
280 |
istream = getInputStream(pConfig, pConnection); |
208 |
280 |
XmlRpcRequest request = getRequest(pConfig, istream); |
209 |
280 |
result = execute(request); |
210 |
280 |
istream.close(); |
211 |
280 |
istream = null; |
212 |
280 |
error = null; |
213 |
280 |
log.debug("execute: Request performed successfully"); |
214 |
0 |
} catch (Throwable t) { |
215 |
0 |
log.error("execute: Error while performing request", t); |
216 |
0 |
result = null; |
217 |
0 |
error = t; |
218 |
0 |
} finally { |
219 |
280 |
if (istream != null) { try { istream.close(); } catch (Throwable ignore) {} } |
220 |
0 |
} |
221 |
280 |
boolean contentLengthRequired = isContentLengthRequired(pConfig); |
222 |
|
ByteArrayOutputStream baos; |
223 |
|
OutputStream ostream; |
224 |
280 |
if (contentLengthRequired) { |
225 |
70 |
baos = new ByteArrayOutputStream(); |
226 |
70 |
ostream = baos; |
227 |
|
} else { |
228 |
210 |
baos = null; |
229 |
210 |
ostream = newOutputStream(pConfig, pConnection); |
230 |
|
} |
231 |
280 |
ostream = getOutputStream(pConfig, ostream); |
232 |
|
try { |
233 |
280 |
if (error == null) { |
234 |
280 |
writeResponse(pConfig, ostream, result); |
235 |
|
} else { |
236 |
0 |
writeError(pConfig, ostream, error); |
237 |
|
} |
238 |
280 |
ostream.close(); |
239 |
280 |
ostream = null; |
240 |
0 |
} finally { |
241 |
280 |
if (ostream != null) { try { ostream.close(); } catch (Throwable ignore) {} } |
242 |
0 |
} |
243 |
280 |
if (baos != null) { |
244 |
70 |
OutputStream dest = getOutputStream(pConfig, pConnection, baos.size()); |
245 |
|
try { |
246 |
70 |
baos.writeTo(dest); |
247 |
70 |
dest.close(); |
248 |
70 |
dest = null; |
249 |
0 |
} finally { |
250 |
70 |
if (dest != null) { try { dest.close(); } catch (Throwable ignore) {} } |
251 |
0 |
} |
252 |
|
} |
253 |
280 |
closeConnection(pConnection); |
254 |
280 |
pConnection = null; |
255 |
0 |
} finally { |
256 |
280 |
if (pConnection != null) { try { closeConnection(pConnection); } catch (Throwable ignore) {} } |
257 |
0 |
} |
258 |
280 |
log.debug("execute: <-"); |
259 |
280 |
} |
260 |
|
} |