001
014
015 package com.liferay.portal.jsonwebservice;
016
017 import com.liferay.portal.events.ServicePreAction;
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.upload.UploadServletRequest;
022 import com.liferay.portal.kernel.util.ContextPathUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.HttpUtil;
025 import com.liferay.portal.kernel.util.InstancePool;
026 import com.liferay.portal.kernel.util.LocaleThreadLocal;
027 import com.liferay.portal.kernel.util.StreamUtil;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.security.ac.AccessControlThreadLocal;
031 import com.liferay.portal.servlet.JSONServlet;
032 import com.liferay.portal.struts.JSONAction;
033 import com.liferay.portal.upload.UploadServletRequestImpl;
034 import com.liferay.portal.util.PortalUtil;
035 import com.liferay.portal.util.PropsValues;
036
037 import java.io.IOException;
038 import java.io.InputStream;
039 import java.io.OutputStream;
040
041 import java.net.URL;
042
043 import java.util.Locale;
044
045 import javax.servlet.RequestDispatcher;
046 import javax.servlet.ServletContext;
047 import javax.servlet.ServletException;
048 import javax.servlet.http.HttpServletRequest;
049 import javax.servlet.http.HttpServletResponse;
050 import javax.servlet.http.HttpSession;
051
052
055 public class JSONWebServiceServlet extends JSONServlet {
056
057 @Override
058 public void destroy() {
059 _jsonWebServiceServiceAction.destroy();
060
061 super.destroy();
062 }
063
064 @Override
065 public void service(
066 HttpServletRequest request, HttpServletResponse response)
067 throws IOException, ServletException {
068
069 if (PortalUtil.isMultipartRequest(request)) {
070 UploadServletRequest uploadServletRequest =
071 new UploadServletRequestImpl(request);
072
073 request = uploadServletRequest;
074 }
075
076 String path = GetterUtil.getString(request.getPathInfo());
077
078 if ((!path.equals(StringPool.BLANK) &&
079 !path.equals(StringPool.SLASH)) ||
080 (request.getParameter("discover") != null)) {
081
082 try {
083 ServicePreAction servicePreAction =
084 (ServicePreAction)InstancePool.get(
085 ServicePreAction.class.getName());
086
087 Locale locale = servicePreAction.initLocale(
088 request, response, null);
089
090 LocaleThreadLocal.setThemeDisplayLocale(locale);
091 }
092 catch (Exception e) {
093 throw new ServletException(e);
094 }
095
096 super.service(request, response);
097
098 return;
099 }
100
101 if (_log.isDebugEnabled()) {
102 _log.debug("Servlet context " + request.getContextPath());
103 }
104
105 String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";
106
107 HttpSession session = request.getSession();
108
109 ServletContext servletContext = session.getServletContext();
110
111 boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
112
113 try {
114 AccessControlThreadLocal.setRemoteAccess(true);
115
116 String contextPath = PropsValues.PORTAL_CTX;
117
118 if (servletContext.getContext(contextPath) != null) {
119 if (!contextPath.equals(StringPool.SLASH) &&
120 apiPath.startsWith(contextPath)) {
121
122 apiPath = apiPath.substring(contextPath.length());
123 }
124
125 RequestDispatcher requestDispatcher =
126 request.getRequestDispatcher(apiPath);
127
128 requestDispatcher.forward(request, response);
129 }
130 else {
131 String requestURI = request.getRequestURI();
132 String requestURL = String.valueOf(request.getRequestURL());
133
134 String serverURL = requestURL.substring(
135 0, requestURL.length() - requestURI.length());
136
137 String queryString = request.getQueryString();
138
139 if (Validator.isNull(queryString)) {
140 queryString = StringPool.BLANK;
141 }
142 else {
143 queryString += StringPool.AMPERSAND;
144 }
145
146 String servletContextPath = ContextPathUtil.getContextPath(
147 servletContext);
148
149 queryString +=
150 "contextPath=" + HttpUtil.encodeURL(servletContextPath);
151
152 apiPath =
153 serverURL + apiPath + StringPool.QUESTION + queryString;
154
155 URL url = new URL(apiPath);
156
157 InputStream inputStream = null;
158
159 try {
160 inputStream = url.openStream();
161
162 OutputStream outputStream = response.getOutputStream();
163
164 StreamUtil.transfer(inputStream, outputStream);
165 }
166 finally {
167 StreamUtil.cleanUp(inputStream);
168
169 AccessControlThreadLocal.setRemoteAccess(remoteAccess);
170 }
171 }
172 }
173 finally {
174 AccessControlThreadLocal.setRemoteAccess(remoteAccess);
175 }
176 }
177
178 @Override
179 protected JSONAction getJSONAction(ServletContext servletContext) {
180 ClassLoader classLoader = (ClassLoader)servletContext.getAttribute(
181 PluginContextListener.PLUGIN_CLASS_LOADER);
182
183 _jsonWebServiceServiceAction = new JSONWebServiceServiceAction(
184 servletContext, classLoader);
185
186 _jsonWebServiceServiceAction.setServletContext(servletContext);
187
188 return _jsonWebServiceServiceAction;
189 }
190
191 private static Log _log = LogFactoryUtil.getLog(
192 JSONWebServiceServlet.class);
193
194 private JSONWebServiceServiceAction _jsonWebServiceServiceAction;
195
196 }