| /******************************************************************************* |
| * 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.UtilHttp; |
| import org.ofbiz.webapp.control.ConfigXMLReader.Event; |
| import org.ofbiz.webapp.control.ConfigXMLReader.RequestMap; |
| |
| @Deprecated |
| public abstract class AbstractJSONEventHandler implements EventHandler { |
| |
| public static final String module = AbstractJSONEventHandler.class.getName(); |
| protected EventHandler service; |
| |
| |
| public abstract void init(ServletContext context) throws EventHandlerException; |
| |
| public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { |
| // call into the java 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; |
| } |
| |
| } |