001    // Copyright 2005 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.tapestry.web;
016    
017    import org.apache.tapestry.util.ContentType;
018    
019    import java.io.IOException;
020    import java.io.OutputStream;
021    import java.io.PrintWriter;
022    
023    /**
024     * Controls the response to the client, and specifically allows for creating the output stream (or
025     * print writer) to which content is sent. This is essentially a generic version of
026     * {@link javax.servlet.http.HttpServletResponse}. Some operations may be unsupported in some
027     * implementations (for example, the portlet implementation can't handle any of the setHeader
028     * methods).
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    public interface WebResponse
034    {
035        /**
036         * Returns a output stream to which output should be sent. This method should only be invoked
037         * once on a response.
038         *
039         * @param contentType
040         *          The encoding type that this outputstream will write content as.
041         * @return the output stream, configured for the given type.
042         *
043         * @throws IOException On io error.
044         */
045    
046        OutputStream getOutputStream(ContentType contentType)
047          throws IOException;
048    
049        /**
050         * Returns a {@link PrintWriter} to which output should be sent. This method should be invoked
051         * once on a response. A second call is expected to be so that an exception page can be
052         * rendered, and the underlying request data is reset.
053         *
054         * @param contentType
055         *          The type of content encoding the writer is for.
056         * @return A new {@link PrintWriter} instance.
057         *
058         * @throws IOException On io error.
059         */
060    
061        PrintWriter getPrintWriter(ContentType contentType)
062          throws IOException;
063    
064        /**
065         * Encodes a URL, which adds information to the URL needed to ensure that the request triggered
066         * by the URL will be associated with the current session (if any). In most cases, the string is
067         * returned unchanged.
068         *
069         * @param url
070         *          The URL to encode.
071         * @return The url encoded.
072         */
073    
074        String encodeURL(String url);
075    
076        /**
077         * Resets any buffered content. This may be used after an error to radically change what the
078         * output will be.
079         */
080    
081        void reset();
082    
083        /**
084         * Sets the response content length header.
085         *
086         * @param contentLength
087         *          The total content length this response will write. 
088         */
089        void setContentLength(int contentLength);
090    
091        /**
092         * Returns a value to be prefixed or suffixed with any client-side JavaScript elements
093         * (variables and function names) to ensure that they are unique with the context of the entire
094         * page. For servlets, this is the empty string.
095         *
096         * @return The namespace that this requests resources should be pre-pended with.
097         */
098    
099        String getNamespace();
100    
101        /**
102         * Sets a response header as a date.
103         * 
104         * @param name
105         *            the name of the header to set
106         * @param date
107         *            the date value to set, in milliseconds since the epoch
108         */
109        void setDateHeader(String name, long date);
110    
111        /**
112         * Sets a response header as a string.
113         * 
114         * @param name
115         *            the name of the header to set
116         * @param value
117         *            the value for the named header
118         */
119    
120        void setHeader(String name, String value);
121    
122        /**
123         * Sets a response header with the given name and integer value.
124         * 
125         * @param name
126         *            the name of the header to set
127         * @param value
128         *            the value for the named header
129         */
130        void setIntHeader(String name, int value);
131    
132        /**
133         * Sets the status code for this response.
134         *
135         * @param status
136         *          The HTTP status code to set on the return header.
137         */
138        void setStatus(int status);
139    
140        /**
141         * Sends an error response.
142         *
143         * @param statusCode
144         *          The error status code to set on the header.
145         * @param message
146         *          The message to give as the reason for error.
147         *
148         * @throws IOException on io error.
149         */
150        void sendError(int statusCode, String message) throws IOException;
151    }