00001
00025 package org.openmobileis.test.mock.embedded;
00026
00027 import java.io.ByteArrayOutputStream;
00028 import java.io.IOException;
00029 import java.io.OutputStreamWriter;
00030 import java.io.PrintWriter;
00031 import java.text.SimpleDateFormat;
00032 import java.util.Date;
00033 import java.util.Hashtable;
00034 import java.util.Locale;
00035
00036 import javax.servlet.ServletOutputStream;
00037 import javax.servlet.http.Cookie;
00038 import javax.servlet.http.HttpServletResponse;
00039
00040 import org.openmobileis.common.util.log.LogManager;
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 public class MockHttpServletResponse implements HttpServletResponse {
00051 public final static String CONTENTLENGTH = "Content-Length";
00052 public final static String CONTENTTYPE = "Content-Type";
00053
00054 public Hashtable headers = new Hashtable();
00055 protected static final SimpleDateFormat datefmt = new SimpleDateFormat(
00056 "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'");
00057
00058 public int status;
00059 public String statusMessage;
00060 public Locale locale;
00061 public ByteArrayOutputStream outStream;
00062 public String characterEncoding;
00063
00064
00068 public MockHttpServletResponse() {
00069 locale = Locale.US;
00070 outStream = new ByteArrayOutputStream();
00071 }
00072
00073
00074
00075
00076 public void addCookie(Cookie arg0) {
00077 }
00078
00079
00080
00081
00082 public void addDateHeader(String header, long date) {
00083 addHeader(header, datefmt.format(new Date(date)));
00084 }
00085
00086
00087
00088
00089 public void addHeader(String header, String value) {
00090 Object o = headers.get(header);
00091 if (o == null)
00092 setHeader(header, value);
00093 else {
00094 if (o instanceof String[]) {
00095 String[] oldVal = (String[]) o;
00096 String[] newVal = new String[oldVal.length + 1];
00097 System.arraycopy(oldVal, 0, newVal, 0, oldVal.length);
00098 newVal[oldVal.length] = value;
00099 headers.put(header, newVal);
00100 } else if (o instanceof String) {
00101 String[] newVal = new String[2];
00102 newVal[0] = (String) o;
00103 newVal[1] = value;
00104 headers.put(header, newVal);
00105 } else
00106 throw new RuntimeException("Invalid content of header hash - "
00107 + o.getClass().getName());
00108 }
00109 }
00110
00111
00112
00113
00114 public void addIntHeader(String header, int value) {
00115 addHeader(header, Integer.toString(value));
00116 }
00117
00118
00119
00120
00121 public boolean containsHeader(String name) {
00122 return headers.contains(name);
00123 }
00124
00125
00126
00127
00128 public String encodeRedirectURL(String url) {
00129 return url;
00130 }
00131
00132
00133
00134
00135 public String encodeRedirectUrl(String url) {
00136 return url;
00137 }
00138
00139
00140
00141
00142 public String encodeURL(String url) {
00143 return url;
00144 }
00145
00146
00147
00148
00149 public String encodeUrl(String url) {
00150 return url;
00151 }
00152
00153
00154
00155
00156 public void sendError(int resCode) throws IOException {
00157 setStatus(resCode);
00158 LogManager.traceError(0, "MockHttpServletResponse Error code:"+resCode);
00159 }
00160
00161
00162
00163
00164 public void sendError(int resCode, String resMessage) throws IOException {
00165 setStatus(resCode, resMessage);
00166 LogManager.traceError(0, "MockHttpServletResponse Error code:"+resCode+" message :"+resMessage);
00167 }
00168
00169
00170
00171
00172 public void sendRedirect(String url) throws IOException {
00173 LogManager.traceWarning(0, "MockHttpServletResponse send redirect call for url:"+url);
00174 }
00175
00176
00177
00178
00179 public void setDateHeader(String name, long value) {
00180 setHeader(name, datefmt.format(new Date(value)));
00181 }
00182
00183
00184
00185
00186 public void setHeader(String name, String value) {
00187 headers.put(name, value);
00188 }
00189
00190
00191
00192
00193 public void setIntHeader(String name, int value) {
00194 setHeader(name, Integer.toString(value));
00195 }
00196
00197
00198
00199
00200 public void setStatus(int resCode) {
00201 setStatus(resCode, "");
00202 }
00203
00204
00205
00206
00207 public void setStatus(int resCode, String resMessage) {
00208 this.status = resCode;
00209 this.statusMessage = resMessage;
00210 }
00211
00212
00213
00214
00215 public void flushBuffer() throws IOException {
00216 }
00217
00218
00219
00220
00221 public int getBufferSize() {
00222 return 0;
00223 }
00224
00225
00226
00227
00228 public String getCharacterEncoding() {
00229 String ct = (String) headers.get(CONTENTTYPE);
00230 if (ct != null) {
00231 int scp = ct.indexOf(';');
00232 if (scp > 0) {
00233 scp = ct.toLowerCase().indexOf("charset=", scp);
00234 if (scp >= 0) {
00235 ct = ct.substring(scp + 8);
00236 scp = ct.indexOf(' ');
00237 if (scp > 0)
00238 ct = ct.substring(0, scp);
00239 scp = ct.indexOf(';');
00240 if (scp > 0)
00241 ct = ct.substring(0, scp);
00242 return ct;
00243 }
00244 }
00245 }
00246 return null;
00247 }
00248
00249
00250
00251
00252 public String getContentType() {
00253 return getHeader(CONTENTTYPE);
00254 }
00255 public String getHeader(String name) {
00256
00257 return (String) headers.get(name);
00258 }
00259
00260
00261
00262
00263 public Locale getLocale() {
00264 return locale;
00265 }
00266
00267
00268
00269
00270 public ServletOutputStream getOutputStream() throws IOException {
00271 return new MockServletOutputStream(outStream);
00272 }
00273
00274
00275
00276
00277 public PrintWriter getWriter() throws IOException {
00278 PrintWriter printWriter = null;
00279 String encoding = getCharacterEncoding();
00280 if (encoding != null) {
00281
00282 printWriter = new PrintWriter(new OutputStreamWriter(outStream, encoding));
00283 } else {
00284
00285 printWriter = new PrintWriter(outStream);
00286 }
00287 return printWriter;
00288 }
00289
00290
00291
00292
00293 public boolean isCommitted() {
00294 return false;
00295 }
00296
00297
00298
00299
00300 public void reset() {
00301 }
00302
00303
00304
00305
00306 public void resetBuffer() {
00307 }
00308
00309
00310
00311
00312 public void setBufferSize(int arg0) {
00313 }
00314
00315
00316
00317
00318 public void setCharacterEncoding(String enc) {
00319 characterEncoding = enc;
00320 }
00321
00322
00323
00324
00325 public void setContentLength(int length) {
00326 setIntHeader(CONTENTLENGTH, length);
00327 }
00328
00329
00330
00331
00332 public void setContentType(String type) {
00333 setHeader(CONTENTTYPE, type != null ? type : "Unknown");
00334 }
00335
00336
00337
00338
00339 public void setLocale(Locale arg0) {
00340 this.locale = locale;
00341 }
00342
00343 }