1 18 19 package org.apache.struts.webapp.tiles.skin; 20 21 import javax.servlet.RequestDispatcher ; 22 import javax.servlet.ServletContext ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 import org.apache.struts.action.ActionForm; 28 import org.apache.struts.action.ActionForward; 29 import org.apache.struts.action.ActionMapping; 30 import org.apache.struts.tiles.ComponentContext; 31 import org.apache.struts.tiles.DefinitionsFactoryException; 32 import org.apache.struts.tiles.actions.TilesAction; 33 34 37 public class SimpleSwitchLayoutAction extends TilesAction 38 { 39 40 public static boolean debug = true; 41 42 public static final String LAYOUT_ATTRIBUTE = "layout.attribute"; 43 44 public static String USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName"; 45 46 public static String DEFAULT_USER_SETTINGS_NAME = "examples.tiles.skin.SELECTED_DEFINITION"; 47 48 49 public static final String CATALOG_NAME = "examples.tiles.skin.CATALOG_NAME"; 50 51 52 public static String DEFAULT_CATALOG_NAME = "tiles.examples.skin.layoutCatalog"; 53 54 public static String CATALOG_NAME_ATTRIBUTE = "catalogName"; 55 56 public static final String CATALOG_SETTING_ATTRIBUTE = "catalogSettings"; 57 58 75 public ActionForward execute( 76 ComponentContext context, 77 ActionMapping mapping, 78 ActionForm form, 79 HttpServletRequest request, 80 HttpServletResponse response) 81 throws Exception 82 { 83 if(debug) 84 System.out.println( "Enter SimpleSwitchLayoutAction" ); 85 86 String layoutDir = "/layouts/"; 87 String userSelection = getUserSetting( context, request ); 88 String layout = (String )context.getAttribute( LAYOUT_ATTRIBUTE ); 90 if(layout==null) 91 throw new ServletException ( "Attribute '" + LAYOUT_ATTRIBUTE + "' is required." ); 92 93 String layoutPath = layoutDir+userSelection+ "/" + layout; 94 95 RequestDispatcher rd = getServlet().getServletContext().getRequestDispatcher( layoutPath ); 96 if(rd==null) 97 { 98 layoutPath = layoutDir + layout; 99 rd = getServlet().getServletContext().getRequestDispatcher( layoutPath ); 100 if(rd==null) 101 throw new ServletException ( "SwitchLayout error : Can't find layout '" 102 + layoutPath + "'." ); 103 } 104 rd.include(request, response); 105 if(debug) 106 System.out.println( "Exit SimpleSwitchLayoutAction" ); 107 return null; 108 } 109 110 115 public static String getUserSetting( ComponentContext context, HttpServletRequest request ) 116 { 117 HttpSession session = request.getSession( false ); 118 if( session == null ) 119 return null; 120 121 String userSettingsName = (String )context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE ); 123 if( userSettingsName == null ) 124 userSettingsName = DEFAULT_USER_SETTINGS_NAME; 125 126 return (String )session.getAttribute(userSettingsName); 127 } 128 129 134 public static void setUserSetting( ComponentContext context, HttpServletRequest request, String setting ) 135 { 136 HttpSession session = request.getSession(); 137 138 String userSettingsName = (String )context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE ); 140 if( userSettingsName == null ) 141 userSettingsName = DEFAULT_USER_SETTINGS_NAME; 142 143 session.setAttribute(userSettingsName, setting); 144 } 145 146 149 public static DefinitionCatalog getCatalog( ComponentContext context, HttpServletRequest request, ServletContext servletContext ) 150 throws ServletException 151 { 152 String catalogName = (String )context.getAttribute( CATALOG_NAME_ATTRIBUTE ); 155 if(catalogName == null) 156 catalogName = DEFAULT_CATALOG_NAME; 157 158 if(debug) 159 System.out.println( "Catalog name=" + catalogName ); 160 try 161 { 162 DefinitionCatalog catalog = (DefinitionCatalog)servletContext.getAttribute( catalogName ); 163 if(catalog == null) 164 { if(debug) 166 System.out.println( "Create catalog" ); 167 String catalogSettings = (String )context.getAttribute( CATALOG_SETTING_ATTRIBUTE ); 168 if(catalogSettings == null) 169 throw new ServletException ( "Error - CustomSkinAction : attribute '" 170 + CATALOG_SETTING_ATTRIBUTE 171 + "' not found in Tile's attributes. Need it to initialize catalog" ); 172 catalog = new DefinitionCatalog( catalogSettings, request, servletContext ); 173 if(debug) 174 System.out.println( "Catalog created" ); 175 servletContext.setAttribute( catalogName, catalog ); 176 } return catalog; 178 } 179 catch(DefinitionsFactoryException ex ) 180 { 181 if(debug) 182 System.out.println( "Exception : " + ex.getMessage() ); 183 throw new ServletException ( ex.getMessage() ); 184 } 185 } 186 } 187 | Popular Tags |