001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.net.http;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.io.PrintWriter;
024import java.io.Serializable;
025import java.util.Locale;
026
027import javax.servlet.ServletOutputStream;
028import javax.servlet.http.Cookie;
029import javax.servlet.http.HttpServletResponse;
030
031import lucee.commons.io.DevNullOutputStream;
032import lucee.commons.lang.Pair;
033import lucee.commons.net.URLEncoder;
034import lucee.runtime.type.dt.DateTimeImpl;
035
036
037
038/**
039 * 
040 */
041public final class HttpServletResponseDummy implements HttpServletResponse,Serializable {
042        
043        private Cookie[] cookies=new Cookie[0];
044        private Pair<String,Object>[] headers=new Pair[0];
045        private int status=200;
046        private String statusCode="OK";
047        private String charset="ISO-8859-1";
048        private int contentLength=-1;
049        private String contentType=null;
050        private Locale locale=Locale.getDefault();
051        private int bufferSize=-1;
052        private boolean commited;
053        //private byte[] outputDatad;
054        private OutputStream out;//=new DevNullOutputStream();
055        private boolean outInit=false;
056
057        /**
058         * Constructor of the class
059         */
060        public HttpServletResponseDummy() {
061                this(DevNullOutputStream.DEV_NULL_OUTPUT_STREAM);
062        }
063
064        public HttpServletResponseDummy(OutputStream out) {
065                this.out=out;
066        }
067        
068        @Override
069        public void addCookie(Cookie cookie) {
070                Cookie[] tmp = new Cookie[cookies.length+1];
071                for(int i=0;i<cookies.length;i++) {
072                        tmp[i]=cookies[i];
073                }
074                tmp[cookies.length]=cookie;
075                cookies=tmp;
076        }
077        
078        @Override
079        public boolean containsHeader(String key) {
080                return ReqRspUtil.get(headers, key)!=null;
081        }
082        
083        @Override
084        public String encodeURL(String value) {
085                return URLEncoder.encode(value);
086        }
087        @Override
088        public String encodeRedirectURL(String url) {
089                return URLEncoder.encode(url);
090        }
091        @Override
092        public String encodeUrl(String value) {
093                return URLEncoder.encode(value);
094        }
095        @Override
096        public String encodeRedirectUrl(String value) {
097                return URLEncoder.encode(value);
098        }
099        
100        @Override
101        public void sendError(int code, String codeText) throws IOException {
102                // TODO impl
103        }
104        @Override
105        public void sendError(int code) throws IOException {
106                // TODO impl
107        }
108        
109        @Override
110        public void sendRedirect(String location) throws IOException {
111                addHeader("location",location);
112        }
113        @Override
114        public void setDateHeader(String key, long value) {
115                setHeader(key, new DateTimeImpl(value,false).castToString());
116        }
117        
118        @Override
119        public void addDateHeader(String key, long value) {
120                addHeader(key, new DateTimeImpl(value,false).castToString());
121        }
122        
123        @Override
124        public void setHeader(String key, String value) {
125                headers=ReqRspUtil.set(headers, key, value);
126        }
127        
128        @Override
129        public void addHeader(String key, String value) {
130                headers=ReqRspUtil.add(headers, key, value);
131        }
132        
133        @Override
134        public void setIntHeader(String key, int value) {
135                setHeader(key, String.valueOf(value));
136        }
137        
138        @Override
139        public void addIntHeader(String key, int value) {
140                addHeader(key, String.valueOf(value));
141        }
142        @Override
143        public void setStatus(int status) {
144                this.status=status; 
145        }
146        @Override
147        public void setStatus(int status, String statusCode) {
148                setStatus(status);
149                this.statusCode=statusCode;  
150        }
151        
152        @Override
153        public String getCharacterEncoding() {
154                return charset;
155        }
156        
157        public void setCharacterEncoding(String charset) {
158                this.charset = charset;
159        }
160        
161        @Override
162        public ServletOutputStream getOutputStream() throws IOException {
163                if(outInit) throw new IOException("output already initallised");
164                outInit=true;
165                return new ServletOutputStreamDummy(out);
166        }
167        @Override
168        public PrintWriter getWriter() throws IOException {
169                if(outInit) throw new IOException("output already initallised");
170                outInit=true;
171                return new PrintWriter(out);
172        }
173        @Override
174        public void setContentLength(int contentLength) {
175                this.contentLength=contentLength;
176        }
177        @Override
178        public void setContentType(String contentType) {
179                this.contentType=contentType;
180        }
181        @Override
182        public void setBufferSize(int size) {
183                this.bufferSize=size;
184        }
185        @Override
186        public int getBufferSize() {
187                return bufferSize;
188        }
189        @Override
190        public void flushBuffer() throws IOException {
191                commited = true;
192        }
193        @Override
194        public void resetBuffer() {
195                commited = true;
196        }
197        @Override
198        public boolean isCommitted() {
199                return commited;
200        }
201        @Override
202        public void reset() {
203                commited = true;
204        }
205        @Override
206        public void setLocale(Locale locale) {
207                this.locale=locale;
208        }
209        @Override
210        public Locale getLocale() {
211                return locale;
212        }
213
214        /**
215         * @return the charset
216         */
217        public String getCharsetEncoding() {
218                return charset;
219        }
220
221        /**
222         * @return the commited
223         */
224        public boolean isCommited() {
225                return commited;
226        }
227
228        /**
229         * @return the contentLength
230         */
231        public int getContentLength() {
232                return contentLength;
233        }
234
235        /**
236         * @return the contentType
237         */
238        public String getContentType() {
239                return contentType;
240        }
241
242        /**
243         * @return the cookies
244         */
245        public Cookie[] getCookies() {
246                return cookies;
247        }
248
249        /**
250         * @return the headers
251         */
252        public Pair<String,Object>[] getHeaders() {
253                return headers;
254        }
255
256        /* *
257         * @return the outputData
258         * /
259        public byte[] getOutputData() {
260                return outputData;
261        }
262
263        public void setOutputData(byte[] outputData) {
264                this.outputData=outputData;
265        }*/
266
267        /**
268         * @return the status
269         */
270        public int getStatus() {
271                return status;
272        }
273
274        /**
275         * @return the statusCode
276         */
277        public String getStatusCode() {
278                return statusCode;
279        }
280        
281}