001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.response; 018 019import org.apache.wicket.request.Response; 020import org.apache.wicket.util.string.AppendingStringBuffer; 021 022 023/** 024 * Response object that writes to a StringWriter. If the StringResponse is later converted to a 025 * String via toString(), the output which was written to the StringResponse will be returned as a 026 * String. 027 * 028 * @author Jonathan Locke 029 */ 030public class StringResponse extends Response 031{ 032 033 private static final int DEFAULT_INITIAL_CAPACITY = 128; 034 035 /** StringWriter to write to */ 036 protected final AppendingStringBuffer out; 037 038 /** 039 * Constructor 040 */ 041 public StringResponse() 042 { 043 this(DEFAULT_INITIAL_CAPACITY); 044 } 045 046 /** 047 * Constructor 048 * 049 * @param initialCapacity 050 * the initial capacity of the internal buffer 051 */ 052 public StringResponse(int initialCapacity) 053 { 054 out = new AppendingStringBuffer(initialCapacity); 055 } 056 057 /** 058 * @see org.apache.wicket.request.Response#write(CharSequence) 059 */ 060 @Override 061 public void write(final CharSequence string) 062 { 063 out.append(string); 064 } 065 066 /** 067 * @see org.apache.wicket.request.Response#reset() 068 */ 069 @Override 070 public void reset() 071 { 072 out.clear(); 073 } 074 075 /** 076 * @see java.lang.Object#toString() 077 */ 078 @Override 079 public String toString() 080 { 081 return out.toString(); 082 } 083 084 /** 085 * @return The internal buffer directly as a {@link CharSequence} 086 */ 087 public CharSequence getBuffer() 088 { 089 return out; 090 } 091 092 @Override 093 public void write(byte[] array) 094 { 095 throw new UnsupportedOperationException(); 096 } 097 098 @Override 099 public void write(byte[] array, int offset, int length) 100 { 101 throw new UnsupportedOperationException(); 102 } 103 104 @Override 105 public String encodeURL(CharSequence url) 106 { 107 return url != null ? url.toString() : null; 108 } 109 110 @Override 111 public Object getContainerResponse() 112 { 113 return null; 114 } 115}