1 28 29 30 package com.caucho.widget; 31 32 import com.caucho.util.L10N; 33 34 import javax.portlet.*; 35 import java.io.IOException ; 36 import java.util.Enumeration ; 37 import java.util.Locale ; 38 import java.util.Map ; 39 import java.util.logging.Logger ; 40 41 public class PortletWidgetConnection 42 extends WidgetConnection 43 { 44 private static L10N L = new L10N( PortletWidgetConnection.class ); 45 46 static protected final Logger log = 47 Logger.getLogger( PortletWidgetConnection.class.getName() ); 48 49 private PortletRequest _portletRequest; 50 private PortletResponse _portletResponse; 51 private ActionRequest _actionRequest; 52 private ActionResponse _actionResponse; 53 private RenderRequest _renderRequest; 54 private RenderResponse _renderResponse; 55 56 private WidgetWriter _widgetWriter; 57 58 static public <S extends WidgetState> 59 S prepare( PortletRequest request, PortletResponse response, Widget<S> top ) 60 throws PortletException 61 { 62 WidgetConnection widgetConnection 63 = PortletWidgetConnection.create( request, response ); 64 65 try { 66 return widgetConnection.prepare( top ); 67 } 68 catch ( WidgetException ex ) { 69 throw new PortletException( ex ); 70 } 71 } 72 73 static public <S extends WidgetState> 74 S render( RenderRequest request, RenderResponse response, Widget<S> top ) 75 throws PortletException, IOException 76 { 77 WidgetConnection widgetConnection 78 = PortletWidgetConnection.create( request, response ); 79 80 try { 81 return widgetConnection.render( top ); 82 } 83 catch ( WidgetException ex ) { 84 throw new PortletException( ex ); 85 } 86 } 87 88 public static PortletWidgetConnection create( PortletRequest request, 89 PortletResponse response ) 90 { 91 PortletWidgetConnection connection = new PortletWidgetConnection(); 92 93 connection.start( request, response ); 94 95 return connection; 96 } 97 98 public static PortletWidgetConnection create( ActionRequest request, 99 ActionResponse response ) 100 { 101 PortletWidgetConnection connection = new PortletWidgetConnection(); 102 103 connection.start( request, response ); 104 105 return connection; 106 } 107 108 public static PortletWidgetConnection create( RenderRequest request, 109 RenderResponse response ) 110 { 111 PortletWidgetConnection connection = new PortletWidgetConnection(); 112 113 connection.start( request, response ); 114 115 return connection; 116 } 117 118 PortletWidgetConnection() 119 { 120 } 121 122 void start( PortletRequest request, PortletResponse response ) 123 { 124 finish(); 125 _portletRequest = request; 126 _portletResponse = response; 127 } 128 129 void start( ActionRequest request, ActionResponse response ) 130 { 131 finish(); 132 _portletRequest = request; 133 _portletResponse = response; 134 _actionRequest = request; 135 _actionResponse = response; 136 } 137 138 void start( RenderRequest request, RenderResponse response ) 139 { 140 finish(); 141 _portletRequest = request; 142 _portletResponse = response; 143 _renderRequest = request; 144 _renderResponse = response; 145 } 146 147 public void finish() 148 { 149 _widgetWriter = null; 150 _portletRequest = null; 151 _portletResponse = null; 152 _actionRequest = null; 153 _actionResponse = null; 154 _renderRequest = null; 155 _renderResponse = null; 156 } 157 158 RenderRequest getRenderRequest() 159 { 160 return _renderRequest; 161 } 162 163 RenderResponse getRenderResponse() 164 { 165 return _renderResponse; 166 } 167 168 public <S extends WidgetState> WidgetURL createURL() 169 throws WidgetException 170 { 171 PortletWidgetURL url = new PortletWidgetURL( this ); 172 173 return url; 174 } 175 176 public String [] getPreferenceValues( String name, String [] defaults ) 177 { 178 return _portletRequest.getPreferences().getValues( name, defaults ); 179 } 180 181 public Object getAttribute( String name ) 182 { 183 return _portletRequest.getAttribute( name ); 184 } 185 186 public void setAttribute( String name, Object object ) 187 { 188 _portletRequest.setAttribute( name, object ); 189 } 190 191 public void removeAttribute( String name ) 192 { 193 _portletRequest.removeAttribute( name ); 194 } 195 196 public Enumeration getAttributeNames() 197 { 198 return _portletRequest.getAttributeNames(); 199 } 200 201 public String getParameter( String name ) 202 { 203 return _portletRequest.getParameter( name ); 204 } 205 206 public String [] getParameterValues( String name ) 207 { 208 return _portletRequest.getParameterValues( name ); 209 } 210 211 public Map getParameterMap() 212 { 213 return _portletRequest.getParameterMap(); 214 } 215 216 public Enumeration getParameterNames() 217 { 218 return _portletRequest.getParameterNames(); 219 } 220 221 public Locale getLocale() 222 { 223 if ( _renderResponse == null ) 224 return _portletRequest.getLocale(); 225 else 226 return _renderResponse.getLocale(); 227 } 228 229 public String getContentType() 230 { 231 String contentType; 232 233 if ( _renderResponse == null ) { 234 contentType = _portletRequest.getResponseContentType(); 235 236 if ( contentType == null ) 237 contentType = "text/html"; 238 } 239 else { 240 contentType = _renderResponse.getContentType(); 241 242 if ( contentType == null ) { 243 contentType = "text/html"; 244 _renderResponse.setContentType( contentType ); 245 } 246 } 247 248 return contentType; 249 } 250 251 public String getRemoteUser() 252 { 253 return _portletRequest.getRemoteUser(); 254 } 255 256 public java.security.Principal getUserPrincipal() 257 { 258 return _portletRequest.getUserPrincipal(); 259 } 260 261 public boolean isUserInRole( String role ) 262 { 263 return _portletRequest.isUserInRole( role ); 264 } 265 266 public boolean isSecure() 267 { 268 return _portletRequest.isSecure(); 269 } 270 271 public String resolveURL( String path ) 272 { 273 return _portletResponse.encodeURL(path); 274 } 275 276 public WidgetWriter getWriter() 277 throws IOException 278 { 279 if ( _widgetWriter == null ) { 280 281 if (_renderResponse == null ) 282 throw new IllegalStateException ( L.l( "Writer for portlets can only be obtained during render phase" ) ); 283 284 _widgetWriter = new WidgetWriter( _renderResponse.getWriter() ); 285 } 286 287 return _widgetWriter; 288 } 289 } 290 | Popular Tags |