1 44 package org.jpublish.util; 45 46 import java.net.URLEncoder ; 47 48 import javax.servlet.http.HttpServletRequest ; 49 import javax.servlet.http.HttpServletResponse ; 50 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 54 60 public class URLUtilities { 61 62 65 public final static String URL_PATH_SEPARATOR = "/"; 66 67 private static Log log = LogFactory.getLog(URLUtilities.class); 68 69 private HttpServletRequest request; 70 private HttpServletResponse response; 71 72 78 public URLUtilities(HttpServletRequest request, 79 HttpServletResponse response) { 80 this.request = request; 81 this.response = response; 82 } 83 84 90 public String buildStandard(String path) { 91 return buildStandard(path, 0); 92 } 93 94 100 public String standard(String path) { 101 return buildStandard(path, 0); 102 } 103 104 113 public String buildStandard(String path, int port) { 114 return build(path, "http", port); 115 } 116 117 126 public String standard(String path, int port) { 127 return build(path, "http", port); 128 } 129 130 136 public String buildSecure(String path) { 137 return buildSecure(path, 0); 138 } 139 140 146 public String secure(String path) { 147 return buildSecure(path, 0); 148 } 149 150 159 public String buildSecure(String path, int port) { 160 return build(path, "https", port); 161 } 162 163 172 public String secure(String path, int port) { 173 return build(path, "https", port); 174 } 175 176 184 protected String build(String path, String protocol, int port) { 185 String serverName = request.getServerName(); 186 String contextPath = request.getContextPath(); 187 188 if (log.isDebugEnabled()) { 189 log.debug("Server name: " + serverName); 190 log.debug("Context path: " + contextPath); 191 } 192 193 if (!contextPath.endsWith(URL_PATH_SEPARATOR)) { 194 contextPath = contextPath + URL_PATH_SEPARATOR; 195 } 196 197 if (path.startsWith(URL_PATH_SEPARATOR)) { 198 path = path.substring(1); 199 } 200 201 String requestPath = contextPath + path; 202 if (log.isDebugEnabled()) { 203 log.debug("Request path: " + requestPath); 204 } 205 206 StringBuffer buffer = new StringBuffer (); 207 buffer.append(protocol); 208 buffer.append("://"); 209 buffer.append(serverName); 210 211 if (port > 0) { 212 buffer.append(":"); 213 buffer.append(port); 214 } 215 216 if (!requestPath.startsWith(URL_PATH_SEPARATOR)) { 217 buffer.append(URL_PATH_SEPARATOR); 218 } 219 220 buffer.append(requestPath); 221 222 if (log.isDebugEnabled()) { 223 log.debug("URL: '" + buffer + "'"); 224 } 225 226 return response.encodeURL(buffer.toString()); 227 } 228 229 236 public String encode(String s) { 237 return URLEncoder.encode(s); 238 } 239 240 } 241 242 | Popular Tags |