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.spring.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.security.ac.AccessControlThreadLocal;
020    import com.liferay.portal.spring.context.TunnelApplicationContext;
021    
022    import javax.servlet.ServletException;
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    import org.springframework.web.servlet.DispatcherServlet;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class RemotingServlet extends DispatcherServlet {
032    
033            public static final String CONTEXT_CLASS =
034                    TunnelApplicationContext.class.getName();
035    
036            public static final String CONTEXT_CONFIG_LOCATION =
037                    "/WEB-INF/remoting-servlet.xml,/WEB-INF/remoting-servlet-ext.xml";
038    
039            @Override
040            public Class<?> getContextClass() {
041                    try {
042                            return Class.forName(CONTEXT_CLASS);
043                    }
044                    catch (Exception e) {
045                            _log.error(e, e);
046                    }
047    
048                    return null;
049            }
050    
051            @Override
052            public String getContextConfigLocation() {
053                    return CONTEXT_CONFIG_LOCATION;
054            }
055    
056            @Override
057            public void service(
058                            HttpServletRequest request, HttpServletResponse response)
059                    throws ServletException {
060    
061                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
062    
063                    try {
064                            AccessControlThreadLocal.setRemoteAccess(true);
065    
066                            super.service(request, response);
067                    }
068                    catch (Exception e) {
069                            throw new ServletException(e);
070                    }
071                    finally {
072                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
073                    }
074            }
075    
076            private static Log _log = LogFactoryUtil.getLog(RemotingServlet.class);
077    
078    }