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                            jsonrpcRequest._id = (Integer)requestBodyMap.get("id");
062                            jsonrpcRequest._jsonrpc = (String)requestBodyMap.get("jsonrpc");
063                            jsonrpcRequest._method = (String)requestBodyMap.get("method");
064                            jsonrpcRequest._parameters = (Map<String, ?>)requestBodyMap.get(
065                                    "params");
066    
067                            if (Validator.isNull(jsonrpcRequest._method)) {
068                                    return null;
069                            }
070    
071                            return jsonrpcRequest;
072                    }
073                    catch (Exception e) {
074                            if (_log.isDebugEnabled()) {
075                                    _log.debug("Unable to parse JSON RPC request", e);
076                            }
077    
078                            return null;
079                    }
080            }
081    
082            public Integer getId() {
083                    return _id;
084            }
085    
086            public String getJsonrpc() {
087                    return _jsonrpc;
088            }
089    
090            public String getMethod() {
091                    return _method;
092            }
093    
094            public String getParameter(String name) {
095                    Object value = _parameters.get(name);
096    
097                    if (value != null) {
098                            return String.valueOf(value);
099                    }
100                    else {
101                            return null;
102                    }
103            }
104    
105            public Set<String> getParameterNames() {
106                    return _parameters.keySet();
107            }
108    
109            public void setId(Integer id) {
110                    _id = id;
111            }
112    
113            public void setJsonrpc(String jsonrpc) {
114                    _jsonrpc = jsonrpc;
115            }
116    
117            public void setMethod(String method) {
118                    _method = method;
119            }
120    
121            public void setParameters(Map<String, ?> parameters) {
122                    _parameters = parameters;
123            }
124    
125            private static Log _log = LogFactoryUtil.getLog(JSONRPCRequest.class);
126    
127            private Integer _id;
128            private String _jsonrpc;
129            private String _method;
130            private Map<String, ?> _parameters;
131    
132    }