|
Author: lektran
Date: Fri Dec 18 10:54:20 2009 New Revision: 892218 URL: http://svn.apache.org/viewvc?rev=892218&view=rev Log: Added a common resquest mapping for sending json responses based on the request attributes, requests requiring json responses should chain the request. Removes the need for a json event wrapper for every type of event. Based on idea put forward by Bob Morley OFBIZ-2778 Modified: ofbiz/trunk/framework/common/build.xml ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml Modified: ofbiz/trunk/framework/common/build.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/build.xml?rev=892218&r1=892217&r2=892218&view=diff ============================================================================== --- ofbiz/trunk/framework/common/build.xml (original) +++ ofbiz/trunk/framework/common/build.xml Fri Dec 18 10:54:20 2009 @@ -40,6 +40,7 @@ <fileset dir="../service/lib" includes="*.jar"/> <fileset dir="../service/build/lib" includes="*.jar"/> <fileset dir="../entityext/build/lib" includes="*.jar"/> + <fileset dir="../webapp/lib" includes="*.jar"/> <fileset dir="../webapp/build/lib" includes="*.jar"/> <fileset dir="../widget/build/lib" includes="*.jar"/> </path> Modified: ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java?rev=892218&r1=892217&r2=892218&view=diff ============================================================================== --- ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java (original) +++ ofbiz/trunk/framework/common/src/org/ofbiz/common/CommonEvents.java Fri Dec 18 10:54:20 2009 @@ -20,8 +20,8 @@ import java.io.IOException; import java.io.PrintWriter; -import java.util.HashMap; -import java.util.Iterator; +import java.io.UnsupportedEncodingException; +import java.io.Writer; import java.util.List; import java.util.Map; @@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse; import javolution.util.FastMap; +import net.sf.json.JSONObject; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.StringUtil; @@ -243,6 +244,40 @@ } return "success"; } + + public static String jsonResponseFromRequestAttributes(HttpServletRequest request, HttpServletResponse response) { + // pull out the service response from the request attribute + Map<String, Object> attrMap = UtilHttp.getJSONAttributeMap(request); + + // create a JSON Object for return + JSONObject json = JSONObject.fromObject(attrMap); + String jsonStr = json.toString(); + if (jsonStr == null) { + Debug.logError("JSON Object was empty; fatal error!", module); + return "success"; + } + + // set the X-JSON content type + response.setContentType("application/x-json"); + // jsonStr.length is not reliable for unicode characters + try { + response.setContentLength(jsonStr.getBytes("UTF8").length); + } catch (UnsupportedEncodingException e) { + Debug.logError("Problems with Json encoding: " + e, module); + } + + // return the JSON String + Writer out; + try { + out = response.getWriter(); + out.write(jsonStr); + out.flush(); + } catch (IOException e) { + Debug.logError(e, module); + } + + return "success"; + } } Modified: ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml?rev=892218&r1=892217&r2=892218&view=diff ============================================================================== --- ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml (original) +++ ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml Fri Dec 18 10:54:20 2009 @@ -166,6 +166,14 @@ <response name="success" type="view-last"/> </request-map> + <!-- Common json reponse events, chain these after events to send json reponses --> + <!-- Standard json response, uses all compatible request attributes --> + <request-map uri="json"> + <security direct-request="false"/> + <event type="java" path="org.ofbiz.common.CommonEvents" invoke="jsonResponseFromRequestAttributes"/> + <response name="success" type="none"/> + </request-map> + <!-- View Mappings --> <view-map name="error" page="/error/error.jsp"/> <view-map name="main" type="none"/> |
| Free forum by Nabble | Edit this page |
