1 7 package com.inversoft.junit; 8 9 import java.net.InetAddress ; 10 import java.net.UnknownHostException ; 11 import javax.servlet.http.HttpServletRequest ; 12 13 14 20 public class URL { 21 22 25 protected String contextPath; 26 27 30 protected String pathInfo; 31 32 35 protected String protocol; 36 37 40 protected String queryString; 41 42 45 protected String serverName; 46 47 50 protected int serverPort; 51 52 55 protected String servletPath; 56 57 58 61 public URL() 62 { 63 } 64 65 73 public URL(java.net.URL url, String context, String servlet) { 74 75 assert (context != null) : "context == null"; 76 assert (context.charAt(0) != '/') : "context starts with /"; 77 78 pathInfo = url.getPath(); 79 protocol = url.getProtocol(); 80 queryString = url.getQuery(); 81 serverName = url.getHost(); 82 serverPort = url.getPort(); 83 contextPath = context; 84 servletPath = servlet; 85 86 if (contextPath != null) { 88 pathInfo = pathInfo.substring(0, contextPath.length()); 89 } 90 91 if (servletPath != null) { 92 pathInfo = pathInfo.substring(0, servletPath.length()); 93 } 94 } 95 96 108 public URL(String context, String pathInfo, String protocol, 109 String queryString, String serverName, String servletPath) { 110 111 this.contextPath = context; 112 this.pathInfo = pathInfo; 113 this.protocol = protocol; 114 this.queryString = queryString; 115 this.serverName = serverName; 116 this.servletPath = servletPath; 117 118 if (serverName == null) { 119 try { 120 serverName = InetAddress.getLocalHost().getHostName(); 121 } catch (UnknownHostException e) { 122 throw new RuntimeException (e); 123 } 124 } 125 int pos = serverName.indexOf(":"); 127 if (pos == -1) { 128 this.serverPort = -1; 129 } else { 130 try { 131 this.serverPort = Integer.parseInt(serverName.substring(pos + 1)); 132 this.serverName = serverName.substring(0, pos); 133 } catch (NumberFormatException e) { 134 throw new IllegalArgumentException ("Invalid port number: " + serverPort); 135 } 136 } 137 } 138 139 152 public URL(HttpServletRequest request, String context, String pathInfo, 153 String protocol, String queryString, String serverName, 154 String servletPath) { 155 156 this.contextPath = context; 157 this.pathInfo = pathInfo; 158 this.protocol = protocol; 159 this.queryString = queryString; 160 this.servletPath = servletPath; 161 162 if (serverName == null) { 163 this.serverName = request.getServerName(); 164 this.serverPort = request.getServerPort(); 165 } else { 166 int pos = serverName.indexOf(":"); 168 if (pos == -1) { 169 serverPort = -1; 170 } else { 171 try { 172 serverPort = Integer.parseInt(serverName.substring(pos + 1)); 173 this.serverName = serverName.substring(0, pos); 174 } catch (NumberFormatException e) { 175 throw new IllegalArgumentException ("Invalid port number: " + serverPort); 176 } 177 } 178 } 179 180 if (context == null) { 181 this.contextPath = request.getContextPath(); 182 } 183 184 if (pathInfo == null) { 185 this.pathInfo = request.getPathInfo(); 186 } 187 188 if (protocol == null) { 189 this.protocol = request.getProtocol(); 190 } 191 192 if (queryString == null) { 193 this.queryString = request.getQueryString(); 194 } 195 } 196 197 198 203 public String getContextPath() { 204 if (contextPath == null) { 205 return ""; 206 } 207 208 return "/" + contextPath; 209 } 210 211 216 public void setContextPath(String path) 217 { 218 contextPath = path; 219 } 220 221 226 public String getPathInfo() 227 { 228 return pathInfo; 229 } 230 231 236 public void setPathInfo(String info) 237 { 238 pathInfo = info; 239 } 240 241 246 public String getProtocol() 247 { 248 return protocol; 249 } 250 251 256 public void setProtocol(String protocol) 257 { 258 this.protocol = protocol; 259 } 260 261 266 public String getQueryString() 267 { 268 return queryString; 269 } 270 271 276 public void setQueryString(String query) 277 { 278 queryString = query; 279 } 280 281 286 public String getServerName() 287 { 288 return serverName; 289 } 290 291 296 public void setServerName(String name) 297 { 298 serverName = name; 299 } 300 301 306 public int getServerPort() 307 { 308 return serverPort; 309 } 310 311 316 public void setServerPort(int port) 317 { 318 serverPort = port; 319 } 320 321 326 public String getServletPath() 327 { 328 return servletPath; 329 } 330 331 336 public void setServletPath(String path) 337 { 338 servletPath = path; 339 } 340 } 341 | Popular Tags |