1 18 package org.apache.beehive.netui.core.urls; 19 20 import org.apache.beehive.netui.core.urltemplates.URLTemplate; 21 import org.apache.beehive.netui.core.urltemplates.URLTemplateDescriptor; 22 import org.apache.beehive.netui.util.config.bean.UrlConfig; 23 import org.apache.beehive.netui.util.config.ConfigUtil; 24 25 import javax.servlet.ServletRequest ; 26 27 28 32 public class DefaultTemplatedURLFormatter implements TemplatedURLFormatter 33 { 34 private static final String DEFAULT_TEMPLATE_REF = "default-url-templates"; 35 36 49 public String getTemplatedURL( ServletRequest request, MutableURI uri, String key, URIContext uriContext ) 50 { 51 String result = null; 54 String templateName = URLTemplateDescriptor.getInstance().getURLTemplateRef( DEFAULT_TEMPLATE_REF, key ); 55 56 if ( templateName != null ) 57 { 58 URLTemplate template = URLTemplateDescriptor.getInstance().getURLTemplate( templateName ); 59 result = formatURIWithTemplate( request, uri, uriContext, template ); 60 } 61 else 62 { 63 result = uri.getURIString( uriContext ); 65 } 66 67 return result; 68 } 69 70 private String formatURIWithTemplate( ServletRequest request, MutableURI uri, 71 URIContext uriContext, URLTemplate template ) 72 { 73 String scheme = uri.getScheme(); 74 String host = uri.getHost(); 75 int port = uri.getPort(); 76 77 if ( scheme == null || scheme.length() == 0 ) { scheme = request.getScheme(); } 78 79 if ( host == null || host.length() == 0 ) { host = request.getServerName(); } 80 81 if ( port < 0 ) { port = request.getServerPort(); } 82 83 template.substitute( URLTemplateDescriptor.SCHEME_TOKEN, scheme ); 84 template.substitute( URLTemplateDescriptor.DOMAIN_TOKEN, host ); 85 template.substitute( URLTemplateDescriptor.PORT_TOKEN, port ); 86 template.substitute( URLTemplateDescriptor.PATH_TOKEN, uri.getPath() ); 87 88 String query = null; 89 query = uri.getQuery( uriContext ); 90 91 if ( query == null ) { 92 query = ""; 93 } 94 95 template.substitute( URLTemplateDescriptor.QUERY_STRING_TOKEN, query ); 96 97 String fragment = uri.getFragment(); 98 99 if ( fragment == null ) { fragment = ""; } 100 101 template.substitute( URLTemplateDescriptor.FRAGMENT_TOKEN, fragment ); 102 103 return template.toString(); 104 } 105 } 106 | Popular Tags |