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