001    // Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
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.apache.tapestry5.internal.services;
016    
017    import java.io.BufferedWriter;
018    import java.io.IOException;
019    import java.io.OutputStream;
020    import java.io.OutputStreamWriter;
021    import java.io.PrintWriter;
022    import java.io.Writer;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    import org.apache.tapestry5.Link;
028    import org.apache.tapestry5.internal.InternalConstants;
029    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
030    import org.apache.tapestry5.services.Response;
031    
032    /**
033     * Implementation of {@link Response} that wraps around an underlying {@link HttpServletResponse}.
034     */
035    public class ResponseImpl implements Response
036    {
037        private final HttpServletRequest request;
038    
039        private final HttpServletResponse response;
040    
041        public ResponseImpl(HttpServletRequest request, HttpServletResponse response)
042        {
043            assert request != null;
044            assert response != null;
045    
046            this.request = request;
047            this.response = response;
048        }
049    
050        public PrintWriter getPrintWriter(String contentType) throws IOException
051        {
052            assert InternalUtils.isNonBlank(contentType);
053            OutputStream os = getOutputStream(contentType);
054    
055            Writer w = new OutputStreamWriter(os, response.getCharacterEncoding());
056    
057            return new PrintWriter(new BufferedWriter(w));
058        }
059    
060        public String encodeURL(String URL)
061        {
062            return response.encodeURL(URL);
063        }
064    
065        public String encodeRedirectURL(String URL)
066        {
067            return response.encodeRedirectURL(URL);
068        }
069    
070        public void sendRedirect(String URL) throws IOException
071        {
072            response.sendRedirect(URL);
073        }
074    
075        public void sendRedirect(Link link) throws IOException
076        {
077            assert link != null;
078            String redirectURL = encodeRedirectURL(link.toRedirectURI());
079    
080            sendRedirect(redirectURL);
081        }
082    
083        public void setStatus(int sc)
084        {
085            response.setStatus(sc);
086        }
087    
088        public OutputStream getOutputStream(String contentType) throws IOException
089        {
090            assert InternalUtils.isNonBlank(contentType);
091            response.setContentType(contentType);
092    
093            return response.getOutputStream();
094        }
095    
096        public void sendError(int sc, String message) throws IOException
097        {
098            response.sendError(sc, message);
099        }
100    
101        public void setContentLength(int length)
102        {
103            response.setContentLength(length);
104        }
105    
106        public void setDateHeader(String name, long date)
107        {
108            response.setDateHeader(name, date);
109        }
110    
111        public void setHeader(String name, String value)
112        {
113            response.setHeader(name, value);
114        }
115    
116        public void setIntHeader(String name, int value)
117        {
118            response.setIntHeader(name, value);
119        }
120    
121        public boolean isCommitted()
122        {
123            return response.isCommitted();
124        }
125    
126        public void disableCompression()
127        {
128            request.setAttribute(InternalConstants.SUPPRESS_COMPRESSION, true);
129        }
130    }