1 20 21 package com.methodhead.shim; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import java.util.Map ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.HashMap ; 30 import com.methodhead.sitecontext.SiteContext; 31 import org.apache.struts.util.MessageResources; 32 import com.methodhead.tree.Tree; 33 import org.apache.struts.Globals; 34 import org.apache.struts.action.ActionMapping; 35 import org.apache.struts.action.DynaActionForm; 36 import org.apache.struts.util.LabelValueBean; 37 import com.methodhead.tree.FoldingTreeNode; 38 import com.methodhead.auth.AuthUser; 39 import com.methodhead.auth.AuthUtil; 40 import com.methodhead.util.OperationContext; 41 import com.methodhead.util.StrutsUtil; 42 import org.apache.commons.lang.StringUtils; 43 import java.util.regex.Pattern ; 44 import java.util.regex.Matcher ; 45 46 49 public class ShimUtils { 50 51 53 55 57 59 65 public static void setSiteMap( 66 HttpServletRequest request, 67 SiteMap siteMap ) { 68 69 SiteContext siteContext = siteMap.getSiteContext(); 70 71 Map siteMapMap = 72 ( Map )request.getSession().getServletContext().getAttribute( 73 ShimGlobals.SITEMAPMAP_KEY ); 74 75 if ( siteMapMap == null ) { 79 siteMapMap = new HashMap (); 80 request.getSession().getServletContext().setAttribute( 81 ShimGlobals.SITEMAPMAP_KEY, siteMapMap ); 82 } 83 84 siteMapMap.put( siteContext.get( "id" ), siteMap ); 85 } 86 87 91 public static SiteMap getSiteMap( 92 HttpServletRequest request ) { 93 94 SiteContext siteContext = SiteContext.getContext( request ); 95 96 if ( siteContext == null ) 97 throw new ShimException( "Couldn't get site context." ); 98 99 Map siteMapMap = 103 ( Map )request.getSession().getServletContext().getAttribute( 104 ShimGlobals.SITEMAPMAP_KEY ); 105 106 if ( siteMapMap == null ) { 107 siteMapMap = new HashMap (); 108 request.getSession().getServletContext().setAttribute( 109 ShimGlobals.SITEMAPMAP_KEY, siteMapMap ); 110 } 111 112 SiteMap siteMap = ( SiteMap )siteMapMap.get( siteContext.get( "id" ) ); 116 117 if ( siteMap == null ) { 118 siteMap = new SiteMap(); 119 siteMap.setSiteContext( siteContext ); 120 siteMap.load(); 121 siteMapMap.put( siteContext.get( "id" ), siteMap ); 122 } 123 124 return siteMap; 125 } 126 127 130 public static SiteMapTree getSiteMapTree( 131 HttpServletRequest request ) { 132 133 return ( SiteMapTree )request.getSession().getAttribute( 134 ShimGlobals.SITEMAPTREE_KEY ); 135 } 136 137 140 169 170 174 244 245 249 public static List getModules( 250 SiteContext siteContext ) { 251 252 List modules = new ArrayList (); 253 modules.add( new TextModule() ); 254 modules.add( new NavModule() ); 255 modules.add( new IncludeModule() ); 256 257 return modules; 258 } 259 260 264 public static String getLinkUrl( 265 Link link ) { 266 267 return link.getAlias() + ".shtml"; 268 } 269 270 277 public static String getLinkUrl( 278 Link link, 279 String path ) { 280 281 287 return link.getAlias() + ".shtml"; 288 } 289 290 293 public static ShimPolicy getPolicy( 294 ActionMapping mapping ) { 295 296 try { 297 return 298 ( ShimPolicy )Class.forName( mapping.getParameter() ).newInstance(); 299 } 300 catch ( Exception e ) { 301 throw new ShimException( 302 "Unexpected exception while instantiating \"" + 303 mapping.getParameter() +"\":" + e.toString() ); 304 } 305 } 306 307 311 public static List getHtmlFragmentOptions( 312 HttpServletRequest request ) { 313 314 HtmlFragment fragment = new HtmlFragment(); 315 fragment.setSiteContext( SiteContext.getContext( request ) ); 316 List list = fragment.loadAll(); 317 318 List options = new ArrayList (); 322 for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 323 HtmlFragment f = ( HtmlFragment )iter.next(); 324 options.add( 325 new LabelValueBean( f.getString( "name" ), "" + f.getInt( "id" ) ) ); 326 } 327 328 return options; 329 } 330 331 335 public static void setUpShimSession( 336 HttpServletRequest request, 337 SiteContext context ) { 338 339 SiteContext.setContext( request, context ); 340 341 request.setAttribute( SiteContext.SITECONTEXT_KEY, context ); 345 346 SiteMapTree tree = new SiteMapTree(); 347 tree.build( ShimUtils.getSiteMap( request ) ); 348 request.getSession().setAttribute( 349 ShimGlobals.SITEMAPTREE_KEY, tree ); 350 } 351 352 355 public static void tearDownShimSession( 356 HttpServletRequest request ) { 357 358 request.getSession().removeAttribute( SiteContext.SITECONTEXT_KEY ); 359 request.getSession().removeAttribute( ShimGlobals.MODE_KEY ); 360 request.getSession().removeAttribute( ShimGlobals.SITEMAPTREE_KEY ); 361 } 362 363 368 public static void setUpMenu( 369 HttpServletRequest request, 370 HttpServletResponse response ) { 371 372 ActionMapping mapping = 373 ( ActionMapping )request.getAttribute( Globals.MAPPING_KEY ); 374 375 DynaActionForm form = 376 ( DynaActionForm )request.getAttribute( mapping.getName() ); 377 378 AuthUser user = AuthUtil.getUser( request ); 379 380 OperationContext op = 381 new OperationContext( mapping, form, request, response, user ); 382 383 ShimPolicy policy = ( ShimPolicy )StrutsUtil.getPolicy( mapping ); 384 385 policy.setUpMenu( op ); 386 } 387 388 391 public static boolean displayEditElements( 392 HttpServletRequest request ) { 393 return request.getSession().getAttribute( SiteContext.SITECONTEXT_KEY ) != null; 394 } 395 396 403 public static String fileNameToLabel( 404 String fileName ) { 405 406 if ( null == fileName ) { 407 return "<null>"; 408 } 409 410 if ( StringUtils.isBlank( fileName ) ) { 411 return ""; 412 } 413 414 fileName = StringUtils.substringBefore( fileName, "." ); 418 419 fileName = StringUtils.capitalize( fileName ); 423 424 Pattern pattern = Pattern.compile( "([A-Z][^A-Z]*)" ); 428 Matcher matcher = pattern.matcher( fileName ); 429 430 StringBuffer buf = new StringBuffer (); 431 432 while ( matcher.find() ) { 433 buf.append( StringUtils.strip( matcher.group( 1 ) ) ); 434 buf.append( " " ); 435 } 436 437 return StringUtils.strip( buf.toString() ); 438 } 439 440 442 } 444 | Popular Tags |