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.Portal;
023    import com.liferay.portal.kernel.util.PortalUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.servlet.JSONServlet;
026    import com.liferay.portal.spring.context.PortalContextLoaderListener;
027    import com.liferay.portal.struts.JSONAction;
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    
040    /**
041     * @author Igor Spasic
042     */
043    public class JSONWebServiceServlet extends JSONServlet {
044    
045            @Override
046            public void service(
047                            HttpServletRequest request, HttpServletResponse response)
048                    throws IOException, ServletException {
049    
050                    String path = GetterUtil.getString(request.getPathInfo());
051    
052                    if (!PropsValues.JSONWS_WEB_SERVICE_API_DISCOVERABLE ||
053                            (!path.equals(StringPool.BLANK) &&
054                             !path.equals(StringPool.SLASH)) ||
055                            (request.getParameter("discover") != null)) {
056    
057                            Locale locale = PortalUtil.getLocale(request, response, true);
058    
059                            LocaleThreadLocal.setThemeDisplayLocale(locale);
060    
061                            super.service(request, response);
062    
063                            return;
064                    }
065    
066                    if (_log.isDebugEnabled()) {
067                            _log.debug("Servlet context " + request.getContextPath());
068                    }
069    
070                    String portalContextPath =
071                            PortalContextLoaderListener.getPortalServletContextPath();
072    
073                    String requestContextPath = request.getContextPath();
074    
075                    if (requestContextPath.equals(portalContextPath)) {
076                            RequestDispatcher requestDispatcher = request.getRequestDispatcher(
077                                    Portal.PATH_MAIN + "/portal/api/jsonws");
078    
079                            requestDispatcher.forward(request, response);
080                    }
081                    else {
082                            ServletContext servletContext = getServletContext();
083    
084                            String redirectPath =
085                                    PortalUtil.getPathContext() + "/api/jsonws?contextName=" +
086                                            HttpUtil.encodeURL(servletContext.getServletContextName());
087    
088                            response.sendRedirect(redirectPath);
089                    }
090            }
091    
092            @Override
093            protected JSONAction getJSONAction(ServletContext servletContext) {
094                    JSONWebServiceServiceAction jsonWebServiceServiceAction =
095                            new JSONWebServiceServiceAction();
096    
097                    jsonWebServiceServiceAction.setServletContext(servletContext);
098    
099                    return jsonWebServiceServiceAction;
100            }
101    
102            private static final Log _log = LogFactoryUtil.getLog(
103                    JSONWebServiceServlet.class);
104    
105    }