001
014
015 package com.liferay.portal.servlet;
016
017 import com.liferay.portal.action.JSONServiceAction;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.servlet.PluginContextListener;
021 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022 import com.liferay.portal.security.ac.AccessControlThreadLocal;
023 import com.liferay.portal.struts.JSONAction;
024 import com.liferay.portal.util.ClassLoaderUtil;
025
026 import java.io.IOException;
027
028 import javax.servlet.ServletConfig;
029 import javax.servlet.ServletContext;
030 import javax.servlet.ServletException;
031 import javax.servlet.http.HttpServlet;
032 import javax.servlet.http.HttpServletRequest;
033 import javax.servlet.http.HttpServletResponse;
034
035
038 public class JSONServlet extends HttpServlet {
039
040 @Override
041 public void init(ServletConfig servletConfig) {
042 ServletContext servletContext = servletConfig.getServletContext();
043
044 _pluginClassLoader = (ClassLoader)servletContext.getAttribute(
045 PluginContextListener.PLUGIN_CLASS_LOADER);
046
047 _jsonAction = getJSONAction(servletContext);
048 }
049
050 @Override
051 public void service(
052 HttpServletRequest request, HttpServletResponse response)
053 throws IOException, ServletException {
054
055 boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
056
057 try {
058 AccessControlThreadLocal.setRemoteAccess(true);
059
060 if (_pluginClassLoader == null) {
061 _jsonAction.execute(null, null, request, response);
062 }
063 else {
064 ClassLoader contextClassLoader =
065 ClassLoaderUtil.getContextClassLoader();
066
067 try {
068 ClassLoaderUtil.setContextClassLoader(_pluginClassLoader);
069
070 _jsonAction.execute(null, null, request, response);
071 }
072 finally {
073 ClassLoaderUtil.setContextClassLoader(contextClassLoader);
074 }
075 }
076 }
077 catch (IOException ioe) {
078 if (!ServletResponseUtil.isClientAbortException(ioe)) {
079 throw ioe;
080 }
081 }
082 catch (SecurityException se) {
083 throw new ServletException(se);
084 }
085 catch (Exception e) {
086 _log.error(e, e);
087 }
088 finally {
089 AccessControlThreadLocal.setRemoteAccess(remoteAccess);
090 }
091 }
092
093 protected JSONAction getJSONAction(ServletContext servletContext) {
094 JSONAction jsonAction = new JSONServiceAction();
095
096 jsonAction.setServletContext(servletContext);
097
098 return jsonAction;
099 }
100
101 private static Log _log = LogFactoryUtil.getLog(JSONServlet.class);
102
103 private JSONAction _jsonAction;
104 private ClassLoader _pluginClassLoader;
105
106 }