Package com.metaparadigm.jsonrpc

Examples of com.metaparadigm.jsonrpc.JSONRPCResult


    // needed to support naughty browsers such as Konqueror and Safari
    // which do not honour the charset set in the response
    response.setContentType("text/plain;charset=utf-8");

    JSONObject jsonReq = null;
    JSONRPCResult jsonRes = null;

    // Decode using the charset in the request if it exists otherwise
    // use UTF-8 as this is what all browser implementations use.
    // The JSON-RPC-Java JavaScript client is ASCII clean so it
    // although here we can correctly handle data from other clients
    // that do not escape non ASCII data
    String charset = request.getCharacterEncoding();
    if (charset == null) {
      charset = "UTF-8";
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset));

    // Read the request
    CharArrayWriter data = new CharArrayWriter();
    char[] buf = new char[BUFFER_SIZE];
    int ret;
    while ((ret = in.read(buf, 0, BUFFER_SIZE)) != -1) {
      data.write(buf, 0, ret);
    }
    writeReceive(data.toString());
    try {
      jsonReq = new JSONObject(data.toString());
      // Process the request
      jsonRes = jsonBridge.call(new Object[] { request }, jsonReq);
    } catch (ParseException e) {
      log.error("can't parse call: " + data, e);
      jsonRes = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, JSONRPCResult.MSG_ERR_PARSE);
    }

    // Write the response
    JsonObjectWriter writer = new JsonObjectWriter(response.getWriter(), 3);
    writer.write(jsonRes);
View Full Code Here


     * @return
     */
    private Message createJSONFaultMessage(Throwable throwable) {
        Message jsonFaultMessage = messageFactory.createMessage();
       
        JSONRPCResult jsonFault = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, null, throwable);
        jsonFaultMessage.setBody(jsonFault);
       
        return jsonFaultMessage;
    }
View Full Code Here

        } catch (RuntimeException re) {
            if (re.getCause() instanceof javax.security.auth.login.LoginException) {
                throw re;
            } else {
                //some other exception
                JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, re);
                return errorResult.toString().getBytes("UTF-8");
            }
        }

        if (!responseMessage.isFault()) {
            //successful execution of the invocation
            try {
                result = responseMessage.getBody();
                JSONObject jsonResponse = new JSONObject();
                jsonResponse.put("result", result);
                jsonResponse.putOpt("id", id);
                //get response to send to client
                return jsonResponse.toString().getBytes("UTF-8");
            } catch (Exception e) {
                throw new ServiceRuntimeException("Unable to create JSON response", e);
            }
        } else {
            //exception thrown while executing the invocation
            Throwable exception = (Throwable)responseMessage.getBody();
            JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, exception );
            return errorResult.toString().getBytes("UTF-8");
        }
   }
View Full Code Here

     * @return
     */
    private Message createJSONFaultMessage(Throwable throwable) {
        Message jsonFaultMessage = messageFactory.createMessage();
       
        JSONRPCResult jsonFault = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, null, throwable);
        jsonFaultMessage.setBody(jsonFault);
       
        return jsonFaultMessage;
    }
View Full Code Here

    }
   
    private Message createJSONFaultMessage(Throwable throwable) {
        Message jsonFaultMessage = messageFactory.createMessage();
       
        JSONRPCResult jsonFault = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, null, throwable);
        jsonFaultMessage.setBody(jsonFault);
       
        return jsonFaultMessage;
    }
View Full Code Here

        } catch (RuntimeException re) {
            if (re.getCause() instanceof javax.security.auth.login.LoginException) {
                throw re;
            } else {
                //some other exception
                JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, re);
                return errorResult.toString().getBytes("UTF-8");
            }
        }

        if (!responseMessage.isFault()) {
            //successful execution of the invocation
            try {
                result = responseMessage.getBody();
                JSONObject jsonResponse = new JSONObject();
                jsonResponse.put("result", result);
                jsonResponse.putOpt("id", id);
                //get response to send to client
                return jsonResponse.toString().getBytes("UTF-8");
            } catch (Exception e) {
                throw new ServiceRuntimeException("Unable to create JSON response", e);
            }
        } else {
            //exception thrown while executing the invocation
            Throwable exception = (Throwable)responseMessage.getBody();
            JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, exception );
            return errorResult.toString().getBytes("UTF-8");
        }
   }
View Full Code Here

                return jsonResponse.toString().getBytes("UTF-8");
            } catch (Exception e) {
                throw new ServiceRuntimeException("Unable to create JSON response", e);
            }
        } catch (InvocationTargetException e) {
              JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, e.getCause() );
             return errorResult.toString().getBytes("UTF-8");
        } catch(RuntimeException e) {
             JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, e.getCause());
             return errorResult.toString().getBytes("UTF-8");
        }
   }
View Full Code Here

                data.write(buf, 0, ret);
            }

            // Process the request
            JSONObject jsonReq = null;
            JSONRPCResult jsonResp = null;
            try {
                jsonReq = new JSONObject(data.toString());
                String method = jsonReq.getString("method");
                if ((method != null) && (method.indexOf('.') < 0)) {
                    jsonReq.putOpt("method", serviceName + "." + method);
                }
                jsonResp = jsonrpcBridge.call(new Object[] {request}, jsonReq);
            } catch (ParseException e) {
                throw new RuntimeException("Unable to parse request", e);
            }

            byte[] bout = jsonResp.toString().getBytes("UTF-8");

            out.write(bout);
            out.flush();
            out.close();
View Full Code Here

        while ((ret = in.read(buf, 0, 4096)) != -1) {
            data.write(buf, 0, ret);
        }

        JSONObject jsonReq = null;
        JSONRPCResult jsonResp = null;
        try {
            jsonReq = new JSONObject(data.toString());
        } catch (ParseException e) {
            throw new RuntimeException("Unable to parse request", e);
        }

        String method = jsonReq.getString("method");
        if ((method != null) && (method.indexOf('.') < 0)) {
            jsonReq.putOpt("method", serviceName + "." + method);
        }

        // invoke the request
        jsonResp = jsonrpcBridge.call(new Object[] {request}, jsonReq);

        byte[] bout = jsonResp.toString().getBytes("UTF-8");

        out.write(bout);
        out.flush();
        out.close();
    }
View Full Code Here

        } catch (RuntimeException re) {
            if (re.getCause() instanceof javax.security.auth.login.LoginException) {
                throw re;
            } else {
                //some other exception
                JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, re);
                return errorResult.toString().getBytes("UTF-8");
            }
        }

        if (!responseMessage.isFault()) {
            //successful execution of the invocation
            try {
                result = responseMessage.getBody();
                JSONObject jsonResponse = new JSONObject();
                jsonResponse.put("result", result);
                jsonResponse.putOpt("id", id);
                //get response to send to client
                return jsonResponse.toString().getBytes("UTF-8");
            } catch (Exception e) {
                throw new ServiceRuntimeException("Unable to create JSON response", e);
            }
        } else {
            //exception thrown while executing the invocation
            Throwable exception = (Throwable)responseMessage.getBody();
            JSONRPCResult errorResult = new JSONRPCResult(JSONRPCResult.CODE_REMOTE_EXCEPTION, id, exception );
            return errorResult.toString().getBytes("UTF-8");
        }
   }
View Full Code Here

TOP

Related Classes of com.metaparadigm.jsonrpc.JSONRPCResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.