1 17 18 package org.ajaxanywhere; 19 20 import javax.servlet.ServletOutputStream ; 21 import javax.servlet.http.HttpServletResponse ; 22 import javax.servlet.http.HttpServletResponseWrapper ; 23 import java.io.*; 24 25 29 public class BufferResponseWrapper extends HttpServletResponseWrapper { 30 31 32 PrintWriter pw; 33 ServletOutputStream sos; 34 private StringWriter writerBuffer; 35 private ByteArrayOutputStream streamBuffer; 36 37 HttpServletResponse originalResponse; 38 39 private String redirect; 40 41 public BufferResponseWrapper(HttpServletResponse httpServletResponse) { 42 super(httpServletResponse); 43 originalResponse = httpServletResponse; 44 } 45 46 public PrintWriter getWriter() throws IOException { 47 if (writerBuffer == null) { 48 writerBuffer = new StringWriter(); 49 pw = new PrintWriter(writerBuffer); 50 } 51 return pw; 52 } 53 54 public ServletOutputStream getOutputStream() throws IOException { 55 if (streamBuffer == null) { 56 streamBuffer = new ByteArrayOutputStream(); 57 sos = new ServletOutputStream () { 58 public void write(int b) throws IOException { 59 streamBuffer.write(b); 60 } 61 62 public void write(byte b[]) throws IOException { 63 streamBuffer.write(b); 64 } 65 66 public void write(byte b[], int off, int len) throws IOException { 67 streamBuffer.write(b, off, len); 68 } 69 }; 70 } 71 return sos; 72 } 73 74 77 public void output(String content) throws IOException{ 78 if (streamBuffer!=null){ 79 streamBuffer.write(content.getBytes(originalResponse.getCharacterEncoding())); 80 } else { 81 writerBuffer.write(content); 82 } 83 } 84 85 public String getBuffer() { 86 87 if (streamBuffer != null) { 88 try { 89 return streamBuffer.toString(originalResponse.getCharacterEncoding()); 90 } catch (UnsupportedEncodingException e) { 91 return streamBuffer.toString(); 92 } 93 } 94 else if (writerBuffer != null) 95 return writerBuffer.toString(); 96 else 97 return ""; 98 } 99 100 public HttpServletResponse getOriginalResponse() { 101 return originalResponse; 102 } 103 104 public String getRedirect() { 105 return redirect; 106 } 107 108 public void sendRedirect(String redirect) throws IOException { 109 String key = "aaxmlrequest=true"; 110 int pos = redirect.indexOf(key); 111 if (pos !=-1) 112 redirect = redirect.substring(0,pos)+redirect.substring(pos+key.length()); 113 114 this.redirect = redirect; 115 } 116 117 public String findSubstring(String firstDelimiter, String lastDelimiter){ 118 String content; 119 if (streamBuffer!=null){ 120 try { 121 content = streamBuffer.toString("UTF-8"); 122 } catch (UnsupportedEncodingException e) { 123 content = streamBuffer.toString(); 124 } 125 } else if (writerBuffer!=null) { 126 content = writerBuffer.toString(); 127 } else { 128 return null; 129 } 130 131 132 int p1 = content.indexOf(firstDelimiter); 133 if (p1 != -1) { 134 p1+=firstDelimiter.length(); 135 int p2 = content.indexOf(lastDelimiter, p1); 136 if (p2!=-1){ 137 return content.substring(p1, p2); 138 } 139 } 140 return null; 141 } 142 143 public void setContentType(String string) { 144 } 146 147 public void flushBuffer() throws IOException { 148 } 150 151 public void setCharacterEncoding(String string) { 152 } 154 155 public void setDateHeader(String string, long l) { 156 } 158 159 public void addDateHeader(String string, long l) { 160 } 162 163 public void setHeader(String string, String string1) { 164 } 166 167 public void addHeader(String string, String string1) { 168 } 170 171 public void setIntHeader(String string, int i) { 172 } 174 175 public void addIntHeader(String string, int i) { 176 } 178 179 public StringWriter getWriterBuffer() { 180 return writerBuffer; 181 } 182 183 public ByteArrayOutputStream getStreamBuffer() { 184 return streamBuffer; 185 } 186 } 187 | Popular Tags |