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.bean.BeanLocator;
021 import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
022 import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
023 import com.liferay.portal.kernel.json.JSONFactoryUtil;
024 import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceAction;
025 import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil;
026 import com.liferay.portal.kernel.log.Log;
027 import com.liferay.portal.kernel.log.LogFactoryUtil;
028 import com.liferay.portal.kernel.upload.UploadException;
029 import com.liferay.portal.kernel.util.ContextPathUtil;
030 import com.liferay.portal.kernel.util.GetterUtil;
031 import com.liferay.portal.kernel.util.StringPool;
032 import com.liferay.portal.spring.context.PortalContextLoaderListener;
033 import com.liferay.portal.util.WebKeys;
034
035 import java.lang.reflect.InvocationTargetException;
036
037 import javax.servlet.ServletContext;
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
048 public class JSONWebServiceServiceAction extends JSONServiceAction {
049
050 public JSONWebServiceServiceAction(
051 ServletContext servletContext, ClassLoader classLoader) {
052
053 _contextPath = ContextPathUtil.getContextPath(servletContext);
054
055 String contextName = _contextPath;
056
057 BeanLocator beanLocator = null;
058
059 if (_contextPath.equals(
060 PortalContextLoaderListener.getPortalServletContextPath()) ||
061 _contextPath.isEmpty()) {
062
063 beanLocator = PortalBeanLocatorUtil.getBeanLocator();
064 }
065 else {
066 contextName = _contextPath;
067
068 if (contextName.startsWith(StringPool.SLASH)) {
069 contextName = contextName.substring(1);
070 }
071
072 beanLocator = PortletBeanLocatorUtil.getBeanLocator(contextName);
073 }
074
075 JSONWebServiceRegistrator jsonWebServiceRegitrator =
076 new JSONWebServiceRegistrator();
077
078 jsonWebServiceRegitrator.processAllBeans(_contextPath, beanLocator);
079
080 if (_log.isInfoEnabled()) {
081 int count =
082 JSONWebServiceActionsManagerUtil.getJSONWebServiceActionsCount(
083 _contextPath);
084
085 _log.info("Configured " + count + " actions for " + _contextPath);
086 }
087 }
088
089 public void destroy() {
090 JSONWebServiceActionsManagerUtil.unregisterJSONWebServiceActions(
091 _contextPath);
092 }
093
094 @Override
095 public String getJSON(
096 ActionMapping actionMapping, ActionForm actionForm,
097 HttpServletRequest request, HttpServletResponse response)
098 throws Exception {
099
100 UploadException uploadException = (UploadException)request.getAttribute(
101 WebKeys.UPLOAD_EXCEPTION);
102
103 if (uploadException != null) {
104 return JSONFactoryUtil.serializeException(uploadException);
105 }
106
107 JSONWebServiceAction jsonWebServiceAction = null;
108
109 try {
110 jsonWebServiceAction = getJSONWebServiceAction(request);
111
112 Object returnObj = jsonWebServiceAction.invoke();
113
114 if (returnObj != null) {
115 return getReturnValue(returnObj);
116 }
117 else {
118 return JSONFactoryUtil.getNullJSON();
119 }
120 }
121 catch (InvocationTargetException ite) {
122 Throwable throwable = ite.getCause();
123
124 if (throwable instanceof SecurityException) {
125 throw (SecurityException)throwable;
126 }
127
128 _log.error(throwable, throwable);
129
130 return JSONFactoryUtil.serializeThrowable(throwable);
131 }
132 catch (Exception e) {
133 if (_log.isWarnEnabled()) {
134 _log.warn(e, e);
135 }
136
137 return JSONFactoryUtil.serializeException(e);
138 }
139 }
140
141 protected JSONWebServiceAction getJSONWebServiceAction(
142 HttpServletRequest request) {
143
144 String path = GetterUtil.getString(request.getPathInfo());
145
146 if (path.equals("/invoke")) {
147 return new JSONWebServiceInvokerAction(request);
148 }
149
150 if (request.getParameter("discover") != null) {
151 return new JSONWebServiceDiscoverAction(request);
152 }
153
154 return JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
155 request);
156 }
157
158 @Override
159 protected String getReroutePath() {
160 return _REROUTE_PATH;
161 }
162
163 private static final String _REROUTE_PATH = "/jsonws";
164
165 private static Log _log = LogFactoryUtil.getLog(
166 JSONWebServiceServiceAction.class);
167
168 private String _contextPath;
169
170 }