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.security.access.control.AccessControlThreadLocal;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.HttpUtil;
022 import com.liferay.portal.kernel.util.LocaleThreadLocal;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.servlet.JSONServlet;
026 import com.liferay.portal.spring.context.PortalContextLoaderListener;
027 import com.liferay.portal.struts.JSONAction;
028 import com.liferay.portal.util.PortalUtil;
029 import com.liferay.portal.util.PropsValues;
030
031 import java.io.IOException;
032
033 import java.util.Locale;
034
035 import javax.servlet.RequestDispatcher;
036 import javax.servlet.ServletContext;
037 import javax.servlet.ServletException;
038 import javax.servlet.http.HttpServletRequest;
039 import javax.servlet.http.HttpServletResponse;
040 import javax.servlet.http.HttpSession;
041
042
045 public class JSONWebServiceServlet extends JSONServlet {
046
047 @Override
048 public void service(
049 HttpServletRequest request, HttpServletResponse response)
050 throws IOException, ServletException {
051
052 String path = GetterUtil.getString(request.getPathInfo());
053
054 if (!PropsValues.JSONWS_WEB_SERVICE_API_DISCOVERABLE ||
055 (!path.equals(StringPool.BLANK) &&
056 !path.equals(StringPool.SLASH)) ||
057 (request.getParameter("discover") != null)) {
058
059 Locale locale = PortalUtil.getLocale(request, response, true);
060
061 LocaleThreadLocal.setThemeDisplayLocale(locale);
062
063 super.service(request, response);
064
065 return;
066 }
067
068 if (_log.isDebugEnabled()) {
069 _log.debug("Servlet context " + request.getContextPath());
070 }
071
072 String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";
073
074 HttpSession session = request.getSession();
075
076 ServletContext servletContext = session.getServletContext();
077
078 boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
079
080 try {
081 AccessControlThreadLocal.setRemoteAccess(true);
082
083 String contextPath =
084 PortalContextLoaderListener.getPortalServletContextPath();
085
086 if (contextPath.isEmpty()) {
087 contextPath = StringPool.SLASH;
088 }
089
090 String proxyPath = PortalUtil.getPathProxy();
091
092 if (servletContext.getContext(contextPath) != null) {
093 if (Validator.isNotNull(proxyPath) &&
094 apiPath.startsWith(proxyPath)) {
095
096 apiPath = apiPath.substring(proxyPath.length());
097 }
098
099 if (!contextPath.equals(StringPool.SLASH) &&
100 apiPath.startsWith(contextPath)) {
101
102 apiPath = apiPath.substring(contextPath.length());
103 }
104
105 RequestDispatcher requestDispatcher =
106 request.getRequestDispatcher(apiPath);
107
108 requestDispatcher.forward(request, response);
109 }
110 else {
111 String servletContextPath = servletContext.getContextPath();
112
113 String redirectPath =
114 PortalUtil.getPathContext() + "/api/jsonws?contextPath=" +
115 HttpUtil.encodeURL(servletContextPath);
116
117 response.sendRedirect(redirectPath);
118 }
119 }
120 finally {
121 AccessControlThreadLocal.setRemoteAccess(remoteAccess);
122 }
123 }
124
125 @Override
126 protected JSONAction getJSONAction(ServletContext servletContext) {
127 JSONWebServiceServiceAction jsonWebServiceServiceAction =
128 new JSONWebServiceServiceAction();
129
130 jsonWebServiceServiceAction.setServletContext(servletContext);
131
132 return jsonWebServiceServiceAction;
133 }
134
135 private static final Log _log = LogFactoryUtil.getLog(
136 JSONWebServiceServlet.class);
137
138 }