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