1 48 49 50 package com.caucho.portal.generic; 51 52 import javax.portlet.*; 53 import java.io.IOException ; 54 import java.io.OutputStream ; 55 import java.io.PrintWriter ; 56 import java.util.Enumeration ; 57 import java.util.LinkedHashSet ; 58 import java.util.Locale ; 59 import java.util.Map ; 60 import java.util.ResourceBundle ; 61 import java.util.Set ; 62 import java.util.logging.Logger ; 63 64 109 110 abstract public class AbstractRenderer implements Renderer 111 { 112 public static final String PAGE_HEADER_RENDERED 113 = "com.caucho.portal.generic.AbstractRenderer.PAGE_HEADER_RENDERED"; 114 115 protected static final Logger log = 116 Logger.getLogger(AbstractRenderer.class.getName()); 117 118 private boolean _isAlwaysWrite = true; 119 private boolean _isAlwaysStream; 120 private String _defaultContentType = "text/html"; 121 private int _bufferSize = -1; 122 123 private Boolean _isDecorateWindow; 124 125 130 public void setDecorateWindow(boolean isDecorateWindow) 131 { 132 _isDecorateWindow = isDecorateWindow ? Boolean.TRUE : Boolean.FALSE; 133 } 134 135 141 public void setAlwaysWrite(boolean isAlwaysWrite) 142 { 143 _isAlwaysWrite = isAlwaysWrite; 144 } 145 146 147 153 public void setAlwaysStream(boolean isAlwaysStream) 154 { 155 _isAlwaysStream = isAlwaysStream; 156 } 157 158 164 public void setDefaultContentType(String defaultContentType) 165 { 166 _defaultContentType = defaultContentType; 167 } 168 169 173 public void setBufferSize(int bufferSize) 174 { 175 _bufferSize = bufferSize; 176 } 177 178 187 public boolean isWindowStateAllowed( PortletRequest request, 188 WindowState windowState ) 189 { 190 return true; 191 } 192 193 202 public boolean isPortletModeAllowed( PortletRequest request, 203 PortletMode portletMode ) 204 { 205 return true; 206 } 207 208 public boolean isAlwaysWrite() 209 { 210 return _isAlwaysWrite; 211 } 212 213 public boolean isAlwaysStream() 214 { 215 return _isAlwaysStream; 216 } 217 218 public String getDefaultContentType() 219 { 220 return _defaultContentType; 221 } 222 223 public int getBufferSize() 224 { 225 return _bufferSize; 226 } 227 228 235 public PrintWriter getWriter( PrintWriter out, 236 RenderRequest request, 237 String namespace ) 238 throws IOException 239 { 240 boolean doPage = (request.getAttribute(PAGE_HEADER_RENDERED) == null); 241 242 boolean doWindow = _isDecorateWindow == null 243 ? !doPage 244 : _isDecorateWindow.booleanValue(); 245 246 if (doPage) { 247 beginPage(out, request, namespace); 248 request.setAttribute(PAGE_HEADER_RENDERED, this); 249 } 250 251 if (doWindow) 252 beginWindow(out, request, namespace); 253 254 return out; 255 } 256 257 262 protected void beginPage( PrintWriter out, 263 RenderRequest request, 264 String namespace ) 265 throws IOException 266 { 267 } 268 269 273 protected void beginWindow( PrintWriter out, 274 RenderRequest request, 275 String namespace ) 276 throws IOException 277 { 278 } 279 280 284 protected void endWindow( PrintWriter out, 285 RenderRequest request, 286 String namespace ) 287 throws IOException 288 { 289 } 290 291 295 protected void endPage( PrintWriter out, 296 RenderRequest request, 297 String namespace ) 298 throws IOException 299 { 300 } 301 302 308 public void finish( PrintWriter out, 309 RenderRequest request, 310 String namespace, 311 boolean isDiscarded ) 312 throws IOException 313 { 314 boolean doPage = (this == request.getAttribute(PAGE_HEADER_RENDERED)); 315 316 boolean doWindow = _isDecorateWindow == null 317 ? !doPage 318 : _isDecorateWindow.booleanValue(); 319 320 if (!isDiscarded) { 321 if (doWindow) 322 endWindow(out, request, namespace); 323 324 if (doPage) 325 endPage(out, request, namespace); 326 } 327 else { 328 if (this == request.getAttribute(PAGE_HEADER_RENDERED)) 329 request.removeAttribute(PAGE_HEADER_RENDERED); 330 } 331 } 332 333 337 public OutputStream getOutputStream( OutputStream out, 338 RenderRequest request, 339 String namespace ) 340 throws IOException 341 { 342 boolean doPage = (request.getAttribute(PAGE_HEADER_RENDERED) == null); 343 344 boolean doWindow = _isDecorateWindow == null 345 ? !doPage 346 : _isDecorateWindow.booleanValue(); 347 348 if (doPage) { 349 beginPage(out, request, namespace); 350 request.setAttribute(PAGE_HEADER_RENDERED, this); 351 } 352 353 if (doWindow) 354 beginWindow(out, request, namespace); 355 356 return out; 357 } 358 359 370 protected void beginPage( OutputStream out, 371 RenderRequest request, 372 String namespace ) 373 throws IOException 374 { 375 } 376 377 387 protected void beginWindow( OutputStream out, 388 RenderRequest request, 389 String namespace ) 390 throws IOException 391 { 392 } 393 394 402 protected void endWindow( OutputStream out, 403 RenderRequest request, 404 String namespace ) 405 throws IOException 406 { 407 } 408 409 416 protected void endPage( OutputStream out, 417 RenderRequest request, 418 String namespace ) 419 throws IOException 420 { 421 } 422 423 429 public void finish( OutputStream out, 430 RenderRequest request, 431 String namespace, 432 boolean isDiscarded ) 433 throws IOException 434 { 435 boolean doPage = (this == request.getAttribute(PAGE_HEADER_RENDERED)); 436 437 boolean doWindow = _isDecorateWindow == null 438 ? !doPage 439 : _isDecorateWindow.booleanValue(); 440 441 if (!isDiscarded) { 442 if (doWindow) 443 endWindow(out, request, namespace); 444 445 if (doPage) 446 endPage(out, request, namespace); 447 } 448 else { 449 if (this == request.getAttribute(PAGE_HEADER_RENDERED)) 450 request.removeAttribute(PAGE_HEADER_RENDERED); 451 } 452 } 453 454 457 protected String getContentType(RenderRequest request) 458 { 459 return request.getResponseContentType(); 460 } 461 462 465 protected Locale getLocale(RenderRequest request) 466 { 467 return request.getLocale(); 468 } 469 470 481 protected String getTitle(RenderRequest request) 482 { 483 String title = (String ) request.getAttribute("javax.portlet.title"); 484 485 if (title == null) { 486 PortletConfig portletConfig 487 = (PortletConfig) request.getAttribute("javax.portlet.portletConfig"); 488 489 Locale locale = getLocale(request); 490 ResourceBundle bundle = portletConfig.getResourceBundle(locale); 491 492 if (bundle != null) 493 title = bundle.getString("javax.portlet.title"); 494 } 495 496 return title; 497 } 498 499 516 protected String getShortTitle( RenderRequest request ) 517 { 518 String title = (String ) request.getAttribute("javax.portlet.short-title"); 519 520 if (title == null) 521 title = (String ) request.getAttribute("javax.portlet.title"); 522 523 if (title == null) { 524 PortletConfig portletConfig 525 = (PortletConfig) request.getAttribute("javax.portlet.portletConfig"); 526 527 Locale locale = getLocale(request); 528 ResourceBundle bundle = portletConfig.getResourceBundle(locale); 529 530 title = bundle.getString("javax.portlet.short-title"); 531 532 if (title == null) 533 title = bundle.getString("javax.portlet.title"); 534 } 535 536 return title; 537 } 538 539 544 protected PortletURL createControlURL(RenderRequest request) 545 { 546 RenderResponse response 547 = (RenderResponse) request.getAttribute("javax.portlet.renderResponse"); 548 549 Map <String , String []> renderParamMap = request.getParameterMap(); 550 551 PortletURL url = response.createRenderURL(); 552 553 url.setParameters(renderParamMap); 554 555 return url; 556 } 557 558 566 protected Set <PortletMode> getPortletModes( RenderRequest request ) 567 { 568 Set <PortletMode> portletModes = new LinkedHashSet <PortletMode>(); 569 570 Enumeration <PortletMode> e = (Enumeration <PortletMode>) 571 request.getPortalContext().getSupportedPortletModes(); 572 573 while (e.hasMoreElements()) { 574 PortletMode portletMode = e.nextElement(); 575 576 if (request.isPortletModeAllowed(portletMode)) 577 portletModes.add(portletMode); 578 } 579 580 return portletModes; 581 } 582 583 591 protected Set <WindowState> getWindowStates( RenderRequest request ) 592 { 593 Set <WindowState> windowStates = new LinkedHashSet <WindowState>(); 594 595 Enumeration <WindowState> e = (Enumeration <WindowState>) 596 request.getPortalContext().getSupportedWindowStates(); 597 598 while (e.hasMoreElements()) { 599 WindowState windowState = e.nextElement(); 600 601 if (request.isWindowStateAllowed(windowState)) 602 windowStates.add(windowState); 603 } 604 605 return windowStates; 606 } 607 608 611 protected ResourceBundle getResourceBundle( RenderRequest request ) 612 { 613 Locale locale = getLocale( request ); 614 PortletConfig portletConfig = getPortletConfig( request ); 615 return portletConfig.getResourceBundle( locale ); 616 } 617 618 621 protected PortletConfig getPortletConfig(RenderRequest request) 622 { 623 return (PortletConfig) request.getAttribute("javax.portlet.portletConfig"); 624 } 625 626 629 protected RenderResponse getRenderResponse(RenderRequest request) 630 { 631 return (RenderResponse) request.getAttribute("javax.portlet.renderResponse"); 632 } 633 634 638 protected PrintWriter printEscaped( PrintWriter out, CharSequence string ) 639 { 640 if ( string == null ) { 641 out.print( (String ) null ); 642 return out; 643 } 644 645 for ( int i = 0; i < string.length(); i++ ) { 646 char ch = string.charAt( i ); 647 648 switch ( ch ) { 649 case '<': out.print( "<" ); break; 650 case '>': out.print( ">" ); break; 651 case '&': out.print( "&" ); break; 652 case '\"': out.print( """ ); break; 653 case '\'': out.print( "’" ); break; 654 default: out.print( ch ); 655 } 656 } 657 658 return out; 659 } 660 661 } 662 663 | Popular Tags |