001
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.util.WebKeys;
034
035 import java.lang.reflect.InvocationTargetException;
036
037 import javax.servlet.http.HttpServletRequest;
038 import javax.servlet.http.HttpServletResponse;
039
040 import org.apache.struts.action.ActionForm;
041 import org.apache.struts.action.ActionMapping;
042
043
047 public class JSONWebServiceServiceAction extends JSONServiceAction {
048
049 @Override
050 public String getJSON(
051 ActionMapping actionMapping, ActionForm actionForm,
052 HttpServletRequest request, HttpServletResponse response)
053 throws Exception {
054
055 UploadException uploadException = (UploadException)request.getAttribute(
056 WebKeys.UPLOAD_EXCEPTION);
057
058 if (uploadException != null) {
059 return JSONFactoryUtil.serializeThrowable(uploadException);
060 }
061
062 JSONWebServiceAction jsonWebServiceAction = null;
063
064 try {
065 jsonWebServiceAction = getJSONWebServiceAction(request);
066
067 Object returnObj = jsonWebServiceAction.invoke();
068
069 if (returnObj != null) {
070 return getReturnValue(returnObj);
071 }
072 else {
073 return JSONFactoryUtil.getNullJSON();
074 }
075 }
076 catch (InvocationTargetException ite) {
077 Throwable throwable = ite.getCause();
078
079 if (throwable instanceof SecurityException) {
080 throw (SecurityException)throwable;
081 }
082
083 if (_log.isDebugEnabled()) {
084 _log.debug(getThrowableMessage(throwable), throwable);
085 }
086 else {
087 _log.error(getThrowableMessage(throwable));
088 }
089
090 return JSONFactoryUtil.serializeThrowable(throwable);
091 }
092 catch (Exception e) {
093 if (_log.isDebugEnabled()) {
094 _log.debug(getThrowableMessage(e), e);
095 }
096 else {
097 _log.error(getThrowableMessage(e));
098 }
099
100 return JSONFactoryUtil.serializeThrowable(e);
101 }
102 }
103
104
107 @Override
108 protected String getCSRFOrigin(HttpServletRequest request) {
109 String uri = request.getRequestURI();
110
111 int x = uri.indexOf("jsonws/");
112
113 if (x < 0) {
114 return ClassUtil.getClassName(this);
115 }
116
117 String path = uri.substring(x + 7);
118
119 String[] pathArray = StringUtil.split(path, CharPool.SLASH);
120
121 if (pathArray.length < 2) {
122 return ClassUtil.getClassName(this);
123 }
124
125 StringBundler sb = new StringBundler(6);
126
127 sb.append(ClassUtil.getClassName(this));
128 sb.append(StringPool.COLON);
129 sb.append(StringPool.SLASH);
130
131 String serviceClassName = pathArray[0];
132
133 sb.append(serviceClassName);
134
135 sb.append(StringPool.SLASH);
136
137 String serviceMethodName = pathArray[1];
138
139 sb.append(serviceMethodName);
140
141 return sb.toString();
142 }
143
144 protected JSONWebServiceAction getJSONWebServiceAction(
145 HttpServletRequest request) {
146
147 String path = GetterUtil.getString(request.getPathInfo());
148
149 if (path.equals("/invoke")) {
150 return new JSONWebServiceInvokerAction(request);
151 }
152
153 if (request.getParameter("discover") != null) {
154 return new JSONWebServiceDiscoverAction(request);
155 }
156
157 return JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
158 request);
159 }
160
161 @Override
162 protected String getReroutePath() {
163 return _REROUTE_PATH;
164 }
165
166 protected String getThrowableMessage(Throwable throwable) {
167 String message = throwable.getMessage();
168
169 if (Validator.isNotNull(message)) {
170 return message;
171 }
172
173 return throwable.toString();
174 }
175
176 private static final String _REROUTE_PATH = "/jsonws";
177
178 private static Log _log = LogFactoryUtil.getLog(
179 JSONWebServiceServiceAction.class);
180
181 }