001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.kernel.util.Validator;
033    import com.liferay.portal.security.auth.PrincipalException;
034    import com.liferay.portal.util.WebKeys;
035    
036    import java.lang.reflect.InvocationTargetException;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Igor Spasic
046     * @author Raymond Aug??
047     */
048    public class JSONWebServiceServiceAction extends JSONServiceAction {
049    
050            @Override
051            public String getJSON(
052                            ActionMapping actionMapping, ActionForm actionForm,
053                            HttpServletRequest request, HttpServletResponse response)
054                    throws Exception {
055    
056                    UploadException uploadException = (UploadException)request.getAttribute(
057                            WebKeys.UPLOAD_EXCEPTION);
058    
059                    if (uploadException != null) {
060                            return JSONFactoryUtil.serializeThrowable(uploadException);
061                    }
062    
063                    JSONWebServiceAction jsonWebServiceAction = null;
064    
065                    try {
066                            jsonWebServiceAction = getJSONWebServiceAction(request);
067    
068                            Object returnObj = jsonWebServiceAction.invoke();
069    
070                            if (returnObj != null) {
071                                    return getReturnValue(returnObj);
072                            }
073                            else {
074                                    return JSONFactoryUtil.getNullJSON();
075                            }
076                    }
077                    catch (InvocationTargetException ite) {
078                            Throwable throwable = ite.getCause();
079    
080                            int status = 0;
081    
082                            if (throwable instanceof PrincipalException ||
083                                    throwable instanceof SecurityException) {
084    
085                                    status = HttpServletResponse.SC_FORBIDDEN;
086                            }
087                            else {
088                                    status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
089                            }
090    
091                            response.setStatus(status);
092    
093                            if (_log.isDebugEnabled()) {
094                                    _log.debug(getThrowableMessage(throwable), throwable);
095                            }
096                            else {
097                                    _log.error(getThrowableMessage(throwable));
098                            }
099    
100                            return JSONFactoryUtil.serializeThrowable(throwable);
101                    }
102                    catch (Exception e) {
103                            if (_log.isDebugEnabled()) {
104                                    _log.debug(getThrowableMessage(e), e);
105                            }
106                            else {
107                                    _log.error(getThrowableMessage(e));
108                            }
109    
110                            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
111    
112                            return JSONFactoryUtil.serializeThrowable(e);
113                    }
114            }
115    
116            /**
117             * @see JSONServiceAction#getCSRFOrigin(HttpServletRequest)
118             */
119            @Override
120            protected String getCSRFOrigin(HttpServletRequest request) {
121                    String uri = request.getRequestURI();
122    
123                    int x = uri.indexOf("jsonws/");
124    
125                    if (x < 0) {
126                            return ClassUtil.getClassName(this);
127                    }
128    
129                    String path = uri.substring(x + 7);
130    
131                    String[] pathArray = StringUtil.split(path, CharPool.SLASH);
132    
133                    if (pathArray.length < 2) {
134                            return ClassUtil.getClassName(this);
135                    }
136    
137                    StringBundler sb = new StringBundler(6);
138    
139                    sb.append(ClassUtil.getClassName(this));
140                    sb.append(StringPool.COLON);
141                    sb.append(StringPool.SLASH);
142    
143                    String serviceClassName = pathArray[0];
144    
145                    sb.append(serviceClassName);
146    
147                    sb.append(StringPool.SLASH);
148    
149                    String serviceMethodName = pathArray[1];
150    
151                    sb.append(serviceMethodName);
152    
153                    return sb.toString();
154            }
155    
156            protected JSONWebServiceAction getJSONWebServiceAction(
157                    HttpServletRequest request) {
158    
159                    String path = GetterUtil.getString(request.getPathInfo());
160    
161                    if (path.equals("/invoke")) {
162                            return new JSONWebServiceInvokerAction(request);
163                    }
164    
165                    if (request.getParameter("discover") != null) {
166                            return new JSONWebServiceDiscoverAction(request);
167                    }
168    
169                    return JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
170                            request);
171            }
172    
173            @Override
174            protected String getReroutePath() {
175                    return _REROUTE_PATH;
176            }
177    
178            protected String getThrowableMessage(Throwable throwable) {
179                    String message = throwable.getMessage();
180    
181                    if (Validator.isNotNull(message)) {
182                            return message;
183                    }
184    
185                    return throwable.toString();
186            }
187    
188            private static final String _REROUTE_PATH = "/jsonws";
189    
190            private static Log _log = LogFactoryUtil.getLog(
191                    JSONWebServiceServiceAction.class);
192    
193    }