001    /** 
002     * Copyright (C) 2009 "Darwin V. Felix" <darwinfelix@users.sourceforge.net>
003     * 
004     * This library is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Lesser General Public
006     * License as published by the Free Software Foundation; either
007     * version 2.1 of the License, or (at your option) any later version.
008     * 
009     * This library is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Lesser General Public License for more details.
013     * 
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this library; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017     */
018    
019    package net.sourceforge.spnego;
020    
021    import java.io.IOException;
022    
023    import javax.servlet.http.HttpServletResponse;
024    import javax.servlet.http.HttpServletResponseWrapper;
025    
026    /**
027     * Class adds capability to track/determine if the HTTP Status 
028     * code has been set.
029     * 
030     * <p>
031     * Also allows the ability to set the content length to zero 
032     * and flush the buffer immediately after setting the HTTP 
033     * Status code.
034     * </p>
035     * 
036     * @author Darwin V. Felix
037     * 
038     */
039    public final class SpnegoHttpServletResponse extends HttpServletResponseWrapper  {
040    
041        private transient boolean statusSet = false;
042    
043        /**
044         * 
045         * @param response
046         */
047        public SpnegoHttpServletResponse(final HttpServletResponse response) {
048            super(response);
049        }
050    
051        /**
052         * Tells if setStatus has been called.
053         * 
054         * @return true if HTTP Status code has been set
055         */
056        public boolean isStatusSet() {
057            return this.statusSet;
058        }
059    
060        @Override
061        public void setStatus(final int status) {
062            super.setStatus(status);
063            this.statusSet = true;
064        }
065    
066        /**
067         * Sets the HTTP Status Code and optionally set the the content 
068         * length to zero and flush the buffer.
069         * 
070         * @param status http status code
071         * @param immediate set to true to set content len to zero and flush
072         * @throws IOException 
073         * 
074         * @see #setStatus(int)
075         */
076        public void setStatus(final int status, final boolean immediate) throws IOException {
077            setStatus(status);
078            if (immediate) {
079                setContentLength(0);
080                flushBuffer();
081            }
082        }
083    }