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