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.action.JSONServiceAction;
018    import com.liferay.portal.jsonwebservice.action.JSONWebServiceDiscoverAction;
019    import com.liferay.portal.jsonwebservice.action.JSONWebServiceInvokerAction;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceAction;
022    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.upload.UploadException;
026    import com.liferay.portal.kernel.util.CharPool;
027    import com.liferay.portal.kernel.util.ClassUtil;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.StringBundler;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.StringUtil;
032    import com.liferay.portal.util.WebKeys;
033    
034    import java.lang.reflect.InvocationTargetException;
035    
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Igor Spasic
044     * @author Raymond Aug??
045     */
046    public class JSONWebServiceServiceAction extends JSONServiceAction {
047    
048            @Override
049            public String getJSON(
050                            ActionMapping actionMapping, ActionForm actionForm,
051                            HttpServletRequest request, HttpServletResponse response)
052                    throws Exception {
053    
054                    UploadException uploadException = (UploadException)request.getAttribute(
055                            WebKeys.UPLOAD_EXCEPTION);
056    
057                    if (uploadException != null) {
058                            return JSONFactoryUtil.serializeException(uploadException);
059                    }
060    
061                    JSONWebServiceAction jsonWebServiceAction = null;
062    
063                    try {
064                            jsonWebServiceAction = getJSONWebServiceAction(request);
065    
066                            Object returnObj = jsonWebServiceAction.invoke();
067    
068                            if (returnObj != null) {
069                                    return getReturnValue(returnObj);
070                            }
071                            else {
072                                    return JSONFactoryUtil.getNullJSON();
073                            }
074                    }
075                    catch (InvocationTargetException ite) {
076                            Throwable throwable = ite.getCause();
077    
078                            if (throwable instanceof SecurityException) {
079                                    throw (SecurityException)throwable;
080                            }
081    
082                            _log.error(throwable, throwable);
083    
084                            return JSONFactoryUtil.serializeThrowable(throwable);
085                    }
086                    catch (Exception e) {
087                            if (_log.isWarnEnabled()) {
088                                    _log.warn(e, e);
089                            }
090    
091                            return JSONFactoryUtil.serializeException(e);
092                    }
093            }
094    
095            /**
096             * @see JSONServiceAction#getCSRFOrigin(HttpServletRequest)
097             */
098            @Override
099            protected String getCSRFOrigin(HttpServletRequest request) {
100                    String uri = request.getRequestURI();
101    
102                    int x = uri.indexOf("jsonws/");
103    
104                    if (x < 0) {
105                            return ClassUtil.getClassName(this);
106                    }
107    
108                    String path = uri.substring(x + 7);
109    
110                    String[] pathArray = StringUtil.split(path, CharPool.SLASH);
111    
112                    if (pathArray.length < 2) {
113                            return ClassUtil.getClassName(this);
114                    }
115    
116                    StringBundler sb = new StringBundler(6);
117    
118                    sb.append(ClassUtil.getClassName(this));
119                    sb.append(StringPool.COLON);
120                    sb.append(StringPool.SLASH);
121    
122                    String serviceClassName = pathArray[0];
123    
124                    sb.append(serviceClassName);
125    
126                    sb.append(StringPool.SLASH);
127    
128                    String serviceMethodName = pathArray[1];
129    
130                    sb.append(serviceMethodName);
131    
132                    return sb.toString();
133            }
134    
135            protected JSONWebServiceAction getJSONWebServiceAction(
136                    HttpServletRequest request) {
137    
138                    String path = GetterUtil.getString(request.getPathInfo());
139    
140                    if (path.equals("/invoke")) {
141                            return new JSONWebServiceInvokerAction(request);
142                    }
143    
144                    if (request.getParameter("discover") != null) {
145                            return new JSONWebServiceDiscoverAction(request);
146                    }
147    
148                    return JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
149                            request);
150            }
151    
152            @Override
153            protected String getReroutePath() {
154                    return _REROUTE_PATH;
155            }
156    
157            private static final String _REROUTE_PATH = "/jsonws";
158    
159            private static Log _log = LogFactoryUtil.getLog(
160                    JSONWebServiceServiceAction.class);
161    
162    }