1 48 49 50 package com.caucho.portal.generic.context; 51 52 import com.caucho.portal.generic.Renderer; 53 54 import javax.portlet.RenderRequest; 55 import javax.portlet.RenderResponse; 56 import java.io.IOException ; 57 import java.io.OutputStream ; 58 import java.io.PrintWriter ; 59 import java.util.logging.Logger ; 60 61 public class RendererResponseHandler 62 extends AbstractResponseHandler 63 { 64 protected static final Logger log = 65 Logger.getLogger(RendererResponseHandler.class.getName()); 66 67 private ConnectionContext _context; 68 private Renderer _renderer; 69 private RenderRequest _renderRequest; 70 private RenderResponse _renderResponse; 71 private String _namespace; 72 73 private PrintWriter _writer; 74 private boolean _writerIsWrapped; 75 private OutputStream _outputStream; 76 private boolean _outputStreamIsWrapped; 77 78 private boolean _wasReset; 79 80 public RendererResponseHandler() 81 { 82 } 83 84 public RendererResponseHandler( ConnectionContext context, 85 ResponseHandler responseHandler, 86 Renderer renderer, 87 RenderRequest renderRequest, 88 RenderResponse renderResponse, 89 String namespace ) 90 { 91 open(context, responseHandler, renderer, renderRequest, renderResponse, namespace); 92 } 93 94 public void open( ConnectionContext context, 95 ResponseHandler responseHandler, 96 Renderer renderer, 97 RenderRequest renderRequest, 98 RenderResponse renderResponse, 99 String namespace ) 100 { 101 if (_renderer != null) 102 throw new IllegalStateException ("already open"); 103 104 super.open(responseHandler); 105 106 _context = context; 107 _renderer = renderer; 108 _renderRequest = renderRequest; 109 _renderResponse = renderResponse; 110 _namespace = namespace; 111 } 112 113 public void finish() 114 throws IOException 115 { 116 try { 117 finishWriter(false); 118 finishOutputStream(false); 119 } 120 catch (Exception ex) { 121 setError(ex); 122 } 123 finally { 124 _wasReset = false; 125 _writer = null; 126 _writerIsWrapped = false; 127 _outputStream = null; 128 _outputStreamIsWrapped = false; 129 _namespace = null; 130 _renderRequest = null; 131 _renderResponse = null; 132 _renderer = null; 133 } 134 } 135 136 public void finishWriter( boolean isDiscarded ) 137 throws IOException 138 { 139 if (_writerIsWrapped) { 140 PrintWriter writer = _writer; 141 _writer = null; 142 _writerIsWrapped = false; 143 144 _renderer.finish( writer, _renderRequest, _namespace, isDiscarded ); 145 } 146 } 147 148 public void finishOutputStream( boolean isDiscarded ) 149 throws IOException 150 { 151 if (_outputStreamIsWrapped) { 152 OutputStream outputStream = _outputStream; 153 _outputStream = null; 154 _outputStreamIsWrapped = false; 155 156 _renderer.finish( outputStream, _renderRequest, _namespace, isDiscarded ); 157 } 158 } 159 160 public String getDefaultContentType() 161 { 162 return _renderer == null ? null : _renderer.getDefaultContentType(); 163 } 164 165 public boolean isAlwaysWrite() 166 { 167 return _renderer != null && _renderer.isAlwaysWrite(); 168 } 169 170 public PrintWriter getWriter() 171 throws IOException 172 { 173 if (_writer != null) 174 return _writer; 175 176 PrintWriter writer = super.getWriter(); 177 178 if (_renderer != null) { 179 180 PrintWriter rendererWriter 181 = _renderer.getWriter( writer, _renderRequest, _namespace ); 182 183 if (rendererWriter != null) { 184 writer = rendererWriter; 185 _writerIsWrapped = true; 186 } 187 } 188 189 return _writer = writer; 190 } 191 192 public boolean isWriter() 193 { 194 return _writerIsWrapped; 195 } 196 197 public boolean isAlwaysStream() 198 { 199 return _renderer != null && _renderer.isAlwaysStream(); 200 } 201 202 public OutputStream getOutputStream() 203 throws IOException 204 { 205 if (_outputStream != null) 206 return _outputStream; 207 208 OutputStream outputStream = super.getOutputStream(); 209 210 if (_renderer != null) { 211 OutputStream rendererStream 212 = _renderer.getOutputStream(outputStream, _renderRequest, _namespace ); 213 214 if (rendererStream != null) { 215 outputStream = rendererStream; 216 _outputStreamIsWrapped = true; 217 } 218 } 219 220 return _outputStream = outputStream; 221 } 222 223 public boolean isOutputStream() 224 { 225 return _outputStreamIsWrapped; 226 } 227 228 public void flushBuffer() 229 throws IOException 230 { 231 if (!_wasReset && !isWriter() && !isOutputStream()) { 232 if (_renderer.isAlwaysWrite()) { 233 getWriter(); 234 } 235 else if (_renderer.isAlwaysStream()) { 236 getOutputStream(); 237 } 238 } 239 } 240 241 public void resetBuffer() 242 { 243 _wasReset = true; 244 245 try { 246 finishWriter(true); 247 finishOutputStream(true); 248 } 249 catch (IOException ex) { 250 setError(ex); 251 } 252 } 253 254 public void reset() 255 { 256 _wasReset = true; 257 258 try { 259 finishWriter(true); 260 finishOutputStream(true); 261 } 262 catch (IOException ex) { 263 setError(ex); 264 } 265 } 266 267 271 public void reset(boolean renderAgain) 272 { 273 try { 274 boolean isWriter = isWriter(); 275 boolean isOutputStream = isOutputStream(); 276 277 finishWriter(true); 278 finishOutputStream(true); 279 280 if (isWriter) 281 getWriter(); 282 283 if (isOutputStream) 284 getOutputStream(); 285 286 } 287 catch (IOException ex) { 288 setError(ex); 289 } 290 } 291 } 292 | Popular Tags |