1 16 package org.apache.myfaces.application.jsp; 17 18 import org.apache.myfaces.webapp.webxml.ServletMapping; 19 import org.apache.myfaces.webapp.webxml.WebXml; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.struts.tiles.*; 24 25 import javax.faces.FacesException; 26 import javax.faces.application.ViewHandler; 27 import javax.faces.component.UIViewRoot; 28 import javax.faces.context.ExternalContext; 29 import javax.faces.context.FacesContext; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletRequest ; 32 import javax.servlet.ServletResponse ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.servlet.http.HttpSession ; 36 import java.io.IOException ; 37 import java.util.List ; 38 import java.util.Locale ; 39 40 81 public class JspTilesViewHandlerImpl 82 extends ViewHandler 83 { 84 private ViewHandler _viewHandler; 85 86 private static final Log log = LogFactory.getLog(JspTilesViewHandlerImpl.class); 87 private static final String TILES_EXTENSION = ".tiles"; 88 private static final String TILES_DEF_ATTR = "tiles-definitions"; 89 90 private DefinitionsFactory _definitionsFactory; 91 92 public JspTilesViewHandlerImpl(ViewHandler viewHandler) 93 { 94 _viewHandler = viewHandler; 95 } 96 97 private DefinitionsFactory getDefinitionsFactory() 98 { 99 if (_definitionsFactory == null) 100 { 101 if (log.isDebugEnabled()) log.debug("JspTilesViewHandlerImpl init"); 102 ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); 103 String tilesDefinitions = context.getInitParameter(TILES_DEF_ATTR); 104 if (tilesDefinitions == null) 105 { 106 log.fatal("No Tiles definition found. Specify Definition files by adding " 107 + TILES_DEF_ATTR + " param in your web.xml"); 108 return null; 109 } 110 111 DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig(); 112 factoryConfig.setDefinitionConfigFiles(tilesDefinitions); 113 try 114 { 115 if (log.isDebugEnabled()) log.debug("Reading tiles definitions"); 116 _definitionsFactory = TilesUtil.createDefinitionsFactory((ServletContext )context.getContext(), 117 factoryConfig); 118 } 119 catch (DefinitionsFactoryException e) 120 { 121 log.fatal("Error reading tiles definitions", e); 122 return null; 123 } 124 } 125 return _definitionsFactory; 126 } 127 128 public void renderView(FacesContext facesContext, UIViewRoot viewToRender) throws IOException , FacesException 129 { 130 if (viewToRender == null) 131 { 132 log.fatal("viewToRender must not be null"); 133 throw new NullPointerException ("viewToRender must not be null"); 134 } 135 136 ExternalContext externalContext = facesContext.getExternalContext(); 137 138 String viewId = facesContext.getViewRoot().getViewId(); 139 ServletMapping servletMapping = getServletMapping(externalContext); 140 141 String defaultSuffix = externalContext.getInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME); 142 String suffix = defaultSuffix != null ? defaultSuffix : ViewHandler.DEFAULT_SUFFIX; 143 if (servletMapping.isExtensionMapping()) 144 { 145 if (!viewId.endsWith(suffix)) 146 { 147 int dot = viewId.lastIndexOf('.'); 148 if (dot == -1) 149 { 150 if (log.isTraceEnabled()) log.trace("Current viewId has no extension, appending default suffix " + suffix); 151 viewId = viewId + suffix; 152 } 153 else 154 { 155 if (log.isTraceEnabled()) log.trace("Replacing extension of current viewId by suffix " + suffix); 156 viewId = viewId.substring(0, dot) + suffix; 157 } 158 facesContext.getViewRoot().setViewId(viewId); 159 } 160 } 161 else if (!viewId.endsWith(suffix)) 162 { 163 dispatch(externalContext, viewToRender, viewId); 165 return; 166 } 167 168 String tilesId = viewId; 169 int idx = tilesId.lastIndexOf('.'); 170 if (idx > 0) 171 { 172 tilesId = tilesId.substring(0, idx) + TILES_EXTENSION; 173 } 174 else 175 { 176 tilesId = tilesId + TILES_EXTENSION; 177 } 178 ServletRequest request = (ServletRequest )externalContext.getRequest(); 179 ServletContext servletContext = (ServletContext )externalContext.getContext(); 180 181 ComponentDefinition definition = null; 182 try 183 { 184 definition = getDefinitionsFactory().getDefinition(tilesId, request, servletContext); 185 if (definition != null) 186 { 187 ComponentContext tileContext = ComponentContext.getContext(request); 189 if (tileContext == null) 190 { 191 tileContext = new ComponentContext(definition.getAttributes()); 192 ComponentContext.setContext(tileContext, request); 193 } 194 else 195 { 196 tileContext.addMissing(definition.getAttributes()); 197 } 198 viewId = definition.getPage(); 199 } 200 } 201 catch (DefinitionsFactoryException e) 202 { 203 throw new FacesException(e); 204 } 205 206 dispatch(externalContext, viewToRender, viewId); 207 } 208 209 private void dispatch(ExternalContext externalContext, UIViewRoot viewToRender, String viewId) 210 throws IOException 211 { 212 if (log.isTraceEnabled()) log.trace("Dispatching to " + viewId); 213 214 if (externalContext.getResponse() instanceof ServletResponse ) { 216 ServletResponse response = (ServletResponse ) externalContext.getResponse(); 217 response.setLocale(viewToRender.getLocale()); 218 } 219 220 externalContext.dispatch(viewId); 221 222 if (externalContext.getRequest() instanceof HttpServletRequest ) { 224 HttpServletResponse response = (HttpServletResponse ) externalContext.getResponse(); 225 HttpServletRequest request = (HttpServletRequest ) externalContext.getRequest(); 226 HttpSession session = request.getSession(false); 227 228 if (session != null) { 229 session.setAttribute(ViewHandler.CHARACTER_ENCODING_KEY, response.getCharacterEncoding()); 230 } 231 } 232 233 234 } 235 236 private static ServletMapping getServletMapping(ExternalContext externalContext) 237 { 238 String servletPath = externalContext.getRequestServletPath(); 239 String requestPathInfo = externalContext.getRequestPathInfo(); 240 241 WebXml webxml = WebXml.getWebXml(externalContext); 242 List mappings = webxml.getFacesServletMappings(); 243 244 boolean isExtensionMapping = requestPathInfo == null; 245 246 for (int i = 0, size = mappings.size(); i < size; i++) 247 { 248 ServletMapping servletMapping = (ServletMapping) mappings.get(i); 249 if (servletMapping.isExtensionMapping() == isExtensionMapping) 250 { 251 String urlpattern = servletMapping.getUrlPattern(); 252 if (isExtensionMapping) 253 { 254 String extension = urlpattern.substring(1, urlpattern.length()); 255 if (servletPath.endsWith(extension)) 256 { 257 return servletMapping; 258 } 259 } 260 else 261 { 262 urlpattern = urlpattern.substring(0, urlpattern.length() - 2); 263 if (servletPath.equals(urlpattern)) 267 { 268 return servletMapping; 269 } 270 } 271 } 272 } 273 log.error("could not find pathMapping for servletPath = " + servletPath + 274 " requestPathInfo = " + requestPathInfo); 275 throw new IllegalArgumentException ("could not find pathMapping for servletPath = " + servletPath + 276 " requestPathInfo = " + requestPathInfo); 277 } 278 279 280 public Locale calculateLocale(FacesContext context) 281 { 282 return _viewHandler.calculateLocale(context); 283 } 284 285 public String calculateRenderKitId(FacesContext context) 286 { 287 return _viewHandler.calculateRenderKitId(context); 288 } 289 290 public UIViewRoot createView(FacesContext context, String viewId) 291 { 292 return _viewHandler.createView(context, viewId); 293 } 294 295 public String getActionURL(FacesContext context, String viewId) 296 { 297 return _viewHandler.getActionURL(context, viewId); 298 } 299 300 public String getResourceURL(FacesContext context, String path) 301 { 302 return _viewHandler.getResourceURL(context, path); 303 } 304 305 public UIViewRoot restoreView(FacesContext context, String viewId) 306 { 307 return _viewHandler.restoreView(context, viewId); 308 } 309 310 public void writeState(FacesContext context) throws IOException 311 { 312 _viewHandler.writeState(context); 313 } 314 315 } 316 | Popular Tags |