1 48 49 50 package com.caucho.portal.generic; 51 52 import com.caucho.portal.generic.context.ConnectionContext; 53 54 import javax.portlet.PortletException; 55 import javax.portlet.PortletRequest; 56 import javax.portlet.PortletSecurityException; 57 import javax.portlet.PortletSession; 58 import java.io.BufferedReader ; 59 import java.io.IOException ; 60 import java.io.InputStream ; 61 import java.io.OutputStream ; 62 import java.io.PrintWriter ; 63 import java.io.UnsupportedEncodingException ; 64 import java.security.Principal ; 65 import java.util.Enumeration ; 66 import java.util.Locale ; 67 import java.util.Set ; 68 import java.util.logging.Level ; 69 import java.util.logging.Logger ; 70 71 72 149 abstract public class PortletConnection 150 { 151 154 public static final String PORTLET_CONNECTION 155 = "com.caucho.portal.generic.PortletConnection"; 156 157 160 public static PortletConnection getConnection(PortletRequest portletRequest) 161 { 162 return (PortletConnection) portletRequest.getAttribute(PORTLET_CONNECTION); 163 } 164 165 169 public static Portal getPortal(PortletRequest portletRequest) 170 { 171 PortletConnection connection = getConnection(portletRequest); 172 173 if (connection == null) 174 return null; 175 else 176 return connection.getPortal(); 177 } 178 179 183 public static Action getAction(PortletRequest portletRequest) 184 { 185 PortletConnection connection = getConnection(portletRequest); 186 187 if (connection == null) 188 return null; 189 else 190 return connection.getCurrentAction(); 191 } 192 193 197 public static Render getRender(PortletRequest portletRequest) 198 { 199 PortletConnection connection = getConnection(portletRequest); 200 201 if (connection == null) 202 return null; 203 else 204 return connection.getCurrentRender(); 205 } 206 207 209 static final public Logger log = 210 Logger.getLogger(PortletConnection.class.getName()); 211 212 private static int _connectionCount = 10; 213 214 private ConnectionContext _context; 215 216 private String _connectionId; 217 218 private Portal _portal; 219 220 private boolean _connectionFailed; 221 private Exception _connectionFailedCause; 222 223 protected PortletConnection() 224 { 225 int id = _connectionCount++; 226 _connectionId = Integer.toString(id, Character.MAX_RADIX); 227 _context = new ConnectionContext(this); 228 } 229 230 233 public String getId() 234 { 235 return _connectionId; 236 } 237 238 public void start(Portal portal, InvocationFactory invocationFactory) 239 { 240 if (_portal != null) 241 throw new IllegalStateException ("missing finish()?"); 242 243 _portal = portal; 244 245 _context.start(invocationFactory); 246 } 247 248 public void finish() 249 { 250 _context.finish(); 251 _portal = null; 252 _connectionFailedCause = null; 253 _connectionFailed = false; 254 } 255 256 public Portal getPortal() 257 { 258 return _portal; 259 } 260 261 265 public void setConnectionFailed() 266 { 267 if (!_connectionFailed) { 268 PortletException ex = new PortletException("connection failed"); 269 setConnectionFailed(ex); 270 } 271 } 272 273 277 public void setConnectionFailed(Exception ex) 278 { 279 if (!_connectionFailed) { 280 _connectionFailed = true; 281 _connectionFailedCause = ex; 282 283 log.log(Level.FINE, ex.toString(), ex); 284 } 285 } 286 287 290 public boolean isConnectionFailed() 291 { 292 return _connectionFailed; 293 } 294 295 300 abstract public boolean handleConstraintFailure( Constraint constraint, 301 int failureCode ) 302 throws IOException ; 303 304 309 abstract public boolean handleException(Exception exception); 310 311 315 abstract public boolean canGuaranteeIntegrity(); 316 317 321 abstract public boolean canGuaranteeConfidentiality(); 322 323 327 abstract public void setAttribute(String name, Object o); 328 329 333 abstract public Object getAttribute(String name); 334 335 339 abstract public void removeAttribute(String name); 340 341 347 abstract public Enumeration getAttributeNames(); 348 349 360 abstract public PortletSession getPortletSession(boolean create); 361 362 367 abstract public String getScheme(); 368 369 374 abstract public String getServerName(); 375 376 381 abstract public int getServerPort(); 382 383 389 abstract public String getContextPath(); 390 391 399 abstract public String getAuthType(); 400 401 407 abstract public boolean isSecure(); 408 409 415 abstract public String getRequestedSessionId(); 416 417 423 abstract public boolean isRequestedSessionIdValid(); 424 425 431 abstract public String getRemoteUser(); 432 433 439 abstract public Principal getUserPrincipal(); 440 441 447 abstract public boolean isUserInRole(String role); 448 449 458 abstract public String getProperty(String propertyName); 459 460 472 abstract public Enumeration getProperties(String propertyName); 473 474 484 abstract public Enumeration getPropertyNames(); 485 486 495 abstract public Set <String > getClientContentTypes(); 496 497 507 abstract public Set <Locale > getClientLocales(); 508 509 519 abstract public Set <String > getClientCharacterEncodings(); 520 521 527 abstract public String getSubmitContentType(); 528 529 535 abstract public int getSubmitContentLength(); 536 537 547 abstract public InputStream getSubmitInputStream() 548 throws IOException , IllegalStateException ; 549 550 562 abstract public void setSubmitCharacterEncoding(String enc) 563 throws UnsupportedEncodingException , IllegalStateException ; 564 565 571 abstract public String getSubmitCharacterEncoding(); 572 573 abstract public BufferedReader getSubmitReader() 574 throws UnsupportedEncodingException , IOException ; 575 576 580 abstract public String encodeURL(String path); 581 582 585 abstract public String resolveURL(String partialUrl); 586 587 591 abstract public String resolveURL(String partialUrl, boolean isSecure) 592 throws PortletSecurityException; 593 594 595 abstract public void sendRedirect(String location) 596 throws IllegalStateException , IOException ; 597 598 606 abstract public void setProperty(String name, String value); 607 608 616 abstract public void addProperty(String name, String value); 617 618 619 622 abstract public void setContentType(String contentType); 623 624 628 abstract public String getContentType(); 629 630 633 abstract public void setLocale(Locale locale); 634 635 639 abstract public Locale getLocale(); 640 641 abstract public void setBufferSize(int size); 642 643 abstract public int getBufferSize(); 644 645 abstract public void flushBuffer() 646 throws IOException ; 647 648 abstract public void resetBuffer(); 649 650 abstract public void reset(); 651 652 abstract public boolean isCommitted(); 653 654 658 abstract public OutputStream getOutputStream() 659 throws IOException ; 660 661 abstract public String getCharacterEncoding(); 662 663 abstract public void setCharacterEncoding(String enc) 664 throws UnsupportedEncodingException ; 665 666 670 abstract public PrintWriter getWriter() 671 throws IOException ; 672 673 674 683 public Action getAction( Window window, String namespace ) 684 throws PortletException, IOException 685 { 686 return _context.getAction(window, namespace); 687 } 688 689 692 public Action getCurrentAction() 693 { 694 return _context.getCurrentAction(); 695 } 696 697 705 public Render getRender( Window window, String namespace ) 706 throws PortletException, IOException 707 { 708 return _context.getRender(window, namespace); 709 } 710 711 714 public Render getCurrentRender() 715 { 716 return _context.getCurrentRender(); 717 } 718 719 720 723 public void checkForFailure() 724 throws PortletException 725 { 726 if (_connectionFailed) { 727 if (_connectionFailedCause == null) 728 throw new PortletException("connection failed"); 729 else 730 throw new PortletException( 731 "connection failed: " + _connectionFailedCause.toString(), 732 _connectionFailedCause ); 733 } 734 } 735 736 740 protected boolean isPrivate() 741 { 742 return _context.isConnectionPrivate(); 743 } 744 745 752 protected int getExpirationCache() 753 { 754 return _context.getConnectionExpirationCache(); 755 } 756 757 } 758 759 | Popular Tags |