001    /* Copyright 2006-2009 the original author or authors.
002     *
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *      http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software
010     * distributed under the License is distributed on an "AS IS" BASIS,
011     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     * See the License for the specific language governing permissions and
013     * limitations under the License.
014     */
015    package org.codehaus.groovy.grails.plugins.springsecurity;
016    
017    import javax.servlet.http.HttpServletRequest;
018    import javax.servlet.http.HttpServletResponse;
019    
020    /**
021     * Uses a {@link ThreadLocal} to store the current request and response.
022     *
023     * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a>
024     */
025    public final class SecurityRequestHolder {
026    
027            private static final ThreadLocal<HttpServletRequest> REQUEST_HOLDER = new ThreadLocal<HttpServletRequest>();
028            private static final ThreadLocal<HttpServletResponse> RESPONSE_HOLDER = new ThreadLocal<HttpServletResponse>();
029    
030            private SecurityRequestHolder() {
031                    // static only
032            }
033    
034            /**
035             * Clear the saved request.
036             */
037            public static void reset() {
038                    REQUEST_HOLDER.set(null);
039                    RESPONSE_HOLDER.set(null);
040            }
041    
042            /**
043             * Set the current request and response.
044             * @param request  the request
045             * @param response  the response
046             */
047            public static void set(final HttpServletRequest request, final HttpServletResponse response) {
048                    REQUEST_HOLDER.set(request);
049                    RESPONSE_HOLDER.set(response);
050            }
051    
052            /**
053             * Get the current request.
054             * @return  the request
055             */
056            public static HttpServletRequest getRequest() {
057                    return REQUEST_HOLDER.get();
058            }
059    
060            /**
061             * Get the current response.
062             * @return  the response
063             */
064            public static HttpServletResponse getResponse() {
065                    return RESPONSE_HOLDER.get();
066            }
067    }