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.servlet;
016    
017    import com.liferay.portal.kernel.servlet.PersistentHttpServletRequestWrapper;
018    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
019    
020    import java.io.Closeable;
021    
022    import java.util.ArrayList;
023    import java.util.Collections;
024    import java.util.Enumeration;
025    import java.util.List;
026    import java.util.Locale;
027    
028    import javax.servlet.RequestDispatcher;
029    import javax.servlet.ServletRequest;
030    import javax.servlet.ServletRequestWrapper;
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpSession;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class ThreadLocalFacadeHttpServletRequestWrapper
038            extends PersistentHttpServletRequestWrapper implements Closeable {
039    
040            public ThreadLocalFacadeHttpServletRequestWrapper(
041                    ServletRequestWrapper servletRequestWrapper,
042                    HttpServletRequest httpServletRequest) {
043    
044                    super(httpServletRequest);
045    
046                    _servletRequestWrapper = servletRequestWrapper;
047    
048                    _nextHttpServletRequestThreadLocal.set(httpServletRequest);
049    
050                    _locales = new ArrayList<Locale>();
051    
052                    Enumeration<Locale> enumeration = httpServletRequest.getLocales();
053    
054                    while (enumeration.hasMoreElements()) {
055                            _locales.add(enumeration.nextElement());
056                    }
057            }
058    
059            @Override
060            public void close() {
061                    if (_servletRequestWrapper != null) {
062                            HttpServletRequest nextHttpServletRequest =
063                                    _nextHttpServletRequestThreadLocal.get();
064    
065                            _servletRequestWrapper.setRequest(nextHttpServletRequest);
066                    }
067            }
068    
069            @Override
070            public Object getAttribute(String name) {
071                    ServletRequest servletRequest = getRequest();
072    
073                    return servletRequest.getAttribute(name);
074            }
075    
076            @Override
077            public Enumeration<String> getAttributeNames() {
078                    ServletRequest servletRequest = getRequest();
079    
080                    return servletRequest.getAttributeNames();
081            }
082    
083            @Override
084            public Enumeration<Locale> getLocales() {
085                    return Collections.enumeration(_locales);
086            }
087    
088            @Override
089            public ServletRequest getRequest() {
090                    return _nextHttpServletRequestThreadLocal.get();
091            }
092    
093            @Override
094            public RequestDispatcher getRequestDispatcher(String uri) {
095                    HttpServletRequest httpServletRequest =
096                            _nextHttpServletRequestThreadLocal.get();
097    
098                    return httpServletRequest.getRequestDispatcher(uri);
099            }
100    
101            @Override
102            public HttpSession getSession() {
103                    return getSession(true);
104            }
105    
106            @Override
107            public HttpSession getSession(boolean create) {
108                    HttpServletRequest httpServletRequest =
109                            (HttpServletRequest)getRequest();
110    
111                    return httpServletRequest.getSession(create);
112            }
113    
114            @Override
115            public void removeAttribute(String name) {
116                    ServletRequest servletRequest = getRequest();
117    
118                    servletRequest.removeAttribute(name);
119            }
120    
121            @Override
122            public void setAttribute(String name, Object o) {
123                    ServletRequest servletRequest = getRequest();
124    
125                    servletRequest.setAttribute(name, o);
126            }
127    
128            @Override
129            public void setRequest(ServletRequest servletRequest) {
130                    _nextHttpServletRequestThreadLocal.set(
131                            (HttpServletRequest)servletRequest);
132            }
133    
134            private static ThreadLocal<HttpServletRequest>
135                    _nextHttpServletRequestThreadLocal =
136                            new AutoResetThreadLocal<HttpServletRequest>(
137                                    ThreadLocalFacadeHttpServletRequestWrapper.class +
138                                            "._nextHttpServletRequestThreadLocal") {
139    
140                                    @Override
141                                    protected HttpServletRequest copy(
142                                            HttpServletRequest httpServletRequest) {
143    
144                                            return httpServletRequest;
145                                    }
146    
147                            };
148    
149            private List<Locale> _locales;
150            private ServletRequestWrapper _servletRequestWrapper;
151    
152    }