001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.action;
022:
023: import com.liferay.portal.kernel.util.ParamUtil;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.struts.JSONAction;
026: import com.liferay.portlet.tags.model.TagsAssetDisplay;
027: import com.liferay.portlet.tags.model.TagsAssetType;
028: import com.liferay.util.CollectionFactory;
029: import com.liferay.util.JSONUtil;
030:
031: import java.lang.reflect.InvocationTargetException;
032: import java.lang.reflect.Method;
033:
034: import java.util.Date;
035: import java.util.Map;
036:
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.apache.struts.action.ActionForm;
043: import org.apache.struts.action.ActionMapping;
044:
045: import org.json.JSONArray;
046: import org.json.JSONObject;
047:
048: /**
049: * <a href="JSONServiceAction.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Brian Wing Shun Chan
052: *
053: */
054: public class JSONServiceAction extends JSONAction {
055:
056: public String getJSON(ActionMapping mapping, ActionForm form,
057: HttpServletRequest req, HttpServletResponse res)
058: throws Exception {
059:
060: String className = ParamUtil.getString(req, "serviceClassName");
061: String methodName = ParamUtil.getString(req,
062: "serviceMethodName");
063: String[] parameters = StringUtil.split(ParamUtil.getString(req,
064: "serviceParameters"));
065:
066: Class classObj = Class.forName(className);
067:
068: Object[] methodAndParameterTypes = getMethodAndParameterTypes(
069: classObj, methodName, parameters.length);
070:
071: if (methodAndParameterTypes != null) {
072: Method method = (Method) methodAndParameterTypes[0];
073: Class[] parameterTypes = (Class[]) methodAndParameterTypes[1];
074: Object[] args = new Object[parameters.length];
075:
076: for (int i = 0; i < parameters.length; i++) {
077: args[i] = getArgValue(req, classObj, methodName,
078: parameters[i], parameterTypes[i]);
079:
080: if (args[i] == null) {
081: return null;
082: }
083: }
084:
085: try {
086: if (_log.isDebugEnabled()) {
087: _log.debug("Invoking class " + classObj
088: + " on method " + method.getName()
089: + " with args " + args);
090: }
091:
092: Object returnObj = method.invoke(classObj, args);
093:
094: if (returnObj != null) {
095: if (returnObj instanceof JSONArray) {
096: JSONArray jsonArray = (JSONArray) returnObj;
097:
098: return jsonArray.toString();
099: } else if (returnObj instanceof JSONObject) {
100: JSONObject jsonObj = (JSONObject) returnObj;
101:
102: return jsonObj.toString();
103: } else if (returnObj instanceof Boolean
104: || returnObj instanceof Double
105: || returnObj instanceof Integer
106: || returnObj instanceof Long
107: || returnObj instanceof Short
108: || returnObj instanceof String) {
109:
110: JSONObject jsonObj = new JSONObject();
111:
112: jsonObj
113: .put("returnValue", returnObj
114: .toString());
115:
116: return jsonObj.toString();
117: } else {
118: String returnValue = getReturnValue(returnObj);
119:
120: if (returnValue == null) {
121: _log
122: .error("Unsupported return type for class "
123: + classObj
124: + " and method "
125: + methodName);
126: }
127:
128: return returnValue;
129: }
130: } else {
131: JSONObject jsonObj = new JSONObject();
132:
133: return jsonObj.toString();
134: }
135: } catch (InvocationTargetException ite) {
136: JSONObject jsonObj = new JSONObject();
137:
138: jsonObj.put("exception", ite.getCause().toString());
139:
140: return jsonObj.toString();
141: }
142: }
143:
144: return null;
145: }
146:
147: protected Object getArgValue(HttpServletRequest req,
148: Class classObj, String methodName, String parameter,
149: Class parameterType) throws Exception {
150:
151: String parameterTypeName = parameterType.getName();
152:
153: if (parameterTypeName.equals("boolean")
154: || parameterTypeName.equals(Boolean.class.getName())) {
155:
156: return Boolean
157: .valueOf(ParamUtil.getBoolean(req, parameter));
158: } else if (parameterTypeName.equals("double")
159: || parameterTypeName.equals(Double.class.getName())) {
160:
161: return new Double(ParamUtil.getDouble(req, parameter));
162: } else if (parameterTypeName.equals("int")
163: || parameterTypeName.equals(Integer.class.getName())) {
164:
165: return new Integer(ParamUtil.getInteger(req, parameter));
166: } else if (parameterTypeName.equals("long")
167: || parameterTypeName.equals(Long.class.getName())) {
168:
169: return new Long(ParamUtil.getLong(req, parameter));
170: } else if (parameterTypeName.equals("short")
171: || parameterTypeName.equals(Short.class.getName())) {
172:
173: return new Short(ParamUtil.getShort(req, parameter));
174: } else if (parameterTypeName.equals(Date.class.getName())) {
175: return new Date(ParamUtil.getLong(req, parameter));
176: } else if (parameterTypeName.equals(String.class.getName())) {
177: return ParamUtil.getString(req, parameter);
178: } else if (parameterTypeName.equals("[Ljava.lang.String;")) {
179: return StringUtil
180: .split(ParamUtil.getString(req, parameter));
181: } else {
182: _log.error("Unsupported parameter type for class "
183: + classObj + ", method " + methodName
184: + ", parameter " + parameter + ", and type "
185: + parameterTypeName);
186:
187: return null;
188: }
189: }
190:
191: protected Object[] getMethodAndParameterTypes(Class classObj,
192: String methodName, int paramtersCount) throws Exception {
193:
194: String key = classObj.getName() + "_METHOD_NAME_" + methodName
195: + "_PARAMETERS_COUNT_" + paramtersCount;
196:
197: Object[] methodAndParameterTypes = (Object[]) _methodCache
198: .get(key);
199:
200: if (methodAndParameterTypes != null) {
201: return methodAndParameterTypes;
202: }
203:
204: Method method = null;
205: Class[] parameterTypes = null;
206:
207: Method[] methods = classObj.getMethods();
208:
209: for (int i = 0; i < methods.length; i++) {
210: Method curMethod = methods[i];
211:
212: if (curMethod.getName().equals(methodName)) {
213: Class[] curParameterTypes = curMethod
214: .getParameterTypes();
215:
216: if (curParameterTypes.length == paramtersCount) {
217: if (method != null) {
218: _log.error("Obscure method name for class "
219: + classObj + ", method " + methodName
220: + ", and parameter count "
221: + paramtersCount);
222:
223: return null;
224: } else {
225: method = curMethod;
226: parameterTypes = curParameterTypes;
227: }
228: }
229: }
230: }
231:
232: if (method != null) {
233: methodAndParameterTypes = new Object[] { method,
234: parameterTypes };
235:
236: _methodCache.put(key, methodAndParameterTypes);
237:
238: return methodAndParameterTypes;
239: } else {
240: _log.error("No method found for class " + classObj
241: + ", method " + methodName
242: + ", and parameter count " + paramtersCount);
243:
244: return null;
245: }
246: }
247:
248: protected String getReturnValue(Object returnObj) throws Exception {
249: if (returnObj instanceof TagsAssetDisplay) {
250: return getReturnValue((TagsAssetDisplay) returnObj);
251: } else if (returnObj instanceof TagsAssetDisplay[]) {
252: return getReturnValue((TagsAssetDisplay[]) returnObj);
253: } else if (returnObj instanceof TagsAssetType) {
254: return getReturnValue((TagsAssetType) returnObj);
255: } else if (returnObj instanceof TagsAssetType[]) {
256: return getReturnValue((TagsAssetType[]) returnObj);
257: }
258:
259: return null;
260: }
261:
262: protected String getReturnValue(TagsAssetDisplay assetDisplay)
263: throws Exception {
264:
265: JSONObject jsonObj = toJSONObject(assetDisplay);
266:
267: return jsonObj.toString();
268: }
269:
270: protected String getReturnValue(TagsAssetDisplay[] assetDisplays)
271: throws Exception {
272:
273: JSONArray jsonArray = new JSONArray();
274:
275: for (int i = 0; i < assetDisplays.length; i++) {
276: TagsAssetDisplay assetDisplay = assetDisplays[i];
277:
278: jsonArray.put(toJSONObject(assetDisplay));
279: }
280:
281: return jsonArray.toString();
282: }
283:
284: protected String getReturnValue(TagsAssetType assetType)
285: throws Exception {
286:
287: JSONObject jsonObj = toJSONObject(assetType);
288:
289: return jsonObj.toString();
290: }
291:
292: protected String getReturnValue(TagsAssetType[] assetTypes)
293: throws Exception {
294:
295: JSONArray jsonArray = new JSONArray();
296:
297: for (int i = 0; i < assetTypes.length; i++) {
298: TagsAssetType assetType = assetTypes[i];
299:
300: jsonArray.put(toJSONObject(assetType));
301: }
302:
303: return jsonArray.toString();
304: }
305:
306: public static JSONObject toJSONObject(TagsAssetDisplay assetDisplay) {
307: JSONObject jsonObj = new JSONObject();
308:
309: JSONUtil.put(jsonObj, "assetId", assetDisplay.getAssetId());
310: JSONUtil.put(jsonObj, "companyId", assetDisplay.getCompanyId());
311: JSONUtil.put(jsonObj, "userId", assetDisplay.getUserId());
312: JSONUtil.put(jsonObj, "userName", assetDisplay.getUserName());
313: JSONUtil.put(jsonObj, "createDate", assetDisplay
314: .getCreateDate());
315: JSONUtil.put(jsonObj, "modifiedDate", assetDisplay
316: .getModifiedDate());
317: JSONUtil.put(jsonObj, "classNameId", assetDisplay
318: .getClassNameId());
319: JSONUtil.put(jsonObj, "className", assetDisplay.getClassName());
320: JSONUtil.put(jsonObj, "classPK", assetDisplay.getClassPK());
321: JSONUtil.put(jsonObj, "portletId", assetDisplay.getPortletId());
322: JSONUtil.put(jsonObj, "portletTitle", assetDisplay
323: .getPortletTitle());
324: JSONUtil.put(jsonObj, "startDate", assetDisplay.getStartDate());
325: JSONUtil.put(jsonObj, "endDate", assetDisplay.getEndDate());
326: JSONUtil.put(jsonObj, "publishDate", assetDisplay
327: .getPublishDate());
328: JSONUtil.put(jsonObj, "expirationDate", assetDisplay
329: .getExpirationDate());
330: JSONUtil.put(jsonObj, "mimeType", assetDisplay.getMimeType());
331: JSONUtil.put(jsonObj, "title", assetDisplay.getTitle());
332: JSONUtil.put(jsonObj, "description", assetDisplay
333: .getDescription());
334: JSONUtil.put(jsonObj, "summary", assetDisplay.getSummary());
335: JSONUtil.put(jsonObj, "url", assetDisplay.getUrl());
336: JSONUtil.put(jsonObj, "height", assetDisplay.getHeight());
337: JSONUtil.put(jsonObj, "width", assetDisplay.getWidth());
338: JSONUtil.put(jsonObj, "priority", assetDisplay.getPriority());
339: JSONUtil.put(jsonObj, "viewCount", assetDisplay.getViewCount());
340: JSONUtil.put(jsonObj, "tagsEntries", assetDisplay
341: .getTagsEntries());
342:
343: return jsonObj;
344: }
345:
346: public static JSONObject toJSONObject(TagsAssetType assetType) {
347: JSONObject jsonObj = new JSONObject();
348:
349: JSONUtil
350: .put(jsonObj, "classNameId", assetType.getClassNameId());
351: JSONUtil.put(jsonObj, "className", assetType.getClassName());
352: JSONUtil.put(jsonObj, "portletId", assetType.getPortletId());
353: JSONUtil.put(jsonObj, "portletTitle", assetType
354: .getPortletTitle());
355:
356: return jsonObj;
357: }
358:
359: private static Log _log = LogFactory
360: .getLog(JSONServiceAction.class);
361:
362: private Map _methodCache = CollectionFactory.getHashMap();
363:
364: }
|