001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces.components;
034:
035: import com.flexive.faces.FxJsfUtils;
036: import com.flexive.war.JsonWriter;
037: import com.metaparadigm.jsonrpc.JSONRPCBridge;
038: import com.metaparadigm.jsonrpc.JSONRPCResult;
039: import org.apache.commons.lang.StringUtils;
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.json.JSONObject;
043:
044: import javax.faces.component.UIComponent;
045: import javax.faces.context.FacesContext;
046: import javax.faces.render.Renderer;
047: import java.io.IOException;
048: import java.io.StringWriter;
049:
050: /**
051: * Renderer for the JsonRpcCall component.
052: *
053: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
054: * @version $Rev: 1 $
055: */
056: public class JsonRpcCallRenderer extends Renderer {
057: private static final Log LOG = LogFactory
058: .getLog(JsonRpcCallRenderer.class);
059: public static final String SESSION_JSON_BRIDGE = "JSONRPCBridge";
060:
061: /**
062: * {@inheritDoc}
063: */
064: @Override
065: public void encodeBegin(FacesContext facesContext,
066: UIComponent uiComponent) throws IOException {
067: JsonRpcCall rpcCall = (JsonRpcCall) uiComponent;
068: if (StringUtils.isBlank(rpcCall.getMethod())) {
069: LOG.error("JsonRpcCall without method called");
070: return;
071: }
072: String jsonRequest = getJsonRequest(rpcCall);
073: try {
074: JSONRPCBridge bridge = (JSONRPCBridge) FxJsfUtils
075: .getSession().getAttribute(SESSION_JSON_BRIDGE);
076: JSONRPCResult result = bridge.call(
077: new Object[] { FxJsfUtils.getRequest() },
078: new JSONObject(jsonRequest));
079: facesContext.getResponseWriter().write(
080: result.getResult().toString());
081: } catch (Throwable e) {
082: LOG.error("Failed to executed JSON/RPC request: "
083: + e.getMessage(), e);
084: }
085: }
086:
087: private String getJsonRequest(JsonRpcCall rpcCall)
088: throws IOException {
089: StringWriter result = new StringWriter();
090: JsonWriter out = new JsonWriter(result);
091: out.startMap();
092: out.writeAttribute("id", 0);
093: out.writeAttribute("method", rpcCall.getMethod());
094:
095: out.startAttribute("params");
096: out.startArray();
097: out.writeLiteral(rpcCall.getArgs(), false);
098: out.closeArray();
099:
100: out.closeMap();
101: out.finishResponse();
102: return result.toString();
103: }
104: }
|