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.util.GetterUtil;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.LocaleThreadLocal;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.servlet.JSONServlet;
025    import com.liferay.portal.spring.context.PortalContextLoaderListener;
026    import com.liferay.portal.struts.JSONAction;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PropsValues;
029    
030    import java.io.IOException;
031    
032    import java.util.Locale;
033    
034    import javax.servlet.RequestDispatcher;
035    import javax.servlet.ServletContext;
036    import javax.servlet.ServletException;
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    import javax.servlet.http.HttpSession;
040    
041    /**
042     * @author Igor Spasic
043     */
044    public class JSONWebServiceServlet extends JSONServlet {
045    
046            @Override
047            public void service(
048                            HttpServletRequest request, HttpServletResponse response)
049                    throws IOException, ServletException {
050    
051                    String path = GetterUtil.getString(request.getPathInfo());
052    
053                    if (!PropsValues.JSONWS_WEB_SERVICE_API_DISCOVERABLE ||
054                            (!path.equals(StringPool.BLANK) &&
055                             !path.equals(StringPool.SLASH)) ||
056                            (request.getParameter("discover") != null)) {
057    
058                            Locale locale = PortalUtil.getLocale(request, response, true);
059    
060                            LocaleThreadLocal.setThemeDisplayLocale(locale);
061    
062                            super.service(request, response);
063    
064                            return;
065                    }
066    
067                    if (_log.isDebugEnabled()) {
068                            _log.debug("Servlet context " + request.getContextPath());
069                    }
070    
071                    String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";
072    
073                    HttpSession session = request.getSession();
074    
075                    ServletContext servletContext = session.getServletContext();
076    
077                    String contextPath =
078                            PortalContextLoaderListener.getPortalServletContextPath();
079    
080                    if (contextPath.isEmpty()) {
081                            contextPath = StringPool.SLASH;
082                    }
083    
084                    String proxyPath = PortalUtil.getPathProxy();
085    
086                    if (servletContext.getContext(contextPath) != null) {
087                            if (Validator.isNotNull(proxyPath) &&
088                                    apiPath.startsWith(proxyPath)) {
089    
090                                    apiPath = apiPath.substring(proxyPath.length());
091                            }
092    
093                            if (!contextPath.equals(StringPool.SLASH) &&
094                                    apiPath.startsWith(contextPath)) {
095    
096                                    apiPath = apiPath.substring(contextPath.length());
097                            }
098    
099                            RequestDispatcher requestDispatcher = request.getRequestDispatcher(
100                                    apiPath);
101    
102                            requestDispatcher.forward(request, response);
103                    }
104                    else {
105                            String servletContextPath = servletContext.getContextPath();
106    
107                            String redirectPath =
108                                    PortalUtil.getPathContext() + "/api/jsonws?contextPath=" +
109                                            HttpUtil.encodeURL(servletContextPath);
110    
111                            response.sendRedirect(redirectPath);
112                    }
113            }
114    
115            @Override
116            protected JSONAction getJSONAction(ServletContext servletContext) {
117                    JSONWebServiceServiceAction jsonWebServiceServiceAction =
118                            new JSONWebServiceServiceAction();
119    
120                    jsonWebServiceServiceAction.setServletContext(servletContext);
121    
122                    return jsonWebServiceServiceAction;
123            }
124    
125            private static final Log _log = LogFactoryUtil.getLog(
126                    JSONWebServiceServlet.class);
127    
128    }