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