1 /*** 2 * 3 */ 4 package net.trajano.twiff.internal.servlet; 5 6 import java.io.Serializable; 7 import javax.servlet.http.HttpServletRequest; 8 import net.trajano.twiff.PageLookup; 9 import net.trajano.twiff.ViewServletConfiguration; 10 import org.apache.commons.lang.StringUtils; 11 12 /*** 13 * This is a simple page lookup implementation. It does not perform any caching. 14 * 15 * @author Archimedes Trajano 16 */ 17 public class ViewPageLookup implements PageLookup { 18 /*** 19 * @param configuration 20 */ 21 public ViewPageLookup(final ViewServletConfiguration configuration) { 22 this.configuration = configuration; 23 } 24 25 /*** 26 * Configuration 27 */ 28 private final ViewServletConfiguration configuration; 29 30 /*** 31 * @see net.trajano.twiff.PageLookup#getPageName(javax.servlet.http.HttpServletRequest) 32 */ 33 public String getPageName(final HttpServletRequest request) { 34 if (StringUtils.isEmpty(request.getPathInfo())) { 35 return request.getServletPath().substring(1, request.getServletPath().lastIndexOf('.')); 36 } else { 37 return request.getPathInfo().substring(1); 38 } 39 } 40 41 /*** 42 * @see net.trajano.twiff.PageLookup#getPageBeanClassName(javax.servlet.http.HttpServletRequest) 43 */ 44 public String getPageBeanClassName(final HttpServletRequest request) { 45 StringBuffer buf = new StringBuffer(configuration.getPackageContext()); 46 String[] components = StringUtils.split(getPageName(request), '/'); 47 for (int i = 0; i < components.length - 1; ++i) { 48 buf.append('.'); 49 buf.append(components[i].toLowerCase()); 50 } 51 buf.append('.'); 52 buf.append(Character.toUpperCase(components[components.length - 1].charAt(0))); 53 buf.append(components[components.length - 1].substring(1)); 54 return buf.toString(); 55 } 56 57 /*** 58 * @see net.trajano.twiff.PageLookup#getPageBeanClass(javax.servlet.http.HttpServletRequest) 59 */ 60 public Class<? extends Serializable> getPageBeanClass(final HttpServletRequest request) throws ClassNotFoundException { 61 return (Class<? extends Serializable>) Class.forName(getPageBeanClassName(request)); 62 } 63 }