001    /**
002     * Copyright (c) 2000-2013 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.JSONDeserializer;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    import java.util.Set;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    import jodd.servlet.ServletUtil;
031    
032    /**
033     * @author Igor Spasic
034     */
035    public class JSONRPCRequest {
036    
037            public static JSONRPCRequest detectJSONRPCRequest(
038                    HttpServletRequest request) {
039    
040                    try {
041                            String requestBody = ServletUtil.readRequestBody(request);
042    
043                            if (Validator.isNull(requestBody) ||
044                                    !requestBody.startsWith(StringPool.OPEN_CURLY_BRACE) ||
045                                    !requestBody.endsWith(StringPool.CLOSE_CURLY_BRACE)) {
046    
047                                    return null;
048                            }
049    
050                            JSONDeserializer<HashMap<Object, Object>> jsonDeserializer =
051                                    JSONFactoryUtil.createJSONDeserializer();
052    
053                            jsonDeserializer.use(null, HashMap.class);
054                            jsonDeserializer.use("parameters", HashMap.class);
055    
056                            HashMap<Object, Object> requestBodyMap =
057                                    jsonDeserializer.deserialize(requestBody);
058    
059                            JSONRPCRequest jsonrpcRequest = new JSONRPCRequest();
060    
061                            Number id = (Number)requestBodyMap.get("id");
062    
063                            if (id != null) {
064                                    jsonrpcRequest._id = Integer.valueOf(id.intValue());
065                            }
066    
067                            jsonrpcRequest._jsonrpc = (String)requestBodyMap.get("jsonrpc");
068                            jsonrpcRequest._method = (String)requestBodyMap.get("method");
069                            jsonrpcRequest._parameters = (Map<String, ?>)requestBodyMap.get(
070                                    "params");
071    
072                            if (Validator.isNull(jsonrpcRequest._method)) {
073                                    return null;
074                            }
075    
076                            return jsonrpcRequest;
077                    }
078                    catch (Exception e) {
079                            if (_log.isDebugEnabled()) {
080                                    _log.debug("Unable to parse JSON RPC request", e);
081                            }
082    
083                            return null;
084                    }
085            }
086    
087            public Integer getId() {
088                    return _id;
089            }
090    
091            public String getJsonrpc() {
092                    return _jsonrpc;
093            }
094    
095            public String getMethod() {
096                    return _method;
097            }
098    
099            public String getParameter(String name) {
100                    Object value = _parameters.get(name);
101    
102                    if (value != null) {
103                            return String.valueOf(value);
104                    }
105                    else {
106                            return null;
107                    }
108            }
109    
110            public Set<String> getParameterNames() {
111                    return _parameters.keySet();
112            }
113    
114            public void setId(Integer id) {
115                    _id = id;
116            }
117    
118            public void setJsonrpc(String jsonrpc) {
119                    _jsonrpc = jsonrpc;
120            }
121    
122            public void setMethod(String method) {
123                    _method = method;
124            }
125    
126            public void setParameters(Map<String, ?> parameters) {
127                    _parameters = parameters;
128            }
129    
130            private static Log _log = LogFactoryUtil.getLog(JSONRPCRequest.class);
131    
132            private Integer _id;
133            private String _jsonrpc;
134            private String _method;
135            private Map<String, ?> _parameters;
136    
137    }