001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.jsonwebservice;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONSerializable;
019    import com.liferay.portal.kernel.json.JSONSerializer;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    
022    import java.lang.reflect.InvocationTargetException;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    /**
028     * @author Igor Spasic
029     */
030    public class JSONRPCResponse implements JSONSerializable {
031    
032            public JSONRPCResponse(
033                    JSONRPCRequest jsonRPCRequest, Object result, Exception exception) {
034    
035                    _jsonrpc = GetterUtil.getString(jsonRPCRequest.getJsonrpc());
036    
037                    Error error = null;
038    
039                    if (!_jsonrpc.equals("2.0")) {
040                            error = new Error(-32700, "Invalid JSON RPC version " + _jsonrpc);
041                            result = null;
042                    }
043                    else if (exception != null) {
044                            int code = -32603;
045    
046                            String message = null;
047    
048                            if (exception instanceof InvocationTargetException) {
049                                    code = -32602;
050    
051                                    Throwable cause = exception.getCause();
052    
053                                    message = cause.toString();
054                            }
055                            else {
056                                    message = exception.toString();
057                            }
058    
059                            if (message == null) {
060                                    message = exception.toString();
061                            }
062    
063                            error = new Error(code, message);
064                            result = null;
065                    }
066    
067                    _error = error;
068                    _id = jsonRPCRequest.getId();
069                    _result = result;
070            }
071    
072            @Override
073            public String toJSONString() {
074                    Map<String, Object> response = new HashMap<>();
075    
076                    if (_error != null) {
077                            response.put("error", _error);
078                    }
079    
080                    if (_id != null) {
081                            response.put("id", _id);
082                    }
083    
084                    if (_jsonrpc != null) {
085                            response.put("jsonrpc", "2.0");
086                    }
087    
088                    if (_result != null) {
089                            response.put("result", _result);
090                    }
091    
092                    JSONSerializer jsonSerializer = JSONFactoryUtil.createJSONSerializer();
093    
094                    jsonSerializer.exclude("*.class");
095                    jsonSerializer.include("error", "result");
096    
097                    return jsonSerializer.serialize(response);
098            }
099    
100            public class Error {
101    
102                    public Error(int code, String message) {
103                            _code = code;
104                            _message = message;
105                    }
106    
107                    public int getCode() {
108                            return _code;
109                    }
110    
111                    public String getMessage() {
112                            return _message;
113                    }
114    
115                    private final int _code;
116                    private final String _message;
117    
118            }
119    
120            private final Error _error;
121            private final Integer _id;
122            private final String _jsonrpc;
123            private final Object _result;
124    
125    }