001    /**
002     * Copyright (c) 2000-2013 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    
033    import java.io.IOException;
034    
035    import java.util.Locale;
036    
037    import javax.servlet.RequestDispatcher;
038    import javax.servlet.ServletContext;
039    import javax.servlet.ServletException;
040    import javax.servlet.http.HttpServletRequest;
041    import javax.servlet.http.HttpServletResponse;
042    import javax.servlet.http.HttpSession;
043    
044    /**
045     * @author Igor Spasic
046     */
047    public class JSONWebServiceServlet extends JSONServlet {
048    
049            @Override
050            public void service(
051                            HttpServletRequest request, HttpServletResponse response)
052                    throws IOException, ServletException {
053    
054                    if (PortalUtil.isMultipartRequest(request)) {
055                            UploadServletRequest uploadServletRequest =
056                                    new UploadServletRequestImpl(request);
057    
058                            request = uploadServletRequest;
059                    }
060    
061                    String path = GetterUtil.getString(request.getPathInfo());
062    
063                    if ((!path.equals(StringPool.BLANK) &&
064                             !path.equals(StringPool.SLASH)) ||
065                            (request.getParameter("discover") != null)) {
066    
067                            Locale locale = PortalUtil.getLocale(request, response, true);
068    
069                            LocaleThreadLocal.setThemeDisplayLocale(locale);
070    
071                            super.service(request, response);
072    
073                            return;
074                    }
075    
076                    if (_log.isDebugEnabled()) {
077                            _log.debug("Servlet context " + request.getContextPath());
078                    }
079    
080                    String apiPath = PortalUtil.getPathMain() + "/portal/api/jsonws";
081    
082                    HttpSession session = request.getSession();
083    
084                    ServletContext servletContext = session.getServletContext();
085    
086                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
087    
088                    try {
089                            AccessControlThreadLocal.setRemoteAccess(true);
090    
091                            String contextPath =
092                                    PortalContextLoaderListener.getPortalServletContextPath();
093    
094                            if (contextPath.isEmpty()) {
095                                    contextPath = StringPool.SLASH;
096                            }
097    
098                            String proxyPath = PortalUtil.getPathProxy();
099    
100                            if (servletContext.getContext(contextPath) != null) {
101                                    if (Validator.isNotNull(proxyPath) &&
102                                            apiPath.startsWith(proxyPath)) {
103    
104                                            apiPath = apiPath.substring(proxyPath.length());
105                                    }
106    
107                                    if (!contextPath.equals(StringPool.SLASH) &&
108                                            apiPath.startsWith(contextPath)) {
109    
110                                            apiPath = apiPath.substring(contextPath.length());
111                                    }
112    
113                                    RequestDispatcher requestDispatcher =
114                                            request.getRequestDispatcher(apiPath);
115    
116                                    requestDispatcher.forward(request, response);
117                            }
118                            else {
119                                    String servletContextPath = ContextPathUtil.getContextPath(
120                                            servletContext);
121    
122                                    String redirectPath =
123                                            PortalUtil.getPathContext() + "/api/jsonws?contextPath=" +
124                                                    HttpUtil.encodeURL(servletContextPath);
125    
126                                    response.sendRedirect(redirectPath);
127                            }
128                    }
129                    finally {
130                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
131                    }
132            }
133    
134            @Override
135            protected JSONAction getJSONAction(ServletContext servletContext) {
136                    JSONWebServiceServiceAction jsonWebServiceServiceAction =
137                            new JSONWebServiceServiceAction();
138    
139                    jsonWebServiceServiceAction.setServletContext(servletContext);
140    
141                    return jsonWebServiceServiceAction;
142            }
143    
144            private static Log _log = LogFactoryUtil.getLog(
145                    JSONWebServiceServlet.class);
146    
147    }