    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
    015package org.apache.tapestry5.internal.services;
    016
    017import java.io.BufferedWriter;
    018import java.io.IOException;
    019import java.io.OutputStream;
    020import java.io.OutputStreamWriter;
    021import java.io.PrintWriter;
    022import java.io.Writer;
    023
    024import javax.servlet.http.HttpServletRequest;
    025import javax.servlet.http.HttpServletResponse;
    026
    027import org.apache.tapestry5.Link;
    028import org.apache.tapestry5.internal.InternalConstants;
    029import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    030import org.apache.tapestry5.services.Response;
    031
    032/**
    033 * Implementation of {@link Response} that wraps around an underlying {@link HttpServletResponse}.
    034 */
    035public 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 addHeader(String name, String value)
    117    {
    118        response.addHeader(name, value);
    119    }
    120
    121    public void setIntHeader(String name, int value)
    122    {
    123        response.setIntHeader(name, value);
    124    }
    125
    126    public boolean isCommitted()
    127    {
    128        return response.isCommitted();
    129    }
    130
    131    public void disableCompression()
    132    {
    133        request.setAttribute(InternalConstants.SUPPRESS_COMPRESSION, true);
    134    }
    135}