001
014
015 package com.liferay.portal.jsonwebservice;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.servlet.PluginContextListener;
020 import com.liferay.portal.kernel.upload.UploadServletRequest;
021 import com.liferay.portal.kernel.util.ContextPathUtil;
022 import com.liferay.portal.kernel.util.GetterUtil;
023 import com.liferay.portal.kernel.util.HttpUtil;
024 import com.liferay.portal.kernel.util.LocaleThreadLocal;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.security.ac.AccessControlThreadLocal;
027 import com.liferay.portal.servlet.JSONServlet;
028 import com.liferay.portal.spring.context.PortalContextLoaderListener;
029 import com.liferay.portal.struts.JSONAction;
030 import com.liferay.portal.upload.UploadServletRequestImpl;
031 import com.liferay.portal.util.PortalUtil;
032
033 import java.io.IOException;
034
035 import java.util.Locale;
036
037 import javax.servlet.RequestDispatcher;
038 import javax.servlet.ServletContext;
039 import javax.servlet.ServletException;
040 import javax.servlet.http.HttpServletRequest;
041 import javax.servlet.http.HttpServletResponse;
042 import javax.servlet.http.HttpSession;
043
044
047 public class JSONWebServiceServlet extends JSONServlet {
048
049 @Override
050 public void destroy() {
051 _jsonWebServiceServiceAction.destroy();
052
053 super.destroy();
054 }
055
056 @Override
057 public void service(
058 HttpServletRequest request, HttpServletResponse response)
059 throws IOException, ServletException {
060
061 if (PortalUtil.isMultipartRequest(request)) {
062 UploadServletRequest uploadServletRequest =
063 new UploadServletRequestImpl(request);
064
065 request = uploadServletRequest;
066 }
067
068 String path = GetterUtil.getString(request.getPathInfo());
069
070 if ((!path.equals(StringPool.BLANK) &&
071 !path.equals(StringPool.SLASH)) ||
072 (request.getParameter("discover") != null)) {
073
074 Locale locale = PortalUtil.getLocale(request, response, true);
075
076 LocaleThreadLocal.setThemeDisplayLocale(locale);
077
078 super.service(request, response);
079
080 return;
081 }
082
083 if (_log.isDebugEnabled()) {
084 _log.debug("Servlet context " + request.getContextPath());
085 }
086
087 String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";
088
089 HttpSession session = request.getSession();
090
091 ServletContext servletContext = session.getServletContext();
092
093 boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
094
095 try {
096 AccessControlThreadLocal.setRemoteAccess(true);
097
098 String contextPath =
099 PortalContextLoaderListener.getPortalServletContextPath();
100
101 if (contextPath.isEmpty()) {
102 contextPath = StringPool.SLASH;
103 }
104
105 if (servletContext.getContext(contextPath) != null) {
106 if (!contextPath.equals(StringPool.SLASH) &&
107 apiPath.startsWith(contextPath)) {
108
109 apiPath = apiPath.substring(contextPath.length());
110 }
111
112 RequestDispatcher requestDispatcher =
113 request.getRequestDispatcher(apiPath);
114
115 requestDispatcher.forward(request, response);
116 }
117 else {
118 String servletContextPath = ContextPathUtil.getContextPath(
119 servletContext);
120
121 String redirectPath =
122 "/api/jsonws?contextPath=" +
123 HttpUtil.encodeURL(servletContextPath);
124
125 response.sendRedirect(redirectPath);
126 }
127 }
128 finally {
129 AccessControlThreadLocal.setRemoteAccess(remoteAccess);
130 }
131 }
132
133 @Override
134 protected JSONAction getJSONAction(ServletContext servletContext) {
135 ClassLoader classLoader = (ClassLoader)servletContext.getAttribute(
136 PluginContextListener.PLUGIN_CLASS_LOADER);
137
138 _jsonWebServiceServiceAction = new JSONWebServiceServiceAction(
139 servletContext, classLoader);
140
141 _jsonWebServiceServiceAction.setServletContext(servletContext);
142
143 return _jsonWebServiceServiceAction;
144 }
145
146 private static Log _log = LogFactoryUtil.getLog(
147 JSONWebServiceServlet.class);
148
149 private JSONWebServiceServiceAction _jsonWebServiceServiceAction;
150
151 }