1 31 32 package org.opencms.site; 33 34 import org.opencms.util.CmsStringUtil; 35 36 45 public final class CmsSiteMatcher implements Cloneable { 46 47 48 private static final int PORT_HTTP = 80; 49 50 51 private static final int PORT_HTTPS = 443; 52 53 54 private static final String SCHEME_HTTP = "http"; 55 56 57 private static final String SCHEME_HTTPS = "https"; 58 59 60 private static final String WILDCARD = "*"; 61 62 63 public static final CmsSiteMatcher DEFAULT_MATCHER = new CmsSiteMatcher(WILDCARD, WILDCARD, 0); 64 65 66 private Integer m_hashCode; 67 68 69 private String m_serverName; 70 71 72 private int m_serverPort; 73 74 75 private String m_serverProtocol; 76 77 85 public CmsSiteMatcher(String serverString) { 86 87 if (serverString == null) { 88 init(WILDCARD, WILDCARD, 0); 89 return; 90 } 91 serverString = serverString.trim(); 93 if (serverString.endsWith("/")) { 95 serverString = serverString.substring(0, serverString.length() - 1); 96 } 97 int pos, serverPort; 98 String serverProtocol, serverName; 99 pos = serverString.indexOf("://"); 101 if (pos >= 0) { 102 serverProtocol = serverString.substring(0, pos); 103 serverString = serverString.substring(pos + 3); 104 } else { 105 serverProtocol = SCHEME_HTTP; 106 } 107 pos = serverString.indexOf(":"); 109 if (pos >= 0) { 110 serverName = serverString.substring(0, pos); 111 try { 112 String port = serverString.substring(pos + 1); 113 pos = port.indexOf("/"); 114 if (pos >= 0) { 115 port = port.substring(0, pos); 116 } 117 serverPort = Integer.valueOf(port).intValue(); 118 } catch (NumberFormatException e) { 119 serverPort = PORT_HTTP; 120 } 121 } else { 122 serverName = serverString; 123 if (SCHEME_HTTPS.equals(serverProtocol)) { 124 serverPort = PORT_HTTPS; 125 } else { 126 serverPort = PORT_HTTP; 127 } 128 } 129 130 pos = serverName.indexOf("/"); 132 if (pos >= 0) { 133 serverName = serverName.substring(0, pos); 134 } 135 136 init(serverProtocol, serverName, serverPort); 138 } 139 140 147 public CmsSiteMatcher(String serverProtocol, String serverName, int serverPort) { 148 149 init(serverProtocol, serverName, serverPort); 150 } 151 152 157 public Object clone() { 158 159 return new CmsSiteMatcher(m_serverProtocol, m_serverName, m_serverPort); 160 } 161 162 165 public boolean equals(Object obj) { 166 167 if (obj == this) { 168 return true; 169 } 170 if (!(obj instanceof CmsSiteMatcher)) { 171 return false; 172 } 173 if ((this == DEFAULT_MATCHER) || (obj == DEFAULT_MATCHER)) { 175 return true; 176 } 177 CmsSiteMatcher other = (CmsSiteMatcher)obj; 178 return (m_serverPort == other.m_serverPort) 179 && m_serverName.equals(other.m_serverName) 180 && m_serverProtocol.equals(other.m_serverProtocol); 181 } 182 183 188 public String getServerName() { 189 190 return m_serverName; 191 } 192 193 198 public int getServerPort() { 199 200 return m_serverPort; 201 } 202 203 208 public String getServerProtocol() { 209 210 return m_serverProtocol; 211 } 212 213 218 public String getUrl() { 219 220 return m_serverProtocol 221 + "://" 222 + m_serverName 223 + (((m_serverPort != PORT_HTTP) && (m_serverPort != PORT_HTTPS)) ? ":" + m_serverPort : ""); 224 } 225 226 229 public int hashCode() { 230 231 if (m_hashCode == null) { 232 m_hashCode = new Integer (toString().hashCode()); 233 } 234 return m_hashCode.intValue(); 235 } 236 237 240 public String toString() { 241 242 StringBuffer result = new StringBuffer (32); 243 if ((m_serverProtocol != null) && !(WILDCARD.equals(m_serverProtocol))) { 244 result.append(m_serverProtocol); 245 result.append("://"); 246 } 247 result.append(m_serverName); 248 if ((m_serverPort > 0) 249 && (!(SCHEME_HTTP.equals(m_serverProtocol) && (m_serverPort == PORT_HTTP))) 250 && (!(SCHEME_HTTPS.equals(m_serverProtocol) && (m_serverPort == PORT_HTTPS)))) { 251 result.append(":"); 252 result.append(m_serverPort); 253 } 254 return result.toString(); 255 } 256 257 264 protected void setServerName(String serverName) { 265 266 if (CmsStringUtil.isEmpty(serverName) || (WILDCARD.equals(serverName))) { 267 m_serverName = WILDCARD; 268 } else { 269 m_serverName = serverName.trim(); 270 } 271 } 272 273 280 protected void setServerPort(int serverPort) { 281 282 m_serverPort = serverPort; 283 if (m_serverPort < 0) { 284 m_serverPort = 0; 285 } 286 } 287 288 295 protected void setServerProtocol(String serverProtocol) { 296 297 if (CmsStringUtil.isEmpty(serverProtocol) || (WILDCARD.equals(serverProtocol))) { 298 m_serverProtocol = WILDCARD; 299 } else { 300 int pos = serverProtocol.indexOf("/"); 301 if (pos > 0) { 302 m_serverProtocol = serverProtocol.substring(0, pos).toLowerCase(); 303 } else { 304 m_serverProtocol = serverProtocol.toLowerCase().trim(); 305 } 306 } 307 } 308 309 316 private void init(String serverProtocol, String serverName, int serverPort) { 317 318 setServerProtocol(serverProtocol); 319 setServerName(serverName); 320 setServerPort(serverPort); 321 } 322 } | Popular Tags |