|
1 |
| package net.sf.crispy.impl.xmlrpc; |
|
2 |
| |
|
3 |
| import java.io.ByteArrayInputStream; |
|
4 |
| import java.io.IOException; |
|
5 |
| import java.io.InputStream; |
|
6 |
| import java.io.OutputStream; |
|
7 |
| import java.util.Enumeration; |
|
8 |
| |
|
9 |
| import javax.servlet.ServletConfig; |
|
10 |
| import javax.servlet.ServletException; |
|
11 |
| import javax.servlet.http.HttpServletRequest; |
|
12 |
| import javax.servlet.http.HttpServletResponse; |
|
13 |
| |
|
14 |
| import net.sf.crispy.server.HttpServiceEndpoint; |
|
15 |
| import net.sf.crispy.server.SingleServiceContainer; |
|
16 |
| import net.sf.crispy.server.SingleServiceContainerImpl; |
|
17 |
| import net.sf.crispy.util.Util; |
|
18 |
| |
|
19 |
| import org.apache.commons.logging.Log; |
|
20 |
| import org.apache.commons.logging.LogFactory; |
|
21 |
| import org.apache.xmlrpc.DefaultHandlerMapping; |
|
22 |
| import org.apache.xmlrpc.XmlRpcHandlerMapping; |
|
23 |
| import org.apache.xmlrpc.XmlRpcRequestProcessor; |
|
24 |
| import org.apache.xmlrpc.XmlRpcServerRequest; |
|
25 |
| import org.apache.xmlrpc.XmlRpcWorker; |
|
26 |
| |
|
27 |
| public class XmlRpcHttpServlet extends HttpServiceEndpoint implements SingleServiceContainer { |
|
28 |
| |
|
29 |
| private static final long serialVersionUID = 2859849674935099151L; |
|
30 |
| |
|
31 |
| protected static final Log log = LogFactory.getLog (XmlRpcHttpServlet.class); |
|
32 |
| private SingleServiceContainer serviceContainer = new SingleServiceContainerImpl(); |
|
33 |
| private String nullValue = null; |
|
34 |
| |
|
35 |
5
| public XmlRpcHttpServlet() { }
|
|
36 |
| |
|
37 |
1
| public void setNullValue (String pvNullValue) {
|
|
38 |
1
| nullValue = pvNullValue;
|
|
39 |
| } |
|
40 |
4
| public String getNullValue() {
|
|
41 |
4
| return nullValue;
|
|
42 |
| } |
|
43 |
| |
|
44 |
3
| public void init(ServletConfig pvConfig) throws ServletException {
|
|
45 |
3
| super.init(pvConfig);
|
|
46 |
3
| Enumeration lvEnumeration = pvConfig.getInitParameterNames();
|
|
47 |
3
| while (lvEnumeration.hasMoreElements()) {
|
|
48 |
3
| String lvKey = (String) lvEnumeration.nextElement();
|
|
49 |
3
| String lvValue = getInitParameter(lvKey);
|
|
50 |
3
| try {
|
|
51 |
3
| Object lvServiceObject = Util.createObject(lvValue);
|
|
52 |
2
| addService(lvKey, lvServiceObject);
|
|
53 |
| } catch (Exception e) { |
|
54 |
1
| throw new ServletException("Exception in init-method: " + e, e);
|
|
55 |
| } |
|
56 |
| } |
|
57 |
| } |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
3
| public void addService(String pvInterfaceName, Object pvServiceImpl) {
|
|
62 |
3
| DefaultHandlerMapping lvMapping = new DefaultHandlerMapping();
|
|
63 |
3
| lvMapping.addHandler(pvInterfaceName, new XmlRpcConvertHandler(pvServiceImpl, getNullValue(), this));
|
|
64 |
3
| serviceContainer.addService(pvInterfaceName, lvMapping);
|
|
65 |
| } |
|
66 |
| |
|
67 |
5
| public Object getService(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws Exception {
|
|
68 |
| |
|
69 |
5
| InputStream is = (InputStream) pvRequest.getAttribute("NewInputStream");
|
|
70 |
| |
|
71 |
5
| XmlRpcRequestProcessor lvProcessor = new XmlRpcRequestProcessor();
|
|
72 |
5
| XmlRpcServerRequest lvRequest = lvProcessor.decodeRequest(is);
|
|
73 |
5
| String lvServiceName = lvRequest.getMethodName();
|
|
74 |
| |
|
75 |
5
| int i = lvServiceName.lastIndexOf('.');
|
|
76 |
5
| lvServiceName = lvServiceName.substring(0, i);
|
|
77 |
5
| Object lvService = getService(lvServiceName);
|
|
78 |
5
| return lvService;
|
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
5
| public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
|
83 |
5
| try {
|
|
84 |
5
| InputStream is = req.getInputStream();
|
|
85 |
| |
|
86 |
5
| byte b[] = Util.inputStream2ByteArray(is);
|
|
87 |
| |
|
88 |
5
| req.setAttribute("NewInputStream", new ByteArrayInputStream(b));
|
|
89 |
5
| Object lvService = getService(req, res);
|
|
90 |
5
| XmlRpcHandlerMapping lvMapping = (XmlRpcHandlerMapping) lvService;
|
|
91 |
| |
|
92 |
5
| XmlRpcWorker lvWorker = new XmlRpcWorker(lvMapping);
|
|
93 |
5
| byte[] result = lvWorker.execute(new ByteArrayInputStream(b), null, null);
|
|
94 |
| |
|
95 |
5
| res.setContentType("text/xml");
|
|
96 |
5
| res.setContentLength(result.length);
|
|
97 |
5
| OutputStream output = res.getOutputStream();
|
|
98 |
5
| output.write(result);
|
|
99 |
5
| output.flush();
|
|
100 |
| } catch (Exception e) { |
|
101 |
0
| e.printStackTrace();
|
|
102 |
| } |
|
103 |
| } |
|
104 |
| |
|
105 |
2
| public void addService(String pvServiceInterfaceName, String pvServiceImplName) {
|
|
106 |
2
| serviceContainer.addService(pvServiceInterfaceName, pvServiceImplName);
|
|
107 |
| } |
|
108 |
| |
|
109 |
1
| public void removeService(String pvServiceInterfaceName) {
|
|
110 |
1
| serviceContainer.removeService(pvServiceInterfaceName);
|
|
111 |
| } |
|
112 |
| |
|
113 |
5
| public Object getService(String pvServiceInterfaceName) {
|
|
114 |
5
| return serviceContainer.getService(pvServiceInterfaceName);
|
|
115 |
| } |
|
116 |
| |
|
117 |
5
| public int getServiceSize() {
|
|
118 |
5
| return serviceContainer.getServiceSize();
|
|
119 |
| } |
|
120 |
| |
|
121 |
| } |