You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ap...@apache.org on 2008/06/26 03:50:39 UTC
svn commit: r671706 - in /ofbiz/trunk/framework:
example/webapp/example/WEB-INF/controller.xml
webapp/src/org/ofbiz/webapp/event/AbstractJSONEventHandler.java
webapp/src/org/ofbiz/webapp/event/JSONSimpleEventHandler.java
Author: apatel
Date: Wed Jun 25 18:50:39 2008
New Revision: 671706
URL: http://svn.apache.org/viewvc?rev=671706&view=rev
Log:
JSON handler for simple method events.
Added:
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/AbstractJSONEventHandler.java (with props)
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONSimpleEventHandler.java (with props)
Modified:
ofbiz/trunk/framework/example/webapp/example/WEB-INF/controller.xml
Modified: ofbiz/trunk/framework/example/webapp/example/WEB-INF/controller.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/example/webapp/example/WEB-INF/controller.xml?rev=671706&r1=671705&r2=671706&view=diff
==============================================================================
--- ofbiz/trunk/framework/example/webapp/example/WEB-INF/controller.xml (original)
+++ ofbiz/trunk/framework/example/webapp/example/WEB-INF/controller.xml Wed Jun 25 18:50:39 2008
@@ -30,6 +30,7 @@
<handler name="groovy" type="request" class="org.ofbiz.webapp.event.GroovyEventHandler"/>
<handler name="jsonservice" type="request" class="org.ofbiz.webapp.event.JSONServiceEventHandler"/>
+ <handler name="jsonsimple" type="request" class="org.ofbiz.webapp.event.JSONSimpleEventHandler"/>
<!--
These can be used to return the reports as views; make sure the classes are compiled and available
Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/AbstractJSONEventHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/AbstractJSONEventHandler.java?rev=671706&view=auto
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/AbstractJSONEventHandler.java (added)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/AbstractJSONEventHandler.java Wed Jun 25 18:50:39 2008
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+
+
+package org.ofbiz.webapp.event;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import javolution.util.FastMap;
+import net.sf.json.JSONObject;
+
+import org.ofbiz.base.util.Debug;
+
+public abstract class AbstractJSONEventHandler implements EventHandler{
+
+ public static final String module = JSONSimpleEventHandler.class.getName();
+ protected EventHandler service;
+
+
+ public abstract void init(ServletContext context) throws EventHandlerException;
+
+ public String invoke(String eventPath, String eventMethod, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {
+ // call into the java handler for parameters parsing and invocation
+ String respCode = service.invoke(eventPath, eventMethod, request, response);
+ // pull out the service response from the request attribute
+ Map attrMap = getAttributesAsMap(request);
+
+ // create a JSON Object for return
+ JSONObject json = JSONObject.fromMap(attrMap);
+ String jsonStr = json.toString();
+ if (jsonStr == null) {
+ throw new EventHandlerException("JSON Object was empty; fatal error!");
+ }
+
+ // 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) {
+ throw new EventHandlerException("Problems with Json encoding", e);
+ }
+
+ // return the JSON String
+ Writer out;
+ try {
+ out = response.getWriter();
+ out.write(jsonStr);
+ out.flush();
+ } catch (IOException e) {
+ throw new EventHandlerException("Unable to get response writer", e);
+ }
+ return respCode;
+ }
+
+ private Map getAttributesAsMap(HttpServletRequest request) {
+ Map attrMap = FastMap.newInstance();
+ Enumeration en = request.getAttributeNames();
+ while (en.hasMoreElements()) {
+ String name = (String) en.nextElement();
+ Object val = request.getAttribute(name);
+ if (val instanceof java.sql.Timestamp) {
+ val = val.toString();
+ }
+ if (val instanceof String || val instanceof Number || val instanceof Map || val instanceof List) {
+ if (Debug.verboseOn()) Debug.logVerbose("Adding attribute to JSON output: " + name, module);
+ attrMap.put(name, val);
+ }
+ }
+
+ return attrMap;
+ }
+
+}
\ No newline at end of file
Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/AbstractJSONEventHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/AbstractJSONEventHandler.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/AbstractJSONEventHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONSimpleEventHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONSimpleEventHandler.java?rev=671706&view=auto
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONSimpleEventHandler.java (added)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONSimpleEventHandler.java Wed Jun 25 18:50:39 2008
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+
+
+package org.ofbiz.webapp.event;
+
+import javax.servlet.ServletContext;
+
+
+public class JSONSimpleEventHandler extends AbstractJSONEventHandler{
+
+ public void init(ServletContext context) throws EventHandlerException {
+ this.service = new SimpleEventHandler();
+ this.service.init(context);
+ }
+
+}
Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONSimpleEventHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONSimpleEventHandler.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONSimpleEventHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain