1 18 package org.apache.beehive.netui.core.urls; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 23 import org.apache.beehive.netui.util.logging.Logger; 24 25 import java.io.PrintStream ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.Iterator ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletRequest ; 32 import javax.servlet.ServletResponse ; 33 34 35 53 public class URLRewriterService 54 { 55 private static final Logger _log = Logger.getInstance( URLRewriterService.class ); 56 57 private static final String URL_REWRITERS_KEY = "url_rewriters"; 58 private static final String TEMPLATTED_URL_FORMATTER_KEY = "templated_url_formatter"; 59 private static final DefaultTemplatedURLFormatter defaultFormatter = new DefaultTemplatedURLFormatter(); 60 61 70 public static String getNamePrefix( ServletContext servletContext, ServletRequest request, String name ) 71 { 72 ArrayList rewriters = getRewriters( request ); 73 74 InternalStringBuilder prefix = new InternalStringBuilder(); 75 76 if ( rewriters != null ) 77 { 78 for ( Iterator i = rewriters.iterator(); i.hasNext(); ) 79 { 80 URLRewriter rewriter = ( URLRewriter ) i.next(); 81 String nextPrefix = rewriter.getNamePrefix( servletContext, request, name ); 82 if ( nextPrefix != null ) { prefix.append( nextPrefix ); } 83 } 84 } 85 86 return prefix.toString(); 87 } 88 89 113 public static void rewriteURL( ServletContext servletContext, ServletRequest request, 114 ServletResponse response, MutableURI url, URLType type, 115 boolean needsToBeSecure ) 116 { 117 ArrayList rewriters = getRewriters( request ); 118 119 if ( rewriters != null ) 120 { 121 for ( Iterator i = rewriters.iterator(); i.hasNext(); ) 122 { 123 URLRewriter rewriter = ( URLRewriter ) i.next(); 124 rewriter.rewriteURL( servletContext, request, response, url, type, needsToBeSecure ); 125 } 126 } 127 128 if ( url instanceof FreezableMutableURI ) 129 { 130 ( ( FreezableMutableURI ) url ).setFrozen( true ); 131 } 132 } 133 134 141 public static List getURLRewriters( ServletRequest request ) 142 { 143 return Collections.unmodifiableList( getRewriters( request ) ); 144 } 145 146 157 public static boolean registerURLRewriter( ServletRequest request, URLRewriter rewriter ) 158 { 159 ArrayList rewriters = getRewriters( request ); 160 161 if ( rewriters == null ) 162 { 163 rewriters = new ArrayList (); 164 rewriters.add( rewriter ); 165 request.setAttribute( URL_REWRITERS_KEY, rewriters ); 166 } 167 else 168 { 169 return addRewriter( rewriters, rewriter, rewriters.size() ); 170 } 171 172 return true; 173 } 174 175 188 public static boolean registerURLRewriter( int index, ServletRequest request, URLRewriter rewriter ) 189 { 190 ArrayList rewriters = getRewriters( request ); 191 192 if ( rewriters == null ) 193 { 194 rewriters = new ArrayList (); 195 rewriters.add( rewriter ); 196 request.setAttribute( URL_REWRITERS_KEY, rewriters ); 197 } 198 else 199 { 200 return addRewriter( rewriters, rewriter, index ); 201 } 202 203 return true; 204 } 205 206 private static ArrayList getRewriters( ServletRequest request ) 207 { 208 return ( ArrayList ) ScopedServletUtils.getScopedRequestAttribute( URL_REWRITERS_KEY, request ); 209 } 210 211 private static boolean addRewriter( ArrayList rewriters, URLRewriter rewriter, int index ) 212 { 213 if ( otherRewritersAllowed( rewriters ) ) 214 { 215 if ( !rewriters.contains( rewriter ) ) 216 { 217 if ( !rewriter.allowOtherRewriters() ) 218 { 219 rewriters.clear(); 220 221 if ( rewriters.size() > 0 && _log.isInfoEnabled() ) 222 { 223 InternalStringBuilder message = new InternalStringBuilder(); 224 message.append( "Register exclusive URLRewriter, \""); 225 message.append( rewriter.getClass().getName() ); 226 message.append( "\". This removes any other URLRewriter objects already registered in the chain." ); 227 _log.info( message.toString() ); 228 } 229 } 230 rewriters.add( index, rewriter ); 231 } 232 } 233 else 234 { 235 if ( _log.isInfoEnabled() ) 236 { 237 InternalStringBuilder message = new InternalStringBuilder(); 238 message.append( "Cannot register URLRewriter, \""); 239 message.append( rewriter.getClass().getName() ); 240 message.append( "\". The URLRewriter, \"" ); 241 message.append( rewriters.get( 0 ).getClass().getName() ); 242 message.append( "\", is already registered and does not allow other rewriters." ); 243 _log.info( message.toString() ); 244 } 245 246 return false; 247 } 248 249 return true; 250 } 251 252 private static boolean otherRewritersAllowed( ArrayList rewriters ) 253 { 254 if ( rewriters != null && rewriters.size() == 1 && 255 ! ( ( URLRewriter ) rewriters.get( 0 ) ).allowOtherRewriters() ) 256 { 257 return false; 258 } 259 260 return true; 261 } 262 263 270 public static void unregisterURLRewriter( ServletRequest request, URLRewriter rewriter ) 271 { 272 if ( rewriter == null ) { return; } 273 274 ArrayList rewriters = getRewriters( request ); 275 276 if ( rewriters == null ) 277 { 278 return; 279 } 280 else 281 { 282 rewriters.remove( rewriter ); 283 284 if ( rewriters.size() == 0 ) 285 { 286 request.removeAttribute( URL_REWRITERS_KEY ); 287 } 288 } 289 } 290 291 296 public static void unregisterAllURLRewriters( ServletRequest request ) 297 { 298 request.removeAttribute( URL_REWRITERS_KEY ); 299 } 300 301 306 public static boolean allowParamsOnFormAction( ServletContext servletContext, ServletRequest request ) 307 { 308 ArrayList rewriters = getRewriters( request ); 309 310 if ( rewriters != null ) 311 { 312 for ( Iterator i = rewriters.iterator(); i.hasNext(); ) 313 { 314 URLRewriter rewriter = ( URLRewriter ) i.next(); 315 if ( !rewriter.allowParamsOnFormAction( servletContext, request ) ) { return false; } 316 } 317 } 318 319 return true; 320 } 321 322 329 public static void dumpURLRewriters( ServletRequest request, PrintStream output ) 330 { 331 ArrayList rewriters = getRewriters( request ); 332 333 if ( output == null ) output = System.err; 334 output.println( "*** List of URLRewriter objects: " + rewriters ); 335 336 if ( rewriters != null ) 337 { 338 int count = 0; 339 for ( Iterator i = rewriters.iterator(); i.hasNext(); ) 340 { 341 URLRewriter rewriter = ( URLRewriter ) i.next(); 342 output.println( " " + count++ + ". " + rewriter.getClass().getName() ); 343 output.println( " allows other rewriters: " + rewriter.allowOtherRewriters() ); 344 output.println( " rewriter: " + rewriter ); 345 } 346 } 347 else 348 { 349 output.println( " No URLRewriter objects are registered with this request." ); 350 } 351 } 352 353 366 public static String getTemplatedURL( ServletRequest request, MutableURI uri, String key, URIContext uriContext ) 367 { 368 TemplatedURLFormatter formatter = getTemplatedURLFormatter( request ); 369 370 if ( formatter != null ) 371 { 372 return formatter.getTemplatedURL( request, uri, key, uriContext ); 373 } 374 375 return defaultFormatter.getTemplatedURL( request, uri, key, uriContext ); 376 } 377 378 private static TemplatedURLFormatter getTemplatedURLFormatter( ServletRequest request ) 379 { 380 return ( TemplatedURLFormatter ) ScopedServletUtils.getScopedRequestAttribute( TEMPLATTED_URL_FORMATTER_KEY, request ); 381 } 382 383 394 public static void registerTemplatedURLFormatter( ServletRequest request, TemplatedURLFormatter formatter ) 395 { 396 request.setAttribute( TEMPLATTED_URL_FORMATTER_KEY, formatter ); 397 } 398 399 404 public static void unregisterTemplatedURLFormatter( ServletRequest request ) 405 { 406 request.removeAttribute( TEMPLATTED_URL_FORMATTER_KEY ); 407 } 408 } 409 | Popular Tags |