From: <mol...@us...> - 2007-06-15 15:50:13
|
Revision: 340 http://svn.sourceforge.net/openutils/?rev=340&view=rev Author: molaschi Date: 2007-06-15 08:50:14 -0700 (Fri, 15 Jun 2007) Log Message: ----------- spring exporter for enabling bean to be called by http request and to return results in JSON protocol Modified Paths: -------------- trunk/openutils-spring/pom.xml Added Paths: ----------- trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/ trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/ trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java Modified: trunk/openutils-spring/pom.xml =================================================================== --- trunk/openutils-spring/pom.xml 2007-06-14 09:02:24 UTC (rev 339) +++ trunk/openutils-spring/pom.xml 2007-06-15 15:50:14 UTC (rev 340) @@ -1,4 +1,5 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>net.sourceforge.openutils</groupId> @@ -44,6 +45,17 @@ </exclusions> </dependency> <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-remoting</artifactId> + <version>2.0.1</version> + </dependency> + <dependency> + <groupId>net.sf.json-lib</groupId> + <artifactId>json-lib</artifactId> + <version>1.1</version> + <classifier>jdk15</classifier> + </dependency> + <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.3</version> Added: trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java (rev 0) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java 2007-06-15 15:50:14 UTC (rev 340) @@ -0,0 +1,112 @@ +package it.openutils.spring.remoting.exporters; + +import java.lang.reflect.Method; +import java.lang.reflect.TypeVariable; +import java.util.Enumeration; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.sf.json.JSONObject; + +import org.apache.commons.beanutils.ConvertUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.NumberUtils; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.remoting.support.RemoteInvocation; +import org.springframework.remoting.support.RemoteInvocationBasedExporter; +import org.springframework.remoting.support.RemoteInvocationResult; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.Controller; + + +/** + * @author mmolaschi + * @version $Id: $ + */ +public class JSONServiceExporter extends RemoteInvocationBasedExporter implements Controller +{ + private static final String METHOD = "__method__"; + + private static final String PARAM_PREFIX = "param"; + + private static final String CONTENT_TYPE = "text/x-json"; + + /** + * {@inheritDoc} + */ + public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception + { + if (request.getParameter(METHOD) != null) + { + String methodRequest = request.getParameter(METHOD); + Method[] methods = this.getServiceInterface().getMethods(); + for (Method method : methods) + { + if (method.getName().equals(methodRequest)) + { + Class[] types = method.getParameterTypes(); + Object[] values = new Object[types.length]; + if (values.length > 0) + { + int i = 0; + Enumeration<String> names = request.getParameterNames(); + while (names.hasMoreElements()) + { + String name = names.nextElement(); + if (name != null && name.startsWith(PARAM_PREFIX)) + { + String posStr = StringUtils.substringAfter(name, PARAM_PREFIX); + int pos = NumberUtils.toInt(posStr); + + String[] reqValues = request.getParameterValues(name); + if (reqValues != null && reqValues.length > 0) + { + if (types[pos].isArray()) + { + values[pos] = ConvertUtils.convert(reqValues, types[pos]); + } + else + { + values[pos] = ConvertUtils.convert(reqValues[0], types[pos]); + } + } + else + { + values[pos] = null; + } + + } + } + } + + RemoteInvocation invocation = new RemoteInvocation(); + invocation.setArguments(values); + invocation.setParameterTypes(types); + invocation.setMethodName(methodRequest); + + RemoteInvocationResult result = invokeAndCreateResult(invocation, this.getProxyForService()); + + JSONObject obj = null; + if (result.hasException()) + { + obj = JSONObject.fromBean(result.getException()); + obj.put("exception", true); + } + else + { + obj = JSONObject.fromBean(result.getValue()); + obj.put("exception", false); + } + + response.setContentType(CONTENT_TYPE); + obj.write(response.getWriter()); + + break; + } + } + + } + return null; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |