1 package org.apache.turbine.util; 2 3 18 19 import java.util.StringTokenizer ; 20 import javax.servlet.ServletConfig ; 21 import javax.servlet.ServletContext ; 22 import javax.servlet.http.HttpServletRequest ; 23 24 import org.apache.commons.lang.StringUtils; 25 26 import org.apache.turbine.Turbine; 27 import org.apache.turbine.util.uri.URIConstants; 28 29 36 public class ServletUtils 37 { 38 42 public static final int HTTP_PORT = URIConstants.HTTP_PORT; 43 44 48 public static final int HTTPS_PORT = URIConstants.HTTPS_PORT; 49 50 54 public static final int FTP_PORT = URIConstants.FTP_PORT; 55 56 61 public static final String URI_SCHEME_SEPARATOR = URIConstants.URI_SCHEME_SEPARATOR; 62 63 73 public static String expandRelative(ServletConfig config, 74 String text) 75 { 76 if (StringUtils.isEmpty(text)) 77 { 78 return text; 79 } 80 81 if (config == null) 82 { 83 return null; 84 } 85 86 if (!text.startsWith("/") && !text.startsWith("./") 88 && !text.startsWith("\\") && !text.startsWith(".\\")) 89 { 90 StringBuffer sb = new StringBuffer (); 91 sb.append("./"); 92 sb.append(text); 93 text = sb.toString(); 94 } 95 96 ServletContext context = config.getServletContext(); 97 String base = context.getRealPath("/"); 98 99 base = (StringUtils.isEmpty(base)) 100 ? config.getInitParameter(Turbine.BASEDIR_KEY) 101 : base; 102 103 if (StringUtils.isEmpty(base)) 104 { 105 return text; 106 } 107 108 String separator = System.getProperty("path.separator"); 109 110 StringTokenizer tokenizer = new StringTokenizer (text, 111 separator); 112 StringBuffer buffer = new StringBuffer (); 113 while (tokenizer.hasMoreTokens()) 114 { 115 buffer.append(base).append(tokenizer.nextToken()); 116 if (tokenizer.hasMoreTokens()) 117 { 118 buffer.append(separator); 119 } 120 } 121 return buffer.toString(); 122 } 123 124 130 public static StringBuffer hostURL(HttpServletRequest req) 131 { 132 return hostURL(req, null); 133 } 134 135 146 public static StringBuffer hostURL(HttpServletRequest req, String scheme) 147 { 148 ServerData serverData = new ServerData(req); 149 150 if (StringUtils.isNotEmpty(scheme)) 151 { 152 serverData.setServerScheme(scheme); 153 } 154 155 StringBuffer sb = new StringBuffer (); 156 157 serverData.getHostUrl(sb); 158 159 return sb; 160 } 161 } 162 | Popular Tags |