001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.upload.UploadServletRequest;
020    import com.liferay.portal.kernel.util.ContextPathUtil;
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.security.ac.AccessControlThreadLocal;
027    import com.liferay.portal.servlet.JSONServlet;
028    import com.liferay.portal.spring.context.PortalContextLoaderListener;
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    
036    import java.util.Locale;
037    
038    import javax.servlet.RequestDispatcher;
039    import javax.servlet.ServletContext;
040    import javax.servlet.ServletException;
041    import javax.servlet.http.HttpServletRequest;
042    import javax.servlet.http.HttpServletResponse;
043    import javax.servlet.http.HttpSession;
044    
045    /**
046     * @author Igor Spasic
047     */
048    public class JSONWebServiceServlet extends JSONServlet {
049    
050            @Override
051            public void service(
052                            HttpServletRequest request, HttpServletResponse response)
053                    throws IOException, ServletException {
054    
055                    if (PortalUtil.isMultipartRequest(request)) {
056                            UploadServletRequest uploadServletRequest =
057                                    new UploadServletRequestImpl(request);
058    
059                            request = uploadServletRequest;
060                    }
061    
062                    String path = GetterUtil.getString(request.getPathInfo());
063    
064                    if (!PropsValues.JSONWS_WEB_SERVICE_API_DISCOVERABLE ||
065                            (!path.equals(StringPool.BLANK) &&
066                             !path.equals(StringPool.SLASH)) ||
067                            (request.getParameter("discover") != null)) {
068    
069                            Locale locale = PortalUtil.getLocale(request, response, true);
070    
071                            LocaleThreadLocal.setThemeDisplayLocale(locale);
072    
073                            super.service(request, response);
074    
075                            return;
076                    }
077    
078                    if (_log.isDebugEnabled()) {
079                            _log.debug("Servlet context " + request.getContextPath());
080                    }
081    
082                    String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";
083    
084                    HttpSession session = request.getSession();
085    
086                    ServletContext servletContext = session.getServletContext();
087    
088                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
089    
090                    try {
091                            AccessControlThreadLocal.setRemoteAccess(true);
092    
093                            String contextPath =
094                                    PortalContextLoaderListener.getPortalServletContextPath();
095    
096                            if (contextPath.isEmpty()) {
097                                    contextPath = StringPool.SLASH;
098                            }
099    
100                            String proxyPath = PortalUtil.getPathProxy();
101    
102                            if (servletContext.getContext(contextPath) != null) {
103                                    if (Validator.isNotNull(proxyPath) &&
104                                            apiPath.startsWith(proxyPath)) {
105    
106                                            apiPath = apiPath.substring(proxyPath.length());
107                                    }
108    
109                                    if (!contextPath.equals(StringPool.SLASH) &&
110                                            apiPath.startsWith(contextPath)) {
111    
112                                            apiPath = apiPath.substring(contextPath.length());
113                                    }
114    
115                                    RequestDispatcher requestDispatcher =
116                                            request.getRequestDispatcher(apiPath);
117    
118                                    requestDispatcher.forward(request, response);
119                            }
120                            else {
121                                    String servletContextPath = ContextPathUtil.getContextPath(
122                                            servletContext);
123    
124                                    String redirectPath =
125                                            PortalUtil.getPathContext() + "/api/jsonws?contextPath=" +
126                                                    HttpUtil.encodeURL(servletContextPath);
127    
128                                    response.sendRedirect(redirectPath);
129                            }
130                    }
131                    finally {
132                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
133                    }
134            }
135    
136            @Override
137            protected JSONAction getJSONAction(ServletContext servletContext) {
138                    JSONWebServiceServiceAction jsonWebServiceServiceAction =
139                            new JSONWebServiceServiceAction();
140    
141                    jsonWebServiceServiceAction.setServletContext(servletContext);
142    
143                    return jsonWebServiceServiceAction;
144            }
145    
146            private static final Log _log = LogFactoryUtil.getLog(
147                    JSONWebServiceServlet.class);
148    
149    }