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.util.ContextPathUtil;
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.security.ac.AccessControlThreadLocal;
026 import com.liferay.portal.servlet.JSONServlet;
027 import com.liferay.portal.spring.context.PortalContextLoaderListener;
028 import com.liferay.portal.struts.JSONAction;
029 import com.liferay.portal.util.PortalUtil;
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 ((!path.equals(StringPool.BLANK) &&
055 !path.equals(StringPool.SLASH)) ||
056 (request.getParameter("discover") != null)) {
057
058 Locale locale = PortalUtil.getLocale(request, response, true);
059
060 LocaleThreadLocal.setThemeDisplayLocale(locale);
061
062 super.service(request, response);
063
064 return;
065 }
066
067 if (_log.isDebugEnabled()) {
068 _log.debug("Servlet context " + request.getContextPath());
069 }
070
071 String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";
072
073 HttpSession session = request.getSession();
074
075 ServletContext servletContext = session.getServletContext();
076
077 boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
078
079 try {
080 AccessControlThreadLocal.setRemoteAccess(true);
081
082 String contextPath =
083 PortalContextLoaderListener.getPortalServletContextPath();
084
085 if (contextPath.isEmpty()) {
086 contextPath = StringPool.SLASH;
087 }
088
089 String proxyPath = PortalUtil.getPathProxy();
090
091 if (servletContext.getContext(contextPath) != null) {
092 if (Validator.isNotNull(proxyPath) &&
093 apiPath.startsWith(proxyPath)) {
094
095 apiPath = apiPath.substring(proxyPath.length());
096 }
097
098 if (!contextPath.equals(StringPool.SLASH) &&
099 apiPath.startsWith(contextPath)) {
100
101 apiPath = apiPath.substring(contextPath.length());
102 }
103
104 RequestDispatcher requestDispatcher =
105 request.getRequestDispatcher(apiPath);
106
107 requestDispatcher.forward(request, response);
108 }
109 else {
110 String servletContextPath = ContextPathUtil.getContextPath(
111 servletContext);
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 Log _log = LogFactoryUtil.getLog(
136 JSONWebServiceServlet.class);
137
138 }