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 java.io.ByteArrayOutputStream;
020import java.io.IOException;
021
022import org.apache.wicket.WicketRuntimeException;
023import org.apache.wicket.request.Response;
024
025
026/**
027 * Response used to capture output as a byte array
028 * 
029 * @author igor.vaynberg
030 */
031public class ByteArrayResponse extends Response
032{
033
034        private ByteArrayOutputStream bytes;
035        private Response original;
036
037        /**
038         * Constructor
039         * 
040         * @param original
041         */
042        public ByteArrayResponse(Response original)
043        {
044                this.original = original;
045                reset();
046        }
047
048        /**
049         * Constructor
050         */
051        public ByteArrayResponse()
052        {
053                this(null);
054        }
055
056        /**
057         * @return bytes
058         */
059        public byte[] getBytes()
060        {
061                return bytes.toByteArray();
062        }
063
064        /**
065         * @see org.apache.wicket.request.Response#write(CharSequence)
066         */
067        @Override
068        public void write(final CharSequence string)
069        {
070                try
071                {
072                        bytes.write(string.toString().getBytes());
073                }
074                catch (IOException e)
075                {
076                        throw new WicketRuntimeException("Cannot write into internal byte stream", e);
077                }
078        }
079
080        /**
081         * @see org.apache.wicket.request.Response#reset()
082         */
083        @Override
084        public void reset()
085        {
086                bytes = new ByteArrayOutputStream();
087        }
088
089
090        /**
091         * @see org.apache.wicket.request.Response#getOutputStream()
092         */
093        @Override
094        public void write(byte[] array)
095        {
096                try
097                {
098                        bytes.write(array);
099                }
100                catch (IOException e)
101                {
102                        throw new WicketRuntimeException("Cannot write into internal byte stream", e);
103                }
104        }
105
106        @Override
107        public void write(byte[] array, int offset, int length)
108        {
109                try
110                {
111                        bytes.write(array, offset, length);
112                }
113                catch (Exception e)
114                {
115                        throw new WicketRuntimeException("Cannot write into internal byte stream", e);
116                }
117
118        }
119
120        @Override
121        public String encodeURL(CharSequence url)
122        {
123                if (original != null)
124                {
125                        return original.encodeURL(url);
126                }
127                else
128                {
129                        return url != null ? url.toString() : null;
130                }
131        }
132
133        @Override
134        public Object getContainerResponse()
135        {
136                return original.getContainerResponse();
137        }
138}