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.SLASH) && !path.equals(StringPool.BLANK)) {
079 try {
080 ServicePreAction servicePreAction =
081 (ServicePreAction)InstancePool.get(
082 ServicePreAction.class.getName());
083
084 Locale locale = servicePreAction.initLocale(
085 request, response, null);
086
087 LocaleThreadLocal.setThemeDisplayLocale(locale);
088 }
089 catch (Exception e) {
090 throw new ServletException(e);
091 }
092
093 super.service(request, response);
094
095 return;
096 }
097
098 if (_log.isDebugEnabled()) {
099 _log.debug("Servlet context " + request.getContextPath());
100 }
101
102 String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";
103
104 HttpSession session = request.getSession();
105
106 ServletContext servletContext = session.getServletContext();
107
108 boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
109
110 try {
111 AccessControlThreadLocal.setRemoteAccess(true);
112
113 String contextPath = PropsValues.PORTAL_CTX;
114
115 if (servletContext.getContext(contextPath) != null) {
116 if (!contextPath.equals(StringPool.SLASH) &&
117 apiPath.startsWith(contextPath)) {
118
119 apiPath = apiPath.substring(contextPath.length());
120 }
121
122 RequestDispatcher requestDispatcher =
123 request.getRequestDispatcher(apiPath);
124
125 requestDispatcher.forward(request, response);
126 }
127 else {
128 String requestURI = request.getRequestURI();
129 String requestURL = String.valueOf(request.getRequestURL());
130
131 String serverURL = requestURL.substring(
132 0, requestURL.length() - requestURI.length());
133
134 String queryString = request.getQueryString();
135
136 if (Validator.isNull(queryString)) {
137 queryString = StringPool.BLANK;
138 }
139 else {
140 queryString += StringPool.AMPERSAND;
141 }
142
143 String servletContextPath = ContextPathUtil.getContextPath(
144 servletContext);
145
146 queryString +=
147 "contextPath=" + HttpUtil.encodeURL(servletContextPath);
148
149 apiPath =
150 serverURL + apiPath + StringPool.QUESTION + queryString;
151
152 URL url = new URL(apiPath);
153
154 InputStream inputStream = null;
155
156 try {
157 inputStream = url.openStream();
158
159 OutputStream outputStream = response.getOutputStream();
160
161 StreamUtil.transfer(inputStream, outputStream);
162 }
163 finally {
164 StreamUtil.cleanUp(inputStream);
165
166 AccessControlThreadLocal.setRemoteAccess(remoteAccess);
167 }
168 }
169 }
170 finally {
171 AccessControlThreadLocal.setRemoteAccess(remoteAccess);
172 }
173 }
174
175 @Override
176 protected JSONAction getJSONAction(ServletContext servletContext) {
177 ClassLoader classLoader = (ClassLoader)servletContext.getAttribute(
178 PluginContextListener.PLUGIN_CLASS_LOADER);
179
180 _jsonWebServiceServiceAction = new JSONWebServiceServiceAction(
181 servletContext, classLoader);
182
183 _jsonWebServiceServiceAction.setServletContext(servletContext);
184
185 return _jsonWebServiceServiceAction;
186 }
187
188 private static Log _log = LogFactoryUtil.getLog(
189 JSONWebServiceServlet.class);
190
191 private JSONWebServiceServiceAction _jsonWebServiceServiceAction;
192
193 }