/*
 * Copyright 2004-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package org.codehaus.groovy.grails.web.servlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <p>Wrapper class for HttpServletResponse that allows setting the content type while getting the writer.
 * 
 * @author Steven Devijver
 * @author Graeme Rocher
 * 
 * @since 0.1
 * 
 * Created - Jul 5, 2005
 */
public class GrailsHttpServletResponse extends HttpServletResponseWrapper {

    private static final Log LOG = LogFactory.getLog(GrailsHttpServletResponse.class);


	private boolean contentTypeSet;
	private boolean redirected;
	

    public GrailsHttpServletResponse(HttpServletResponse httpServletResponse) {
        super(httpServletResponse);
    }

	/**
	 * Returns whether the request has been redirected
	 * 
	 * @return the redirected True if it has been
	 */
	public boolean isRedirected() {
		return redirected;
	}


    public HttpServletResponse getDelegate() {
        return (HttpServletResponse)getResponse();
    }

    public void sendError(int error, String message) throws IOException {
        this.redirected = true;
        super.sendError(error, message);
	}

	public void sendError(int error) throws IOException {
        this.redirected = true;
        super.sendError(error);
	}

	public void sendRedirect(String url) throws IOException {
		this.redirected = true;
        if(LOG.isDebugEnabled()) {
            LOG.debug("Executing redirect with response ["+getResponse()+"] to location ["+url+"]");
        }

        super.sendRedirect(url);
	}

	public ServletOutputStream getOutputStream(String contentType, String characterEncoding) throws IOException {
		setContentType(contentType + ";charset=" + characterEncoding);
		return super.getOutputStream();
	}


	public PrintWriter getWriter(String contentType, String characterEncoding) throws IOException {
		setContentType(contentType + ";charset=" + characterEncoding);
		return super.getWriter();
	}

	public PrintWriter getWriter(String contentType) throws IOException {
		setContentType(contentType);
		return super.getWriter();
	}


	public void setContentType(String contentType) {
		if(!contentTypeSet) {
			this.contentTypeSet = true;
			super.setContentType(contentType);
		}
	}
}

@@ -795,16 +795,54 @@
 Stream;%0A
+import javax.servlet.ServletResponse;%0A
 import j
@@ -872,32 +872,32 @@
 ervletResponse;%0A
-
 import javax.ser
@@ -934,16 +934,50 @@
 rapper;%0A
+import javax.servlet.http.Cookie;%0A
 import j
@@ -1023,16 +1023,41 @@
 tWriter;
+%0Aimport java.util.Locale;
 %0A%0A/**%0A *
@@ -2439,25 +2439,1363 @@
 t(url);%0A%09%7D%0A%0A
-%09
+    public void setLocale(Locale locale) %7B%0A        getResponse().setLocale(locale);%0A    %7D%0A%0A    public void setBufferSize(int i) %7B%0A        getResponse().setBufferSize(i);%0A    %7D%0A%0A    public String getCharacterEncoding() %7B%0A        return super.getCharacterEncoding();%0A    %7D%0A%0A    public void setStatus(int i) %7B%0A        ((HttpServletResponse)getResponse()).setStatus(i);%0A    %7D%0A%0A    public void addCookie(Cookie cookie) %7B%0A        ((HttpServletResponse)getResponse()).addCookie(cookie);%0A    %7D%0A%0A    public boolean containsHeader(String s) %7B%0A        return ((HttpServletResponse)getResponse()).containsHeader(s);%0A    %7D%0A%0A    public void setDateHeader(String s, long l) %7B%0A        ((HttpServletResponse)getResponse()).setDateHeader(s, l);%0A    %7D%0A%0A    public void addDateHeader(String s, long l) %7B%0A        ((HttpServletResponse)getResponse()).addDateHeader(s, l);%0A    %7D%0A%0A    public void setHeader(String s, String s1) %7B%0A        ((HttpServletResponse)getResponse()).setHeader(s, s1);%0A    %7D%0A%0A    public void addHeader(String s, String s1) %7B%0A        ((HttpServletResponse)getResponse()).addHeader(s, s1);%0A    %7D%0A%0A    public void setIntHeader(String s, int i) %7B%0A        ((HttpServletResponse)getResponse()).setIntHeader(s, i);%0A    %7D%0A%0A    public void addIntHeader(String s, int i) %7B%0A        ((HttpServletResponse)getResponse()).addIntHeader(s, i);%0A    %7D%0A%0A    
 public Servl
@@ -4472,13 +4472,351 @@
 e);%0A%09%09%7D%0A
-
 %09%7D
+%0A%0A    public ServletResponse getResponse() %7B%0A        ServletResponse original = super.getResponse();%0A        while(original instanceof HttpServletResponseWrapper) %7B%0A            original = ((HttpServletResponseWrapper)original).getResponse();%0A        %7D%0A        return original != null ? (HttpServletResponse)original : this;         %0A    %7D
 %0A%7D%0A

fix for cookies, headers etc. being broken in tomcat