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.jsonwebservice.JSONWebServiceConfigurator;
024 import com.liferay.portal.kernel.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.upload.UploadException;
027 import com.liferay.portal.kernel.util.ContextPathUtil;
028 import com.liferay.portal.kernel.util.GetterUtil;
029 import com.liferay.portal.kernel.util.ServiceLoader;
030 import com.liferay.portal.util.ClassLoaderUtil;
031 import com.liferay.portal.util.PortalUtil;
032 import com.liferay.portal.util.PropsValues;
033 import com.liferay.portal.util.WebKeys;
034
035 import java.lang.reflect.InvocationTargetException;
036
037 import java.util.List;
038
039 import javax.servlet.ServletContext;
040 import javax.servlet.http.HttpServletRequest;
041 import javax.servlet.http.HttpServletResponse;
042
043 import org.apache.struts.action.ActionForm;
044 import org.apache.struts.action.ActionMapping;
045
046
050 public class JSONWebServiceServiceAction extends JSONServiceAction {
051
052 public JSONWebServiceServiceAction(
053 ServletContext servletContext, ClassLoader classLoader) {
054
055 String contextPath = ContextPathUtil.getContextPath(servletContext);
056
057 if ((classLoader == null) &&
058 contextPath.equals(PortalUtil.getPathContext())) {
059
060 classLoader = ClassLoaderUtil.getPortalClassLoader();
061 }
062
063 _jsonWebServiceConfigurator = getJSONWebServiceConfigurator(
064 classLoader);
065
066 _jsonWebServiceConfigurator.init(servletContext, classLoader);
067
068 _jsonWebServiceConfigurator.clean();
069
070 if (PropsValues.JSON_WEB_SERVICE_ENABLED) {
071 try {
072 _jsonWebServiceConfigurator.configure();
073 }
074 catch (Exception e) {
075 _log.error(e, e);
076 }
077 }
078 else {
079 if (_log.isInfoEnabled()) {
080 _log.info("JSON web service is disabled");
081 }
082 }
083 }
084
085 public void destroy() {
086 _jsonWebServiceConfigurator.clean();
087 }
088
089 @Override
090 public String getJSON(
091 ActionMapping actionMapping, ActionForm actionForm,
092 HttpServletRequest request, HttpServletResponse response)
093 throws Exception {
094
095 UploadException uploadException = (UploadException)request.getAttribute(
096 WebKeys.UPLOAD_EXCEPTION);
097
098 if (uploadException != null) {
099 return JSONFactoryUtil.serializeException(uploadException);
100 }
101
102 JSONWebServiceAction jsonWebServiceAction = null;
103
104 try {
105 jsonWebServiceAction = getJSONWebServiceAction(request);
106
107 Object returnObj = jsonWebServiceAction.invoke();
108
109 if (returnObj != null) {
110 return getReturnValue(returnObj);
111 }
112 else {
113 return JSONFactoryUtil.getNullJSON();
114 }
115 }
116 catch (InvocationTargetException ite) {
117 Throwable throwable = ite.getCause();
118
119 if (throwable instanceof SecurityException) {
120 throw (SecurityException)throwable;
121 }
122
123 _log.error(throwable, throwable);
124
125 return JSONFactoryUtil.serializeThrowable(throwable);
126 }
127 catch (Exception e) {
128 _log.error(e, e);
129
130 return JSONFactoryUtil.serializeException(e);
131 }
132 }
133
134 protected JSONWebServiceAction getJSONWebServiceAction(
135 HttpServletRequest request) {
136
137 String path = GetterUtil.getString(request.getPathInfo());
138
139 if (path.equals("/invoke")) {
140 return new JSONWebServiceInvokerAction(request);
141 }
142
143 if (request.getParameter("discover") != null) {
144 return new JSONWebServiceDiscoverAction(request);
145 }
146
147 return JSONWebServiceActionsManagerUtil.getJSONWebServiceAction(
148 request);
149 }
150
151 protected JSONWebServiceConfigurator getJSONWebServiceConfigurator(
152 ClassLoader classLoader) {
153
154 JSONWebServiceConfigurator jsonWebServiceConfigurator = null;
155
156 try {
157 List<JSONWebServiceConfigurator> jsonWebServiceConfigurators =
158 ServiceLoader.load(
159 classLoader, JSONWebServiceConfigurator.class);
160
161 if (!jsonWebServiceConfigurators.isEmpty()) {
162 jsonWebServiceConfigurator = jsonWebServiceConfigurators.get(0);
163 }
164 }
165 catch (Exception e) {
166 _log.error(e, e);
167 }
168
169 if (jsonWebServiceConfigurator == null) {
170 jsonWebServiceConfigurator = new JSONWebServiceConfiguratorImpl();
171 }
172
173 return jsonWebServiceConfigurator;
174 }
175
176 @Override
177 protected String getReroutePath() {
178 return _REROUTE_PATH;
179 }
180
181 private static final String _REROUTE_PATH = "/jsonws";
182
183 private static Log _log = LogFactoryUtil.getLog(
184 JSONWebServiceServiceAction.class);
185
186 private JSONWebServiceConfigurator _jsonWebServiceConfigurator;
187
188 }