001: /*
002: * $Id: Response.java 462116 2006-09-08 22:41:11Z ehillenius $ $Revision:
003: * 1.6 $ $Date: 2006-09-09 00:41:11 +0200 (Sat, 09 Sep 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket;
019:
020: import java.io.OutputStream;
021: import java.util.List;
022: import java.util.Locale;
023:
024: import wicket.markup.ComponentTag;
025: import wicket.util.string.AppendingStringBuffer;
026: import wicket.util.string.Strings;
027: import wicket.util.time.Time;
028:
029: /**
030: * Abstract base class for different implementations of response writing. A
031: * subclass must implement write(String) to write a String to the response
032: * destination (whether it be a browser, a file, a test harness or some other
033: * place). A subclass may optionally implement close(), encodeURL(String),
034: * redirect(String), isRedirect() or setContentType(String) as appropriate.
035: *
036: * @author Jonathan Locke
037: */
038: public abstract class Response {
039: /** Default encoding of output stream */
040: private String defaultEncoding;
041:
042: /**
043: * Closes the response output stream
044: */
045: public void close() {
046: }
047:
048: /**
049: * Called when the Response needs to reset itself.
050: * Subclasses can empty there buffer or build up state.
051: */
052: public void reset() {
053:
054: }
055:
056: /**
057: * An implementation of this method is only required if a subclass wishes to
058: * support sessions via URL rewriting. This default implementation simply
059: * returns the URL String it is passed.
060: *
061: * @param url
062: * The URL to encode
063: * @return The encoded url
064: */
065: public CharSequence encodeURL(final CharSequence url) {
066: return url;
067: }
068:
069: /**
070: * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT.
071: *
072: * Loops over all the response filters that were set (if any) with the give
073: * response returns the response buffer itself if there where now filters or
074: * the response buffer that was created/returned by the filter(s)
075: *
076: * @param responseBuffer
077: * The response buffer to be filtered
078: * @return Returns the filtered string buffer.
079: */
080: public final AppendingStringBuffer filter(
081: AppendingStringBuffer responseBuffer) {
082: List responseFilters = Application.get()
083: .getRequestCycleSettings().getResponseFilters();
084: if (responseFilters == null) {
085: return responseBuffer;
086: }
087: for (int i = 0; i < responseFilters.size(); i++) {
088: IResponseFilter filter = (IResponseFilter) responseFilters
089: .get(i);
090: responseBuffer = filter.filter(responseBuffer);
091: }
092: return responseBuffer;
093: }
094:
095: /**
096: * Get the default encoding
097: *
098: * @return default encoding
099: */
100: public String getCharacterEncoding() {
101: if (this .defaultEncoding == null) {
102: return Application.get().getRequestCycleSettings()
103: .getResponseRequestEncoding();
104: } else {
105: return this .defaultEncoding;
106: }
107: }
108:
109: /**
110: * @return The output stream for this response
111: */
112: public abstract OutputStream getOutputStream();
113:
114: /**
115: * Returns true if a redirection has occurred. The default implementation
116: * always returns false since redirect is not implemented by default.
117: *
118: * @return True if the redirect method has been called, making this response
119: * a redirect.
120: */
121: public boolean isRedirect() {
122: return false;
123: }
124:
125: /**
126: * CLIENTS SHOULD NEVER CALL THIS METHOD FOR DAY TO DAY USE!
127: * <p>
128: * A subclass may override this method to implement redirection. Subclasses
129: * which have no need to do redirection may choose not to override this
130: * default implementation, which does nothing. For example, if a subclass
131: * wishes to write output to a file or is part of a testing harness, there
132: * may be no meaning to redirection.
133: * </p>
134: *
135: * @param url
136: * The URL to redirect to
137: */
138: public void redirect(final String url) {
139: }
140:
141: /**
142: * Set the default encoding for the output.
143: * Note: It is up to the derived class to make use of the information.
144: * Class Respsonse simply stores the value, but does not apply
145: * it anywhere automatically.
146: *
147: * @param encoding
148: */
149: public void setCharacterEncoding(final String encoding) {
150: this .defaultEncoding = encoding;
151: }
152:
153: /**
154: * Set the content length on the response, if appropriate in the subclass.
155: * This default implementation does nothing.
156: *
157: * @param length
158: * The length of the content
159: */
160: public void setContentLength(final long length) {
161: }
162:
163: /**
164: * Set the content type on the response, if appropriate in the subclass.
165: * This default implementation does nothing.
166: *
167: * @param mimeType
168: * The mime type
169: */
170: public void setContentType(final String mimeType) {
171: }
172:
173: /**
174: * Set the contents last modified time, if appropriate in the subclass.
175: * This default implementation does nothing.
176: * @param time
177: * The time object
178: */
179: public void setLastModifiedTime(Time time) {
180: }
181:
182: /**
183: * @param locale
184: * Locale to use for this response
185: */
186: public void setLocale(final Locale locale) {
187: }
188:
189: /**
190: * Writes the given tag to via the write(String) abstract method.
191: *
192: * @param tag
193: * The tag to write
194: */
195: public final void write(final ComponentTag tag) {
196: write(tag.toString());
197: }
198:
199: /**
200: * Writes the given string to the Response subclass output destination.
201: *
202: * @param string
203: * The string to write
204: */
205: public abstract void write(final CharSequence string);
206:
207: /**
208: * Writes the given string to the Response subclass output destination and
209: * appends a cr/nl depending on the OS
210: *
211: * @param string
212: */
213: public final void println(final CharSequence string) {
214: write(string);
215: write(Strings.LINE_SEPARATOR);
216: }
217: }
|