Author: apatel
Date: Mon Jul 27 12:22:21 2009 New Revision: 798114 URL: http://svn.apache.org/viewvc?rev=798114&view=rev Log: Adding Json event handler for multi service. Patch from OFBIZ-2762. Thanks Rishi and Chirag for implementation. Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONServiceMultiEventHandler.java (with props) Modified: ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml 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=798114&r1=798113&r2=798114&view=diff ============================================================================== --- ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml (original) +++ ofbiz/trunk/framework/common/webcommon/WEB-INF/common-controller.xml Mon Jul 27 12:22:21 2009 @@ -35,6 +35,7 @@ <handler name="simple" type="request" class="org.ofbiz.webapp.event.SimpleEventHandler"/> <handler name="groovy" type="request" class="org.ofbiz.webapp.event.GroovyEventHandler"/> <handler name="jsonservice" type="request" class="org.ofbiz.webapp.event.JSONServiceEventHandler"/> + <handler name="jsonservice-multi" type="request" class="org.ofbiz.webapp.event.JSONServiceMultiEventHandler"/> <handler name="jsonsimple" type="request" class="org.ofbiz.webapp.event.JSONSimpleEventHandler"/> <handler name="rome" type="request" class="org.ofbiz.webapp.event.RomeEventHandler"/> Added: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONServiceMultiEventHandler.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONServiceMultiEventHandler.java?rev=798114&view=auto ============================================================================== --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONServiceMultiEventHandler.java (added) +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONServiceMultiEventHandler.java Mon Jul 27 12:22:21 2009 @@ -0,0 +1,86 @@ +/******************************************************************************* + * 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.Map; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.sf.json.JSONObject; + +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.UtilHttp; +import org.ofbiz.webapp.control.ConfigXMLReader.Event; +import org.ofbiz.webapp.control.ConfigXMLReader.RequestMap; + +/** + * JSONServiceMultiEventHandler - JSON Object Wrapper around the ServiceMultiEventHandler + */ +public class JSONServiceMultiEventHandler implements EventHandler { + + public static final String module = JSONServiceMultiEventHandler.class.getName(); + protected EventHandler service; + + public void init(ServletContext context) throws EventHandlerException { + this.service = new ServiceMultiEventHandler(); + this.service.init(context); + } + + public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { + // call into the service handler for parameters parsing and invocation + String respCode = service.invoke(event, requestMap, request, 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) { + 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; + } + +} Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONServiceMultiEventHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONServiceMultiEventHandler.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/JSONServiceMultiEventHandler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain |
Free forum by Nabble | Edit this page |