001package org.apache.turbine.modules.screens; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.BufferedReader; 023import java.io.CharArrayWriter; 024import java.io.InputStreamReader; 025import java.io.OutputStreamWriter; 026import java.io.PrintWriter; 027 028import javax.servlet.http.HttpServletRequest; 029 030import org.apache.turbine.annotation.TurbineService; 031import org.apache.turbine.pipeline.PipelineData; 032import org.apache.turbine.services.jsonrpc.JsonRpcService; 033import org.apache.turbine.util.RunData; 034import org.jabsorb.JSONRPCBridge; 035 036 037/** 038 * A Screen class for dealing with JSON-RPC requests. Typically you would 039 * extend this class and override the doOutput() method to use TurbineJsonRpc 040 * to register the POJOs that will provide the functions you are making 041 * available via JSON-RPC. Use JSONSecureScreen if you need the user to be 042 * logged in prior to executing the functions you provide. 043 * 044 * <p>Here is an example from a superclass: 045 * <code> 046 * public void doOutput(PipelineData data) throws Exception 047 * { 048 * User user = data.getUser(); 049 * 050 * MyJsonFunctions myFunctions = new MyJsonFunctions(user.getName()); 051 * 052 * // Session specific 053 * TurbineJsonRpc.registerObject(data.getSession(), "myFunctions", myFunctions); 054 * 055 * // Global 056 * //TurbineJsonRpc.registerObjectGlobal("testGlobal", testObject); 057 * 058 * super.doOutput(data); 059 * } 060 * </code> 061 * 062 * <p>The class MyFunctions would be something like: 063 * <code> 064 * public class MyJsonFunctions 065 * { 066 * private String getHello(String clientParameter) 067 * { 068 * return "Hello " + clientParameter; 069 * } 070 * } 071 * </code> 072 * 073 * <p>This code is derived from the com.metaparadigm.jsonrpc.JSONRPCServlet and (after upgrade) checked against 074 * org.jabsorb.JSONRPCServlet. 075 * 076 * @author brad@folkens.com 077 * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a> 078 * @version $Id: JSONScreen.java 1812628 2017-10-19 12:34:25Z gk $ 079 */ 080public class JSONScreen extends RawScreen 081{ 082 protected static final String JSONRPC_CONTENT_TYPE = "application/json;charset=utf-8"; 083 084 protected final static int BUFFER_SIZE = 4096; 085 086 /** Injected service instance */ 087 @TurbineService 088 protected JsonRpcService jsonRpcService; 089 090 /** 091 * @see org.apache.turbine.modules.screens.RawScreen#getContentType(org.apache.turbine.pipeline.PipelineData) 092 */ 093 @Override 094 protected String getContentType(PipelineData pipelineData) 095 { 096 return JSONRPC_CONTENT_TYPE; 097 } 098 099 /** 100 * Output the dynamic content. 101 * 102 * Encodign is UTF-8. @{@link #JSONRPC_CONTENT_TYPE}: {@value #JSONRPC_CONTENT_TYPE}. 103 * 104 * @param pipelineData The PipelineData object. 105 */ 106 @Override 107 protected void doOutput(PipelineData pipelineData) throws Exception 108 { 109 RunData data = getRunData(pipelineData); 110 data.declareDirectResponse(); 111 HttpServletRequest request = data.getRequest(); 112 113 // we require utf-8, cft 114 String charset = "UTF-8"; //request.getCharacterEncoding(); 115 BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset)); 116 117 // Read the request 118 CharArrayWriter cdata = new CharArrayWriter(); 119 char buf[] = new char[BUFFER_SIZE]; 120 int ret; 121 while ((ret = in.read(buf, 0, BUFFER_SIZE)) != -1) 122 { 123 cdata.write(buf, 0, ret); 124 } 125 126 // Find the JSONRPCBridge for this session or create one 127 // if it doesn't exist 128 JSONRPCBridge json_bridge = jsonRpcService.getBridge(data.getSession()); 129 130 // Process the request 131 Object json_res = jsonRpcService.processCall(cdata, json_bridge, request); 132 133 PrintWriter out = new PrintWriter( 134 new OutputStreamWriter(data.getResponse().getOutputStream(),charset)); 135 out.print(json_res.toString()); 136 out.flush(); 137 out.close(); 138 } 139}