View Javadoc

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      public XmlRpcHttpServlet() { }
36  
37      public void setNullValue (String pvNullValue) {
38      	nullValue = pvNullValue;
39      }
40      public String getNullValue() {
41  		return nullValue;
42  	}
43      
44  	public void init(ServletConfig pvConfig) throws ServletException {
45  		super.init(pvConfig);
46  		Enumeration lvEnumeration = pvConfig.getInitParameterNames();
47  		while (lvEnumeration.hasMoreElements()) {
48  			String lvKey = (String) lvEnumeration.nextElement();
49  			String lvValue = getInitParameter(lvKey);
50  			try {
51  				Object lvServiceObject = Util.createObject(lvValue);
52  				addService(lvKey, lvServiceObject);
53  			} catch (Exception e) {
54  				throw new ServletException("Exception in init-method: " + e, e);
55  			}
56  		}		
57  	}
58  
59      
60      
61      public void addService(String pvInterfaceName, Object pvServiceImpl) {
62      	DefaultHandlerMapping lvMapping = new DefaultHandlerMapping();
63      	lvMapping.addHandler(pvInterfaceName, new XmlRpcConvertHandler(pvServiceImpl, getNullValue(), this));
64      	serviceContainer.addService(pvInterfaceName, lvMapping);
65      }
66  
67      public Object getService(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws Exception {
68      	
69      	InputStream is = (InputStream) pvRequest.getAttribute("NewInputStream");
70      	
71  		XmlRpcRequestProcessor lvProcessor = new XmlRpcRequestProcessor();
72  		XmlRpcServerRequest lvRequest = lvProcessor.decodeRequest(is);
73  		String lvServiceName = lvRequest.getMethodName();
74  		
75  		int i = lvServiceName.lastIndexOf('.');
76  		lvServiceName = lvServiceName.substring(0, i);
77  		Object lvService = getService(lvServiceName);
78  		return lvService;
79      }
80      
81          
82      public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
83      	try {
84      		InputStream is = req.getInputStream();
85      		// save InputStream, InputStream must twice read 
86      		byte b[] = Util.inputStream2ByteArray(is);
87      		
88      		req.setAttribute("NewInputStream", new ByteArrayInputStream(b));    		
89      		Object lvService = getService(req, res);
90      		XmlRpcHandlerMapping lvMapping = (XmlRpcHandlerMapping) lvService;
91      		
92      		XmlRpcWorker lvWorker = new XmlRpcWorker(lvMapping);
93      		byte[] result = lvWorker.execute(new ByteArrayInputStream(b), null, null);
94      		
95  	        res.setContentType("text/xml");
96  	        res.setContentLength(result.length);
97  	        OutputStream output = res.getOutputStream();
98  	        output.write(result);
99  	        output.flush();
100 	    } catch (Exception e) {
101 	    	e.printStackTrace();
102 	    }
103     }
104     
105 	public void addService(String pvServiceInterfaceName, String pvServiceImplName) {
106 		serviceContainer.addService(pvServiceInterfaceName, pvServiceImplName);
107 	}
108 
109 	public void removeService(String pvServiceInterfaceName) {
110 		serviceContainer.removeService(pvServiceInterfaceName);
111 	}
112 
113 	public Object getService(String pvServiceInterfaceName) {
114 		return serviceContainer.getService(pvServiceInterfaceName);
115 	}
116 
117 	public int getServiceSize() {
118 		return serviceContainer.getServiceSize();
119 	}
120 
121 }